summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-05-02 13:15:48 (EDT)
committerpelder2007-05-02 13:15:48 (EDT)
commitc4bc0a1084f23db6eeabd41798a45f407a9cb922 (patch)
tree4861d0a1ab68e5eb2d24f0d7be476e08a8171a3f
parent0207798868de8a3925deeb32cfb42e2257c37100 (diff)
downloadorg.eclipse.jet-c4bc0a1084f23db6eeabd41798a45f407a9cb922.zip
org.eclipse.jet-c4bc0a1084f23db6eeabd41798a45f407a9cb922.tar.gz
org.eclipse.jet-c4bc0a1084f23db6eeabd41798a45f407a9cb922.tar.bz2
[185143] Enable document XML loading to proceed without errors, in the case where no schema location is specified.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/EMFXMLModelLoader.java34
-rw-r--r--tests/org.eclipse.jet.tests/data/beanModel.badlocation.xml21
-rw-r--r--tests/org.eclipse.jet.tests/data/newproject.nolocation.xml4
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/model/load/TestXMLDocumentLoads.java75
4 files changed, 115 insertions, 19 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/EMFXMLModelLoader.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/EMFXMLModelLoader.java
index bc75db4..7e3e47c 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/EMFXMLModelLoader.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/runtime/model/EMFXMLModelLoader.java
@@ -24,10 +24,13 @@ import java.util.Map;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.EPackage.Registry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.Resource.Factory;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.util.BasicExtendedMetaData;
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceFactoryImpl;
@@ -42,6 +45,27 @@ public class EMFXMLModelLoader implements IModelLoader
private static final String XML_TYPE = "xml"; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
+ private final static class MyExtendedMetaData extends BasicExtendedMetaData {
+
+
+ public MyExtendedMetaData(Registry registry)
+ {
+ super(registry);
+ }
+
+ public EPackage getPackage(String namespace)
+ {
+ // Check the demand registry for packages. BasicExtendedMetaData does not do this.
+ // As a result, demand created packages (packages corresponding to XML namespaces
+ // for which there is no schema) are lost. This override finds them and thus
+ // allows parsing of this category of document.
+ EPackage ePkg = super.getPackage(namespace);
+ if(ePkg == null) {
+ ePkg = demandRegistry.getEPackage(namespace);
+ }
+ return ePkg;
+ }
+ }
/**
*
*/
@@ -77,12 +101,14 @@ public class EMFXMLModelLoader implements IModelLoader
/**
* Create the standard load options for EMF Resources. This method includes a request that
* EMF ExtendedMetaData be respected.
+ * @param resourceSet TODO
* @return
*/
- private Map getLoadOptions()
+ private Map getLoadOptions(ResourceSet resourceSet)
{
Map options = new HashMap();
- options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
+ options.put(XMLResource.OPTION_EXTENDED_META_DATA, new MyExtendedMetaData(resourceSet.getPackageRegistry()));
+
return options;
}
@@ -122,7 +148,7 @@ public class EMFXMLModelLoader implements IModelLoader
// Resource emfResource = factory.createResource(emfURI);
Resource emfResource = resourceSet.createResource(emfURI);
- Map options = getLoadOptions();
+ Map options = getLoadOptions(resourceSet);
emfResource.load(options);
return getDocumentRoot(emfResource);
@@ -143,7 +169,7 @@ public class EMFXMLModelLoader implements IModelLoader
final Resource.Factory factory = (Factory)resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().get(kind);
Resource emfResource = factory.createResource(emfURI);
- Map options = getLoadOptions();
+ Map options = getLoadOptions(resourceSet);
emfResource.load(new ByteArrayInputStream(serializedModel.getBytes("UTF-8")), options); //$NON-NLS-1$
diff --git a/tests/org.eclipse.jet.tests/data/beanModel.badlocation.xml b/tests/org.eclipse.jet.tests/data/beanModel.badlocation.xml
new file mode 100644
index 0000000..e819cbf
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/data/beanModel.badlocation.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="missing.beanModel.xsd">
+ <beanModel basePackage="org.example" name="Library">
+ <bean name="Library">
+ <attribute kind="FIELD" name="name" type="String"/>
+ <attribute kind="FIELD" name="yearFounded" type="int"/>
+ <attribute kind="DERIVED" name="yearsOpen" type="int"/>
+ <attribute kind="LIST" name="books" type="Book"/>
+ <attribute kind="LIST" name="authors" type="Author"/>
+ </bean>
+ <bean name="Book">
+ <attribute kind="FIELD" name="title" type="String"/>
+ <attribute kind="FIELD" name="pages" type="int"/>
+ <attribute kind="LIST" name="author" type="Author"/>
+ </bean>
+ <bean name="Author">
+ <attribute kind="FIELD" name="name" type="String"/>
+ <attribute kind="LIST" name="books" type="Book"/>
+ </bean>
+ </beanModel>
+</root>
diff --git a/tests/org.eclipse.jet.tests/data/newproject.nolocation.xml b/tests/org.eclipse.jet.tests/data/newproject.nolocation.xml
new file mode 100644
index 0000000..28ae5f1
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/data/newproject.nolocation.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<p:newProjectModel xmlns:p="org.eclipse.jet.transforms.newproject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <project name="test3.foo.bar"/> <!-- HELLO -->
+</p:newProjectModel>
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/model/load/TestXMLDocumentLoads.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/model/load/TestXMLDocumentLoads.java
index db8e8e2..52cf8b0 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/model/load/TestXMLDocumentLoads.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/model/load/TestXMLDocumentLoads.java
@@ -1,6 +1,7 @@
package org.eclipse.jet.tests.model.load;
import java.io.IOException;
+import java.net.MalformedURLException;
import java.net.URL;
import junit.framework.TestCase;
@@ -26,17 +27,7 @@ public class TestXMLDocumentLoads extends TestCase {
* for XSD schemas without a target namespace.
* @throws IOException
*/
- public void testOne() throws IOException {
- testVerifyXMLLoadsDoNotAffectGlobalPackageRegistry();
- }
-
- /**
- * This test verifies the the XML Model loader does not put dynamically loaded
- * XSD packages in the the default package registry. This is especially important
- * for XSD schemas without a target namespace.
- * @throws IOException
- */
- public void testVerifyXMLLoadsDoNotAffectGlobalPackageRegistry() throws IOException {
+ public void testPlainXML() throws IOException {
final int pkgRegSz = EPackage.Registry.INSTANCE.size();
URL plainURL = new URL("platform:/plugin/org.eclipse.jet.tests/data/plain.xml");
@@ -45,24 +36,78 @@ public class TestXMLDocumentLoads extends TestCase {
assertNotNull(plainModel1);
assertEquals(pkgRegSz, EPackage.Registry.INSTANCE.size());
+
+ final Object plainModel2 = loader.load(plainURL);
+ assertNotNull(plainModel2);
+
+ assertEquals(pkgRegSz, EPackage.Registry.INSTANCE.size());
+ }
+
+ /**
+ * This test verifies the the XML Model loader does not put dynamically loaded
+ * XSD packages in the the default package registry. This is especially important
+ * for XSD schemas without a target namespace.
+ * @throws IOException
+ */
+ public void testXML_knownSchema_schemaLocation() throws IOException {
+ final int pkgRegSz = EPackage.Registry.INSTANCE.size();
URL newProjectURL = new URL("platform:/plugin/org.eclipse.jet.tests/data/newproject.xml");
final Object newProjectModel = loader.load(newProjectURL);
assertNotNull(newProjectModel);
assertEquals(pkgRegSz, EPackage.Registry.INSTANCE.size());
+ }
+
+ /**
+ * This test verifies the the XML Model loader does not put dynamically loaded
+ * XSD packages in the the default package registry. This is especially important
+ * for XSD schemas without a target namespace.
+ * @throws IOException
+ */
+ public void testXML_knownSchema_noNamespaceSchemaLocation() throws IOException {
+ final int pkgRegSz = EPackage.Registry.INSTANCE.size();
URL beanModelURL = new URL("platform:/plugin/org.eclipse.jet.tests/data/beanModel.xml");
final Object beanModel = loader.load(beanModelURL);
assertNotNull(beanModel);
assertEquals(pkgRegSz, EPackage.Registry.INSTANCE.size());
+ }
+
+ /**
+ * This test verifies the the XML Model loader does not put dynamically loaded
+ * XSD packages in the the default package registry. This is especially important
+ * for XSD schemas without a target namespace.
+ * @throws IOException
+ */
+ public void testXML_unknownSchema() throws IOException {
+ final int pkgRegSz = EPackage.Registry.INSTANCE.size();
- final Object plainModel2 = loader.load(plainURL);
- assertNotNull(plainModel2);
+ URL newProjectURL = new URL("platform:/plugin/org.eclipse.jet.tests/data/newproject.nolocation.xml");
+ final Object newProjectModel = loader.load(newProjectURL);
+ assertNotNull(newProjectModel);
assertEquals(pkgRegSz, EPackage.Registry.INSTANCE.size());
-
-
}
+
+ /**
+ * This test verifies the the XML Model loader does not put dynamically loaded
+ * XSD packages in the the default package registry. This is especially important
+ * for XSD schemas without a target namespace.
+ * @throws MalformedURLException
+ */
+ public void testXML_missingSchema_noNamespace() throws MalformedURLException {
+ try {
+ URL beanModelURL = new URL("platform:/plugin/org.eclipse.jet.tests/data/beanModel.badlocation.xml");
+ loader.load(beanModelURL);
+ fail();
+ } catch (MalformedURLException e) {
+ throw e;
+ } catch (IOException e) {
+ // success
+ }
+ }
+
+
}