Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Content is not allowed in prolog.
Content is not allowed in prolog. [message #985359] Thu, 15 November 2012 05:35 Go to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
dear all
i am trying to run a xtext project that i developped 6 months ago , then i downloaded the latest version of xtext eclipse, then i create a new xtext project and i migrate my source code. but when i rum the project it rise this exception

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Content is not allowed in prolog.
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:195)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:240)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1505)
at org.xtext.example.mydsl.ui.popup.actions.RunClass.getProg(RunClass.java:245)
at org.xtext.example.mydsl.ui.popup.actions.RunClass.run(RunClass.java:86)
at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3554)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3179)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1022)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:916)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:585)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:540)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Caused by: org.xml.sax.SAXParseExceptionpublicId: platform:/resource/prova/a.myDsl; systemId: platform:/resource/prova/a.myDsl; lineNumber: 4; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:391)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1404)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1014)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:625)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:819)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:748)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:525)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:392)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)



i saw that the exception is rised when this method is executed

private Prog getProg() throws IOException {
StandaloneSetup standalone = new StandaloneSetup();
standalone.setPlatformUri(workingPath);
Injector injector = new MyDslStandaloneSetup()
.createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = ((com.google.inject.Injector) injector)
.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
Boolean.TRUE);

Resource resource = resourceSet.createResource(URI.createURI(uriPrefix
+ fileName));
InputStream in = new FileInputStream(workingPath + fileName);

resource.load(in, resourceSet.getLoadOptions());

if (resource.getContents().size() == 0) {
MessageDialog.openError(shell, "File not found",
"The file is empty: no folder has been generated");
return null;
}
Prog model = (Prog) resource.getContents().get(0);
return model;

}


i don't know if something is changed in the Xtext libraries, but i remember that this cose works.

thanx before for the answer

