Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Q: Loading XMLResources containing non-modeled namespaces?
Q: Loading XMLResources containing non-modeled namespaces? [message #386108] Wed, 16 June 2004 11:26 Go to next message
No real name is currently offline No real nameFriend
Messages: 121
Registered: July 2009
Senior Member
Hi,
I want to be able to load an object instance from persistent storage in
XML, such that the XML document is allowed to contain namespaces which
were unknown at the time the ecore was created.

Example: I generate a model using the library.xsd from the EMF Overview.
I progammatically instantiated a library and book and persist it using
XMLResourceImpl. I load that same XML document and instantiate the
Library. So far, so good.

Now I introduce an element from another non-modelled namespace, foo:

<?xml version="1.0" encoding="ASCII"?>
<library:Library xmlns:library="http://www.example.eclipse.org/Library">

<m:foo xmlns:m="http://example.org/foo">
<m:msg>Have a nice day</m:msg>
</m:foo>

<books title="Once upon a time" pages="100" category="Biography"
author="/1"/>
</library:Library>

I receive an exception:
org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)

Is it possible that the unknown namespace, foo, is ignored and I still
instantiate the library portion without exception?

Thanks,
chris

PS. Here is my code to load:

File file = new File("xml/lib2.xml");
String filename = file.getAbsolutePath();
URI uri = URI.createFileURI(filename);

LibraryPackage pkg = LibraryPackageImpl.init();

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().
getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());

Resource resource = resourceSet.createResource(uri);
resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #386109 is a reply to message #386108] Wed, 16 June 2004 12:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Chris,

The exceptions are recorded and aren't thrown until the whole document is
processed, so you should pretty much already have what you want if you just
ignore errors. You should maybe consider using wildcards to allow such
content in the document rather than just wanting the errors to be ignored.
Another feature you can take advantage of is to use
XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled, all the
unrecognized content is recorded in the EObjectToExtensionMap and no
exceptions produced.


Chris Giblin wrote:

> Hi,
> I want to be able to load an object instance from persistent storage in
> XML, such that the XML document is allowed to contain namespaces which
> were unknown at the time the ecore was created.
>
> Example: I generate a model using the library.xsd from the EMF Overview.
> I progammatically instantiated a library and book and persist it using
> XMLResourceImpl. I load that same XML document and instantiate the
> Library. So far, so good.
>
> Now I introduce an element from another non-modelled namespace, foo:
>
> <?xml version="1.0" encoding="ASCII"?>
> <library:Library xmlns:library="http://www.example.eclipse.org/Library">
>
> <m:foo xmlns:m="http://example.org/foo">
> <m:msg>Have a nice day</m:msg>
> </m:foo>
>
> <books title="Once upon a time" pages="100" category="Biography"
> author="/1"/>
> </library:Library>
>
> I receive an exception:
> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>
> Is it possible that the unknown namespace, foo, is ignored and I still
> instantiate the library portion without exception?
>
> Thanks,
> chris
>
> PS. Here is my code to load:
>
> File file = new File("xml/lib2.xml");
> String filename = file.getAbsolutePath();
> URI uri = URI.createFileURI(filename);
>
> LibraryPackage pkg = LibraryPackageImpl.init();
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().
> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>
> Resource resource = resourceSet.createResource(uri);
> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #386111 is a reply to message #386109] Wed, 16 June 2004 12:13 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 121
Registered: July 2009
Senior Member
Ed,
Mega-cool. That did it:

HashMap options = new HashMap(5);
options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
new Boolean(true));
resource.load(new FileInputStream(filename),options);

Thanks much,
Chris


Ed Merks wrote:

> Chris,
>
> The exceptions are recorded and aren't thrown until the whole document is
> processed, so you should pretty much already have what you want if you just
> ignore errors. You should maybe consider using wildcards to allow such
> content in the document rather than just wanting the errors to be ignored.
> Another feature you can take advantage of is to use
> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled, all the
> unrecognized content is recorded in the EObjectToExtensionMap and no
> exceptions produced.
>
>
> Chris Giblin wrote:
>
>
>>Hi,
>>I want to be able to load an object instance from persistent storage in
>>XML, such that the XML document is allowed to contain namespaces which
>>were unknown at the time the ecore was created.
>>
>>Example: I generate a model using the library.xsd from the EMF Overview.
>>I progammatically instantiated a library and book and persist it using
>>XMLResourceImpl. I load that same XML document and instantiate the
>>Library. So far, so good.
>>
>>Now I introduce an element from another non-modelled namespace, foo:
>>
>><?xml version="1.0" encoding="ASCII"?>
>><library:Library xmlns:library="http://www.example.eclipse.org/Library">
>>
>> <m:foo xmlns:m="http://example.org/foo">
>> <m:msg>Have a nice day</m:msg>
>> </m:foo>
>>
>> <books title="Once upon a time" pages="100" category="Biography"
>>author="/1"/>
>></library:Library>
>>
>>I receive an exception:
>>org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
>>found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>
>>Is it possible that the unknown namespace, foo, is ignored and I still
>>instantiate the library portion without exception?
>>
>>Thanks,
>>chris
>>
>>PS. Here is my code to load:
>>
>>File file = new File("xml/lib2.xml");
>>String filename = file.getAbsolutePath();
>>URI uri = URI.createFileURI(filename);
>>
>>LibraryPackage pkg = LibraryPackageImpl.init();
>>
>>ResourceSet resourceSet = new ResourceSetImpl();
>>resourceSet.getResourceFactoryRegistry().
>> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>>
>>Resource resource = resourceSet.createResource(uri);
>>resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>
>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #386115 is a reply to message #386111] Wed, 16 June 2004 12:37 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Chris,

I think this will be a pretty popular feature. The recorded out-of-band content,
i.e., the stuff in the map, will even be written out again on save, although
relative order with respect to recognized content won't be preserved.

As an aside, using Boolean.TRUE is better than new Boolean(true). ;-)


Chris Giblin wrote:

> Ed,
> Mega-cool. That did it:
>
> HashMap options = new HashMap(5);
> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
> new Boolean(true));
> resource.load(new FileInputStream(filename),options);
>
> Thanks much,
> Chris
>
> Ed Merks wrote:
>
> > Chris,
> >
> > The exceptions are recorded and aren't thrown until the whole document is
> > processed, so you should pretty much already have what you want if you just
> > ignore errors. You should maybe consider using wildcards to allow such
> > content in the document rather than just wanting the errors to be ignored.
> > Another feature you can take advantage of is to use
> > XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled, all the
> > unrecognized content is recorded in the EObjectToExtensionMap and no
> > exceptions produced.
> >
> >
> > Chris Giblin wrote:
> >
> >
> >>Hi,
> >>I want to be able to load an object instance from persistent storage in
> >>XML, such that the XML document is allowed to contain namespaces which
> >>were unknown at the time the ecore was created.
> >>
> >>Example: I generate a model using the library.xsd from the EMF Overview.
> >>I progammatically instantiated a library and book and persist it using
> >>XMLResourceImpl. I load that same XML document and instantiate the
> >>Library. So far, so good.
> >>
> >>Now I introduce an element from another non-modelled namespace, foo:
> >>
> >><?xml version="1.0" encoding="ASCII"?>
> >><library:Library xmlns:library="http://www.example.eclipse.org/Library">
> >>
> >> <m:foo xmlns:m="http://example.org/foo">
> >> <m:msg>Have a nice day</m:msg>
> >> </m:foo>
> >>
> >> <books title="Once upon a time" pages="100" category="Biography"
> >>author="/1"/>
> >></library:Library>
> >>
> >>I receive an exception:
> >>org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
> >>found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
> >>
> >>Is it possible that the unknown namespace, foo, is ignored and I still
> >>instantiate the library portion without exception?
> >>
> >>Thanks,
> >>chris
> >>
> >>PS. Here is my code to load:
> >>
> >>File file = new File("xml/lib2.xml");
> >>String filename = file.getAbsolutePath();
> >>URI uri = URI.createFileURI(filename);
> >>
> >>LibraryPackage pkg = LibraryPackageImpl.init();
> >>
> >>ResourceSet resourceSet = new ResourceSetImpl();
> >>resourceSet.getResourceFactoryRegistry().
> >> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
> >>
> >>Resource resource = resourceSet.createResource(uri);
> >>resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
> >
> >


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415903 is a reply to message #386111] Thu, 10 January 2008 10:19 Go to previous messageGo to next message
Rene Ladan is currently offline Rene LadanFriend
Messages: 51
Registered: July 2009
Member
This works nice indeed.
But is there a way to preserve the original namespaces/prefixes upon save?
I assume they get overridden by the namespace of the ecore model upon which
the editor is based when loading the model.

For the record, the code is in xyz.presentation.xyzEditor.handleChangedResources()
(usually in abc.editor/xyz.presentation/xyzEditor.java)

Thanks,
Rene

Chris Giblin wrote:
> Ed,
> Mega-cool. That did it:
>
> HashMap options = new HashMap(5);
> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
> new Boolean(true));
> resource.load(new FileInputStream(filename),options);
>
> Thanks much,
> Chris
>
>
> Ed Merks wrote:
>
>> Chris,
>>
>> The exceptions are recorded and aren't thrown until the whole document is
>> processed, so you should pretty much already have what you want if you
>> just
>> ignore errors. You should maybe consider using wildcards to allow such
>> content in the document rather than just wanting the errors to be
>> ignored.
>> Another feature you can take advantage of is to use
>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled, all
>> the
>> unrecognized content is recorded in the EObjectToExtensionMap and no
>> exceptions produced.
>>
>>
>> Chris Giblin wrote:
>>
>>
>>> Hi,
>>> I want to be able to load an object instance from persistent storage in
>>> XML, such that the XML document is allowed to contain namespaces which
>>> were unknown at the time the ecore was created.
>>>
>>> Example: I generate a model using the library.xsd from the EMF Overview.
>>> I progammatically instantiated a library and book and persist it using
>>> XMLResourceImpl. I load that same XML document and instantiate the
>>> Library. So far, so good.
>>>
>>> Now I introduce an element from another non-modelled namespace, foo:
>>>
>>> <?xml version="1.0" encoding="ASCII"?>
>>> <library:Library xmlns:library="http://www.example.eclipse.org/Library">
>>>
>>> <m:foo xmlns:m="http://example.org/foo">
>>> <m:msg>Have a nice day</m:msg>
>>> </m:foo>
>>>
>>> <books title="Once upon a time" pages="100" category="Biography"
>>> author="/1"/>
>>> </library:Library>
>>>
>>> I receive an exception:
>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>
>>> Is it possible that the unknown namespace, foo, is ignored and I still
>>> instantiate the library portion without exception?
>>>
>>> Thanks,
>>> chris
>>>
>>> PS. Here is my code to load:
>>>
>>> File file = new File("xml/lib2.xml");
>>> String filename = file.getAbsolutePath();
>>> URI uri = URI.createFileURI(filename);
>>>
>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>
>>> ResourceSet resourceSet = new ResourceSetImpl();
>>> resourceSet.getResourceFactoryRegistry().
>>> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>>>
>>> Resource resource = resourceSet.createResource(uri);
>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>
>>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415906 is a reply to message #415903] Thu, 10 January 2008 11:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Rene,

Namespace should always be preserved. XMI serializations don't
generally preserve prefixes because the prefixes used while loading are
not saved anywhere. XML serializations where the model uses a document
root will try to preserve prefixes because the documents root's
XMLNSPrefix map records them. But the prefixes will be bubbled to the
top of the document and if there are collisions, the prefixes will be
changed to make them unique. The XMLHelper has methods for getting and
setting prefix map at the end of loading or before saving, so I suppose
one could specialize a resource implementation to exploit that to record
the information somewhere other than on the document root.


Rene Ladan wrote:
> This works nice indeed.
> But is there a way to preserve the original namespaces/prefixes upon
> save?
> I assume they get overridden by the namespace of the ecore model upon
> which
> the editor is based when loading the model.
>
> For the record, the code is in
> xyz.presentation.xyzEditor.handleChangedResources()
> (usually in abc.editor/xyz.presentation/xyzEditor.java)
>
> Thanks,
> Rene
>
> Chris Giblin wrote:
>> Ed,
>> Mega-cool. That did it:
>>
>> HashMap options = new HashMap(5);
>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>> new Boolean(true));
>> resource.load(new FileInputStream(filename),options);
>>
>> Thanks much,
>> Chris
>>
>>
>> Ed Merks wrote:
>>
>>> Chris,
>>>
>>> The exceptions are recorded and aren't thrown until the whole
>>> document is
>>> processed, so you should pretty much already have what you want if
>>> you just
>>> ignore errors. You should maybe consider using wildcards to allow such
>>> content in the document rather than just wanting the errors to be
>>> ignored.
>>> Another feature you can take advantage of is to use
>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled,
>>> all the
>>> unrecognized content is recorded in the EObjectToExtensionMap and no
>>> exceptions produced.
>>>
>>>
>>> Chris Giblin wrote:
>>>
>>>
>>>> Hi,
>>>> I want to be able to load an object instance from persistent
>>>> storage in
>>>> XML, such that the XML document is allowed to contain namespaces which
>>>> were unknown at the time the ecore was created.
>>>>
>>>> Example: I generate a model using the library.xsd from the EMF
>>>> Overview.
>>>> I progammatically instantiated a library and book and persist it using
>>>> XMLResourceImpl. I load that same XML document and instantiate the
>>>> Library. So far, so good.
>>>>
>>>> Now I introduce an element from another non-modelled namespace, foo:
>>>>
>>>> <?xml version="1.0" encoding="ASCII"?>
>>>> <library:Library
>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>
>>>> <m:foo xmlns:m="http://example.org/foo">
>>>> <m:msg>Have a nice day</m:msg>
>>>> </m:foo>
>>>>
>>>> <books title="Once upon a time" pages="100" category="Biography"
>>>> author="/1"/>
>>>> </library:Library>
>>>>
>>>> I receive an exception:
>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>
>>>> Is it possible that the unknown namespace, foo, is ignored and I still
>>>> instantiate the library portion without exception?
>>>>
>>>> Thanks,
>>>> chris
>>>>
>>>> PS. Here is my code to load:
>>>>
>>>> File file = new File("xml/lib2.xml");
>>>> String filename = file.getAbsolutePath();
>>>> URI uri = URI.createFileURI(filename);
>>>>
>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>
>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>> resourceSet.getResourceFactoryRegistry().
>>>> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>>>>
>>>> Resource resource = resourceSet.createResource(uri);
>>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>>
>>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415908 is a reply to message #415903] Thu, 10 January 2008 15:12 Go to previous messageGo to next message
Rene Ladan is currently offline Rene LadanFriend
Messages: 51
Registered: July 2009
Member
Rene Ladan wrote:
> This works nice indeed.
> But is there a way to preserve the original namespaces/prefixes upon save?
> I assume they get overridden by the namespace of the ecore model upon which
> the editor is based when loading the model.
>
The trick doesn't seem to work when loading external resources having foreign
namespaces without a prefix.
The following model has a namespace "XML" without a prefix. The element Root
is non-abstract and referenced via a cross-resource reference.

<Root xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="XML" name="parameters">

This is the exception I get :

