You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jaxme-dev@ws.apache.org by jo...@apache.org on 2004/08/16 10:34:45 UTC
cvs commit: ws-jaxme/src/jaxme/org/apache/ws/jaxme/junit MarshallerTest.java
jochen 2004/08/16 01:34:45
Modified: . jaxme.properties.sample status.xml
src/jaxme/org/apache/ws/jaxme/util NamespaceSupport.java
src/jaxme/org/apache/ws/jaxme/impl JMMarshallerImpl.java
JMXmlSerializerImpl.java
src/jaxme/org/apache/ws/jaxme JMXmlSerializer.java
src/jaxme/org/apache/ws/jaxme/junit MarshallerTest.java
Log:
The Marshaller is now creating the attributes xsi:schemaLocation and xsi:noNamespaceSchemaLocation properly.
PR: JAXME-22
Revision Changes Path
1.5 +1 -0 ws-jaxme/jaxme.properties.sample
Index: jaxme.properties.sample
===================================================================
RCS file: /home/cvs/ws-jaxme/jaxme.properties.sample,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- jaxme.properties.sample 23 Jul 2004 07:18:00 -0000 1.4
+++ jaxme.properties.sample 16 Aug 2004 08:34:44 -0000 1.5
@@ -64,6 +64,7 @@
#
#docbook.home=C:/Prg/docbook-xml-4.2
#docbook.home=/usr/local/docbook-xml-4.2
+#docbook.home=${forrest.home}/context/resources/schema/docbook/4.2
#
############################################################################
#
1.21 +5 -0 ws-jaxme/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/ws-jaxme/status.xml,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- status.xml 15 Aug 2004 11:46:28 -0000 1.20
+++ status.xml 16 Aug 2004 08:34:44 -0000 1.21
@@ -59,6 +59,11 @@
The xs:all type is now properly handled. See JAXME-16 and
JAXME-15 in Jira.
</action>
+ <action dev="JW" type="fix" context="runtime">
+ The marshaller properties for setting the attributes
+ xsi:schemaLocation, or xsi:noNamespaceSchemaLocation,
+ are now handled properly. See JAXME-22 in Jira.
+ </action>
</release>
<release version="0.3" date="2004-Jul-19">
<action dev="CL" type="enhancement" context="generator">
1.5 +2 -2 ws-jaxme/src/jaxme/org/apache/ws/jaxme/util/NamespaceSupport.java
Index: NamespaceSupport.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/util/NamespaceSupport.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- NamespaceSupport.java 29 Mar 2004 12:29:28 -0000 1.4
+++ NamespaceSupport.java 16 Aug 2004 08:34:44 -0000 1.5
@@ -62,7 +62,7 @@
cachedURI = pURI;
}
}
-
+
/** <p>Removes a prefix declaration. Assumes that the prefix is the
* current prefix. If not, throws a IllegalStateException.</p>
*/
1.6 +64 -1 ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMMarshallerImpl.java
Index: JMMarshallerImpl.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMMarshallerImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- JMMarshallerImpl.java 12 Apr 2004 15:09:18 -0000 1.5
+++ JMMarshallerImpl.java 16 Aug 2004 08:34:44 -0000 1.6
@@ -112,6 +112,8 @@
private String indentationSeparator = DEFAULT_JAXME_INDENTATION_SEPARATOR;
private boolean xmlDeclaration;
private Class xmlWriterClass;
+ private String noNamespaceSchemaLocation, schemaLocation;
+
/** <p>Sets the controllers encoding; to be used in
* marshalling. Defaults to {@link #DEFAULT_JAXB_ENCODING}.</p>
*
@@ -219,6 +221,61 @@
*/
public String getIndentationSeparator() { return indentationSeparator; }
+ /** <p>Sets the schema location. The marshaller will use this to
+ * create an attribute <code>xsi:schemaLocation</code>. Equivalent
+ * to <code>setProperty(JAXB_SCHEMA_LOCATION, pValue)</code>.
+ * Defaults to null, in which case the attribute isn't created.</p>
+ * @see Marshaller#JAXB_SCHEMA_LOCATION
+ * @see #setProperty(String, Object)
+ * @see #getSchemaLocation()
+ */
+ public void setSchemaLocation(String pValue) throws PropertyException {
+ if (pValue != null && noNamespaceSchemaLocation != null) {
+ throw new PropertyException("The properties schemaLocation and noNamespaceSchemaLocation are mutually exclusive.");
+ }
+ schemaLocation = pValue;
+ }
+
+ /** <p>Returns the schema location. The marshaller will use this to
+ * create an attribute <code>xsi:schemaLocation</code>. Equivalent
+ * to <code>setProperty(JAXB_SCHEMA_LOCATION, pValue)</code>.
+ * Defaults to null, in which case the attribute isn't created.</p>
+ * @see Marshaller#JAXB_SCHEMA_LOCATION
+ * @see #setProperty(String, Object)
+ * @see #setSchemaLocation(String)
+ */
+ public String getSchemaLocation() {
+ return schemaLocation;
+ }
+
+ /** <p>Sets the schema location without namespace. The marshaller
+ * will use this to create an attribute <code>xsi:noNamespaceSchemaLocation</code>.
+ * Equivalent to <code>setProperty(JAXB_NO_NAMESPACE_SCHEMA_LOCATION,
+ * pValue)</code>. Defaults to null, in which case the attribute isn't
+ * created.</p>
+ * @see Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
+ * @see #setProperty(String, Object)
+ * @see #getNoNamespaceSchemaLocation()
+ */
+ public void setNoNamespaceSchemaLocation(String pValue) throws PropertyException {
+ if (pValue != null && noNamespaceSchemaLocation != null) {
+ throw new PropertyException("The properties schemaLocation and noNamespaceSchemaLocation are mutually exclusive.");
+ }
+ noNamespaceSchemaLocation = pValue;
+ }
+
+ /** <p>Returns the schema location. The marshaller will use this to
+ * create an attribute <code>xsi:noNamespaceSchemaLocation</code>. Equivalent
+ * to <code>setProperty(JAXB_SCHEMA_LOCATION, pValue)</code>.
+ * Defaults to null, in which case the attribute isn't created.</p>
+ * @see Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
+ * @see #setProperty(String, Object)
+ * @see #setNoNamespaceSchemaLocation(String)
+ */
+ public String getNoNamespaceSchemaLocation() {
+ return noNamespaceSchemaLocation;
+ }
+
public void setProperty(String pProperty, Object pValue)
throws PropertyException {
if (pProperty.startsWith("jaxb.")) {
@@ -227,6 +284,12 @@
return;
} else if (Marshaller.JAXB_FORMATTED_OUTPUT.equals(pProperty)) {
setIndentation(((Boolean) pValue).booleanValue());
+ return;
+ } else if (Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION.equals(pProperty)) {
+ setNoNamespaceSchemaLocation((String) pValue);
+ return;
+ } else if (Marshaller.JAXB_SCHEMA_LOCATION.equals(pProperty)) {
+ setSchemaLocation((String) pValue);
return;
}
} else if (pProperty.startsWith("jaxme.")) {
1.4 +42 -3 ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMXmlSerializerImpl.java
Index: JMXmlSerializerImpl.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMXmlSerializerImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JMXmlSerializerImpl.java 16 Feb 2004 23:39:57 -0000 1.3
+++ JMXmlSerializerImpl.java 16 Aug 2004 08:34:44 -0000 1.4
@@ -22,6 +22,7 @@
import org.apache.ws.jaxme.JMMarshaller;
import org.apache.ws.jaxme.JMXmlSerializer;
+import org.apache.ws.jaxme.XMLConstants;
import org.apache.ws.jaxme.util.NamespaceSupport;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
@@ -37,6 +38,7 @@
private static final Attributes zeroAttributes = new AttributesImpl();
protected class Data implements JMXmlSerializer.Data {
+ private boolean rootElementCreated;
private int cnt;
private JMMarshaller marshaller;
private ContentHandler handler;
@@ -71,6 +73,11 @@
pc = pSuggestedPrefix + ++cnt;
}
}
+
+ public boolean isRootElementCreated() { return rootElementCreated; }
+ public void setRootElementCreated(boolean pCreated) {
+ rootElementCreated = pCreated;
+ }
}
public void init(JAXBContextImpl pFactory) throws JAXBException {}
@@ -160,8 +167,12 @@
if (uri == null) uri = "";
String name = pName.getLocalPart();
String qName = getElementQName(pData, uri, name);
- handler.startElement(uri, name, qName,
- getAttributes(pData, pElement));
+ AttributesImpl attrs = getAttributes(pData, pElement);
+ if (!pData.isRootElementCreated()) {
+ createSchemaLocationAttributes(pData, attrs);
+ pData.setRootElementCreated(true);
+ }
+ handler.startElement(uri, name, qName, attrs);
marshalChilds(pData, pElement);
handler.endElement(uri, name, qName);
for (;;) {
@@ -171,5 +182,33 @@
}
handler.endPrefixMapping(prefix);
}
+ }
+
+ protected void createSchemaLocationAttributes(JMXmlSerializer.Data pData, AttributesImpl pAttrs) throws SAXException {
+ JMMarshallerImpl m = (JMMarshallerImpl) pData.getJMMarshaller();
+ String schemaLocation = m.getSchemaLocation();
+ String schemaLocationAttribute;
+ if (schemaLocation != null) {
+ schemaLocationAttribute = XMLConstants.XML_SCHEMA_NS_ATTR;
+ } else {
+ schemaLocation = m.getNoNamespaceSchemaLocation();
+ if (schemaLocation != null) {
+ schemaLocationAttribute = XMLConstants.XML_SCHEMA_NO_NS_ATTR;
+ } else {
+ schemaLocationAttribute = null;
+ }
+ }
+ if (schemaLocation != null) {
+ NamespaceSupport nss = pData.getNamespaceContext();
+ String xsi = nss.getPrefix(XMLConstants.XML_SCHEMA_URI);
+ if (xsi == null) {
+ xsi = pData.getNewPrefix(XMLConstants.XML_SCHEMA_URI, "xsi");
+ pData.getContentHandler().startPrefixMapping(xsi, XMLConstants.XML_SCHEMA_URI);
+ }
+ String xsiQName = (xsi == null || "".equals(xsi)) ?
+ schemaLocationAttribute : (xsi + ":" + schemaLocationAttribute);
+ pAttrs.addAttribute(XMLConstants.XML_SCHEMA_URI, schemaLocationAttribute, xsiQName, "CDATA",
+ schemaLocation);
+ }
}
}
1.4 +13 -1 ws-jaxme/src/jaxme/org/apache/ws/jaxme/JMXmlSerializer.java
Index: JMXmlSerializer.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/JMXmlSerializer.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JMXmlSerializer.java 16 Feb 2004 23:39:48 -0000 1.3
+++ JMXmlSerializer.java 16 Aug 2004 08:34:44 -0000 1.4
@@ -64,6 +64,18 @@
/** <p>Returns the {@link javax.xml.bind.DatatypeConverterInterface}.</p>
*/
public DatatypeConverterInterface getDatatypeConverter();
+
+ /** <p>Returns whether the root element has already been generated.
+ * If this isn't the case, then the caller must take care of
+ * attributes like xsi:schemaLocation, and the like.</p>
+ */
+ public boolean isRootElementCreated();
+
+ /** <p>Sets whether the root element has already been generated.
+ * If this isn't the case, then the caller must take care of
+ * attributes like xsi:schemaLocation, and the like.</p>
+ */
+ public void setRootElementCreated(boolean pCreated);
}
/** <p>Initializes the JMXmlSerializer; called by the JAXBContextImpl
* before the serializer is returned to the Marshaller. This method
1.13 +153 -45 ws-jaxme/src/jaxme/org/apache/ws/jaxme/junit/MarshallerTest.java
Index: MarshallerTest.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/junit/MarshallerTest.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- MarshallerTest.java 27 Jul 2004 08:41:18 -0000 1.12
+++ MarshallerTest.java 16 Aug 2004 08:34:44 -0000 1.13
@@ -16,6 +16,7 @@
*/
package org.apache.ws.jaxme.junit;
+import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigDecimal;
@@ -24,8 +25,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
-import java.util.Comparator;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.TimeZone;
import javax.xml.bind.JAXBContext;
@@ -58,9 +60,13 @@
import org.apache.ws.jaxme.test.misc.types.impl.AllSimpleTypesSerializer;
import org.apache.ws.jaxme.test.misc.types.impl.AllTypesElementImpl;
import org.apache.ws.jaxme.util.Duration;
+import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
/**
@@ -70,17 +76,19 @@
public class MarshallerTest extends BaseTestCase {
private JAXBContextImpl factory;
+ /** <p>Creates a new instance of MarshallerTest.</p>
+ */
public MarshallerTest(String arg) { super(arg); }
public void setUp() throws JAXBException {
factory = (JAXBContextImpl) JAXBContext.newInstance("org.apache.ws.jaxme.test.misc.types");
}
- public JAXBContextImpl getFactory() {
+ protected JAXBContextImpl getFactory() {
return factory;
}
- public Calendar getDateTime() {
+ protected Calendar getDateTime() {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.DAY_OF_MONTH, 22);
cal.set(Calendar.MONTH, 11);
@@ -91,7 +99,8 @@
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
- public Calendar getTime() {
+
+ protected Calendar getTime() {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 43);
@@ -102,7 +111,8 @@
cal.set(Calendar.DAY_OF_MONTH, 0);
return cal;
}
- public Calendar getDate() {
+
+ protected Calendar getDate() {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.set(Calendar.DAY_OF_MONTH, 22);
cal.set(Calendar.MONTH, 11);
@@ -113,15 +123,17 @@
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
- public Duration getDuration() {
+
+ protected Duration getDuration() {
Duration d = new Duration(false, 1, 2, 3, 4, 5, 6, 7);
return d;
}
- public byte[] getHexBytes() {
+
+ protected byte[] getHexBytes() {
return new byte[]{1, 17, 35, 78, 115, -99, -69, -1};
}
- public AllSimpleTypes getAllSimpleTypesElement() {
+ protected AllSimpleTypes getAllSimpleTypesElement() {
AllSimpleTypes element = new AllSimpleTypesImpl();
element.setStringElem("This is a string with german Umlauts: \u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc");
element.setDoubleElem(23979782937923.2398798);
@@ -150,7 +162,7 @@
return element;
}
- public AllTypesElement getAllTypesElement() throws JAXBException {
+ protected AllTypesElement getAllTypesElement() throws JAXBException {
AllTypesElement element = (AllTypesElement) getFactory().getElement(AllTypesElement.class);
element.setAllSimpleTypesElement(getAllSimpleTypesElement());
List list = element.getListTypeElement();
@@ -161,7 +173,7 @@
return element;
}
- public String getAllSimpleTypesElementString() {
+ protected String getAllSimpleTypesElementString() {
AllTypesElementImpl elem = new AllTypesElementImpl();
String uri = elem.getQName().getNamespaceURI();
return
@@ -191,44 +203,47 @@
"</ex:AllSimpleTypesElement>";
}
- private String getAllTypesElementString(boolean pSchemaDeclaration) {
+ private String getAllTypesElementString(boolean pSchemaDeclaration, boolean pFormatted) {
AllTypesElementImpl elem = new AllTypesElementImpl();
String uri = elem.getQName().getNamespaceURI();
String xsi = pSchemaDeclaration ? " xmlns:xsi=\""
+ XMLConstants.XML_SCHEMA_URI + "\" xsi:"
+ XMLConstants.XML_SCHEMA_NS_ATTR + "=\"foo.xsd\"" : "";
+ String lf = pFormatted ? "\n" : "";
+ String sep = pFormatted ? " " : "";
+ String sep2 = sep + sep;
return
- "<ex:AllTypesElement xmlns:ex=\"" + uri + "\"" + xsi + ">\n" +
- " <ex:AllSimpleTypesElement>\n" +
- " <ex:StringElem>This is a string with german Umlauts: äöüßÄÖÜ</ex:StringElem>\n" +
- " <ex:IntElem>-2139879</ex:IntElem>\n" +
- " <ex:LongElem>1290380128309182303</ex:LongElem>\n" +
- " <ex:ShortElem>23878</ex:ShortElem>\n" +
- " <ex:DoubleElem>2.397978293792324E13</ex:DoubleElem>\n" +
- " <ex:FloatElem>-34768.348</ex:FloatElem>\n" +
- " <ex:DateTimeElem>2002-11-22T16:43:37Z</ex:DateTimeElem>\n" +
- " <ex:DateElem>2002-11-22Z</ex:DateElem>\n" +
- " <ex:TimeElem>16:43:37Z</ex:TimeElem>\n" +
- " <ex:DurationElem>P1Y2M3DT4H5M6.7S</ex:DurationElem>\n" +
- " <ex:HexBinaryElem>0111234E739DBBFF</ex:HexBinaryElem>\n" +
- " <ex:Base64BinaryElem>AREjTnOdu/8=</ex:Base64BinaryElem>\n" +
- " <ex:NmTokenElem>a-name-token</ex:NmTokenElem>\n" +
- " <ex:NmTokensElem>a-name-token another:name-token</ex:NmTokensElem>\n" +
- " <ex:DecimalElem>602300000000000000000000</ex:DecimalElem>\n" +
- " <ex:IntegerElem>-602300000000000000000000</ex:IntegerElem>\n" +
- " <ex:NonNegativeIntegerElem>101</ex:NonNegativeIntegerElem>\n" +
- " <ex:PositiveIntegerElem>602300000000000000000000</ex:PositiveIntegerElem>\n" +
- " <ex:NonPositiveIntegerElem>-21714</ex:NonPositiveIntegerElem>\n" +
- " <ex:NegativeIntegerElem>-21714</ex:NegativeIntegerElem>\n" +
- " <ex:QNameElem>ex:ias</ex:QNameElem>\n" +
- " <ex:BooleanElem>true</ex:BooleanElem>\n" +
- " </ex:AllSimpleTypesElement>\n" +
- " <ex:ListTypeElement>7 -3 0</ex:ListTypeElement>\n" +
- " <ex:UnionTypeElement>2002-11-22T16:43:37Z</ex:UnionTypeElement>\n" +
+ "<ex:AllTypesElement xmlns:ex=\"" + uri + "\"" + xsi + ">" + lf +
+ sep + "<ex:AllSimpleTypesElement>" + lf +
+ sep2 + "<ex:StringElem>This is a string with german Umlauts: äöüßÄÖÜ</ex:StringElem>" + lf +
+ sep2 + "<ex:IntElem>-2139879</ex:IntElem>" + lf +
+ sep2 + "<ex:LongElem>1290380128309182303</ex:LongElem>" + lf +
+ sep2 + "<ex:ShortElem>23878</ex:ShortElem>" + lf +
+ sep2 + "<ex:DoubleElem>2.397978293792324E13</ex:DoubleElem>" + lf +
+ sep2 + "<ex:FloatElem>-34768.348</ex:FloatElem>" + lf +
+ sep2 + "<ex:DateTimeElem>2002-11-22T16:43:37Z</ex:DateTimeElem>" + lf +
+ sep2 + "<ex:DateElem>2002-11-22Z</ex:DateElem>" + lf +
+ sep2 + "<ex:TimeElem>16:43:37Z</ex:TimeElem>" + lf +
+ sep2 + "<ex:DurationElem>P1Y2M3DT4H5M6.7S</ex:DurationElem>" + lf +
+ sep2 + "<ex:HexBinaryElem>0111234E739DBBFF</ex:HexBinaryElem>" + lf +
+ sep2 + "<ex:Base64BinaryElem>AREjTnOdu/8=</ex:Base64BinaryElem>" + lf +
+ sep2 + "<ex:NmTokenElem>a-name-token</ex:NmTokenElem>" + lf +
+ sep2 + "<ex:NmTokensElem>a-name-token another:name-token</ex:NmTokensElem>" + lf +
+ sep2 + "<ex:DecimalElem>602300000000000000000000</ex:DecimalElem>" + lf +
+ sep2 + "<ex:IntegerElem>-602300000000000000000000</ex:IntegerElem>" + lf +
+ sep2 + "<ex:NonNegativeIntegerElem>101</ex:NonNegativeIntegerElem>" + lf +
+ sep2 + "<ex:PositiveIntegerElem>602300000000000000000000</ex:PositiveIntegerElem>" + lf +
+ sep2 + "<ex:NonPositiveIntegerElem>-21714</ex:NonPositiveIntegerElem>" + lf +
+ sep2 + "<ex:NegativeIntegerElem>-21714</ex:NegativeIntegerElem>" + lf +
+ sep2 + "<ex:QNameElem>ex:ias</ex:QNameElem>" + lf +
+ sep2 + "<ex:BooleanElem>true</ex:BooleanElem>" + lf +
+ sep + "</ex:AllSimpleTypesElement>" + lf +
+ sep + "<ex:ListTypeElement>7 -3 0</ex:ListTypeElement>" + lf +
+ sep + "<ex:UnionTypeElement>2002-11-22T16:43:37Z</ex:UnionTypeElement>" + lf +
"</ex:AllTypesElement>";
}
- public XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
+ protected XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
// Verify whether we can create a SAX Parser; it's better to detect this here
// than within JAXB
SAXParserFactory spf = SAXParserFactory.newInstance();
@@ -238,7 +253,7 @@
return sp.getXMLReader();
}
- public void verifyAllSimpleTypesElement(AllSimpleTypes pElement) {
+ protected void verifyAllSimpleTypesElement(AllSimpleTypes pElement) {
assertStringEquals("This is a string with german Umlauts: \u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc",
pElement.getStringElem());
assertEquals(new Double("2.397978293792324E13").doubleValue(),
@@ -257,7 +272,7 @@
assertTrue(pElement.isBooleanElem());
}
- public void verifyAllTypesElement(AllTypesElement pElement) {
+ protected void verifyAllTypesElement(AllTypesElement pElement) {
verifyAllSimpleTypesElement(pElement.getAllSimpleTypesElement());
List list = pElement.getListTypeElement();
assertEquals(3, list.size());
@@ -270,6 +285,8 @@
assertStringEquals(dateTime1, dateTime2);
}
+ /** <p>Tests unmarshalling of a simple element.</p>
+ */
public void testUnmarshalSimpleElements() throws Exception {
XMLReader xr = getXMLReader();
AllSimpleTypesHandler unmarshaller = new AllSimpleTypesHandler();
@@ -295,7 +312,7 @@
throws Exception {
JAXBContext myFactory = getFactory();
Unmarshaller unmarshaller = myFactory.createUnmarshaller();
- String s = getAllTypesElementString(pSchemaLocation);
+ String s = getAllTypesElementString(pSchemaLocation, true);
StringReader sr = new StringReader(s);
AllTypesElement result = (AllTypesElement) unmarshaller.unmarshal(new InputSource(sr));
verifyAllTypesElement(result);
@@ -303,6 +320,8 @@
verifyAllTypesElement(result);
}
+ /** <p>Tests marshalling of a simple element.</p>
+ */
public void testMarshalSimpleElements() throws Exception {
AllSimpleTypesSerializer marshaller = new AllSimpleTypesSerializer();
marshaller.init(getFactory());
@@ -315,6 +334,8 @@
assertStringEquals(getAllSimpleTypesElementString(), sw.toString());
}
+ /** <p>Tests marshalling of a complex element.</p>
+ */
public void testMarshalComplexElements() throws Exception {
JAXBContext myFactory = getFactory();
JMMarshallerImpl marshaller = (JMMarshallerImpl) myFactory.createMarshaller();
@@ -322,7 +343,7 @@
Class c = marshaller.getXMLWriterClass();
XMLWriter w = (XMLWriter) c.newInstance();
w.init(marshaller);
- String rawInput = getAllTypesElementString(false);
+ String rawInput = getAllTypesElementString(false, true);
StringBuffer input = new StringBuffer();
for (int i = 0; i < rawInput.length(); i++) {
char ch = rawInput.charAt(i);
@@ -418,7 +439,7 @@
assertEquals(60, duration2.getSeconds());
}
- public String getAllElementString(boolean pRandom) {
+ protected String getAllElementString(boolean pRandom) {
AllElementImpl elem = new AllElementImpl();
String uri = elem.getQName().getNamespaceURI();
String[] elements = new String[]{
@@ -464,6 +485,9 @@
return result.toString();
}
+ /** <p>Tests marshalling and unmarshalling of an element with "xs:all"
+ * contents.</p>
+ */
public void testAllElement() throws Exception {
String s = getAllElementString(true);
System.out.println(s);
@@ -478,5 +502,89 @@
String expect = getAllElementString(false);
assertEquals(expect, got);
+ }
+
+ private RootElementHandler getRootElementInfo(String pElement) throws ParserConfigurationException, IOException, SAXException {
+ RootElementHandler reh = new RootElementHandler();
+ XMLReader xr = getXMLReader();
+ try {
+ xr.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
+ } catch (SAXNotSupportedException e) {
+ } catch (SAXNotRecognizedException e) {
+ }
+ try {
+ xr.setFeature("http://xml.org/sax/features/xmlns-uris", true);
+ } catch (SAXNotSupportedException e) {
+ } catch (SAXNotRecognizedException e) {
+ }
+ xr.setContentHandler(reh);
+ xr.parse(new InputSource(new StringReader(pElement)));
+ return reh;
+ }
+
+ private class RootElementHandler extends DefaultHandler {
+ Attributes rootElementAttributes;
+ String rootElementURI, rootElementLocalName, rootElementQName;
+ private boolean isRootElement = true;
+ public void startElement(String pNamespaceURI, String pLocalName, String pQName,
+ Attributes pAttr) {
+ if (isRootElement) {
+ isRootElement = false;
+ rootElementAttributes = pAttr;
+ rootElementURI = pNamespaceURI;
+ rootElementLocalName = pLocalName;
+ rootElementQName = pQName;
+ }
+ }
+ }
+
+ private String getContents(String pElement) {
+ int offset1 = pElement.indexOf('>');
+ int offset2 = pElement.lastIndexOf('<');
+ assertTrue(offset2 > offset1);
+ return pElement.substring(offset1+1, offset2);
+ }
+
+ protected void assertEquals(Attributes pAttr1, Attributes pAttr2) {
+ assertEquals(pAttr1.getLength(), pAttr2.getLength());
+ for (int i = 0; i < pAttr1.getLength(); i++) {
+ String uri = pAttr1.getURI(i);
+ String localName = pAttr1.getLocalName(i);
+ String value = pAttr1.getValue(i);
+ assertNotNull(value);
+ assertEquals(value, pAttr2.getValue(uri, localName));
+ }
+ }
+
+ /** <p>Tests the {@link Marshaller} property
+ * {@link Marshaller#JAXB_SCHEMA_LOCATION}.</p>
+ */
+ public void testSchemaLocation() throws Exception {
+ String s = getAllTypesElementString(true, false);
+ JAXBContext context = getFactory();
+ AllTypesElement e = (AllTypesElement) context.createUnmarshaller().unmarshal(new InputSource(new StringReader(s)));
+ StringWriter sw = new StringWriter();
+ Marshaller marshaller = context.createMarshaller();
+ marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "foo.xsd");
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
+ marshaller.setProperty(Marshaller.JAXB_ENCODING, "ASCII");
+ marshaller.marshal(e, sw);
+ String got = sw.toString();
+ String s1 = "xsi:" + XMLConstants.XML_SCHEMA_NS_ATTR + "='foo.xsd'";
+ String s2 = "xsi:" + XMLConstants.XML_SCHEMA_NS_ATTR + "=\"foo.xsd\"";
+ int offset1 = s.indexOf(s1);
+ int offset2 = s.indexOf(s2);
+ int offset3 = s.indexOf('>');
+ assertTrue((offset1 > 0 && offset1 < offset3)
+ || (offset2 > 0 && offset2 < offset3));
+
+ // When validating the result, we are unsure about the
+ // order of the attributes in the outermost element.
+ RootElementHandler reh1 = getRootElementInfo(s);
+ RootElementHandler reh2 = getRootElementInfo(got);
+ assertEquals(reh1.rootElementURI, reh2.rootElementURI);
+ assertEquals(reh1.rootElementLocalName, reh2.rootElementLocalName);
+ assertEquals(reh1.rootElementAttributes, reh2.rootElementAttributes);
+ assertEquals(getContents(s), getContents(got));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org