Author: frankb
Date: Wed Apr 5 13:27:22 2006
New Revision: 391789
URL: http://svn.apache.org/viewcvs?rev=391789&view=rev
Log:
TUSCANY-119
Added:
incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentNoNamespaceSchemaLocation.xsd
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentSchemaLocation.xsd
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml
Modified:
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
Modified:
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java?rev=391789&r1=391788&r2=391789&view=diff
==============================================================================
---
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
(original)
+++
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/XMLDocumentImpl.java
Wed Apr 5 13:27:22 2006
@@ -23,11 +23,13 @@
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.tuscany.sdo.SDOPackage;
import org.apache.tuscany.sdo.util.DataObjectUtil;
+import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
@@ -91,6 +93,8 @@
protected EObject documentRoot;
+ protected final static String WHITESPACE_REGEX = "\\s";
+
protected static XMLParserPool globalXMLParserPool = new XMLParserPoolImpl();
//TODO clean up the options thing
@@ -327,23 +331,192 @@
resource.setXMLVersion(xmlVersion);
}
+ /**
+ * @return an EMap containing the schema locations or null when no map
+ */
+ protected EMap getSchemaLocationMap()
+ {
+ EMap result = null;
+ if ((documentRoot != null) && (extendedMetaData != null))
+ {
+ EReference xsiSchemaLocationMapFeature = extendedMetaData
+ .getXSISchemaLocationMapFeature(documentRoot.eClass());
+ if (xsiSchemaLocationMapFeature != null)
+ {
+ result = (EMap) documentRoot.eGet(xsiSchemaLocationMapFeature);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * @param value
+ * from schema location map.
+ * @return string form of URI from provided value, deresolved if appropriate.
+ */
+ protected String deresolve(String value)
+ {
+ URI location = URI.createURI(value);
+ URI resourceURI = resource.getURI();
+ boolean shouldDeresolve = resourceURI != null && !resourceURI.isRelative()
+ && resourceURI.isHierarchical();
+ if (shouldDeresolve && !location.isRelative())
+ {
+ URI deresolvedURI = location.deresolve(resourceURI, true, true, false);
+ if (deresolvedURI.hasRelativePath())
+ {
+ location = deresolvedURI;
+ }
+ }
+ return location.toString();
+ }
+
+ /**
+ * @param value
+ * for schema location from input parameter.
+ * @return string form of URI from provided value, resolved if appropriate.
+ */
+ protected String resolve(String value)
+ {
+ URI location = URI.createURI(value);
+ URI resourceURI = resource.getURI();
+ boolean shouldResolve = resourceURI != null && resourceURI.isHierarchical()
+ && !resourceURI.isRelative();
+ if (shouldResolve && location.isRelative() && location.hasRelativePath())
+ {
+ location = location.resolve(resourceURI, false);
+ }
+ return location.toString();
+ }
+
public String getSchemaLocation()
{
- throw new UnsupportedOperationException(); //TODO
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ if (!xsiSchemaLocationMap.isEmpty())
+ {
+ StringBuffer xsiSchemaLocation = new StringBuffer();
+ for (Iterator i = xsiSchemaLocationMap.entrySet().iterator(); i
+ .hasNext();)
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ String namespace = (String) entry.getKey();
+ if (namespace != null)
+ {
+ if (xsiSchemaLocation.length() > 0)
+ {
+ xsiSchemaLocation.append(' ');
+ }
+ xsiSchemaLocation.append(namespace);
+ xsiSchemaLocation.append(' ');
+ String value = entry.getValue().toString();
+ xsiSchemaLocation.append(deresolve(value));
+ }
+ }
+ return xsiSchemaLocation.toString().equals("") ? null
+ : xsiSchemaLocation.toString();
+ }
+ }
+ return null;
}
public void setSchemaLocation(String schemaLocation)
{
- throw new UnsupportedOperationException(); //TODO
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ // only remove the entries from xsiSchemaLocationMap that contain a
+ // non-null key
+ for (Iterator i = xsiSchemaLocationMap.entrySet().iterator();
i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+ if (entry.getKey() != null)
+ {
+ i.remove();
+ }
+ }
+ if (xsiSchemaLocationMap.size() == 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.FALSE);
+ }
+ if (schemaLocation != null)
+ {
+ String[] values = schemaLocation.split(WHITESPACE_REGEX);
+ for (int i = 0; i < values.length; i++) // note: also incremented in
+ // loop
+ {
+ String key = values[i++];
+ if (i < values.length)
+ {
+ xsiSchemaLocationMap.put(key, resolve(values[i]));
+ }
+ }
+ if (xsiSchemaLocationMap.size() != 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
+ }
+ }
+ }
}
public String getNoNamespaceSchemaLocation()
{
- throw new UnsupportedOperationException(); //TODO
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ StringBuffer xsiSchemaLocation = new StringBuffer();
+ if (!xsiSchemaLocationMap.isEmpty())
+ {
+ Object valueObject = xsiSchemaLocationMap.get(null);
+ if (valueObject != null)
+ {
+ String valueString = (String) valueObject;
+ String[] values = valueString.split(WHITESPACE_REGEX);
+ for (int i = 0; i < values.length; i++)
+ {
+ if (xsiSchemaLocation.length() > 0)
+ {
+ xsiSchemaLocation.append(' ');
+ }
+ xsiSchemaLocation.append(deresolve(values[i]));
+ }
+ }
+ String result = xsiSchemaLocation.toString();
+ return result.equals("") ? null : result;
+ }
+ }
+ return null;
}
public void setNoNamespaceSchemaLocation(String schemaLocation)
{
- throw new UnsupportedOperationException(); //TODO
+ EMap xsiSchemaLocationMap = getSchemaLocationMap();
+ if (xsiSchemaLocationMap != null)
+ {
+ // only remove the entries from xsiSchemaLocationMap that contain a null
+ // key
+ xsiSchemaLocationMap.removeKey(null);
+ if (xsiSchemaLocationMap.size() == 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.FALSE);
+ }
+ if (schemaLocation != null)
+ {
+ String[] values = schemaLocation.split(WHITESPACE_REGEX);
+ for (int i = 0; i < values.length; i++)
+ {
+ xsiSchemaLocationMap.put(null, resolve(values[i]));
+ }
+ if (xsiSchemaLocationMap.size() != 0)
+ {
+ resource.getDefaultSaveOptions().put(
+ XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
+ }
+ }
+ }
}
}
Added:
incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java?rev=391789&view=auto
==============================================================================
---
incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java
(added)
+++
incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java
Wed Apr 5 13:27:22 2006
@@ -0,0 +1,107 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as
applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.sdo.test;
+
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XMLHelper;
+
+
+public class XMLDocumentTestCase extends TestCase
+{
+ private final String TEST_XML_DOCUMENT = "/XMLDocumentTestCase.xml";
+
+ // remember that NS1... and NS2... items are expected in sorted order by name
+ private final String NS1_SCHEMA_NAME = "http://www.example.com/open";
+
+ private final String NS1_SCHEMA_LOCATION = "/open.xsd";
+
+ private final String NS2_SCHEMA_NAME =
"http://www.example.com/xmlDocumentSchemaLocation";
+
+ private final String NS2_SCHEMA_LOCATION = "/XMLDocumentSchemaLocation.xsd";
+
+ private final String NS_SET_NAME_LOCATION = "namespace schemaLocation";
+
+ //private final String NNS_SCHEMA_LOCATION =
"http://www.example.com/XMLDocumentNoNamespaceSchemaLocation.xsd";
+ private final String NNS_SCHEMA_LOCATION =
"/XMLDocumentNoNamespaceSchemaLocation.xsd";
+
+ private final String NNS_SET_LOCATION = "noNamespaceSchemaLocation";
+
+ /**
+ * This method will load an xml document consisting of a xsi:schemaLocation
and
+ * xsi:noNamespaceSchemaLocation defined. It will then use the XMLDocument
API to get and
+ * set the schemaLocation property.
+ *
+ * @throws IOException
+ */
+ public void testSchemaLocation() throws IOException
+ {
+ // load the xml document which has xsi:noNamespaceSchemaLocation and
xsi:schemaLocation defined
+ XMLDocument doc =
XMLHelper.INSTANCE.load(getClass().getResourceAsStream(TEST_XML_DOCUMENT));
+
+ // get the schemaLocation
+ assertEquals(NS1_SCHEMA_NAME + " " + NS1_SCHEMA_LOCATION + " " +
NS2_SCHEMA_NAME + " " + NS2_SCHEMA_LOCATION, doc.getSchemaLocation());
+
+ // set the schemaLocation to another value and test to see if the value
was set
+ doc.setSchemaLocation(NS_SET_NAME_LOCATION);
+ assertEquals(NS_SET_NAME_LOCATION, doc.getSchemaLocation());
+
+ // remove the schemaLocation and ensure it returns null
+ doc.setSchemaLocation(null);
+ assertNull(doc.getSchemaLocation());
+
+ // ensure changes to schemaLocation have not changed
noNamespaceSchemaLocation
+ assertEquals(NNS_SCHEMA_LOCATION, doc.getNoNamespaceSchemaLocation());
+ }
+
+ /**
+ * This method will load an xml document consisting of a xsi:schemaLocation
and
+ * xsi:noNamespaceSchemaLocation defined. It will then use the XMLDocument
API to get and
+ * set the noNamespaceSchemaLocation property.
+ *
+ * @throws IOException
+ */
+ public void testNoNamespaceSchemaLocation() throws IOException
+ {
+ // load the xml document which has xsi:noNamespaceSchemaLocation and
xsi:schemaLocation defined
+ XMLDocument doc =
XMLHelper.INSTANCE.load(getClass().getResourceAsStream(TEST_XML_DOCUMENT));
+
+ // get the noNamespaceSchemaLocation
+ assertEquals(NNS_SCHEMA_LOCATION, doc.getNoNamespaceSchemaLocation());
+
+ // set the noNameSpaceSchemaLocation to another value and test to see if
the value was set
+ doc.setNoNamespaceSchemaLocation(NNS_SET_LOCATION);
+ assertEquals(NNS_SET_LOCATION, doc.getNoNamespaceSchemaLocation());
+
+ // remove the noNameSpaceSchemaLocation and ensure it returns null
+ doc.setNoNamespaceSchemaLocation(null);
+ assertNull(doc.getNoNamespaceSchemaLocation());
+
+ // ensure changes to noNameSpaceSchemaLocation have not changed
schemaLocation
+ assertEquals(NS1_SCHEMA_NAME + " " + NS1_SCHEMA_LOCATION + " " +
NS2_SCHEMA_NAME + " " + NS2_SCHEMA_LOCATION, doc.getSchemaLocation());
+ }
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ }
+
+}
Added:
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentNoNamespaceSchemaLocation.xsd
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentNoNamespaceSchemaLocation.xsd?rev=391789&view=auto
==============================================================================
---
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentNoNamespaceSchemaLocation.xsd
(added)
+++
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentNoNamespaceSchemaLocation.xsd
Wed Apr 5 13:27:22 2006
@@ -0,0 +1,12 @@
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
+ <xsd:element name="aNoNamespaceSchemaLocationElement">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="anElement" type="xsd:string" minOccurs="1"
maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="date" type="xsd:date"/>
+ </xsd:complexType>
+ </xsd:element>
+
+</xsd:schema>
Added:
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentSchemaLocation.xsd
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentSchemaLocation.xsd?rev=391789&view=auto
==============================================================================
---
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentSchemaLocation.xsd
(added)
+++
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentSchemaLocation.xsd
Wed Apr 5 13:27:22 2006
@@ -0,0 +1,16 @@
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ targetNamespace="http://www.example.com/xmlDocumentSchemaLocation"
+ xmlns:sl="http://www.example.com/xmlDocumentSchemaLocation">
+
+ <xsd:element name="schemaLocationElement" type="xsd:string" />
+
+ <xsd:element name="purchaseReport">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:any minOccurs="1" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="periodEnding" type="xsd:date"/>
+ </xsd:complexType>
+ </xsd:element>
+
+</xsd:schema>
Added:
incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml?rev=391789&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/XMLDocumentTestCase.xml
Wed Apr 5 13:27:22 2006
@@ -0,0 +1,14 @@
+<sl:purchaseReport
+ xmlns:sl="http://www.example.com/xmlDocumentSchemaLocation"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.example.com/xmlDocumentSchemaLocation
+ /XMLDocumentSchemaLocation.xsd
+ http://www.example.com/open
+ /open.xsd"
+ xsi:noNamespaceSchemaLocation="/XMLDocumentNoNamespaceSchemaLocation.xsd"
+ periodEnding="2007-12-31">
+ <sl:schemaLocationElement>some string</sl:schemaLocationElement>
+ <aNoNamespaceSchemaLocationElement date="2006-04-01">
+ <anElement>another string</anElement>
+ </aNoNamespaceSchemaLocationElement>
+</sl:purchaseReport>