org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
at org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
at org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
at org.eclipse.jface.window.Window.open(Window.java:796)
at org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
at org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
at org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
at org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
at org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'XML' not found. (file:.../parameters-XML.ecore, 4, 35)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
at org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
at org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
.... 42 more

Eclipse 3.3.1.1, EMF 2.3.1

Any ideas?

Thanks,
Rene
w
> Chris Giblin wrote:
>> Ed,
>> Mega-cool. That did it:
>>
>> HashMap options = new HashMap(5);
>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>> new Boolean(true));
>> resource.load(new FileInputStream(filename),options);
>>
>> Thanks much,
>> Chris
>>
>>
>> Ed Merks wrote:
>>
>>> Chris,
>>>
>>> The exceptions are recorded and aren't thrown until the whole
>>> document is
>>> processed, so you should pretty much already have what you want if
>>> you just
>>> ignore errors. You should maybe consider using wildcards to allow such
>>> content in the document rather than just wanting the errors to be
>>> ignored.
>>> Another feature you can take advantage of is to use
>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled,
>>> all the
>>> unrecognized content is recorded in the EObjectToExtensionMap and no
>>> exceptions produced.
>>>
>>>
>>> Chris Giblin wrote:
>>>
>>>
>>>> Hi,
>>>> I want to be able to load an object instance from persistent storage in
>>>> XML, such that the XML document is allowed to contain namespaces which
>>>> were unknown at the time the ecore was created.
>>>>
>>>> Example: I generate a model using the library.xsd from the EMF
>>>> Overview.
>>>> I progammatically instantiated a library and book and persist it using
>>>> XMLResourceImpl. I load that same XML document and instantiate the
>>>> Library. So far, so good.
>>>>
>>>> Now I introduce an element from another non-modelled namespace, foo:
>>>>
>>>> <?xml version="1.0" encoding="ASCII"?>
>>>> <library:Library
>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>
>>>> <m:foo xmlns:m="http://example.org/foo">
>>>> <m:msg>Have a nice day</m:msg>
>>>> </m:foo>
>>>>
>>>> <books title="Once upon a time" pages="100" category="Biography"
>>>> author="/1"/>
>>>> </library:Library>
>>>>
>>>> I receive an exception:
>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>
>>>> Is it possible that the unknown namespace, foo, is ignored and I still
>>>> instantiate the library portion without exception?
>>>>
>>>> Thanks,
>>>> chris
>>>>
>>>> PS. Here is my code to load:
>>>>
>>>> File file = new File("xml/lib2.xml");
>>>> String filename = file.getAbsolutePath();
>>>> URI uri = URI.createFileURI(filename);
>>>>
>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>
>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>> resourceSet.getResourceFactoryRegistry().
>>>> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>>>>
>>>> Resource resource = resourceSet.createResource(uri);
>>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>>
>>>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415911 is a reply to message #415908] Thu, 10 January 2008 17:12 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070907090505090104040205
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Rene,

Does it call this method in XMLHandler and if so, I wonder why it
doesn't return a demand created package for your URI:

protected EPackage handleMissingPackage(String uriString)
{
if (XMLResource.XML_SCHEMA_URI.equals(uriString))
{
return xmlSchemaTypePackage;
}
else if (extendedMetaData != null)
{
if (recordUnknownFeature)
{
return extendedMetaData.demandPackage(uriString);
}


Rene Ladan wrote:
> Rene Ladan wrote:
>> This works nice indeed.
>> But is there a way to preserve the original namespaces/prefixes upon
>> save?
>> I assume they get overridden by the namespace of the ecore model upon
>> which
>> the editor is based when loading the model.
>>
> The trick doesn't seem to work when loading external resources having
> foreign
> namespaces without a prefix.
> The following model has a namespace "XML" without a prefix. The
> element Root
> is non-abstract and referenced via a cross-resource reference.
>
> <Root xmi:version="2.0"
> xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="XML" name="parameters">
>
> This is the exception I get :
>
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
> 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>
> at
> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>
> at
> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>
> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
> at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
> at
> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
> at org.eclipse.jface.window.Window.open(Window.java:796)
> at
> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>
> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
> at
> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>
> at
> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>
> at
> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>
> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
> at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
> at
> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>
> at
> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>
> at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
> at
> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>
> at
> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>
> at
> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>
> at
> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> at java.lang.reflect.Method.invoke(Unknown Source)
> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
> with uri 'XML' not found. (file:.../parameters-XML.ecore, 4, 35)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
> Source)
> at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
> Source)
> at javax.xml.parsers.SAXParser.parse(Unknown Source)
> at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>
> .... 42 more
>
> Eclipse 3.3.1.1, EMF 2.3.1
>
> Any ideas?
>
> Thanks,
> Rene
> w
>> Chris Giblin wrote:
>>> Ed,
>>> Mega-cool. That did it:
>>>
>>> HashMap options = new HashMap(5);
>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>> new Boolean(true));
>>> resource.load(new FileInputStream(filename),options);
>>>
>>> Thanks much,
>>> Chris
>>>
>>>
>>> Ed Merks wrote:
>>>
>>>> Chris,
>>>>
>>>> The exceptions are recorded and aren't thrown until the whole
>>>> document is
>>>> processed, so you should pretty much already have what you want if
>>>> you just
>>>> ignore errors. You should maybe consider using wildcards to allow
>>>> such
>>>> content in the document rather than just wanting the errors to be
>>>> ignored.
>>>> Another feature you can take advantage of is to use
>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled,
>>>> all the
>>>> unrecognized content is recorded in the EObjectToExtensionMap and no
>>>> exceptions produced.
>>>>
>>>>
>>>> Chris Giblin wrote:
>>>>
>>>>
>>>>> Hi,
>>>>> I want to be able to load an object instance from persistent
>>>>> storage in
>>>>> XML, such that the XML document is allowed to contain namespaces
>>>>> which
>>>>> were unknown at the time the ecore was created.
>>>>>
>>>>> Example: I generate a model using the library.xsd from the EMF
>>>>> Overview.
>>>>> I progammatically instantiated a library and book and persist it
>>>>> using
>>>>> XMLResourceImpl. I load that same XML document and instantiate the
>>>>> Library. So far, so good.
>>>>>
>>>>> Now I introduce an element from another non-modelled namespace, foo:
>>>>>
>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>> <library:Library
>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>
>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>> <m:msg>Have a nice day</m:msg>
>>>>> </m:foo>
>>>>>
>>>>> <books title="Once upon a time" pages="100" category="Biography"
>>>>> author="/1"/>
>>>>> </library:Library>
>>>>>
>>>>> I receive an exception:
>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>
>>>>> Is it possible that the unknown namespace, foo, is ignored and I
>>>>> still
>>>>> instantiate the library portion without exception?
>>>>>
>>>>> Thanks,
>>>>> chris
>>>>>
>>>>> PS. Here is my code to load:
>>>>>
>>>>> File file = new File("xml/lib2.xml");
>>>>> String filename = file.getAbsolutePath();
>>>>> URI uri = URI.createFileURI(filename);
>>>>>
>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>
>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>> resourceSet.getResourceFactoryRegistry().
>>>>> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>>>>>
>>>>> Resource resource = resourceSet.createResource(uri);
>>>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>>>
>>>>


--------------070907090505090104040205
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rene,<br>
<br>
Does it call this method in XMLHandler and if so, I wonder why it
doesn't return a demand created package for your URI:<br>
<blockquote><small>&nbsp; protected EPackage handleMissingPackage(String
uriString)<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; if (XMLResource.XML_SCHEMA_URI.equals(uriString))<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return xmlSchemaTypePackage;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; else if (extendedMetaData != null)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (recordUnknownFeature)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; return extendedMetaData.demandPackage(uriString);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</small><br>
</blockquote>
<br>
Rene Ladan wrote:
<blockquote cite="mid:fm5cl4$o3t$1@build.eclipse.org" type="cite">Rene
Ladan wrote:
<br>
<blockquote type="cite">This works nice indeed.
<br>
But is there a way to preserve the original namespaces/prefixes upon
save?
<br>
I assume they get overridden by the namespace of the ecore model upon
which
<br>
the editor is based when loading the model.
<br>
<br>
</blockquote>
The trick doesn't seem to work when loading external resources having
foreign
<br>
namespaces without a prefix.
<br>
The following model has a namespace "XML" without a prefix.&nbsp; The
element Root
<br>
is non-abstract and referenced via a cross-resource reference.
<br>
<br>
&lt;Root xmi:version="2.0"
<br>
&nbsp;&nbsp;&nbsp; xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://www.omg.org/XMI">"http://www.omg.org/XMI"</a>
xmlns:xsi=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
<br>
&nbsp;&nbsp;&nbsp; xmlns="XML" name="parameters"&gt;
<br>
<br>
This is the exception I get :
<br>
<br>
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
'XML' not found. (<a class="moz-txt-link-freetext" href="file:../parameters-XML.ecore">file:../parameters-XML.ecore </a>, 4, 35)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
<br>
at
org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
<br>
at
org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
<br>
at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
<br>
at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
<br>
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
<br>
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
<br>
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
<br>
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
<br>
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
<br>
at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
<br>
at org.eclipse.jface.window.Window.open(Window.java:796)
<br>
at
org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
<br>
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
<br>
at
org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
<br>
at
org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
<br>
at
org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
<br>
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
<br>
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
<br>
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
<br>
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
<br>
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
<br>
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
<br>
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
<br>
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
<br>
at
org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
<br>
at
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
<br>
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
<br>
at
org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
<br>
at
org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
<br>
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
<br>
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
<br>
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
<br>
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
<br>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
<br>
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
<br>
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
<br>
at java.lang.reflect.Method.invoke(Unknown Source)
<br>
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
<br>
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
<br>
at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
<br>
at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
<br>
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
with uri 'XML' not found. (<a class="moz-txt-link-freetext" href="file:.../parameters-XML.ecore">file:.../parameters-XML.ecore </a>, 4, 35)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
<br>
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
Source)
<br>
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
Source)
<br>
at javax.xml.parsers.SAXParser.parse(Unknown Source)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
<br>
..... 42 more
<br>
<br>
Eclipse 3.3.1.1, EMF 2.3.1
<br>
<br>
Any ideas?
<br>
<br>
Thanks,
<br>
Rene
<br>
w
<br>
<blockquote type="cite">Chris Giblin wrote:
<br>
<blockquote type="cite">Ed,
<br>
Mega-cool. That did it:
<br>
<br>
&nbsp; HashMap options = new HashMap(5);
<br>
&nbsp; options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; new Boolean(true));
<br>
&nbsp; resource.load(new FileInputStream(filename),options);
<br>
<br>
Thanks much,
<br>
&nbsp;Chris
<br>
<br>
<br>
Ed Merks wrote:
<br>
<br>
<blockquote type="cite">Chris,
<br>
<br>
The exceptions are recorded and aren't thrown until the whole document
is
<br>
processed, so you should pretty much already have what you want if you
just
<br>
ignore errors.&nbsp; You should maybe consider using wildcards to allow such
<br>
content in the document rather than just wanting the errors to be
ignored.
<br>
Another feature you can take advantage of is to use
<br>
XMLResource.OPTION_RECORD_UNKNOWN_FEATURES.&nbsp; When this is enabled, all
the
<br>
unrecognized content is recorded in the EObjectToExtensionMap and no
<br>
exceptions produced.
<br>
<br>
<br>
Chris Giblin wrote:
<br>
<br>
<br>
<blockquote type="cite">Hi,
<br>
I want to be able to load an object instance from persistent storage in
<br>
XML, such that the XML document is allowed to contain namespaces which
<br>
were unknown at the time the ecore was created.
<br>
<br>
Example: I generate a model using the library.xsd from the EMF
Overview.
<br>
I progammatically instantiated a library and book and persist it using
<br>
XMLResourceImpl. I load that same XML document and instantiate the
<br>
Library. So far, so good.
<br>
<br>
Now I introduce an element from another non-modelled namespace, foo:
<br>
<br>
&lt;?xml version="1.0" encoding="ASCII"?&gt;
<br>
&lt;library:Library
xmlns:library=<a class="moz-txt-link-rfc2396E" href="http://www.example.eclipse.org/Library">"http://www.example.eclipse.org/Library"</a>&gt;
<br>
<br>
&nbsp; &lt;m:foo xmlns:m=<a class="moz-txt-link-rfc2396E" href="http://example.org/foo">"http://example.org/foo"</a>&gt;
<br>
&nbsp;&nbsp; &lt;m:msg&gt;Have a nice day&lt;/m:msg&gt;
<br>
&nbsp; &lt;/m:foo&gt;
<br>
<br>
&nbsp; &lt;books title="Once upon a time" pages="100" category="Biography"
<br>
author="/1"/&gt;
<br>
&lt;/library:Library&gt;
<br>
<br>
I receive an exception:
<br>
org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
<br>
found. (<a class="moz-txt-link-freetext" href="file:/c:/dev/libraryEmfXsd/xml/lib2.xml">file:/c:/dev/libraryEmfXsd/xml/lib2.xml </a>, 4, 43)
<br>
<br>
Is it possible that the unknown namespace, foo, is ignored and I still
<br>
instantiate the library portion without exception?
<br>
<br>
Thanks,
<br>
chris
<br>
<br>
PS. Here is my code to load:
<br>
<br>
File file = new File("xml/lib2.xml");
<br>
String filename = file.getAbsolutePath();
<br>
URI uri = URI.createFileURI(filename);
<br>
<br>
LibraryPackage pkg = LibraryPackageImpl.init();
<br>
<br>
ResourceSet resourceSet = new ResourceSetImpl();
<br>
resourceSet.getResourceFactoryRegistry().
<br>
&nbsp;&nbsp;&nbsp; getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
<br>
<br>
Resource resource = resourceSet.createResource(uri);
<br>
resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
<br>
</blockquote>
<br>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
</body>
</html>

