Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » file parsing
file parsing [message #987833] Wed, 28 November 2012 10:36 Go to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
hi All
I'am writing again for the same problem caus i didn't understand the answer then i can' fix the problem

what i'm trying to do is to parse my file, and the code is the follow



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) {





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

// TODO Auto-generated catch block
e.printStackTrace();
}




}

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



private Prog getProg() throws IOException {

/*Environment configuration*/
StandaloneSetup standalone = new StandaloneSetup();
standalone.setPlatformUri(workingPath);
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);

/*File parsing*/

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

if(resource.getContents().size() == 0) {
System.out.println("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;
}






}


so what i don't understand is why with xtext 2.0 this code work and with the latest versione didn't and 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:206)
at org.xtext.example.mydsl.ui.popup.actions.RunClass.run(RunClass.java:88)
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)
... 34 more

[Updated on: Wed, 28 November 2012 10:37]

Report message to a moderator

Re: file parsing [message #987845 is a reply to message #987833] Wed, 28 November 2012 11:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6315
Registered: July 2009
Senior Member
Never never ever call mydslstandalonesetup from eclipse code

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: file parsing [message #988300 is a reply to message #987845] Thu, 29 November 2012 10:23 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
ok, thanx
can you give me any reference where i can see how i can obtain the root AST element, without using mydslstandalonesetup
Re: file parsing [message #988378 is a reply to message #988300] Thu, 29 November 2012 16:30 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2500
Registered: July 2009
Senior Member
On 2012-29-11 11:23, mark j wrote:
> ok, thanx can you give me any reference where i can see how i can
> obtain the root AST element, without using mydslstandalonesetup

That depends on what you want to do - which file is it you want to get
the AST for? Is it open in an editor? Do you want the dirty state or the
state saved in the file?

In general, just use EMF and open the resource; the parser is registered
for your language extension, so you will get a parsed EMF resource back
- its content is the AST model.

- henrik
Re: file parsing [message #988494 is a reply to message #988378] Fri, 30 November 2012 09:07 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member

>Is it open in an editor?
yes

>Do you want the dirty state or the
>state saved in the file?
i want to load the Ast of the state saved in the file

if you can show me an example or some documentation, thay will help me so.
thanks
Mark
Re: file parsing [message #988572 is a reply to message #988494] Fri, 30 November 2012 14:46 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2500
Registered: July 2009
Senior Member
On 2012-30-11 10:07, mark j wrote:
>
>> Is it open in an editor?
> yes
>> Do you want the dirty state or the
>> state saved in the file?
> i want to load the Ast of the state saved in the file
>
> if you can show me an example or some documentation, thay will help me so.
> thanks
> Mark
You use EMF to open the resource from a URI that references the resource
you want to open. Consult the EMF documentation.

Look at Resource and ResourceSet.

- henrik
Re: file parsing [message #989014 is a reply to message #988572] Tue, 04 December 2012 10:29 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
hi, i readed the EMF documentation and how to open the resource so i changed my code

public class RunClass implements IObjectActionDelegate {
private IStructuredSelection selection;


public void run(IAction action) {
IFile file = (IFile)selection.getFirstElement();
XtextResourceSet xtextResourceSet = new XtextResourceSet();
URI uri = URI.createURI(file.getFullPath().toString());
Resource resource = xtextResourceSet.getResource(uri, true);

}


public void selectionChanged(IAction action, ISelection selection) {
this.selection = (IStructuredSelection)selection;
}

public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}

}

but it not work, it raise this exception, any suggestion?? how i can fix this error


org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.xml.sax.SAXParseExceptionpublicId: /prova/a.myDsl; systemId: file:///prova/a.myDsl; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:159)
at org.xtext.example.mydsl.ui.popup.actions.RunClass.run(RunClass.java:96)
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: /prova/a.myDsl; systemId: file:///prova/a.myDsl; lineNumber: 1; 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)
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.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1284)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
... 32 more
Re: file parsing [message #989039 is a reply to message #989014] Tue, 04 December 2012 11:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6315
Registered: July 2009
Senior Member
Hi are you sure The file extension is correct (case sensitive) in
General you might get inspired by
http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-gen
erator-from-a-context-menu/

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: file parsing [message #989059 is a reply to message #989039] Tue, 04 December 2012 13:33 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
yes i'm sure.
Re: file parsing [message #989125 is a reply to message #989059] Tue, 04 December 2012 17:37 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2837
Registered: July 2009
Senior Member
Am 04.12.12 14:33, schrieb mark j:
> yes i'm sure.

Where do you register the resource factory? Can you show the xml snippet
/ the code?

--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: file parsing [message #989218 is a reply to message #989125] Wed, 05 December 2012 09:31 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
i didn't , i figured is did automatically.. no??
if not can u give some documentation or example to do it.
thanx
Re: file parsing [message #989219 is a reply to message #989218] Wed, 05 December 2012 09:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6315
Registered: July 2009
Senior Member
Hi,

if you call your code from eclipse this should happen automatically.
did you have a look at my code? it processes a model file from an eclipse command.

~Christian
Re: file parsing [message #989220 is a reply to message #989219] Wed, 05 December 2012 09:43 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
no, can u give the link.
thanks
Re: file parsing [message #989242 is a reply to message #989220] Wed, 05 December 2012 10:37 Go to previous messageGo to next message
mark j is currently offline mark j
Messages: 14
Registered: November 2012
Junior Member
now it works. i din't nothing, only create a new project and migrate my code there.
thanx for your support.
Re: file parsing [message #989272 is a reply to message #989218] Wed, 05 December 2012 12:58 Go to previous message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2837
Registered: July 2009
Senior Member
Am 05.12.12 10:31, schrieb mark j:
> i didn't , i figured is did automatically.. no??
> if not can u give some documentation or example to do it.
> thanx

Make sure the plugin.xml of your runtime project uses the right file
extension.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Previous Topic:[Xtext] Changing java package naming after project is created
Next Topic:xtext injected parser
Goto Forum:
  


Current Time: Wed Sep 24 00:41:25 GMT 2014

Powered by FUDForum. Page generated in 0.02667 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software