From: <th...@us...> - 2004-01-06 10:09:16
|
Update of /cvsroot/jaxme/JaxMe/src/runtime/de/ispsoft/jaxme In directory sc8-pr-cvs1:/tmp/cvs-serv18280/src/runtime/de/ispsoft/jaxme Modified Files: JMAnyElement.java Log Message: added toXMLNoNS method to write XML without namespaces Index: JMAnyElement.java =================================================================== RCS file: /cvsroot/jaxme/JaxMe/src/runtime/de/ispsoft/jaxme/JMAnyElement.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- JMAnyElement.java 8 Oct 2003 10:57:01 -0000 1.9 +++ JMAnyElement.java 6 Jan 2004 10:09:11 -0000 1.10 @@ -1,13 +1,24 @@ package de.ispsoft.jaxme; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Iterator; import java.util.List; import de.ispsoft.jaxme.jaxb.MarshallerImpl; import de.ispsoft.jaxme.jaxb.UnmarshallerImpl; +import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; +import org.xml.sax.Locator; import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; /** <p>An implementation for an abstract element. This is @@ -16,7 +27,8 @@ * @author <a href="mailto:jo...@is...">Jochen Wiedmann</a> */ public class JMAnyElement implements java.io.Serializable, Cloneable, JMNode { - protected static final org.xml.sax.Attributes emptyAttributes = new org.xml.sax.helpers.AttributesImpl(); + + protected static final Attributes emptyAttributes = new AttributesImpl(); private static MarshallerImpl invisibleMarshaller = new MarshallerImpl(); private static UnmarshallerImpl invisibleUnmarshaller = new UnmarshallerImpl(); @@ -25,8 +37,7 @@ protected String namespaceURI; List attributes; List childs; - private static java.text.DateFormat defaultDateFormat = - new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); + private static DateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z"); /** Creates a new JMAnyElement with the given namespace URI * and local name. @@ -82,8 +93,7 @@ * actual values */ public void setAttribute(String pNamespaceURI, String pLocalName, String pValue, - UnmarshallerImpl pUnmarshaller) - throws SAXException { + UnmarshallerImpl pUnmarshaller) throws SAXException { if (attributes != null) { int s = attributes.size(); for (int i = 0; i < s; i++) { @@ -152,12 +162,15 @@ public Object getElement(String pNamespaceURI, String pLocalName) { String uri = (pNamespaceURI == null) ? "" : pNamespaceURI; List myChilds = getChilds(); - for (java.util.Iterator iter = myChilds.iterator(); iter.hasNext();) { - Object o = iter.next(); + Object o = null; + JMAnyElement elem = null; + String elemUri = null; + for (Iterator iter = myChilds.iterator(); iter.hasNext();) { + o = iter.next(); if (o instanceof JMAnyElement) { - JMAnyElement elem = (JMAnyElement) o; + elem = (JMAnyElement) o; if (pLocalName.equals(elem.getLocalName())) { - String elemUri = elem.getNamespaceURI(); + elemUri = elem.getNamespaceURI(); if (elemUri == null) { elemUri = ""; } @@ -181,7 +194,7 @@ */ public void addChild(Object pChild) { if (childs == null) { - childs = new java.util.ArrayList(); + childs = new ArrayList(); } childs.add(pChild); } @@ -201,7 +214,7 @@ * If you wish to define your own prefixes, use the two * arguments form of this method.</p> */ - public void toSAX(org.xml.sax.ContentHandler contentHandler) + public void toSAX(ContentHandler contentHandler) throws SAXException { toSAX(contentHandler, null); } @@ -219,9 +232,8 @@ * for prefixes; may be null, in which case default mappings * are established. */ - public void toSAX(org.xml.sax.ContentHandler pContentHandler, - NamespaceSupport pNamespaceSupport) - throws SAXException { + public void toSAX(ContentHandler pContentHandler, NamespaceSupport pNamespaceSupport) + throws SAXException { toSAX(pContentHandler, pNamespaceSupport, false); } @@ -240,10 +252,8 @@ * @param pNamespaceInitialized True, if <code>initNamespaceSupport()</code> * has been called. */ - public void toSAX(org.xml.sax.ContentHandler pContentHandler, - NamespaceSupport pNamespaceSupport, - boolean pNamespaceInitialized) - throws SAXException { + public void toSAX(ContentHandler pContentHandler, NamespaceSupport pNamespaceSupport, + boolean pNamespaceInitialized) throws SAXException { if (pNamespaceSupport == null) { pNamespaceSupport = new NamespaceSupport(); pNamespaceSupport.setMarshaller(invisibleMarshaller); @@ -273,11 +283,9 @@ /** <p>Returns the elements attribute list suitable for a * SAX ContentHandler.</p> */ - public org.xml.sax.helpers.AttributesImpl - getAttributes(org.xml.sax.ContentHandler pContentHandler, - NamespaceSupport pNamespaceSupport) - throws org.xml.sax.SAXException { - org.xml.sax.helpers.AttributesImpl attr = new org.xml.sax.helpers.AttributesImpl(); + public AttributesImpl getAttributes(ContentHandler pContentHandler, + NamespaceSupport pNamespaceSupport) throws SAXException { + AttributesImpl attr = new AttributesImpl(); if (attributes != null) { int s = attributes.size(); for (int i = 0; i < s; i++) { @@ -318,12 +326,12 @@ * URL, then a default mapping is established.</p> */ protected void childsToSAX(ContentHandler pContentHandler, - NamespaceSupport pNamespaceSupport) - throws SAXException { + NamespaceSupport pNamespaceSupport) throws SAXException { if (childs != null) { int s = childs.size(); + Object o = null; for (int i = 0; i < s; i++) { - Object o = childs.get(i); + o = childs.get(i); if (o instanceof String) { char[] chars = o.toString().toCharArray(); pContentHandler.characters(chars, 0, chars.length); @@ -349,16 +357,17 @@ JMAnyElement jmae = (JMAnyElement) super.clone(); if (attributes != null) { int s = attributes.size(); - jmae.attributes = new java.util.ArrayList(s); + jmae.attributes = new ArrayList(s); for (int i = 0; i < s; i++) { jmae.attributes.add(((JMAttribute) attributes.get(i)).clone()); } } if (childs != null) { int s = childs.size(); - jmae.childs = new java.util.ArrayList(s); + jmae.childs = new ArrayList(s); + Object o = null; for (int i = 0; i < s; i++) { - Object o = childs.get(i); + o = childs.get(i); if (o instanceof String) { jmae.childs.add(o.toString()); } else { @@ -382,22 +391,21 @@ * If you want to use your own mappings, create a NamespaceSupport * and use the two arguments version of this method.</p> */ - public void toWriter(java.io.Writer pWriter) - throws org.xml.sax.SAXException, java.io.IOException { + public void toWriter(Writer pWriter) throws SAXException, IOException { toWriter(pWriter, null); } /** <p>Serializes the element into the given writer. The given * NamespaceSupport is used to create prefix mappings.</p> */ - public void toWriter(java.io.Writer pWriter, NamespaceSupport pNamespaceSupport) - throws org.xml.sax.SAXException, java.io.IOException { + public void toWriter(Writer pWriter, NamespaceSupport pNamespaceSupport) + throws SAXException, IOException { try { - toSAX(new de.ispsoft.jaxme.XMLSerializer(pWriter), pNamespaceSupport, false); - } catch (org.xml.sax.SAXException e) { + toSAX(new XMLSerializer(pWriter), pNamespaceSupport, false); + } catch (SAXException e) { Exception f = e.getException(); - if (f != null && f instanceof java.io.IOException) { - throw (java.io.IOException) f; + if (f != null && f instanceof IOException) { + throw (IOException) f; } throw e; } @@ -420,16 +428,46 @@ */ public String toXML(NamespaceSupport pNamespaceSupport) { try { - java.io.StringWriter sw = new java.io.StringWriter(); + StringWriter sw = new StringWriter(); toWriter(sw, pNamespaceSupport); return sw.toString(); - } catch (org.xml.sax.SAXException e) { + } catch (SAXException e) { throw new NullPointerException("Unexpected SAX exception: " + e.getMessage()); - } catch (java.io.IOException e) { + } catch (IOException e) { throw new NullPointerException("Unexpected IO exception: " + e.getMessage()); } } + /** <p>Serializes the element <b>WITHOUT NAMESPACES</b> into the given writer.</p> + */ + public String toXMLNoNS() { + try { + StringWriter sw = new StringWriter(); + toWriterNoNS(sw, null); + return sw.toString(); + } catch (SAXException e) { + throw new NullPointerException("Unexpected SAX exception: " + e.getMessage()); + } catch (IOException e) { + throw new NullPointerException("Unexpected IO exception: " + e.getMessage()); + } + } + + /** <p>Serializes the element <b>WITHOUT NAMESPACES</b> into the given writer. + * Therefore the XMLSerializerNoNS is used.</p> + */ + public void toWriterNoNS(Writer pWriter, NamespaceSupport pNamespaceSupport) + throws SAXException, IOException { + try { + toSAX(new XMLSerializerNoNS(pWriter), pNamespaceSupport, false); + } catch (SAXException e) { + Exception f = e.getException(); + if (f != null && f instanceof IOException) { + throw (IOException) f; + } + throw e; + } + } + /** <p>Validates this elements contents. Doesn't work recursively.</p> * * @param pContext A possible context to use in error messages or null @@ -437,7 +475,7 @@ * @throws ValidationException The element is invalid; details are * specified in the error message */ - protected void validateMe(String pContext, org.xml.sax.Locator pLocator) + protected void validateMe(String pContext, Locator pLocator) throws ValidationException { } @@ -465,12 +503,12 @@ * its property jaxme.format.datetime to achieve the same * functionality. */ - public static java.util.Date parseDate(String pDate) { + public static Date parseDate(String pDate) { try { synchronized (defaultDateFormat) { return defaultDateFormat.parse(pDate); } - } catch (java.text.ParseException e) { + } catch (ParseException e) { throw new IllegalArgumentException("Invalid date string: " + pDate); } } @@ -479,7 +517,7 @@ * its property jaxme.format.datetime to achieve the same * functionality. */ - public static String formatDate(java.util.Date pDate) { + public static String formatDate(Date pDate) { synchronized (defaultDateFormat) { return defaultDateFormat.format(pDate); } |