--------------070907090505090104040205--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415917 is a reply to message #415911] Fri, 11 January 2008 08:51 Go to previous messageGo to next message
Rene Ladan is currently offline Rene LadanFriend
Messages: 51
Registered: July 2009
Member
Ed Merks wrote:
> Rene,
>
> Does it call this method in XMLHandler and if so, I wonder why it
> doesn't return a demand created package for your URI:
>
> protected EPackage handleMissingPackage(String uriString)
> {
> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
> {
> return xmlSchemaTypePackage;
> }
> else if (extendedMetaData != null)
> {
> if (recordUnknownFeature)
> {
> return extendedMetaData.demandPackage(uriString);
> }
>
The XyzEditor.java file imports XMLHandler but leaves it otherwise unused.
According to the (partial) backtrace, handleMissingPackage() is not called.
I've tried setting up breakpoints for PackageNotFoundException and at the
start of method handleChangedResources() but they get skipped over (running
in debug view).

>
> Rene Ladan wrote:
>> Rene Ladan wrote:
>>> This works nice indeed.
>>> But is there a way to preserve the original namespaces/prefixes upon
>>> save?
>>> I assume they get overridden by the namespace of the ecore model upon
>>> which
>>> the editor is based when loading the model.
>>>
>> The trick doesn't seem to work when loading external resources having
>> foreign
>> namespaces without a prefix.
>> The following model has a namespace "XML" without a prefix. The
>> element Root
>> is non-abstract and referenced via a cross-resource reference.
>>
>> <Root xmi:version="2.0"
>> xmlns:xmi="http://www.omg.org/XMI"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns="XML" name="parameters">
>>
>> This is the exception I get :
>>
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
>> 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>
>> at
>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>
>> at
>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>
>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>> at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>> at
>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>> at org.eclipse.jface.window.Window.open(Window.java:796)
>> at
>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>
>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>> at
>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>
>> at
>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>
>> at
>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>
>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>> at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>> at
>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>
>> at
>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>
>> at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>> at
>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>
>> at
>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>
>> at
>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>
>> at
>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>
>> at
>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>
>> at
>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>
>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>> at java.lang.reflect.Method.invoke(Unknown Source)
>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
>> with uri 'XML' not found. (file:.../parameters-XML.ecore, 4, 35)
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>
>> at
>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>> Source)
>> at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>> Source)
>> at
>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>> Source)
>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>> at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>> at
>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>
>> at
>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>
>> .... 42 more
>>
>> Eclipse 3.3.1.1, EMF 2.3.1
>>
>> Any ideas?
>>
>> Thanks,
>> Rene
>> w
>>> Chris Giblin wrote:
>>>> Ed,
>>>> Mega-cool. That did it:
>>>>
>>>> HashMap options = new HashMap(5);
>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>> new Boolean(true));
>>>> resource.load(new FileInputStream(filename),options);
>>>>
>>>> Thanks much,
>>>> Chris
>>>>
>>>>
>>>> Ed Merks wrote:
>>>>
>>>>> Chris,
>>>>>
>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>> document is
>>>>> processed, so you should pretty much already have what you want if
>>>>> you just
>>>>> ignore errors. You should maybe consider using wildcards to allow
>>>>> such
>>>>> content in the document rather than just wanting the errors to be
>>>>> ignored.
>>>>> Another feature you can take advantage of is to use
>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is enabled,
>>>>> all the
>>>>> unrecognized content is recorded in the EObjectToExtensionMap and no
>>>>> exceptions produced.
>>>>>
>>>>>
>>>>> Chris Giblin wrote:
>>>>>
>>>>>
>>>>>> Hi,
>>>>>> I want to be able to load an object instance from persistent
>>>>>> storage in
>>>>>> XML, such that the XML document is allowed to contain namespaces
>>>>>> which
>>>>>> were unknown at the time the ecore was created.
>>>>>>
>>>>>> Example: I generate a model using the library.xsd from the EMF
>>>>>> Overview.
>>>>>> I progammatically instantiated a library and book and persist it
>>>>>> using
>>>>>> XMLResourceImpl. I load that same XML document and instantiate the
>>>>>> Library. So far, so good.
>>>>>>
>>>>>> Now I introduce an element from another non-modelled namespace, foo:
>>>>>>
>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>> <library:Library
>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>
>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>> </m:foo>
>>>>>>
>>>>>> <books title="Once upon a time" pages="100" category="Biography"
>>>>>> author="/1"/>
>>>>>> </library:Library>
>>>>>>
>>>>>> I receive an exception:
>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>
>>>>>> Is it possible that the unknown namespace, foo, is ignored and I
>>>>>> still
>>>>>> instantiate the library portion without exception?
>>>>>>
>>>>>> Thanks,
>>>>>> chris
>>>>>>
>>>>>> PS. Here is my code to load:
>>>>>>
>>>>>> File file = new File("xml/lib2.xml");
>>>>>> String filename = file.getAbsolutePath();
>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>
>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>
>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>> getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
>>>>>>
>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>
>>>>>
>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415918 is a reply to message #415917] Fri, 11 January 2008 09:21 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Rene,

All three places that throw PackageNotFoundException (all in XMLHandler)
all call handleMissingPackage before throwing that exception, so I'm
confused by your analysis that handleMissingPackage isn't even called.
Keep in mind that the exception is recorded and thrown later so set a
breakpoint in the constructor of the exception. It seems impossible for
that not to get hit unless there is a bug in the debugger, which seems
unlikely. Does you debugger work at all? I.e., can you get it to stop
anywhere in any of the EMF code?


Rene Ladan wrote:
> Ed Merks wrote:
>> Rene,
>>
>> Does it call this method in XMLHandler and if so, I wonder why it
>> doesn't return a demand created package for your URI:
>>
>> protected EPackage handleMissingPackage(String uriString)
>> {
>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>> {
>> return xmlSchemaTypePackage;
>> }
>> else if (extendedMetaData != null)
>> {
>> if (recordUnknownFeature)
>> {
>> return extendedMetaData.demandPackage(uriString);
>> }
>>
> The XyzEditor.java file imports XMLHandler but leaves it otherwise
> unused.
> According to the (partial) backtrace, handleMissingPackage() is not
> called.
> I've tried setting up breakpoints for PackageNotFoundException and at the
> start of method handleChangedResources() but they get skipped over
> (running
> in debug view).
>
>>
>> Rene Ladan wrote:
>>> Rene Ladan wrote:
>>>> This works nice indeed.
>>>> But is there a way to preserve the original namespaces/prefixes
>>>> upon save?
>>>> I assume they get overridden by the namespace of the ecore model
>>>> upon which
>>>> the editor is based when loading the model.
>>>>
>>> The trick doesn't seem to work when loading external resources
>>> having foreign
>>> namespaces without a prefix.
>>> The following model has a namespace "XML" without a prefix. The
>>> element Root
>>> is non-abstract and referenced via a cross-resource reference.
>>>
>>> <Root xmi:version="2.0"
>>> xmlns:xmi="http://www.omg.org/XMI"
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>> xmlns="XML" name="parameters">
>>>
>>> This is the exception I get :
>>>
>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
>>> 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>
>>> at
>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>
>>> at
>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>
>>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>> at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>> at
>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>
>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>> at
>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>
>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>> at
>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>
>>> at
>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>
>>> at
>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>
>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>> at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>> at
>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>
>>> at
>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>
>>> at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>> at
>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>
>>> at
>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>
>>> at
>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>
>>> at
>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>
>>> at
>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>
>>> at
>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>
>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>> Package with uri 'XML' not found. (file:.../parameters-XML.ecore, 4,
>>> 35)
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>
>>> at
>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>> Source)
>>> at
>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>> Source)
>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>> at
>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>
>>> at
>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>
>>> .... 42 more
>>>
>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>
>>> Any ideas?
>>>
>>> Thanks,
>>> Rene
>>> w
>>>> Chris Giblin wrote:
>>>>> Ed,
>>>>> Mega-cool. That did it:
>>>>>
>>>>> HashMap options = new HashMap(5);
>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>> new Boolean(true));
>>>>> resource.load(new FileInputStream(filename),options);
>>>>>
>>>>> Thanks much,
>>>>> Chris
>>>>>
>>>>>
>>>>> Ed Merks wrote:
>>>>>
>>>>>> Chris,
>>>>>>
>>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>>> document is
>>>>>> processed, so you should pretty much already have what you want
>>>>>> if you just
>>>>>> ignore errors. You should maybe consider using wildcards to
>>>>>> allow such
>>>>>> content in the document rather than just wanting the errors to be
>>>>>> ignored.
>>>>>> Another feature you can take advantage of is to use
>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>> enabled, all the
>>>>>> unrecognized content is recorded in the EObjectToExtensionMap and no
>>>>>> exceptions produced.
>>>>>>
>>>>>>
>>>>>> Chris Giblin wrote:
>>>>>>
>>>>>>
>>>>>>> Hi,
>>>>>>> I want to be able to load an object instance from persistent
>>>>>>> storage in
>>>>>>> XML, such that the XML document is allowed to contain namespaces
>>>>>>> which
>>>>>>> were unknown at the time the ecore was created.
>>>>>>>
>>>>>>> Example: I generate a model using the library.xsd from the EMF
>>>>>>> Overview.
>>>>>>> I progammatically instantiated a library and book and persist it
>>>>>>> using
>>>>>>> XMLResourceImpl. I load that same XML document and instantiate the
>>>>>>> Library. So far, so good.
>>>>>>>
>>>>>>> Now I introduce an element from another non-modelled namespace,
>>>>>>> foo:
>>>>>>>
>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>> <library:Library
>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>
>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>> </m:foo>
>>>>>>>
>>>>>>> <books title="Once upon a time" pages="100" category="Biography"
>>>>>>> author="/1"/>
>>>>>>> </library:Library>
>>>>>>>
>>>>>>> I receive an exception:
>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
>>>>>>> 'foo' not
>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>
>>>>>>> Is it possible that the unknown namespace, foo, is ignored and I
>>>>>>> still
>>>>>>> instantiate the library portion without exception?
>>>>>>>
>>>>>>> Thanks,
>>>>>>> chris
>>>>>>>
>>>>>>> PS. Here is my code to load:
>>>>>>>
>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>> String filename = file.getAbsolutePath();
>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>
>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>
>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>> XMLResourceFactoryImpl());
>>>>>>>
>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>
>>>>>>
>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415924 is a reply to message #415918] Fri, 11 January 2008 12:12 Go to previous messageGo to next message
Rene Ladan is currently offline Rene LadanFriend
Messages: 51
Registered: July 2009
Member
Ed Merks wrote:
> Rene,
>
> All three places that throw PackageNotFoundException (all in XMLHandler)
> all call handleMissingPackage before throwing that exception, so I'm
> confused by your analysis that handleMissingPackage isn't even called.
> Keep in mind that the exception is recorded and thrown later so set a
> breakpoint in the constructor of the exception.
It turns out that the exception is indeed thrown, as is ClassNotFoundException()
(the EMF variant). I also tried changing the namespace to "http://www.w3.org/XML"
(not official, but it looks like XML has none), but that didn't help either (it
tries to substitute the html namespace). An empty namespace gives an exception
similar to the first one.

> It seems impossible for
> that not to get hit unless there is a bug in the debugger, which seems
> unlikely. Does you debugger work at all? I.e., can you get it to stop
> anywhere in any of the EMF code?
>
Yes, I can set a breakpoint at initializeEditingDomain() upon which the
IDE stops.

>
> Rene Ladan wrote:
>> Ed Merks wrote:
>>> Rene,
>>>
>>> Does it call this method in XMLHandler and if so, I wonder why it
>>> doesn't return a demand created package for your URI:
>>>
>>> protected EPackage handleMissingPackage(String uriString)
>>> {
>>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>>> {
>>> return xmlSchemaTypePackage;
>>> }
>>> else if (extendedMetaData != null)
>>> {
>>> if (recordUnknownFeature)
>>> {
>>> return extendedMetaData.demandPackage(uriString);
>>> }
>>>
>> The XyzEditor.java file imports XMLHandler but leaves it otherwise
>> unused.
>> According to the (partial) backtrace, handleMissingPackage() is not
>> called.
>> I've tried setting up breakpoints for PackageNotFoundException and at the
>> start of method handleChangedResources() but they get skipped over
>> (running
>> in debug view).
>>
>>>
>>> Rene Ladan wrote:
>>>> Rene Ladan wrote:
>>>>> This works nice indeed.
>>>>> But is there a way to preserve the original namespaces/prefixes
>>>>> upon save?
>>>>> I assume they get overridden by the namespace of the ecore model
>>>>> upon which
>>>>> the editor is based when loading the model.
>>>>>
>>>> The trick doesn't seem to work when loading external resources
>>>> having foreign
>>>> namespaces without a prefix.
>>>> The following model has a namespace "XML" without a prefix. The
>>>> element Root
>>>> is non-abstract and referenced via a cross-resource reference.
>>>>
>>>> <Root xmi:version="2.0"
>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>> xmlns="XML" name="parameters">
>>>>
>>>> This is the exception I get :
>>>>
>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
>>>> 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>>
>>>> at
>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>>
>>>> at
>>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>>
>>>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>>> at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>>> at
>>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>>
>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>>> at
>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>>
>>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>>> at
>>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>>
>>>> at
>>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>>
>>>> at
>>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>>
>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>> at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>>> at
>>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>>
>>>> at
>>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>>
>>>> at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>>> at
>>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>>
>>>> at
>>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>>
>>>> at
>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>>
>>>> at
>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>>
>>>> at
>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>>
>>>> at
>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>>
>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>>> Package with uri 'XML' not found. (file:.../parameters-XML.ecore, 4,
>>>> 35)
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>>
>>>> at
>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>>> Source)
>>>> at
>>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>>> Source)
>>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>>> at
>>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>>
>>>> at
>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>>
>>>> .... 42 more
>>>>
>>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>>
>>>> Any ideas?
>>>>
>>>> Thanks,
>>>> Rene
>>>> w
>>>>> Chris Giblin wrote:
>>>>>> Ed,
>>>>>> Mega-cool. That did it:
>>>>>>
>>>>>> HashMap options = new HashMap(5);
>>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>>> new Boolean(true));
>>>>>> resource.load(new FileInputStream(filename),options);
>>>>>>
>>>>>> Thanks much,
>>>>>> Chris
>>>>>>
>>>>>>
>>>>>> Ed Merks wrote:
>>>>>>
>>>>>>> Chris,
>>>>>>>
>>>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>>>> document is
>>>>>>> processed, so you should pretty much already have what you want
>>>>>>> if you just
>>>>>>> ignore errors. You should maybe consider using wildcards to
>>>>>>> allow such
>>>>>>> content in the document rather than just wanting the errors to be
>>>>>>> ignored.
>>>>>>> Another feature you can take advantage of is to use
>>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>>> enabled, all the
>>>>>>> unrecognized content is recorded in the EObjectToExtensionMap and no
>>>>>>> exceptions produced.
>>>>>>>
>>>>>>>
>>>>>>> Chris Giblin wrote:
>>>>>>>
>>>>>>>
>>>>>>>> Hi,
>>>>>>>> I want to be able to load an object instance from persistent
>>>>>>>> storage in
>>>>>>>> XML, such that the XML document is allowed to contain namespaces
>>>>>>>> which
>>>>>>>> were unknown at the time the ecore was created.
>>>>>>>>
>>>>>>>> Example: I generate a model using the library.xsd from the EMF
>>>>>>>> Overview.
>>>>>>>> I progammatically instantiated a library and book and persist it
>>>>>>>> using
>>>>>>>> XMLResourceImpl. I load that same XML document and instantiate the
>>>>>>>> Library. So far, so good.
>>>>>>>>
>>>>>>>> Now I introduce an element from another non-modelled namespace,
>>>>>>>> foo:
>>>>>>>>
>>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>>> <library:Library
>>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>>
>>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>>> </m:foo>
>>>>>>>>
>>>>>>>> <books title="Once upon a time" pages="100" category="Biography"
>>>>>>>> author="/1"/>
>>>>>>>> </library:Library>
>>>>>>>>
>>>>>>>> I receive an exception:
>>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
>>>>>>>> 'foo' not
>>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>>
>>>>>>>> Is it possible that the unknown namespace, foo, is ignored and I
>>>>>>>> still
>>>>>>>> instantiate the library portion without exception?
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> chris
>>>>>>>>
>>>>>>>> PS. Here is my code to load:
>>>>>>>>
>>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>>> String filename = file.getAbsolutePath();
>>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>>
>>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>>
>>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>>> XMLResourceFactoryImpl());
>>>>>>>>
>>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>>> resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>>
>>>>>>>
>>>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415925 is a reply to message #415924] Fri, 11 January 2008 12:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------090006020605040809090509
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Rene,