Re: Content is not allowed in prolog. [message #985734 is a reply to message #985359] Thu, 15 November 2012 16:18 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 656
Registered: July 2009
Senior Member
It is apparent from the stacktrace that you are running your code
snippet inside Eclipse. I'd be surprised if that code had ever worked in
that environment before.

You are calling the StandaloneSetup and the MylanguagStandaloneSetup of
your language inside an Eclipse application. That will mess up your EMF
registries as has been discussed here often. And they are messed up, as
EMF uses an XMIResource to parse your language file.

Within Eclipse the EMF registries are usually populated by means of
extension points. So calling the standalone setup is not necessary, as
long as you make sure your plugin.xml and the generated plugin.xml_gen
are in sync.

Furthermore, within Eclipse, you should almost never have to explicitly
get the Injector of your lang. Instead, make sure your own classes are
also created by means of Dependency Injection. Consider reading the docs
about that. If your popup action is declared by means of the extension
point, use the ExecutableExtensionFactory of your language (docs).

Am 15.11.12 15:03, schrieb mark j:
> dear all
> i am trying to run a xtext project that i developped 6 months ago , then
> i downloaded the latest version of xtext eclipse, then i create a new
> xtext project and i migrate my source code. but when i rum the project
> it rise this exception
>
> org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Content is
> not allowed in prolog.
> at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:195)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:240)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1505)
>
> at
> org.xtext.example.mydsl.ui.popup.actions.RunClass.getProg(RunClass.java:245)
>
> at org.xtext.example.mydsl.ui.popup.actions.RunClass.run(RunClass.java:86)
> at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251)
> at
> org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
>
> at
> org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
>
> at
> org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
>
> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)
> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3554)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3179)
> at
> org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1022)
>
> at
> org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
>
> at
> org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:916)
>
> at
> org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
>
> at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:585)
> at
> org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
>
> at
> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:540)
> at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
> at
> org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
>
> at
> org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
>
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
>
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
>
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
>
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
>
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>
> at java.lang.reflect.Method.invoke(Method.java:616)
> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
> at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
> at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
> Caused by: org.xml.sax.SAXParseExceptionpublicId:
> platform:/resource/prova/a.myDsl; systemId:
> platform:/resource/prova/a.myDsl; lineNumber: 4; columnNumber: 1;
> Content is not allowed in prolog.
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
>
> at
> com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
>
> at
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:391)
>
> at
> com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1404)
>
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1014)
>
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:625)
>
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
>
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:819)
>
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:748)
>
> at
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
>
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
>
> at
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:525)
>
> at javax.xml.parsers.SAXParser.parse(SAXParser.java:392)
> at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)
>
>
>
> i saw that the exception is rised when this method is executed
>
> private Prog getProg() throws IOException {
> StandaloneSetup standalone = new StandaloneSetup();
> standalone.setPlatformUri(workingPath);
> Injector injector = new MyDslStandaloneSetup()
> .createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet = ((com.google.inject.Injector) injector)
> .getInstance(XtextResourceSet.class);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
> Boolean.TRUE);
>
> Resource resource = resourceSet.createResource(URI.createURI(uriPrefix
> + fileName));
> InputStream in = new FileInputStream(workingPath + fileName);
>
> resource.load(in, resourceSet.getLoadOptions());
>
> if (resource.getContents().size() == 0) {
> MessageDialog.openError(shell, "File not found",
> "The file is empty: no folder has been generated");
> return null;
> }
> Prog model = (Prog) resource.getContents().get(0);
> return model;
>
> }
>
>
> i don't know if something is changed in the Xtext libraries, but i
> remember that this cose works.
>
> thanx before for the answer
>
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Content is not allowed in prolog. [message #987593 is a reply to message #985734] Tue, 27 November 2012 04:50 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
i'am sure that code had worked and i will post the class. i don't know if there some changes in the xtext libraries,
however what i want to do, is to abtain the AST of my file to transform it in String, so please how i can change my code and more precisly the methid getProg() to do this.
thanx a lot.

package org.xtext.example.mydsl.ui.popup.actions;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashMap;

import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.mwe.utils.StandaloneSetup;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.xtext.example.mydsl.MyDslStandaloneSetup;
import org.xtext.example.mydsl.myDsl.Abs;
import org.xtext.example.mydsl.myDsl.App;
import org.xtext.example.mydsl.myDsl.FileSF;
import org.xtext.example.mydsl.myDsl.Let;
import org.xtext.example.mydsl.myDsl.Nrif;
import org.xtext.example.mydsl.myDsl.Prog;
import org.xtext.example.mydsl.myDsl.T;
import org.xtext.example.mydsl.myDsl.T1;
import org.xtext.example.mydsl.myDsl.T2;
import org.xtext.example.mydsl.myDsl.T3;
import org.xtext.example.mydsl.myDsl.Tapp;
import org.xtext.example.mydsl.myDsl.Term;
import org.xtext.example.mydsl.myDsl.Trif;
import org.xtext.example.mydsl.myDsl.Uq;
import org.xtext.example.mydsl.myDsl.Var;
import com.google.inject.Injector;




public class RunClass implements IObjectActionDelegate {

private Shell shell;
private String fileName;
private String workingPath;
private String uriPrefix;


/**
* Constructor for Action1.
*/
public RunClass() {
super();
}

/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}

/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {




MessageDialog.openInformation(
shell,
"org.xtext.example.mydsl.ui",
"run was executed.");


Term ty = null;
try {
ty = getProg().getMain();
} catch (IOException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
String inter = termToStringLci(ty);



MessageDialog.openInformation(
shell,
"org.xtext.example.mydsl.ui",
inter);


}

/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
fileName = toFileName(selection.toString());
workingPath = toWorkingPath(selection.toString());
uriPrefix = toUriPrefix(selection.toString());
}








/**
* restituisce la stringa che rappresentazione del lambda termine pulito dal
* tipo
* @param t termine
* @return stringa che rappresentazione del lambda termine pulito dal tipo
*/
private String termToStringLci(Term t) {

if (t instanceof Var) {
return ((Var) t).getName();
} else if (t instanceof Abs) {
return "(\\" + (((Abs) t).getParam()) + "."
+ termToStringLci(((Abs) t).getBody()) + ")";
} else if (t instanceof App) {
return "(" + termToStringLci(((App) t).getT1()) + " "
+ termToStringLci(((App) t).getT2()) + ")";
} else if (t instanceof Uq) {
return termToStringLci(((Uq) t).getTerm());
} else if (t instanceof Tapp) {
return termToStringLci(((Tapp) t).getTerm());
} else if (t instanceof Nrif) {
return termToStringLci(((Nrif) t).getNome().getTerm());
} else if (t instanceof Let) {
return "((\\" + (((Let) t).getVar()) + "."
+ termToStringLci(((Let) t).getT2()) + ") "
+ termToStringLci(((Let) t).getT1()) + ")";
}
return "";
}





/**
* restituisce la radice dell'albero sintattico
* @return la radice dell'albero sintattico
* @throws IOException
*/
private Prog getProg() throws IOException {



StandaloneSetup standalone = new StandaloneSetup();
standalone.setPlatformUri(workingPath);
Injector injector = new MyDslStandaloneSetup()
.createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = ((com.google.inject.Injector) injector)
.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
Boolean.TRUE);

Resource resource = resourceSet.createResource(URI.createURI(uriPrefix
+ fileName));
InputStream in = new FileInputStream(workingPath + fileName);

resource.load(in, resourceSet.getLoadOptions());

if (resource.getContents().size() == 0) {
MessageDialog.openError(shell, "File not found",
"The file is empty: no folder has been generated");
return null;
}
Prog model = (Prog) resource.getContents().get(0);
return model;

}






