Loading XML without a given namespace as an EMF model: "Package with uri" null "not found" - Exception

I have an XSD file that I converted to an ecore model and from where I created the model code. Now I would like to download the xml file for this schema, but keep getting the error:

org.eclipse.emf.ecore.xmi.PackageNotFoundException: 
Package with uri 'null' not found.
(file:/C:/Users/mboeschen/safety/devel/eclipse_plugins...
/de.offis.etas.load/examples/minimal.xml, 2, 7)

As this happens immediately after the root tag in my XML file, I suspect that something is wrong after reading the root tag.

My code is as follows:

public static void main(String[] args) throws IOException {

    MinimalPackage.eINSTANCE.eClass();  
    MinimalPackage packageInstance = MinimalPackage.eINSTANCE;
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put("*", new XMLResourceFactoryImpl());

    // Obtain a new resource set
    ResourceSet resSet = new ResourceSetImpl();
    resSet.setResourceFactoryRegistry(reg);

    resSet.getPackageRegistry().put(MinimalPackage.eNS_URI,
            MinimalPackage.eINSTANCE);
    resSet.getPackageRegistry().put(null,
            MinimalPackage.eINSTANCE);

    // Get the resource
    URI uri = URI
    .createFileURI("C:/Users/mboeschen/safety/devel/eclipse_plugins...
                    /de.offis.etas.load/examples/minimal.xml");
    Resource resource = resSet.getResource(uri, true);
    RootType r = (RootType) resource.getContents().get(0);

    System.out.println(r);

The schema file is as follows:

<?xml version="1.0" encoding="US-ASCII"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="Root">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Inner" type="MyType">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="MyType">
    <xs:sequence> </xs:sequence>
</xs:complexType>
</xs:schema>

And this is the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Inner>
</Inner>
</Root>

Any ideas what is going on here? Any help appreciated!

+4
source share
3 answers

, @Severin . :

XMLResource, XML EMF/Ecore. , . , :

  • ResourceSet
  • Resource ( ResourceFactory)
  • Resource.load()

( ) /, , .

. Resource XMLResource, .

:

  • XMLResource.OPTION_MISSING_PACKAGE_HANDLER , , EMF EPackage
  • XMLResource.OPTION_EXTENDED_META_DATA (, , true), ExtendedMetaData, XML Ecore.

,

  • EPackage.
  • (.. // , EPackage)

MissingPackageHandler, :

MissingPackageHandler mph = new MissingPackageHandler() {
  @Override
  public EPackage getPackage(final String nsURI) {
    return MyModelEPackage.eINSTANCE;
  }
};

ExtendedMetaData:

BasicExtendedMetaData bemd = new BasicExtendedMetaData(new EPackageRegistryImpl(EPackage.Registry.INSTANCE)) {
  @Override
  protected boolean isFeatureNamespaceMatchingLax() {
    return true;
  }
};

EPackage , MissingPackageHandler , ExtendedMetaData.

@Severin :

BasicExtendedMetaData bemd = new BasicExtendedMetaData() {
  private static final String NAMESPACE_GOOGLE_EXT_2_2 = "http://www.google.com/kml/ext/2.2";
  private static final String NAMESPACE_OPENGIS_2_2 = "http://www.opengis.net/kml/2.2";
  private static final String NAMESPACE_DEFAULT = NAMESPACE_OPENGIS_2_2;

  @Override
  public EPackage getPackage(String namespace) {
    if(namespace==null) {
      namespace = NAMESPACE_DEFAULT;
    }
    return super.getPackage(namespace);
  }

  @Override
  public EStructuralFeature getElement(String namespace, String name) {
    // try to find feature in OPENGIS package
    EStructuralFeature result = super.getElement(NAMESPACE_OPENGIS_2_2, name);
    if (feature == null) {
      // if not found, try GOOGLE_EXT
      feature = super.getElement(NAMESPACE_GOOGLE_EXT_2_2, name);
    }
    return feature;
  }
});
+3

, , , [YourModelName] ResourceFactoryImpl. KML, :

    public class OgckmlResourceFactoryImpl extends ResourceFactoryImpl {
            ...
        /**
         * Creates an instance of the resource.
         * <!-- begin-user-doc -->
         * <!-- end-user-doc -->
         * @generated NOT
         */
        @Override
        public Resource createResource(URI uri) {
            XMLResource result = new OgckmlResourceImpl(uri);
            setupOptions(result);
            return result;
        }

        protected void setupOptions(XMLResource result)
        {
            ...
            result.getDefaultLoadOptions().put(XMLResource.OPTION_EXTENDED_META_DATA, new BasicExtendedMetaData()
            {
                private static final String NAMESPACE_GOOGLE_EXT_2_2 = "http://www.google.com/kml/ext/2.2";
                private static final String NAMESPACE_OPENGIS_2_2 = "http://www.opengis.net/kml/2.2";
                private static final String NAMESPACE_DEFAULT = NAMESPACE_OPENGIS_2_2;

                @Override
                public EPackage getPackage(String namespace) {
                    if(namespace==null){
                        namespace = NAMESPACE_DEFAULT;
                    }
                    return super.getPackage(namespace);
                }

                @Override
                public EStructuralFeature getElement(String namespace, String name)
                {
                    if (feature == null)
                        feature = super.getElement(NAMESPACE_OPENGIS_2_2, name);
                    if (feature == null)
                        feature = super.getElement(NAMESPACE_GOOGLE_EXT_2_2, name);
                    return feature;
                }
        });
...
    }
    }
+2

I have the same ATL issue that I did to avoid the "PackageNotFoundException": Fist problem. You must not allow the model namespace ur NULL, define the namespace URI "nsURI" . to identify the Ecore model in the code generators of "xml files. Thus, simply by calling nsURI, any name will correspond to ur EPackage. I also rewrite xsi: schemLocation =" .... "manulay, it may not make sense, but in the end he solves the problem

0
source

All Articles