If I change the LibraryExample.java generated in the *.tests project
generated for the XML Schema library tutorial like this:

XMLResource resource =
(XMLResource)resourceSet.createResource(URI.createURI("http:///My.library"));
DocumentRoot documentRoot =
LibraryFactory.eINSTANCE.createDocumentRoot();
Library root = LibraryFactory.eINSTANCE.createLibrary();
documentRoot.setLibrary(root);
resource.getContents().add(documentRoot);
Map<Object, Object> options = new HashMap<Object, Object>();
// options.put(XMLResource.OPTION_USE_FILE_BUFFER, Boolean.TRUE);
resource.save(System.out, options);
StringWriter stringWriter = new StringWriter();
resource.save(stringWriter, null);
System.out.println();

XMLResource resource2 =
(XMLResource)resourceSet.createResource(URI.createURI("http:///My2.library"));
resource2.load(new InputSource(new
StringReader(stringWriter.getBuffer().toString().replace("xmlns:library ",
"xmlns:other").replace("library:library", "other:library"))), null);
resource2.save(System.out, null);
System.out.println();

XMLResource resource3 =
(XMLResource)resourceSet.createResource(URI.createURI("http:///My2.library"));
options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
Boolean.TRUE);
resource3.load(new InputSource(new
StringReader(stringWriter.getBuffer().toString().replace("www.example.com ",
"www.example.org"))), options);
resource3.save(System.out, null);

it prints out this:

Enter a list of file paths or URIs that have content like this:
<?xml version="1.0" encoding="ASCII"?>
<library:library xmlns:library="http://www.example.com/Library"/>
<?xml version="1.0" encoding="ASCII"?>
<other:library xmlns:other="http://www.example.com/Library"/>
<?xml version="1.0" encoding="ASCII"?>
<library:library xmlns:library="http://www.example.org/Library"/>

So it appears to be working well. It's hard to comment on what's going
wrong for you because I'm not actually what you're doing and you don't
seem to be able to set a breakpoint and stop in handleMissingPackage, or
did you actually manage that?


Rene Ladan wrote:
> Ed Merks wrote:
>> Rene,
>>
>> All three places that throw PackageNotFoundException (all in
>> XMLHandler) all call handleMissingPackage before throwing that
>> exception, so I'm confused by your analysis that handleMissingPackage
>> isn't even called. Keep in mind that the exception is recorded and
>> thrown later so set a breakpoint in the constructor of the exception.
> It turns out that the exception is indeed thrown, as is
> ClassNotFoundException()
> (the EMF variant). I also tried changing the namespace to
> "http://www.w3.org/XML"
> (not official, but it looks like XML has none), but that didn't help
> either (it
> tries to substitute the html namespace). An empty namespace gives an
> exception
> similar to the first one.
>
>> It seems impossible for that not to get hit unless there is a bug in
>> the debugger, which seems unlikely. Does you debugger work at all?
>> I.e., can you get it to stop anywhere in any of the EMF code?
>>
> Yes, I can set a breakpoint at initializeEditingDomain() upon which the
> IDE stops.
>
>>
>> Rene Ladan wrote:
>>> Ed Merks wrote:
>>>> Rene,
>>>>
>>>> Does it call this method in XMLHandler and if so, I wonder why it
>>>> doesn't return a demand created package for your URI:
>>>>
>>>> protected EPackage handleMissingPackage(String uriString)
>>>> {
>>>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>>>> {
>>>> return xmlSchemaTypePackage;
>>>> }
>>>> else if (extendedMetaData != null)
>>>> {
>>>> if (recordUnknownFeature)
>>>> {
>>>> return extendedMetaData.demandPackage(uriString);
>>>> }
>>>>
>>> The XyzEditor.java file imports XMLHandler but leaves it otherwise
>>> unused.
>>> According to the (partial) backtrace, handleMissingPackage() is not
>>> called.
>>> I've tried setting up breakpoints for PackageNotFoundException and
>>> at the
>>> start of method handleChangedResources() but they get skipped over
>>> (running
>>> in debug view).
>>>
>>>>
>>>> Rene Ladan wrote:
>>>>> Rene Ladan wrote:
>>>>>> This works nice indeed.
>>>>>> But is there a way to preserve the original namespaces/prefixes
>>>>>> upon save?
>>>>>> I assume they get overridden by the namespace of the ecore model
>>>>>> upon which
>>>>>> the editor is based when loading the model.
>>>>>>
>>>>> The trick doesn't seem to work when loading external resources
>>>>> having foreign
>>>>> namespaces without a prefix.
>>>>> The following model has a namespace "XML" without a prefix. The
>>>>> element Root
>>>>> is non-abstract and referenced via a cross-resource reference.
>>>>>
>>>>> <Root xmi:version="2.0"
>>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>> xmlns="XML" name="parameters">
>>>>>
>>>>> This is the exception I get :
>>>>>
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with
>>>>> uri 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>>>
>>>>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>>>> at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>>>> at
>>>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>>>
>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>> at
>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>>>> at
>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>>>
>>>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>>>> at
>>>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>>>
>>>>> at
>>>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>>>
>>>>> at
>>>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>>>
>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>> at
>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>> at
>>>>> org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>>>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>>>> at
>>>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>>>
>>>>> at
>>>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>>>
>>>>> at
>>>>> org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>>>> at
>>>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>>>
>>>>> at
>>>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>>>
>>>>> at
>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>>>
>>>>> at
>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>>>
>>>>> at
>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>>>
>>>>> at
>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>>>
>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>>>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>>>> Package with uri 'XML' not found. (file:.../parameters-XML.ecore,
>>>>> 4, 35)
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>>>
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>>>> Source)
>>>>> at
>>>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>>>> Source)
>>>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>>>> at
>>>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>>>
>>>>> at
>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>>>
>>>>> .... 42 more
>>>>>
>>>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>>>
>>>>> Any ideas?
>>>>>
>>>>> Thanks,
>>>>> Rene
>>>>> w
>>>>>> Chris Giblin wrote:
>>>>>>> Ed,
>>>>>>> Mega-cool. That did it:
>>>>>>>
>>>>>>> HashMap options = new HashMap(5);
>>>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>>>> new Boolean(true));
>>>>>>> resource.load(new FileInputStream(filename),options);
>>>>>>>
>>>>>>> Thanks much,
>>>>>>> Chris
>>>>>>>
>>>>>>>
>>>>>>> Ed Merks wrote:
>>>>>>>
>>>>>>>> Chris,
>>>>>>>>
>>>>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>>>>> document is
>>>>>>>> processed, so you should pretty much already have what you want
>>>>>>>> if you just
>>>>>>>> ignore errors. You should maybe consider using wildcards to
>>>>>>>> allow such
>>>>>>>> content in the document rather than just wanting the errors to
>>>>>>>> be ignored.
>>>>>>>> Another feature you can take advantage of is to use
>>>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>>>> enabled, all the
>>>>>>>> unrecognized content is recorded in the EObjectToExtensionMap
>>>>>>>> and no
>>>>>>>> exceptions produced.
>>>>>>>>
>>>>>>>>
>>>>>>>> Chris Giblin wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>> Hi,
>>>>>>>>> I want to be able to load an object instance from persistent
>>>>>>>>> storage in
>>>>>>>>> XML, such that the XML document is allowed to contain
>>>>>>>>> namespaces which
>>>>>>>>> were unknown at the time the ecore was created.
>>>>>>>>>
>>>>>>>>> Example: I generate a model using the library.xsd from the EMF
>>>>>>>>> Overview.
>>>>>>>>> I progammatically instantiated a library and book and persist
>>>>>>>>> it using
>>>>>>>>> XMLResourceImpl. I load that same XML document and instantiate
>>>>>>>>> the
>>>>>>>>> Library. So far, so good.
>>>>>>>>>
>>>>>>>>> Now I introduce an element from another non-modelled
>>>>>>>>> namespace, foo:
>>>>>>>>>
>>>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>>>> <library:Library
>>>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>>>
>>>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>>>> </m:foo>
>>>>>>>>>
>>>>>>>>> <books title="Once upon a time" pages="100"
>>>>>>>>> category="Biography"
>>>>>>>>> author="/1"/>
>>>>>>>>> </library:Library>
>>>>>>>>>
>>>>>>>>> I receive an exception:
>>>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
>>>>>>>>> 'foo' not
>>>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>>>
>>>>>>>>> Is it possible that the unknown namespace, foo, is ignored and
>>>>>>>>> I still
>>>>>>>>> instantiate the library portion without exception?
>>>>>>>>>
>>>>>>>>> Thanks,
>>>>>>>>> chris
>>>>>>>>>
>>>>>>>>> PS. Here is my code to load:
>>>>>>>>>
>>>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>>>> String filename = file.getAbsolutePath();
>>>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>>>
>>>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>>>
>>>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>>>> XMLResourceFactoryImpl());
>>>>>>>>>
>>>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>>>> resource.load(new
>>>>>>>>> FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>>>
>>>>>>>>
>>>>


--------------090006020605040809090509
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rene,<br>
<br>
If I change the LibraryExample.java generated in the *.tests project
generated for the XML Schema library tutorial like this:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; XMLResource resource =
(XMLResource)resourceSet.createResource(URI.createURI(<a class="moz-txt-link-rfc2396E" href="http:///My.library">"http:///My.library"</a>));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; DocumentRoot documentRoot =
LibraryFactory.eINSTANCE.createDocumentRoot();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Library root = LibraryFactory.eINSTANCE.createLibrary();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; documentRoot.setLibrary(root);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource.getContents().add(documentRoot);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; Map&lt;Object, Object&gt; options = new HashMap&lt;Object,
Object&gt;();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; // options.put(XMLResource.OPTION_USE_FILE_BUFFER,
Boolean.TRUE);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource.save(System.out, options);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; StringWriter stringWriter = new StringWriter();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource.save(stringWriter, null);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; System.out.println();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; XMLResource resource2 =
(XMLResource)resourceSet.createResource(URI.createURI(<a class="moz-txt-link-rfc2396E" href="http:///My2.library">"http:///My2.library"</a>));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource2.load(new InputSource(new
StringReader(stringWriter.getBuffer().toString().replace("xmlns:library ",
"xmlns:other").replace("library:library", "other:library"))), null);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource2.save(System.out, null);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; System.out.println();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; XMLResource resource3 =
(XMLResource)resourceSet.createResource(URI.createURI(<a class="moz-txt-link-rfc2396E" href="http:///My2.library">"http:///My2.library"</a>));<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
Boolean.TRUE);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource3.load(new InputSource(new
StringReader(stringWriter.getBuffer().toString().replace("<a class="moz-txt-link-abbreviated" href="http://www.example.com">www.example.com</a>",
"<a class="moz-txt-link-abbreviated" href="http://www.example.org">www.example.org</a>"))), options);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; resource3.save(System.out, null);<br>
<br>
it prints out this:<br>
<blockquote>Enter a list of file paths or URIs that have content like
this:<br>
&lt;?xml version="1.0" encoding="ASCII"?&gt;<br>
&lt;library:library xmlns:library=<a class="moz-txt-link-rfc2396E" href="http://www.example.com/Library">"http://www.example.com/Library"</a>/&gt;<br>
&lt;?xml version="1.0" encoding="ASCII"?&gt;<br>
&lt;other:library xmlns:other=<a class="moz-txt-link-rfc2396E" href="http://www.example.com/Library">"http://www.example.com/Library"</a>/&gt;<br>
&lt;?xml version="1.0" encoding="ASCII"?&gt;<br>
&lt;library:library xmlns:library=<a class="moz-txt-link-rfc2396E" href="http://www.example.org/Library">"http://www.example.org/Library"</a>/&gt;<br>
</blockquote>
So it appears to be working well.&nbsp;&nbsp; It's hard to comment on what's
going wrong for you because I'm not actually what you're doing and you
don't seem to be able to set a breakpoint and stop in
handleMissingPackage, or did you actually manage that?<br>
<br>
<br>
Rene Ladan wrote:
<blockquote cite="mid:fm7mg4$o0l$1@build.eclipse.org" type="cite">Ed
Merks wrote:
<br>
<blockquote type="cite">Rene,
<br>
<br>
All three places that throw PackageNotFoundException (all in
XMLHandler) all call handleMissingPackage before throwing that
exception, so I'm confused by your analysis that handleMissingPackage
isn't even called.&nbsp; Keep in mind that the exception is recorded and
thrown later so set a breakpoint in the constructor of the exception.
<br>
</blockquote>
It turns out that the exception is indeed thrown, as is
ClassNotFoundException()
<br>
(the EMF variant).&nbsp; I also tried changing the namespace to
<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/XML">"http://www.w3.org/XML"</a>
<br>
(not official, but it looks like XML has none), but that didn't help
either (it
<br>
tries to substitute the html namespace).&nbsp; An empty namespace gives an
exception
<br>
similar to the first one.
<br>
<br>
<blockquote type="cite">&nbsp;It seems impossible for that not to get hit
unless there is a bug in the debugger, which seems unlikely.&nbsp; Does you
debugger work at all?&nbsp; I.e., can you get it to stop anywhere in any of
the EMF code?
<br>
<br>
</blockquote>
Yes, I can set a breakpoint at initializeEditingDomain() upon which the
<br>
IDE stops.
<br>
<br>
<blockquote type="cite"><br>
Rene Ladan wrote:
<br>
<blockquote type="cite">Ed Merks wrote:
<br>
<blockquote type="cite">Rene,
<br>
<br>
Does it call this method in XMLHandler and if so, I wonder why it
doesn't return a demand created package for your URI:
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected EPackage handleMissingPackage(String uriString)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if (XMLResource.XML_SCHEMA_URI.equals(uriString))
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return xmlSchemaTypePackage;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; }
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; else if (extendedMetaData != null)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (recordUnknownFeature)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return extendedMetaData.demandPackage(uriString);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<br>
<br>
</blockquote>
The XyzEditor.java file imports XMLHandler but leaves it otherwise
unused.
<br>
According to the (partial) backtrace, handleMissingPackage() is not
called.
<br>
I've tried setting up breakpoints for PackageNotFoundException and at
the
<br>
start of method handleChangedResources() but they get skipped over
(running
<br>
in debug view).
<br>
<br>
<blockquote type="cite"><br>
Rene Ladan wrote:
<br>
<blockquote type="cite">Rene Ladan wrote:
<br>
<blockquote type="cite">This works nice indeed.
<br>
But is there a way to preserve the original namespaces/prefixes upon
save?
<br>
I assume they get overridden by the namespace of the ecore model upon
which
<br>
the editor is based when loading the model.
<br>
<br>
</blockquote>
The trick doesn't seem to work when loading external resources having
foreign
<br>
namespaces without a prefix.
<br>
The following model has a namespace "XML" without a prefix.&nbsp; The
element Root
<br>
is non-abstract and referenced via a cross-resource reference.
<br>
<br>
&lt;Root xmi:version="2.0"
<br>
&nbsp;&nbsp;&nbsp; xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://www.omg.org/XMI">"http://www.omg.org/XMI"</a>
xmlns:xsi=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
<br>
&nbsp;&nbsp;&nbsp; xmlns="XML" name="parameters"&gt;
<br>
<br>
This is the exception I get :
<br>
<br>
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
'XML' not found. (<a class="moz-txt-link-freetext" href="file:../parameters-XML.ecore">file:../parameters-XML.ecore </a>, 4, 35)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
<br>
at
org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
<br>
at
org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
<br>
at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
<br>
at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
<br>
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
<br>
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
<br>
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
<br>
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
<br>
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
<br>
at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
<br>
at org.eclipse.jface.window.Window.open(Window.java:796)
<br>
at
org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
<br>
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
<br>
at
org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
<br>
at
org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
<br>
at
org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
<br>
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
<br>
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
<br>
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
<br>
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
<br>
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
<br>
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
<br>
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
<br>
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
<br>
at
org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
<br>
at
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
<br>
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
<br>
at
org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
<br>
at
org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
<br>
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
<br>
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
<br>
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
<br>
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
<br>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
<br>
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
<br>
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
<br>
at java.lang.reflect.Method.invoke(Unknown Source)
<br>
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
<br>
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
<br>
at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
<br>
at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
<br>
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
with uri 'XML' not found. (<a class="moz-txt-link-freetext" href="file:.../parameters-XML.ecore">file:.../parameters-XML.ecore </a>, 4, 35)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
<br>
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
Source)
<br>
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
Source)
<br>
at javax.xml.parsers.SAXParser.parse(Unknown Source)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
<br>
..... 42 more
<br>
<br>
Eclipse 3.3.1.1, EMF 2.3.1
<br>
<br>
Any ideas?
<br>
<br>
Thanks,
<br>
Rene
<br>
w
<br>
<blockquote type="cite">Chris Giblin wrote:
<br>
<blockquote type="cite">Ed,
<br>
Mega-cool. That did it:
<br>
<br>
&nbsp; HashMap options = new HashMap(5);
<br>
&nbsp; options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; new Boolean(true));
<br>
&nbsp; resource.load(new FileInputStream(filename),options);
<br>
<br>
Thanks much,
<br>
&nbsp;Chris
<br>
<br>
<br>
Ed Merks wrote:
<br>
<br>
<blockquote type="cite">Chris,
<br>
<br>
The exceptions are recorded and aren't thrown until the whole document
is
<br>
processed, so you should pretty much already have what you want if you
just
<br>
ignore errors.&nbsp; You should maybe consider using wildcards to allow such
<br>
content in the document rather than just wanting the errors to be
ignored.
<br>
Another feature you can take advantage of is to use
<br>
XMLResource.OPTION_RECORD_UNKNOWN_FEATURES.&nbsp; When this is enabled, all
the
<br>
unrecognized content is recorded in the EObjectToExtensionMap and no
<br>
exceptions produced.
<br>
<br>
<br>
Chris Giblin wrote:
<br>
<br>
<br>
<blockquote type="cite">Hi,
<br>
I want to be able to load an object instance from persistent storage in
<br>
XML, such that the XML document is allowed to contain namespaces which
<br>
were unknown at the time the ecore was created.
<br>
<br>
Example: I generate a model using the library.xsd from the EMF
Overview.
<br>
I progammatically instantiated a library and book and persist it using
<br>
XMLResourceImpl. I load that same XML document and instantiate the
<br>
Library. So far, so good.
<br>
<br>
Now I introduce an element from another non-modelled namespace, foo:
<br>
<br>
&lt;?xml version="1.0" encoding="ASCII"?&gt;
<br>
&lt;library:Library
xmlns:library=<a class="moz-txt-link-rfc2396E" href="http://www.example.eclipse.org/Library">"http://www.example.eclipse.org/Library"</a>&gt;
<br>
<br>
&nbsp; &lt;m:foo xmlns:m=<a class="moz-txt-link-rfc2396E" href="http://example.org/foo">"http://example.org/foo"</a>&gt;
<br>
&nbsp;&nbsp; &lt;m:msg&gt;Have a nice day&lt;/m:msg&gt;
<br>
&nbsp; &lt;/m:foo&gt;
<br>
<br>
&nbsp; &lt;books title="Once upon a time" pages="100" category="Biography"
<br>
author="/1"/&gt;
<br>
&lt;/library:Library&gt;
<br>
<br>
I receive an exception:
<br>
org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
<br>
found. (<a class="moz-txt-link-freetext" href="file:/c:/dev/libraryEmfXsd/xml/lib2.xml">file:/c:/dev/libraryEmfXsd/xml/lib2.xml </a>, 4, 43)
<br>
<br>
Is it possible that the unknown namespace, foo, is ignored and I still
<br>
instantiate the library portion without exception?
<br>
<br>
Thanks,
<br>
chris
<br>
<br>
PS. Here is my code to load:
<br>
<br>
File file = new File("xml/lib2.xml");
<br>
String filename = file.getAbsolutePath();
<br>
URI uri = URI.createFileURI(filename);
<br>
<br>
LibraryPackage pkg = LibraryPackageImpl.init();
<br>
<br>
ResourceSet resourceSet = new ResourceSetImpl();
<br>
resourceSet.getResourceFactoryRegistry().
<br>
&nbsp;&nbsp;&nbsp; getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
<br>
<br>
Resource resource = resourceSet.createResource(uri);
<br>
resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
<br>
</blockquote>
<br>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
</body>
</html>