/* SERVICE METHODS */
private String toFileName(String name) {
int idx = getLastSeparatorIndex(name);

if (idx == -1)
return "";

return name.substring(idx + 1, name.length() - 1);
}

private String toWorkingPath(String name) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
String path = root.getLocation().toString() + "/";

int idx = getLastSeparatorIndex(name);
if (idx == -1)
return "";

return path + name.substring(3, idx) + "/";
}

private String toUriPrefix(String name) {
int idx = name.indexOf('/');
if (idx == -1)
return "";

return "platform:/resource"
+ name.substring(idx, name.lastIndexOf('/')) + "/";
}

private int getLastSeparatorIndex(String name) {
int idx = name.length() - 1;

while (name.charAt(idx) != '/') {
idx--;
if (idx < 0)
return -1;
}

return idx;
}



}
Re: Content is not allowed in prolog. [message #988752 is a reply to message #987593] Sun, 02 December 2012 13:28 Go to previous message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 656
Registered: July 2009
Senior Member
As I said, don't use StandaloneSetup when running in Eclipse.
You might want to read
http://koehnlein.blogspot.de/2012/11/xtext-tip-how-do-i-get-guice-injector.html
on that topic.


Am 27.11.12 10:50, schrieb mark j:
> i'am sure that code had worked and i will post the class. i don't know
> if there some changes in the xtext libraries, however what i want to do,
> is to abtain the AST of my file to transform it in String, so please how
> i can change my code and more precisly the methid getProg() to do this.
> thanx a lot.
>
> package org.xtext.example.mydsl.ui.popup.actions;
>
>
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.FileReader;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
> import java.io.PrintStream;
> import java.util.HashMap;
>
> import org.eclipse.core.resources.IWorkspaceRoot;
> import org.eclipse.core.resources.ResourcesPlugin;
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.mwe.utils.StandaloneSetup;
> import org.eclipse.jface.action.IAction;
> import org.eclipse.jface.dialogs.MessageDialog;
> import org.eclipse.jface.viewers.ISelection;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.ui.IObjectActionDelegate;
> import org.eclipse.ui.IWorkbenchPart;
> import org.eclipse.xtext.resource.XtextResource;
> import org.eclipse.xtext.resource.XtextResourceSet;
> import org.xtext.example.mydsl.MyDslStandaloneSetup;
> import org.xtext.example.mydsl.myDsl.Abs;
> import org.xtext.example.mydsl.myDsl.App;
> import org.xtext.example.mydsl.myDsl.FileSF;
> import org.xtext.example.mydsl.myDsl.Let;
> import org.xtext.example.mydsl.myDsl.Nrif;
> import org.xtext.example.mydsl.myDsl.Prog;
> import org.xtext.example.mydsl.myDsl.T;
> import org.xtext.example.mydsl.myDsl.T1;
> import org.xtext.example.mydsl.myDsl.T2;
> import org.xtext.example.mydsl.myDsl.T3;
> import org.xtext.example.mydsl.myDsl.Tapp;
> import org.xtext.example.mydsl.myDsl.Term;
> import org.xtext.example.mydsl.myDsl.Trif;
> import org.xtext.example.mydsl.myDsl.Uq;
> import org.xtext.example.mydsl.myDsl.Var;
> import com.google.inject.Injector;
>
>
>
>
> public class RunClass implements IObjectActionDelegate {
>
> private Shell shell;
> private String fileName;
> private String workingPath;
> private String uriPrefix;
>
>
> /**
> * Constructor for Action1.
> */
> public RunClass() {
> super();
> }
>
> /**
> * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
> */
> public void setActivePart(IAction action, IWorkbenchPart targetPart) {
> shell = targetPart.getSite().getShell();
> }
>
> /**
> * @see IActionDelegate#run(IAction)
> */
> public void run(IAction action) {
>
>
>
>
> MessageDialog.openInformation(
> shell,
> "org.xtext.example.mydsl.ui",
> "run was executed.");
>
>
> Term ty = null;
> try {
> ty = getProg().getMain();
> } catch (IOException e) {
>
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> String inter = termToStringLci(ty);
>
>
> MessageDialog.openInformation(
> shell,
> "org.xtext.example.mydsl.ui",
> inter);
>
> }
>
> /**
> * @see IActionDelegate#selectionChanged(IAction, ISelection)
> */
> public void selectionChanged(IAction action, ISelection selection) {
> fileName = toFileName(selection.toString());
> workingPath = toWorkingPath(selection.toString());
> uriPrefix = toUriPrefix(selection.toString());
> }
>
>
>
>
>
>
>
>
> /**
> * restituisce la stringa che rappresentazione del lambda termine pulito dal
> * tipo
> * @param t termine
> * @return stringa che rappresentazione del lambda termine pulito dal tipo
> */
> private String termToStringLci(Term t) {
>
> if (t instanceof Var) {
> return ((Var) t).getName();
> } else if (t instanceof Abs) {
> return "(\\" + (((Abs) t).getParam()) + "."
> + termToStringLci(((Abs) t).getBody()) + ")";
> } else if (t instanceof App) {
> return "(" + termToStringLci(((App) t).getT1()) + " "
> + termToStringLci(((App) t).getT2()) + ")";
> } else if (t instanceof Uq) {
> return termToStringLci(((Uq) t).getTerm());
> } else if (t instanceof Tapp) {
> return termToStringLci(((Tapp) t).getTerm());
> } else if (t instanceof Nrif) {
> return termToStringLci(((Nrif) t).getNome().getTerm());
> } else if (t instanceof Let) {
> return "((\\" + (((Let) t).getVar()) + "."
> + termToStringLci(((Let) t).getT2()) + ") "
> + termToStringLci(((Let) t).getT1()) + ")";
> }
> return "";
> }
>
>
>
>
>
> /**
> * restituisce la radice dell'albero sintattico
> * @return la radice dell'albero sintattico
> * @throws IOException
> */
> private Prog getProg() throws IOException {
>
>
>
> StandaloneSetup standalone = new StandaloneSetup();
> standalone.setPlatformUri(workingPath);
> Injector injector = new MyDslStandaloneSetup()
> .createInjectorAndDoEMFRegistration();
> XtextResourceSet resourceSet = ((com.google.inject.Injector) injector)
> .getInstance(XtextResourceSet.class);
> resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL,
> Boolean.TRUE);
>
> Resource resource = resourceSet.createResource(URI.createURI(uriPrefix
> + fileName));
> InputStream in = new FileInputStream(workingPath + fileName);
>
> resource.load(in, resourceSet.getLoadOptions());
>
> if (resource.getContents().size() == 0) {
> MessageDialog.openError(shell, "File not found",
> "The file is empty: no folder has been generated");
> return null;
> }
> Prog model = (Prog) resource.getContents().get(0);
> return model;
>
> }
>
>
>
>
>
>
> /* SERVICE METHODS */
> private String toFileName(String name) {
> int idx = getLastSeparatorIndex(name);
>
> if (idx == -1)
> return "";
>
> return name.substring(idx + 1, name.length() - 1);
> }
>
> private String toWorkingPath(String name) {
> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
> String path = root.getLocation().toString() + "/";
>
> int idx = getLastSeparatorIndex(name);
> if (idx == -1)
> return "";
>
> return path + name.substring(3, idx) + "/";
> }
>
> private String toUriPrefix(String name) {
> int idx = name.indexOf('/');
> if (idx == -1)
> return "";
>
> return "platform:/resource"
> + name.substring(idx, name.lastIndexOf('/')) + "/";
> }
>
> private int getLastSeparatorIndex(String name) {
> int idx = name.length() - 1;
>
> while (name.charAt(idx) != '/') {
> idx--;
> if (idx < 0)
> return -1;
> }
>
> return idx;
> }
>
>
> }
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Previous Topic:Need hover for keyword or block of elements
Next Topic:Build the descriptions index differently
Goto Forum:
  


Current Time: Thu Aug 21 04:23:24 EDT 2014

Powered by FUDForum. Page generated in 0.01913 seconds