Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » XSD » Load EMF from xml stream(Problems loading from stream)
Load EMF from xml stream [message #765029] Tue, 13 December 2011 09:42
Frank Missing name is currently offline Frank Missing nameFriend
Messages: 2
Registered: November 2011
Junior Member

I have a question regarding loading an EMF model from a stream.
I have two functions:

public static EObject load(File file) throws IOException {

    ResourceSet resSet = new ResourceSetImpl();

    Map<String, Object> loadMap = configure(resSet);

    URI fileUri = URI.createFileURI(file.getAbsolutePath());
    Resource res = resSet.getResource(fileUri, true);

    return res.getContents().get(0);

private static Map<String, Object> configure(ResourceSet resSet) throws IOException {

    Map<String, Object> parserFeatures = new HashMap<String, Object>();
    Map<String, Object> parserProperties = new LinkedHashMap<String, Object>();
    Map<String, Object> loadMap = new HashMap<String, Object>();

    loadMap.put(XMLResource.OPTION_USE_PARSER_POOL, new XMLParserPoolImpl());
    loadMap.put(XMLResource.OPTION_EXTENDED_META_DATA, ExtendedMetaData.INSTANCE);

    parserFeatures.put("htp://", Boolean.TRUE);
    URL find = FileLocator.find(Activator.getContext().getBundle(), new Path("model/pack.xsd"), null);
    parserProperties.put("htp://", "htp://");

    InputStream schemaStream = find.openStream();
    parserProperties.put("htp://", schemaStream);
    resSet.getLoadOptions().put(XMLResource.OPTION_USE_PARSER_POOL, new XMLParserPoolImpl());
    resSet.getLoadOptions().put(XMLResource.OPTION_PARSER_FEATURES, parserFeatures);
    resSet.getLoadOptions().put(XMLResource.OPTION_PARSER_PROPERTIES, parserProperties);

    resSet.getPackageRegistry().put(PackPackage.eINSTANCE.getNsURI(), PackPackage.eINSTANCE);
    resSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("pack", new PackResourceFactoryImpl());
    return loadMap;

which are working fine.

When it comes to load the model from a stream, I get into trouble ...

public static EObject load(InputStream inStream) throws IOException {
      ResourceSet resSet = new ResourceSetImpl();
     Map<String, Object> loadMap = configure(resSet);
     Resource res = resSet.createResource(URI.createURI("htp:///My.pack"), "pack");
     res.load(inStream, loadMap);
     return res.getContents().get(0);

I get this error:
org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'null' not found. (htp:///My.pack, 2, 7)

If I remove this line:

loadMap.put(XMLResource.OPTION_EXTENDED_META_DATA, ExtendedMetaData.INSTANCE);

it works with streams also. However, the documentation says to use this setting for models created from a schema. So what is going wrong? Any idea?

Thanks in advance
(I had to use htp instead of http since I am not allowed to post links yet ...)
Previous Topic:Serialize XSD Object
Next Topic:choice between groups in xsd
Goto Forum:

Current Time: Fri Feb 21 19:46:58 GMT 2020

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

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

Back to the top