--------------090006020605040809090509--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415927 is a reply to message #415925] Fri, 11 January 2008 14:17 Go to previous messageGo to next message
Rene Ladan is currently offline Rene LadanFriend
Messages: 51
Registered: July 2009
Member
Ed Merks wrote:
> Rene,
>
[..]
> So it appears to be working well. It's hard to comment on what's going
> wrong for you because I'm not actually what you're doing and you don't
> seem to be able to set a breakpoint and stop in handleMissingPackage, or
> did you actually manage that?
I managed to set a breakpoint there. If I add a prefixless namespace having
value "XML", this happens at line 2465 of XMLHandler.java:
uriString == "XML"
extendedMetadata == null
return null
This produces a PackageNotFoundException, but the program keeps trying to add
a node "Root" to the top of the (new?) tree. This fails too because, causing
a ClassNotFoundException which gets thrown using the error() method.

The same exceptions occur if I open the file (attached) in the Sample EMF Editor.
After performing "Register metamodel" on the metamodel (an ATL menu extension)
the exception is still thrown. However, after performing the same action on a partial
metamodel describing only the relevant part where the package name is "XML", the
file is actually editable in the Sample EMF Editor.

Maybe I should just remove it from the metamodel since it's not really worth the
trouble...
>
>
> Rene Ladan wrote:
>> Ed Merks wrote:
>>> Rene,
>>>
>>> All three places that throw PackageNotFoundException (all in
>>> XMLHandler) all call handleMissingPackage before throwing that
>>> exception, so I'm confused by your analysis that handleMissingPackage
>>> isn't even called. Keep in mind that the exception is recorded and
>>> thrown later so set a breakpoint in the constructor of the exception.
>> It turns out that the exception is indeed thrown, as is
>> ClassNotFoundException()
>> (the EMF variant). I also tried changing the namespace to
>> "http://www.w3.org/XML"
>> (not official, but it looks like XML has none), but that didn't help
>> either (it
>> tries to substitute the html namespace). An empty namespace gives an
>> exception
>> similar to the first one.
>>
>>> It seems impossible for that not to get hit unless there is a bug in
>>> the debugger, which seems unlikely. Does you debugger work at all?
>>> I.e., can you get it to stop anywhere in any of the EMF code?
>>>
>> Yes, I can set a breakpoint at initializeEditingDomain() upon which the
>> IDE stops.
>>
>>>
>>> Rene Ladan wrote:
>>>> Ed Merks wrote:
>>>>> Rene,
>>>>>
>>>>> Does it call this method in XMLHandler and if so, I wonder why it
>>>>> doesn't return a demand created package for your URI:
>>>>>
>>>>> protected EPackage handleMissingPackage(String uriString)
>>>>> {
>>>>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>>>>> {
>>>>> return xmlSchemaTypePackage;
>>>>> }
>>>>> else if (extendedMetaData != null)
>>>>> {
>>>>> if (recordUnknownFeature)
>>>>> {
>>>>> return extendedMetaData.demandPackage(uriString);
>>>>> }
>>>>>
>>>> The XyzEditor.java file imports XMLHandler but leaves it otherwise
>>>> unused.
>>>> According to the (partial) backtrace, handleMissingPackage() is not
>>>> called.
>>>> I've tried setting up breakpoints for PackageNotFoundException and
>>>> at the
>>>> start of method handleChangedResources() but they get skipped over
>>>> (running
>>>> in debug view).
>>>>
>>>>>
>>>>> Rene Ladan wrote:
>>>>>> Rene Ladan wrote:
>>>>>>> This works nice indeed.
>>>>>>> But is there a way to preserve the original namespaces/prefixes
>>>>>>> upon save?
>>>>>>> I assume they get overridden by the namespace of the ecore model
>>>>>>> upon which
>>>>>>> the editor is based when loading the model.
>>>>>>>
>>>>>> The trick doesn't seem to work when loading external resources
>>>>>> having foreign
>>>>>> namespaces without a prefix.
>>>>>> The following model has a namespace "XML" without a prefix. The
>>>>>> element Root
>>>>>> is non-abstract and referenced via a cross-resource reference.
>>>>>>
>>>>>> <Root xmi:version="2.0"
>>>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>>> xmlns="XML" name="parameters">
>>>>>>
>>>>>> This is the exception I get :
>>>>>>
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>>>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with
>>>>>> uri 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>>>>
>>>>>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>>>>> at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>>>>> at
>>>>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>>>>
>>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>> at
>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>>>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>>>>> at
>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>>>>
>>>>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>>>>> at
>>>>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>>>>
>>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>> at
>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>> at
>>>>>> org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>>>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>>>>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>>>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>>>>> at
>>>>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>>>>> at
>>>>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>>>>
>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>>>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>>>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>>>>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>>>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>>>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>>>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>>>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>>>>> Package with uri 'XML' not found. (file:.../parameters-XML.ecore,
>>>>>> 4, 35)
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>>>>
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>>>>> Source)
>>>>>> at
>>>>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>>>>> Source)
>>>>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>>>>> at
>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>>>>
>>>>>> at
>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>>>>
>>>>>> .... 42 more
>>>>>>
>>>>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>>>>
>>>>>> Any ideas?
>>>>>>
>>>>>> Thanks,
>>>>>> Rene
>>>>>> w
>>>>>>> Chris Giblin wrote:
>>>>>>>> Ed,
>>>>>>>> Mega-cool. That did it:
>>>>>>>>
>>>>>>>> HashMap options = new HashMap(5);
>>>>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>>>>> new Boolean(true));
>>>>>>>> resource.load(new FileInputStream(filename),options);
>>>>>>>>
>>>>>>>> Thanks much,
>>>>>>>> Chris
>>>>>>>>
>>>>>>>>
>>>>>>>> Ed Merks wrote:
>>>>>>>>
>>>>>>>>> Chris,
>>>>>>>>>
>>>>>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>>>>>> document is
>>>>>>>>> processed, so you should pretty much already have what you want
>>>>>>>>> if you just
>>>>>>>>> ignore errors. You should maybe consider using wildcards to
>>>>>>>>> allow such
>>>>>>>>> content in the document rather than just wanting the errors to
>>>>>>>>> be ignored.
>>>>>>>>> Another feature you can take advantage of is to use
>>>>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>>>>> enabled, all the
>>>>>>>>> unrecognized content is recorded in the EObjectToExtensionMap
>>>>>>>>> and no
>>>>>>>>> exceptions produced.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Chris Giblin wrote:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>> I want to be able to load an object instance from persistent
>>>>>>>>>> storage in
>>>>>>>>>> XML, such that the XML document is allowed to contain
>>>>>>>>>> namespaces which
>>>>>>>>>> were unknown at the time the ecore was created.
>>>>>>>>>>
>>>>>>>>>> Example: I generate a model using the library.xsd from the EMF
>>>>>>>>>> Overview.
>>>>>>>>>> I progammatically instantiated a library and book and persist
>>>>>>>>>> it using
>>>>>>>>>> XMLResourceImpl. I load that same XML document and instantiate
>>>>>>>>>> the
>>>>>>>>>> Library. So far, so good.
>>>>>>>>>>
>>>>>>>>>> Now I introduce an element from another non-modelled
>>>>>>>>>> namespace, foo:
>>>>>>>>>>
>>>>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>>>>> <library:Library
>>>>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>>>>
>>>>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>>>>> </m:foo>
>>>>>>>>>>
>>>>>>>>>> <books title="Once upon a time" pages="100"
>>>>>>>>>> category="Biography"
>>>>>>>>>> author="/1"/>
>>>>>>>>>> </library:Library>
>>>>>>>>>>
>>>>>>>>>> I receive an exception:
>>>>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
>>>>>>>>>> 'foo' not
>>>>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>>>>
>>>>>>>>>> Is it possible that the unknown namespace, foo, is ignored and
>>>>>>>>>> I still
>>>>>>>>>> instantiate the library portion without exception?
>>>>>>>>>>
>>>>>>>>>> Thanks,
>>>>>>>>>> chris
>>>>>>>>>>
>>>>>>>>>> PS. Here is my code to load:
>>>>>>>>>>
>>>>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>>>>> String filename = file.getAbsolutePath();
>>>>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>>>>
>>>>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>>>>
>>>>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>>>>> XMLResourceFactoryImpl());
>>>>>>>>>>
>>>>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>>>>> resource.load(new
>>>>>>>>>> FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>>>>
>>>>>>>>>
>>>>>
>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415931 is a reply to message #415927] Fri, 11 January 2008 16:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------010003070401000107040406
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Rene,

Comments below.

Rene Ladan wrote:
> Ed Merks wrote:
>> Rene,
>>
> [..]
>> So it appears to be working well. It's hard to comment on what's
>> going wrong for you because I'm not actually what you're doing and
>> you don't seem to be able to set a breakpoint and stop in
>> handleMissingPackage, or did you actually manage that?
> I managed to set a breakpoint there. If I add a prefixless namespace
> having
> value "XML", this happens at line 2465 of XMLHandler.java:
> uriString == "XML"
> extendedMetadata == null
> return null
> This produces a PackageNotFoundException, but the program keeps trying
> to add
> a node "Root" to the top of the (new?) tree. This fails too because,
> causing
> a ClassNotFoundException which gets thrown using the error() method.
Given that the option you said you are using would have ensured that
extendedMetaData was not null, I would conclude that you aren't using
the option:

recordUnknownFeature =
Boolean.TRUE.equals(options.get(XMLResource.OPTION_RECORD_UN KNOWN_FEATURE));

if (recordUnknownFeature && extendedMetaData == null)
{
setExtendedMetaDataOption(Boolean.TRUE);
}


>
> The same exceptions occur if I open the file (attached) in the Sample
> EMF Editor.
> After performing "Register metamodel" on the metamodel (an ATL menu
> extension)
> the exception is still thrown. However, after performing the same
> action on a partial
> metamodel describing only the relevant part where the package name is
> "XML", the
> file is actually editable in the Sample EMF Editor.
I don't understand this part. I thought the whole line of discussion
was that the option above wasn't working?
>
> Maybe I should just remove it from the metamodel since it's not really
> worth the
> trouble...
>>
>>
>> Rene Ladan wrote:
>>> Ed Merks wrote:
>>>> Rene,
>>>>
>>>> All three places that throw PackageNotFoundException (all in
>>>> XMLHandler) all call handleMissingPackage before throwing that
>>>> exception, so I'm confused by your analysis that
>>>> handleMissingPackage isn't even called. Keep in mind that the
>>>> exception is recorded and thrown later so set a breakpoint in the
>>>> constructor of the exception.
>>> It turns out that the exception is indeed thrown, as is
>>> ClassNotFoundException()
>>> (the EMF variant). I also tried changing the namespace to
>>> "http://www.w3.org/XML"
>>> (not official, but it looks like XML has none), but that didn't help
>>> either (it
>>> tries to substitute the html namespace). An empty namespace gives
>>> an exception
>>> similar to the first one.
>>>
>>>> It seems impossible for that not to get hit unless there is a bug
>>>> in the debugger, which seems unlikely. Does you debugger work at
>>>> all? I.e., can you get it to stop anywhere in any of the EMF code?
>>>>
>>> Yes, I can set a breakpoint at initializeEditingDomain() upon which the
>>> IDE stops.
>>>
>>>>
>>>> Rene Ladan wrote:
>>>>> Ed Merks wrote:
>>>>>> Rene,
>>>>>>
>>>>>> Does it call this method in XMLHandler and if so, I wonder why it
>>>>>> doesn't return a demand created package for your URI:
>>>>>>
>>>>>> protected EPackage handleMissingPackage(String uriString)
>>>>>> {
>>>>>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>>>>>> {
>>>>>> return xmlSchemaTypePackage;
>>>>>> }
>>>>>> else if (extendedMetaData != null)
>>>>>> {
>>>>>> if (recordUnknownFeature)
>>>>>> {
>>>>>> return extendedMetaData.demandPackage(uriString);
>>>>>> }
>>>>>>
>>>>> The XyzEditor.java file imports XMLHandler but leaves it otherwise
>>>>> unused.
>>>>> According to the (partial) backtrace, handleMissingPackage() is
>>>>> not called.
>>>>> I've tried setting up breakpoints for PackageNotFoundException and
>>>>> at the
>>>>> start of method handleChangedResources() but they get skipped over
>>>>> (running
>>>>> in debug view).
>>>>>
>>>>>>
>>>>>> Rene Ladan wrote:
>>>>>>> Rene Ladan wrote:
>>>>>>>> This works nice indeed.
>>>>>>>> But is there a way to preserve the original namespaces/prefixes
>>>>>>>> upon save?
>>>>>>>> I assume they get overridden by the namespace of the ecore
>>>>>>>> model upon which
>>>>>>>> the editor is based when loading the model.
>>>>>>>>
>>>>>>> The trick doesn't seem to work when loading external resources
>>>>>>> having foreign
>>>>>>> namespaces without a prefix.
>>>>>>> The following model has a namespace "XML" without a prefix. The
>>>>>>> element Root
>>>>>>> is non-abstract and referenced via a cross-resource reference.
>>>>>>>
>>>>>>> <Root xmi:version="2.0"
>>>>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>>>> xmlns="XML" name="parameters">
>>>>>>>
>>>>>>> This is the exception I get :
>>>>>>>
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>>>>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with
>>>>>>> uri 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>>>>>
>>>>>>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>>>>>> at
>>>>>>> org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>>>>>> at
>>>>>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>>>>>
>>>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>>> at
>>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>>>>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>>>>>> at
>>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>>>>>
>>>>>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>>>>>> at
>>>>>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>>>>>
>>>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>>> at
>>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>>> at
>>>>>>> org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>>>>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>>>>>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>>>>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>>>>>> at
>>>>>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>>>>>
>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>>>>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>>>>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>>>>>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>>>>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>>>>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>>>>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>>>>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>>>>>> Package with uri 'XML' not found.
>>>>>>> (file:.../parameters-XML.ecore, 4, 35)
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>>>>>
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>>>>>> Source)
>>>>>>> at
>>>>>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>>>>>> Source)
>>>>>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>>>>>
>>>>>>> at
>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>>>>>
>>>>>>> .... 42 more
>>>>>>>
>>>>>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>>>>>
>>>>>>> Any ideas?
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Rene
>>>>>>> w
>>>>>>>> Chris Giblin wrote:
>>>>>>>>> Ed,
>>>>>>>>> Mega-cool. That did it:
>>>>>>>>>
>>>>>>>>> HashMap options = new HashMap(5);
>>>>>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>>>>>> new Boolean(true));
>>>>>>>>> resource.load(new FileInputStream(filename),options);
>>>>>>>>>
>>>>>>>>> Thanks much,
>>>>>>>>> Chris
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Ed Merks wrote:
>>>>>>>>>
>>>>>>>>>> Chris,
>>>>>>>>>>
>>>>>>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>>>>>>> document is
>>>>>>>>>> processed, so you should pretty much already have what you
>>>>>>>>>> want if you just
>>>>>>>>>> ignore errors. You should maybe consider using wildcards to
>>>>>>>>>> allow such
>>>>>>>>>> content in the document rather than just wanting the errors
>>>>>>>>>> to be ignored.
>>>>>>>>>> Another feature you can take advantage of is to use
>>>>>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>>>>>> enabled, all the
>>>>>>>>>> unrecognized content is recorded in the EObjectToExtensionMap
>>>>>>>>>> and no
>>>>>>>>>> exceptions produced.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Chris Giblin wrote:
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>> Hi,
>>>>>>>>>>> I want to be able to load an object instance from persistent
>>>>>>>>>>> storage in
>>>>>>>>>>> XML, such that the XML document is allowed to contain
>>>>>>>>>>> namespaces which
>>>>>>>>>>> were unknown at the time the ecore was created.
>>>>>>>>>>>
>>>>>>>>>>> Example: I generate a model using the library.xsd from the
>>>>>>>>>>> EMF Overview.
>>>>>>>>>>> I progammatically instantiated a library and book and
>>>>>>>>>>> persist it using
>>>>>>>>>>> XMLResourceImpl. I load that same XML document and
>>>>>>>>>>> instantiate the
>>>>>>>>>>> Library. So far, so good.
>>>>>>>>>>>
>>>>>>>>>>> Now I introduce an element from another non-modelled
>>>>>>>>>>> namespace, foo:
>>>>>>>>>>>
>>>>>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>>>>>> <library:Library
>>>>>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>>>>>
>>>>>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>>>>>> </m:foo>
>>>>>>>>>>>
>>>>>>>>>>> <books title="Once upon a time" pages="100"
>>>>>>>>>>> category="Biography"
>>>>>>>>>>> author="/1"/>
>>>>>>>>>>> </library:Library>
>>>>>>>>>>>
>>>>>>>>>>> I receive an exception:
>>>>>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
>>>>>>>>>>> 'foo' not
>>>>>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>>>>>
>>>>>>>>>>> Is it possible that the unknown namespace, foo, is ignored
>>>>>>>>>>> and I still
>>>>>>>>>>> instantiate the library portion without exception?
>>>>>>>>>>>
>>>>>>>>>>> Thanks,
>>>>>>>>>>> chris
>>>>>>>>>>>
>>>>>>>>>>> PS. Here is my code to load:
>>>>>>>>>>>
>>>>>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>>>>>> String filename = file.getAbsolutePath();
>>>>>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>>>>>
>>>>>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>>>>>
>>>>>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>>>>>> XMLResourceFactoryImpl());
>>>>>>>>>>>
>>>>>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>>>>>> resource.load(new
>>>>>>>>>>> FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>>>>>
>>>>>>>>>>
>>>>>>
>>


--------------010003070401000107040406
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rene,<br>
<br>
Comments below.<br>
<br>
Rene Ladan wrote:
<blockquote cite="mid:fm7tpq$mmq$1@build.eclipse.org" type="cite">Ed
Merks wrote:
<br>
<blockquote type="cite">Rene,
<br>
<br>
</blockquote>
[..]
<br>
<blockquote type="cite">So it appears to be working well.&nbsp;&nbsp; It's hard
to comment on what's going wrong for you because I'm not actually what
you're doing and you don't seem to be able to set a breakpoint and stop
in handleMissingPackage, or did you actually manage that?
<br>
</blockquote>
I managed to set a breakpoint there.&nbsp; If I add a prefixless namespace
having
<br>
value "XML", this happens at line 2465 of XMLHandler.java:
<br>
&nbsp; uriString == "XML"
<br>
&nbsp; extendedMetadata == null
<br>
&nbsp; return null
<br>
This produces a PackageNotFoundException, but the program keeps trying
to add
<br>
a node "Root" to the top of the (new?) tree.&nbsp; This fails too because,
causing
<br>
a ClassNotFoundException which gets thrown using the error() method.
<br>
</blockquote>
Given that the option you said you are using would have ensured that
extendedMetaData was not null, I would conclude that you aren't using
the option:<br>
<br>
<small>&nbsp;&nbsp;&nbsp; recordUnknownFeature =
Boolean.TRUE.equals(options.get(XMLResource.OPTION_RECORD_UN KNOWN_FEATURE));&nbsp;
<br>
&nbsp;&nbsp;&nbsp; if (recordUnknownFeature &amp;&amp; extendedMetaData == null)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setExtendedMetaDataOption(Boolean.TRUE);<br>
&nbsp;&nbsp;&nbsp; }</small><br>
&nbsp;&nbsp;&nbsp; <br>
<br>
<blockquote cite="mid:fm7tpq$mmq$1@build.eclipse.org" type="cite"><br>
The same exceptions occur if I open the file (attached) in the Sample
EMF Editor.
<br>
After performing "Register metamodel" on the metamodel (an ATL menu
extension)
<br>
the exception is still thrown.&nbsp; However, after performing the same
action on a partial
<br>
metamodel describing only the relevant part where the package name is
"XML", the
<br>
file is actually editable in the Sample EMF Editor.
<br>
</blockquote>
I don't understand this part.&nbsp; I thought the whole line of discussion
was that the option above wasn't working?<br>
<blockquote cite="mid:fm7tpq$mmq$1@build.eclipse.org" type="cite"><br>
Maybe I should just remove it from the metamodel since it's not really
worth the
<br>
trouble...
<br>
<blockquote type="cite"><br>
<br>
Rene Ladan wrote:
<br>
<blockquote type="cite">Ed Merks wrote:
<br>
<blockquote type="cite">Rene,
<br>
<br>
All three places that throw PackageNotFoundException (all in
XMLHandler) all call handleMissingPackage before throwing that
exception, so I'm confused by your analysis that handleMissingPackage
isn't even called.&nbsp; Keep in mind that the exception is recorded and
thrown later so set a breakpoint in the constructor of the exception.
<br>
</blockquote>
It turns out that the exception is indeed thrown, as is
ClassNotFoundException()
<br>
(the EMF variant).&nbsp; I also tried changing the namespace to
<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/XML">"http://www.w3.org/XML"</a>
<br>
(not official, but it looks like XML has none), but that didn't help
either (it
<br>
tries to substitute the html namespace).&nbsp; An empty namespace gives an
exception
<br>
similar to the first one.
<br>
<br>
<blockquote type="cite">&nbsp;It seems impossible for that not to get
hit unless there is a bug in the debugger, which seems unlikely.&nbsp; Does
you debugger work at all?&nbsp; I.e., can you get it to stop anywhere in any
of the EMF code?
<br>
<br>
</blockquote>
Yes, I can set a breakpoint at initializeEditingDomain() upon which the
<br>
IDE stops.
<br>
<br>
<blockquote type="cite"><br>
Rene Ladan wrote:
<br>
<blockquote type="cite">Ed Merks wrote:
<br>
<blockquote type="cite">Rene,
<br>
<br>
Does it call this method in XMLHandler and if so, I wonder why it
doesn't return a demand created package for your URI:
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected EPackage handleMissingPackage(String uriString)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; if (XMLResource.XML_SCHEMA_URI.equals(uriString))
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return xmlSchemaTypePackage;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; }
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; else if (extendedMetaData != null)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (recordUnknownFeature)
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return extendedMetaData.demandPackage(uriString);
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
<br>
<br>
</blockquote>
The XyzEditor.java file imports XMLHandler but leaves it otherwise
unused.
<br>
According to the (partial) backtrace, handleMissingPackage() is not
called.
<br>
I've tried setting up breakpoints for PackageNotFoundException and at
the
<br>
start of method handleChangedResources() but they get skipped over
(running
<br>
in debug view).
<br>
<br>
<blockquote type="cite"><br>
Rene Ladan wrote:
<br>
<blockquote type="cite">Rene Ladan wrote:
<br>
<blockquote type="cite">This works nice indeed.
<br>
But is there a way to preserve the original namespaces/prefixes upon
save?
<br>
I assume they get overridden by the namespace of the ecore model upon
which
<br>
the editor is based when loading the model.
<br>
<br>
</blockquote>
The trick doesn't seem to work when loading external resources having
foreign
<br>
namespaces without a prefix.
<br>
The following model has a namespace "XML" without a prefix.&nbsp; The
element Root
<br>
is non-abstract and referenced via a cross-resource reference.
<br>
<br>
&lt;Root xmi:version="2.0"
<br>
&nbsp;&nbsp;&nbsp; xmlns:xmi=<a class="moz-txt-link-rfc2396E" href="http://www.omg.org/XMI">"http://www.omg.org/XMI"</a>
xmlns:xsi=<a class="moz-txt-link-rfc2396E" href="http://www.w3.org/2001/XMLSchema-instance">"http://www.w3.org/2001/XMLSchema-instance"</a>
<br>
&nbsp;&nbsp;&nbsp; xmlns="XML" name="parameters"&gt;
<br>
<br>
This is the exception I get :
<br>
<br>
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
'XML' not found. (<a class="moz-txt-link-freetext" href="file:../parameters-XML.ecore">file:../parameters-XML.ecore </a>, 4, 35)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
<br>
at
org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
<br>
at
org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
<br>
at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
<br>
at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
<br>
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
<br>
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
<br>
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
<br>
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
<br>
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
<br>
at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
<br>
at org.eclipse.jface.window.Window.open(Window.java:796)
<br>
at
org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
<br>
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
<br>
at
org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
<br>
at
org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
<br>
at
org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
<br>
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
<br>
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
<br>
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
<br>
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
<br>
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
<br>
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
<br>
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
<br>
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
<br>
at
org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
<br>
at
org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
<br>
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
<br>
at
org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
<br>
at
org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
<br>
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
<br>
at
org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
<br>
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
<br>
at
org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
<br>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
<br>
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
<br>
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
<br>
at java.lang.reflect.Method.invoke(Unknown Source)
<br>
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
<br>
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
<br>
at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
<br>
at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
<br>
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
with uri 'XML' not found. (<a class="moz-txt-link-freetext" href="file:.../parameters-XML.ecore">file:.../parameters-XML.ecore </a>, 4, 35)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
<br>
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
Source)
<br>
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
Source)
<br>
at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
Source)
<br>
at javax.xml.parsers.SAXParser.parse(Unknown Source)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
<br>
at
org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
<br>
at
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
<br>
..... 42 more
<br>
<br>
Eclipse 3.3.1.1, EMF 2.3.1
<br>
<br>
Any ideas?
<br>
<br>
Thanks,
<br>
Rene
<br>
w
<br>
<blockquote type="cite">Chris Giblin wrote:
<br>
<blockquote type="cite">Ed,
<br>
Mega-cool. That did it:
<br>
<br>
&nbsp; HashMap options = new HashMap(5);
<br>
&nbsp; options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; new Boolean(true));
<br>
&nbsp; resource.load(new FileInputStream(filename),options);
<br>
<br>
Thanks much,
<br>
&nbsp;Chris
<br>
<br>
<br>
Ed Merks wrote:
<br>
<br>
<blockquote type="cite">Chris,
<br>
<br>
The exceptions are recorded and aren't thrown until the whole document
is
<br>
processed, so you should pretty much already have what you want if you
just
<br>
ignore errors.&nbsp; You should maybe consider using wildcards to allow such
<br>
content in the document rather than just wanting the errors to be
ignored.
<br>
Another feature you can take advantage of is to use
<br>
XMLResource.OPTION_RECORD_UNKNOWN_FEATURES.&nbsp; When this is enabled, all
the
<br>
unrecognized content is recorded in the EObjectToExtensionMap and no
<br>
exceptions produced.
<br>
<br>
<br>
Chris Giblin wrote:
<br>
<br>
<br>
<blockquote type="cite">Hi,
<br>
I want to be able to load an object instance from persistent storage in
<br>
XML, such that the XML document is allowed to contain namespaces which
<br>
were unknown at the time the ecore was created.
<br>
<br>
Example: I generate a model using the library.xsd from the EMF
Overview.
<br>
I progammatically instantiated a library and book and persist it using
<br>
XMLResourceImpl. I load that same XML document and instantiate the
<br>
Library. So far, so good.
<br>
<br>
Now I introduce an element from another non-modelled namespace, foo:
<br>
<br>
&lt;?xml version="1.0" encoding="ASCII"?&gt;
<br>
&lt;library:Library
xmlns:library=<a class="moz-txt-link-rfc2396E" href="http://www.example.eclipse.org/Library">"http://www.example.eclipse.org/Library"</a>&gt;
<br>
<br>
&nbsp; &lt;m:foo xmlns:m=<a class="moz-txt-link-rfc2396E" href="http://example.org/foo">"http://example.org/foo"</a>&gt;
<br>
&nbsp;&nbsp; &lt;m:msg&gt;Have a nice day&lt;/m:msg&gt;
<br>
&nbsp; &lt;/m:foo&gt;
<br>
<br>
&nbsp; &lt;books title="Once upon a time" pages="100" category="Biography"
<br>
author="/1"/&gt;
<br>
&lt;/library:Library&gt;
<br>
<br>
I receive an exception:
<br>
org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'foo' not
<br>
found. (<a class="moz-txt-link-freetext" href="file:/c:/dev/libraryEmfXsd/xml/lib2.xml">file:/c:/dev/libraryEmfXsd/xml/lib2.xml </a>, 4, 43)
<br>
<br>
Is it possible that the unknown namespace, foo, is ignored and I still
<br>
instantiate the library portion without exception?
<br>
<br>
Thanks,
<br>
chris
<br>
<br>
PS. Here is my code to load:
<br>
<br>
File file = new File("xml/lib2.xml");
<br>
String filename = file.getAbsolutePath();
<br>
URI uri = URI.createFileURI(filename);
<br>
<br>
LibraryPackage pkg = LibraryPackageImpl.init();
<br>
<br>
ResourceSet resourceSet = new ResourceSetImpl();
<br>
resourceSet.getResourceFactoryRegistry().
<br>
&nbsp;&nbsp;&nbsp; getExtensionToFactoryMap().put("*",new XMLResourceFactoryImpl());
<br>
<br>
Resource resource = resourceSet.createResource(uri);
<br>
resource.load(new FileInputStream(filename),Collections.EMPTY_MAP);
<br>
</blockquote>
<br>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<br>
</blockquote>
</blockquote>
<br>
</body>
</html>

