|
Executing examples/misc/AddressPrinter under the current JaxMe2 (CVS HEAD) after resolving addAttributes problems results in
Loaded address Doe, Jane. Details: <p:Address xmlns:p="http://jaxme.sf.net/examples/misc/address"> <p:Name> <p:First>Jane</p:First> <p:Middle>Lee</p:Middle> <p:Middle>Chris</p:Middle> <p:Last>Doe</p:Last> <p:Initials>(JD)</p:Initials> </p:Name> <p:Postal> <p:Street>34 Main Street</p:Street> <p:ZIP>02215</p:ZIP> <p:City>Boston</p:City> <p:State>MA</p:State> </p:Postal> <p:PhoneDetails> <p:Phone p:type="Work"> <p:PhoneNumber>555.5789</p:PhoneNumber> </p:Phone> <p:Phone p:type="Fax"> <p:PhoneNumber>555.1212</p:PhoneNumber> </p:Phone> </p:PhoneDetails> <p:EmailDetails> <p:Email p:type="Private"> <p:EmailAddress>[EMAIL PROTECTED]</p:EmailAddress> </p:Email> <p:Email p:type="Office"> <p:EmailAddress>[EMAIL PROTECTED]</p:EmailAddress> </p:Email> </p:EmailDetails> </p:Address>
Actually the input XML document, Address.xml is slightly different from the above output.
<?xml version="1.0"?> <!-- A sample document for the Address.xsd schema. This sample document is used in the docs, see docs/GenerateJava.html. --> <Address xmlns="http://jaxme.sf.net/examples/misc/address"> <Name> <First>Jane</First> <Middle>Lee</Middle> <Middle>Chris</Middle> <Last>Doe</Last> <Initials>(JD)</Initials> </Name> <Postal> <Street>34 Main Street</Street> <ZIP>02215</ZIP> <City>Boston</City> <State>MA</State> </Postal> <PhoneDetails> <Phone type="Work"> <PhoneNumber>555.5789</PhoneNumber> </Phone> <Phone type="Fax"> <PhoneNumber>555.1212</PhoneNumber> </Phone> </PhoneDetails> <EmailDetails> <Email type="Private"> <EmailAddress>[EMAIL PROTECTED]</EmailAddress> </Email> <Email type="Office"> <EmailAddress>[EMAIL PROTECTED]</EmailAddress> </Email> </EmailDetails> </Address>
What brought this difference is the logic of adding a new prefix in JMXmlSerializerImpl.getQName. The way the current JMXmlSerializerImpl works is the same as JAXB in Sun’s JWSDP 1.1.
protected String getQName(JMXmlSerializer.Data pData, String pURI, String pLocalName) throws SAXException { NamespaceSupport nss = pData.getNamespaceSupport(); String prefix = nss.getPrefix(pURI); if (prefix == null) { prefix = pData.getNewPrefix(pURI); pData.getContentHandler().startPrefixMapping(prefix, pURI); } return prefix + ":" + pLocalName; }
On the other hand, JWSDP 1.2 provides a default XML serializer which doesn’t add a prefix where a default namespacing is sufficient and proper. For example, modifying the above method into the following one
protected String getQName(JMXmlSerializer.Data pData, String pURI, String pLocalName) throws SAXException { NamespaceSupport nss = pData.getNamespaceSupport(); String prefix = nss.getPrefix(pURI); String p = getPreferredPrefix(pURI); if (prefix == null) { if (p == null) { p = ""; if (!nss.isPrefixDeclared(p)) { nss.declarePrefix(p, pURI); pData.getContentHandler().startPrefixMapping(p, pURI); }
} return pLocalName; } if (prefix.equals("")) { return pLocalName; } return prefix + ":" + pLocalName; }
shows
Loaded address Doe, Jane. Details: <Address xmlns="http://jaxme.sf.net/examples/misc/address"> <Name> <First>Jane</First> <Middle>Lee</Middle> <Middle>Chris</Middle> <Last>Doe</Last> <Initials>(JD)</Initials> </Name> <Postal> <Street>34 Main Street</Street> <ZIP>02215</ZIP> <City>Boston</City> <State>MA</State> </Postal> <PhoneDetails> <Phone type="Work"> <PhoneNumber>555.5789</PhoneNumber> </Phone> <Phone type="Fax"> <PhoneNumber>555.1212</PhoneNumber> </Phone> </PhoneDetails> <EmailDetails> <Email type="Private"> <EmailAddress>[EMAIL PROTECTED]</EmailAddress> </Email> <Email type="Office"> <EmailAddress>[EMAIL PROTECTED]</EmailAddress> </Email> </EmailDetails> </Address>
The attached file is the patch for the serializer.
Thanks,
Ias.
|
Index: JMXmlSerializerImpl.java
===================================================================
RCS file: /cvsroot/jaxme/JaxMe2/src/net/sf/jaxme/impl/JMXmlSerializerImpl.java,v
retrieving revision 1.1
diff -u -r1.1 JMXmlSerializerImpl.java
--- JMXmlSerializerImpl.java 4 Dec 2002 17:11:51 -0000 1.1
+++ JMXmlSerializerImpl.java 1 Aug 2003 06:20:53 -0000
@@ -70,10 +70,25 @@
String pLocalName) throws SAXException {
NamespaceSupport nss = pData.getNamespaceSupport();
String prefix = nss.getPrefix(pURI);
- if (prefix == null) {
- prefix = pData.getNewPrefix(pURI);
- pData.getContentHandler().startPrefixMapping(prefix, pURI);
- }
+ String p = getPreferredPrefix(pURI);
+ if (prefix == null)
+ {
+ if (p == null)
+ {
+ p = "";
+ if (!nss.isPrefixDeclared(p))
+ {
+ nss.declarePrefix(p, pURI);
+ pData.getContentHandler().startPrefixMapping(p, pURI);
+ }
+
+ }
+ return pLocalName;
+ }
+ if (prefix.equals(""))
+ {
+ return pLocalName;
+ }
return prefix + ":" + pLocalName;
}