--------------010003070401000107040406--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415934 is a reply to message #415931] Fri, 11 January 2008 16:24 Go to previous messageGo to next message
Rene Ladan is currently offline Rene LadanFriend
Messages: 51
Registered: July 2009
Member
Ed Merks wrote:
> Rene,
>
> Comments below.
>
> Rene Ladan wrote:
>> Ed Merks wrote:
>>> Rene,
>>>
>> [..]
>>> So it appears to be working well. It's hard to comment on what's
>>> going wrong for you because I'm not actually what you're doing and
>>> you don't seem to be able to set a breakpoint and stop in
>>> handleMissingPackage, or did you actually manage that?
>> I managed to set a breakpoint there. If I add a prefixless namespace
>> having
>> value "XML", this happens at line 2465 of XMLHandler.java:
>> uriString == "XML"
>> extendedMetadata == null
>> return null
>> This produces a PackageNotFoundException, but the program keeps trying
>> to add
>> a node "Root" to the top of the (new?) tree. This fails too because,
>> causing
>> a ClassNotFoundException which gets thrown using the error() method.
> Given that the option you said you are using would have ensured that
> extendedMetaData was not null, I would conclude that you aren't using
> the option:
>
> recordUnknownFeature =
> Boolean.TRUE.equals(options.get(XMLResource.OPTION_RECORD_UN KNOWN_FEATURE));
>
> if (recordUnknownFeature && extendedMetaData == null)
> {
> setExtendedMetaDataOption(Boolean.TRUE);
> }
>
I am, but possibly not at all the required places:
MetaEditor.java:

protected void handleChangedResources() {
EMap<String,String> m;
if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict())) {
editingDomain.getCommandStack().flush();

updateProblemIndication = false;
for (Resource resource : changedResources) {
if (resource.isLoaded()) {
resource.unload();
try {
HashMap options = new HashMap(5);
options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
...

>>
>> The same exceptions occur if I open the file (attached) in the Sample
>> EMF Editor.
>> After performing "Register metamodel" on the metamodel (an ATL menu
>> extension)
>> the exception is still thrown. However, after performing the same
>> action on a partial
>> metamodel describing only the relevant part where the package name is
>> "XML", the
>> file is actually editable in the Sample EMF Editor.
> I don't understand this part. I thought the whole line of discussion
> was that the option above wasn't working?
It does, but indeed not in the above situation when using a user-generated
editor.

>>
>> Maybe I should just remove it from the metamodel since it's not really
>> worth the
>> trouble...
>>>
>>>
>>> Rene Ladan wrote:
>>>> Ed Merks wrote:
>>>>> Rene,
>>>>>
>>>>> All three places that throw PackageNotFoundException (all in
>>>>> XMLHandler) all call handleMissingPackage before throwing that
>>>>> exception, so I'm confused by your analysis that
>>>>> handleMissingPackage isn't even called. Keep in mind that the
>>>>> exception is recorded and thrown later so set a breakpoint in the
>>>>> constructor of the exception.
>>>> It turns out that the exception is indeed thrown, as is
>>>> ClassNotFoundException()
>>>> (the EMF variant). I also tried changing the namespace to
>>>> "http://www.w3.org/XML"
>>>> (not official, but it looks like XML has none), but that didn't help
>>>> either (it
>>>> tries to substitute the html namespace). An empty namespace gives
>>>> an exception
>>>> similar to the first one.
>>>>
>>>>> It seems impossible for that not to get hit unless there is a bug
>>>>> in the debugger, which seems unlikely. Does you debugger work at
>>>>> all? I.e., can you get it to stop anywhere in any of the EMF code?
>>>>>
>>>> Yes, I can set a breakpoint at initializeEditingDomain() upon which the
>>>> IDE stops.
>>>>
>>>>>
>>>>> Rene Ladan wrote:
>>>>>> Ed Merks wrote:
>>>>>>> Rene,
>>>>>>>
>>>>>>> Does it call this method in XMLHandler and if so, I wonder why it
>>>>>>> doesn't return a demand created package for your URI:
>>>>>>>
>>>>>>> protected EPackage handleMissingPackage(String uriString)
>>>>>>> {
>>>>>>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>>>>>>> {
>>>>>>> return xmlSchemaTypePackage;
>>>>>>> }
>>>>>>> else if (extendedMetaData != null)
>>>>>>> {
>>>>>>> if (recordUnknownFeature)
>>>>>>> {
>>>>>>> return extendedMetaData.demandPackage(uriString);
>>>>>>> }
>>>>>>>
>>>>>> The XyzEditor.java file imports XMLHandler but leaves it otherwise
>>>>>> unused.
>>>>>> According to the (partial) backtrace, handleMissingPackage() is
>>>>>> not called.
>>>>>> I've tried setting up breakpoints for PackageNotFoundException and
>>>>>> at the
>>>>>> start of method handleChangedResources() but they get skipped over
>>>>>> (running
>>>>>> in debug view).
>>>>>>
>>>>>>>
>>>>>>> Rene Ladan wrote:
>>>>>>>> Rene Ladan wrote:
>>>>>>>>> This works nice indeed.
>>>>>>>>> But is there a way to preserve the original namespaces/prefixes
>>>>>>>>> upon save?
>>>>>>>>> I assume they get overridden by the namespace of the ecore
>>>>>>>>> model upon which
>>>>>>>>> the editor is based when loading the model.
>>>>>>>>>
>>>>>>>> The trick doesn't seem to work when loading external resources
>>>>>>>> having foreign
>>>>>>>> namespaces without a prefix.
>>>>>>>> The following model has a namespace "XML" without a prefix. The
>>>>>>>> element Root
>>>>>>>> is non-abstract and referenced via a cross-resource reference.
>>>>>>>>
>>>>>>>> <Root xmi:version="2.0"
>>>>>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>>>>> xmlns="XML" name="parameters">
>>>>>>>>
>>>>>>>> This is the exception I get :
>>>>>>>>
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>>>>>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with
>>>>>>>> uri 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>>>>>>
>>>>>>>> at org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>>>>>>> at
>>>>>>>> org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>>>>>>> at
>>>>>>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>>>>>>
>>>>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>>>> at
>>>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>>>>>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>>>>>>> at
>>>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>>>>>>
>>>>>>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>>>>>>> at
>>>>>>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>>>>>>
>>>>>>>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>>>> at
>>>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>>>> at
>>>>>>>> org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>>>>>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>>>>>>> at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>>>>>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>>>>>>> at
>>>>>>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>>>>>>
>>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>>>>>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>>>>>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>>>>>>> at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>>>>>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>>>>>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>>>>>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>>>>>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>>>>>>> Package with uri 'XML' not found.
>>>>>>>> (file:.../parameters-XML.ecore, 4, 35)
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>>>>>>
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>>>>>>> Source)
>>>>>>>> at
>>>>>>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>>>>>>> Source)
>>>>>>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>>>>>>
>>>>>>>> at
>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>>>>>>
>>>>>>>> .... 42 more
>>>>>>>>
>>>>>>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>>>>>>
>>>>>>>> Any ideas?
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> Rene
>>>>>>>> w
>>>>>>>>> Chris Giblin wrote:
>>>>>>>>>> Ed,
>>>>>>>>>> Mega-cool. That did it:
>>>>>>>>>>
>>>>>>>>>> HashMap options = new HashMap(5);
>>>>>>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>>>>>>> new Boolean(true));
>>>>>>>>>> resource.load(new FileInputStream(filename),options);
>>>>>>>>>>
>>>>>>>>>> Thanks much,
>>>>>>>>>> Chris
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Ed Merks wrote:
>>>>>>>>>>
>>>>>>>>>>> Chris,
>>>>>>>>>>>
>>>>>>>>>>> The exceptions are recorded and aren't thrown until the whole
>>>>>>>>>>> document is
>>>>>>>>>>> processed, so you should pretty much already have what you
>>>>>>>>>>> want if you just
>>>>>>>>>>> ignore errors. You should maybe consider using wildcards to
>>>>>>>>>>> allow such
>>>>>>>>>>> content in the document rather than just wanting the errors
>>>>>>>>>>> to be ignored.
>>>>>>>>>>> Another feature you can take advantage of is to use
>>>>>>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>>>>>>> enabled, all the
>>>>>>>>>>> unrecognized content is recorded in the EObjectToExtensionMap
>>>>>>>>>>> and no
>>>>>>>>>>> exceptions produced.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Chris Giblin wrote:
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>> Hi,
>>>>>>>>>>>> I want to be able to load an object instance from persistent
>>>>>>>>>>>> storage in
>>>>>>>>>>>> XML, such that the XML document is allowed to contain
>>>>>>>>>>>> namespaces which
>>>>>>>>>>>> were unknown at the time the ecore was created.
>>>>>>>>>>>>
>>>>>>>>>>>> Example: I generate a model using the library.xsd from the
>>>>>>>>>>>> EMF Overview.
>>>>>>>>>>>> I progammatically instantiated a library and book and
>>>>>>>>>>>> persist it using
>>>>>>>>>>>> XMLResourceImpl. I load that same XML document and
>>>>>>>>>>>> instantiate the
>>>>>>>>>>>> Library. So far, so good.
>>>>>>>>>>>>
>>>>>>>>>>>> Now I introduce an element from another non-modelled
>>>>>>>>>>>> namespace, foo:
>>>>>>>>>>>>
>>>>>>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>>>>>>> <library:Library
>>>>>>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>>>>>>
>>>>>>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>>>>>>> </m:foo>
>>>>>>>>>>>>
>>>>>>>>>>>> <books title="Once upon a time" pages="100"
>>>>>>>>>>>> category="Biography"
>>>>>>>>>>>> author="/1"/>
>>>>>>>>>>>> </library:Library>
>>>>>>>>>>>>
>>>>>>>>>>>> I receive an exception:
>>>>>>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature
>>>>>>>>>>>> 'foo' not
>>>>>>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>>>>>>
>>>>>>>>>>>> Is it possible that the unknown namespace, foo, is ignored
>>>>>>>>>>>> and I still
>>>>>>>>>>>> instantiate the library portion without exception?
>>>>>>>>>>>>
>>>>>>>>>>>> Thanks,
>>>>>>>>>>>> chris
>>>>>>>>>>>>
>>>>>>>>>>>> PS. Here is my code to load:
>>>>>>>>>>>>
>>>>>>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>>>>>>> String filename = file.getAbsolutePath();
>>>>>>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>>>>>>
>>>>>>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>>>>>>
>>>>>>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>>>>>>> XMLResourceFactoryImpl());
>>>>>>>>>>>>
>>>>>>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>>>>>>> resource.load(new
>>>>>>>>>>>> FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>
>>>
>
Re: Q: Loading XMLResources containing non-modeled namespaces? [message #415936 is a reply to message #415934] Fri, 11 January 2008 16:35 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Rene,

Comments below.

Rene Ladan wrote:
> Ed Merks wrote:
>> Rene,
>>
>> Comments below.
>>
>> Rene Ladan wrote:
>>> Ed Merks wrote:
>>>> Rene,
>>>>
>>> [..]
>>>> So it appears to be working well. It's hard to comment on what's
>>>> going wrong for you because I'm not actually what you're doing and
>>>> you don't seem to be able to set a breakpoint and stop in
>>>> handleMissingPackage, or did you actually manage that?
>>> I managed to set a breakpoint there. If I add a prefixless
>>> namespace having
>>> value "XML", this happens at line 2465 of XMLHandler.java:
>>> uriString == "XML"
>>> extendedMetadata == null
>>> return null
>>> This produces a PackageNotFoundException, but the program keeps
>>> trying to add
>>> a node "Root" to the top of the (new?) tree. This fails too
>>> because, causing
>>> a ClassNotFoundException which gets thrown using the error() method.
>> Given that the option you said you are using would have ensured that
>> extendedMetaData was not null, I would conclude that you aren't using
>> the option:
>>
>> recordUnknownFeature =
>> Boolean.TRUE.equals(options.get(XMLResource.OPTION_RECORD_UN KNOWN_FEATURE));
>>
>> if (recordUnknownFeature && extendedMetaData == null)
>> {
>> setExtendedMetaDataOption(Boolean.TRUE);
>> }
>>
> I am, but possibly not at all the required places:
> MetaEditor.java:
You can put an option in the resource set (resourceSet.getLoadOptions())
and then they will all be used. You can also specialize the generated
resource factory impl so that you always set this option when you create
the resource.
>
> protected void handleChangedResources() {
> EMap<String,String> m;
> if (!changedResources.isEmpty() && (!isDirty() ||
> handleDirtyConflict())) {
> editingDomain.getCommandStack().flush();
>
> updateProblemIndication = false;
> for (Resource resource : changedResources) {
> if (resource.isLoaded()) {
> resource.unload();
> try {
> HashMap options = new HashMap(5);
>
> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.TRUE);
> ...
>
>>>
>>> The same exceptions occur if I open the file (attached) in the
>>> Sample EMF Editor.
>>> After performing "Register metamodel" on the metamodel (an ATL menu
>>> extension)
>>> the exception is still thrown. However, after performing the same
>>> action on a partial
>>> metamodel describing only the relevant part where the package name
>>> is "XML", the
>>> file is actually editable in the Sample EMF Editor.
>> I don't understand this part. I thought the whole line of discussion
>> was that the option above wasn't working?
> It does, but indeed not in the above situation when using a
> user-generated
> editor.
>
>>>
>>> Maybe I should just remove it from the metamodel since it's not
>>> really worth the
>>> trouble...
>>>>
>>>>
>>>> Rene Ladan wrote:
>>>>> Ed Merks wrote:
>>>>>> Rene,
>>>>>>
>>>>>> All three places that throw PackageNotFoundException (all in
>>>>>> XMLHandler) all call handleMissingPackage before throwing that
>>>>>> exception, so I'm confused by your analysis that
>>>>>> handleMissingPackage isn't even called. Keep in mind that the
>>>>>> exception is recorded and thrown later so set a breakpoint in the
>>>>>> constructor of the exception.
>>>>> It turns out that the exception is indeed thrown, as is
>>>>> ClassNotFoundException()
>>>>> (the EMF variant). I also tried changing the namespace to
>>>>> "http://www.w3.org/XML"
>>>>> (not official, but it looks like XML has none), but that didn't
>>>>> help either (it
>>>>> tries to substitute the html namespace). An empty namespace gives
>>>>> an exception
>>>>> similar to the first one.
>>>>>
>>>>>> It seems impossible for that not to get hit unless there is a
>>>>>> bug in the debugger, which seems unlikely. Does you debugger
>>>>>> work at all? I.e., can you get it to stop anywhere in any of the
>>>>>> EMF code?
>>>>>>
>>>>> Yes, I can set a breakpoint at initializeEditingDomain() upon
>>>>> which the
>>>>> IDE stops.
>>>>>
>>>>>>
>>>>>> Rene Ladan wrote:
>>>>>>> Ed Merks wrote:
>>>>>>>> Rene,
>>>>>>>>
>>>>>>>> Does it call this method in XMLHandler and if so, I wonder why
>>>>>>>> it doesn't return a demand created package for your URI:
>>>>>>>>
>>>>>>>> protected EPackage handleMissingPackage(String uriString)
>>>>>>>> {
>>>>>>>> if (XMLResource.XML_SCHEMA_URI.equals(uriString))
>>>>>>>> {
>>>>>>>> return xmlSchemaTypePackage;
>>>>>>>> }
>>>>>>>> else if (extendedMetaData != null)
>>>>>>>> {
>>>>>>>> if (recordUnknownFeature)
>>>>>>>> {
>>>>>>>> return extendedMetaData.demandPackage(uriString);
>>>>>>>> }
>>>>>>>>
>>>>>>> The XyzEditor.java file imports XMLHandler but leaves it
>>>>>>> otherwise unused.
>>>>>>> According to the (partial) backtrace, handleMissingPackage() is
>>>>>>> not called.
>>>>>>> I've tried setting up breakpoints for PackageNotFoundException
>>>>>>> and at the
>>>>>>> start of method handleChangedResources() but they get skipped
>>>>>>> over (running
>>>>>>> in debug view).
>>>>>>>
>>>>>>>>
>>>>>>>> Rene Ladan wrote:
>>>>>>>>> Rene Ladan wrote:
>>>>>>>>>> This works nice indeed.
>>>>>>>>>> But is there a way to preserve the original
>>>>>>>>>> namespaces/prefixes upon save?
>>>>>>>>>> I assume they get overridden by the namespace of the ecore
>>>>>>>>>> model upon which
>>>>>>>>>> the editor is based when loading the model.
>>>>>>>>>>
>>>>>>>>> The trick doesn't seem to work when loading external resources
>>>>>>>>> having foreign
>>>>>>>>> namespaces without a prefix.
>>>>>>>>> The following model has a namespace "XML" without a prefix.
>>>>>>>>> The element Root
>>>>>>>>> is non-abstract and referenced via a cross-resource reference.
>>>>>>>>>
>>>>>>>>> <Root xmi:version="2.0"
>>>>>>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>>>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>>>>>> xmlns="XML" name="parameters">
>>>>>>>>>
>>>>>>>>> This is the exception I get :
>>>>>>>>>
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1Diagnos ticWrappedException:
>>>>>>>>> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
>>>>>>>>> with uri 'XML' not found. (file:../parameters-XML.ecore, 4, 35)
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDe mandLoadException(ResourceSetImpl.java:316)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:275)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResou rce(ResourceSetImpl.java:398)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction$LoadResour ceDialog.processResources(LoadResourceAction.java:126)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.common.ui.dialogs.ResourceDialog.okPressed(R esourceDialog.java:331)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.jface.dialogs.Dialog.buttonPressed(Dialog.java:4 64)
>>>>>>>>> at
>>>>>>>>> org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.jav a:616)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:227)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>>>>>
>>>>>>>>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:820 )
>>>>>>>>> at org.eclipse.jface.window.Window.open(Window.java:796)
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.edit.ui.action.LoadResourceAction.run(LoadRe sourceAction.java:77)
>>>>>>>>>
>>>>>>>>> at org.eclipse.jface.action.Action.runWithEvent(Action.java:498 )
>>>>>>>>> at
>>>>>>>>> org.eclipse.jface.action.ActionContributionItem.handleWidget Selection(ActionContributionItem.java:546)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.jface.action.ActionContributionItem.access$2(Act ionContributionItem.java:490)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.jface.action.ActionContributionItem$5.handleEven t(ActionContributionItem.java:402)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :66)
>>>>>>>>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:938)
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.ja va:3682)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.swt.widgets.Display.readAndDispatch(Display.java :3293)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.jav a:2389)
>>>>>>>>>
>>>>>>>>> at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2353)
>>>>>>>>> at
>>>>>>>>> org.eclipse.ui.internal.Workbench.access$4(Workbench.java:22 19)
>>>>>>>>> at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:466)
>>>>>>>>> at
>>>>>>>>> org.eclipse.core.databinding.observable.Realm.runWithDefault (Realm.java:289)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Work bench.java:461)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.j ava:149)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.ui.internal.ide.application.IDEApplication.start (IDEApplication.java:106)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.equinox.internal.app.EclipseAppHandle.run(Eclips eAppHandle.java:169)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:106)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .start(EclipseAppLauncher.java:76)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:363)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseS tarter.java:176)
>>>>>>>>>
>>>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>>>>>> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>>>>>>>> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
>>>>>>>>> Source)
>>>>>>>>> at java.lang.reflect.Method.invoke(Unknown Source)
>>>>>>>>> at
>>>>>>>>> org.eclipse.equinox.launcher.Main.invokeFramework(Main.java: 508)
>>>>>>>>> at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
>>>>>>>>> at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
>>>>>>>>> at org.eclipse.equinox.launcher.Main.main(Main.java:1148)
>>>>>>>>> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException:
>>>>>>>>> Package with uri 'XML' not found.
>>>>>>>>> (file:.../parameters-XML.ecore, 4, 35)
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(X MLHandler.java:2453)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getFactoryForPrefi x(XMLHandler.java:2285)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType (XMLHandler.java:1239)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XM LHandler.java:1336)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XML Handler.java:970)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.processElement(XMI Handler.java:83)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:953)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHa ndler.java:684)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMIHandler.startElement(XMIHa ndler.java:167)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .startElement(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator. startElement(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanStartElement(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$ContentDriver.scanRootElementHook(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl$FragmentContentDriver.next(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl$PrologDriver.next(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerIm pl.next(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentS cannerImpl.scanDocument(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.parsers.XML11Configuratio n.parse(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(U nknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser .parse(Unknown
>>>>>>>>> Source)
>>>>>>>>> at
>>>>>>>>> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSA XParser.parse(Unknown
>>>>>>>>> Source)
>>>>>>>>> at javax.xml.parsers.SAXParser.parse(Unknown Source)
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:179)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1354)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1155)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo ad(ResourceSetImpl.java:256)
>>>>>>>>>
>>>>>>>>> at
>>>>>>>>> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLo adHelper(ResourceSetImpl.java:271)
>>>>>>>>>
>>>>>>>>> .... 42 more
>>>>>>>>>
>>>>>>>>> Eclipse 3.3.1.1, EMF 2.3.1
>>>>>>>>>
>>>>>>>>> Any ideas?
>>>>>>>>>
>>>>>>>>> Thanks,
>>>>>>>>> Rene
>>>>>>>>> w
>>>>>>>>>> Chris Giblin wrote:
>>>>>>>>>>> Ed,
>>>>>>>>>>> Mega-cool. That did it:
>>>>>>>>>>>
>>>>>>>>>>> HashMap options = new HashMap(5);
>>>>>>>>>>> options.put(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
>>>>>>>>>>> new Boolean(true));
>>>>>>>>>>> resource.load(new FileInputStream(filename),options);
>>>>>>>>>>>
>>>>>>>>>>> Thanks much,
>>>>>>>>>>> Chris
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Ed Merks wrote:
>>>>>>>>>>>
>>>>>>>>>>>> Chris,
>>>>>>>>>>>>
>>>>>>>>>>>> The exceptions are recorded and aren't thrown until the
>>>>>>>>>>>> whole document is
>>>>>>>>>>>> processed, so you should pretty much already have what you
>>>>>>>>>>>> want if you just
>>>>>>>>>>>> ignore errors. You should maybe consider using wildcards
>>>>>>>>>>>> to allow such
>>>>>>>>>>>> content in the document rather than just wanting the errors
>>>>>>>>>>>> to be ignored.
>>>>>>>>>>>> Another feature you can take advantage of is to use
>>>>>>>>>>>> XMLResource.OPTION_RECORD_UNKNOWN_FEATURES. When this is
>>>>>>>>>>>> enabled, all the
>>>>>>>>>>>> unrecognized content is recorded in the
>>>>>>>>>>>> EObjectToExtensionMap and no
>>>>>>>>>>>> exceptions produced.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> Chris Giblin wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>> Hi,
>>>>>>>>>>>>> I want to be able to load an object instance from
>>>>>>>>>>>>> persistent storage in
>>>>>>>>>>>>> XML, such that the XML document is allowed to contain
>>>>>>>>>>>>> namespaces which
>>>>>>>>>>>>> were unknown at the time the ecore was created.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Example: I generate a model using the library.xsd from the
>>>>>>>>>>>>> EMF Overview.
>>>>>>>>>>>>> I progammatically instantiated a library and book and
>>>>>>>>>>>>> persist it using
>>>>>>>>>>>>> XMLResourceImpl. I load that same XML document and
>>>>>>>>>>>>> instantiate the
>>>>>>>>>>>>> Library. So far, so good.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Now I introduce an element from another non-modelled
>>>>>>>>>>>>> namespace, foo:
>>>>>>>>>>>>>
>>>>>>>>>>>>> <?xml version="1.0" encoding="ASCII"?>
>>>>>>>>>>>>> <library:Library
>>>>>>>>>>>>> xmlns:library="http://www.example.eclipse.org/Library">
>>>>>>>>>>>>>
>>>>>>>>>>>>> <m:foo xmlns:m="http://example.org/foo">
>>>>>>>>>>>>> <m:msg>Have a nice day</m:msg>
>>>>>>>>>>>>> </m:foo>
>>>>>>>>>>>>>
>>>>>>>>>>>>> <books title="Once upon a time" pages="100"
>>>>>>>>>>>>> category="Biography"
>>>>>>>>>>>>> author="/1"/>
>>>>>>>>>>>>> </library:Library>
>>>>>>>>>>>>>
>>>>>>>>>>>>> I receive an exception:
>>>>>>>>>>>>> org.eclipse.emf.ecore.xmi.FeatureNotFoundException:
>>>>>>>>>>>>> Feature 'foo' not
>>>>>>>>>>>>> found. (file:/c:/dev/libraryEmfXsd/xml/lib2.xml, 4, 43)
>>>>>>>>>>>>>
>>>>>>>>>>>>> Is it possible that the unknown namespace, foo, is ignored
>>>>>>>>>>>>> and I still
>>>>>>>>>>>>> instantiate the library portion without exception?
>>>>>>>>>>>>>
>>>>>>>>>>>>> Thanks,
>>>>>>>>>>>>> chris
>>>>>>>>>>>>>
>>>>>>>>>>>>> PS. Here is my code to load:
>>>>>>>>>>>>>
>>>>>>>>>>>>> File file = new File("xml/lib2.xml");
>>>>>>>>>>>>> String filename = file.getAbsolutePath();
>>>>>>>>>>>>> URI uri = URI.createFileURI(filename);
>>>>>>>>>>>>>
>>>>>>>>>>>>> LibraryPackage pkg = LibraryPackageImpl.init();
>>>>>>>>>>>>>
>>>>>>>>>>>>> ResourceSet resourceSet = new ResourceSetImpl();
>>>>>>>>>>>>> resourceSet.getResourceFactoryRegistry().
>>>>>>>>>>>>> getExtensionToFactoryMap().put("*",new
>>>>>>>>>>>>> XMLResourceFactoryImpl());
>>>>>>>>>>>>>
>>>>>>>>>>>>> Resource resource = resourceSet.createResource(uri);
>>>>>>>>>>>>> resource.load(new
>>>>>>>>>>>>> FileInputStream(filename),Collections.EMPTY_MAP);
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>
>>>>
>>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Interface Override Issue
Next Topic:[Newbie] Lack of documentation
Goto Forum:
  


Current Time: Sat Apr 20 02:08:50 GMT 2024

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

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

Back to the top