[jdom-interest] [PATCH] DOMOutputter and namespace aware Documents

Jason Hunter jhunter at xquery.com
Fri May 26 09:27:59 PDT 2006


Looks smart.  Any objections?

-jh-

Ortwin Glück wrote:
> I propose the attached change to DOMOutputter.
> 
> This seems to work. Test case:
> 
> package test;
> 
> import org.jdom.Document;
> import org.jdom.Element;
> import org.jdom.output.DOMOutputter;
> 
> public class JDomTest extends TestCase {
>      private Document doc;
> 
>      public JDomTest() {
>          doc = new Document();
>          Element root = new Element("root");
>          Element el = new Element("a");
>          el.setText("abc");
>          root.addContent(el);
>          doc.setRootElement(root);
>      }
> 
>      public void testNsSupport() throws Exception {
>          DOMOutputter out = new DOMOutputter();
>          out.setForceNamespaceAware(true);
>          org.w3c.dom.Document dom = out.output(doc);
>          org.w3c.dom.Element el = dom.getDocumentElement();
>          System.out.println("Dom impl: "+ el.getClass().getName());
>          assertNotNull(el.getLocalName());
>      }
> }
> 
> 
> ------------------------------------------------------------------------
> 
> Index: org/jdom/output/DOMOutputter.java
> ===================================================================
> RCS file: /home/cvspublic/jdom/src/java/org/jdom/output/DOMOutputter.java,v
> retrieving revision 1.41
> diff -u -r1.41 DOMOutputter.java
> --- org/jdom/output/DOMOutputter.java	3 Sep 2004 06:03:42 -0000	1.41
> +++ org/jdom/output/DOMOutputter.java	23 May 2006 09:29:30 -0000
> @@ -86,6 +86,8 @@
>  
>      /** Adapter to use for interfacing with the DOM implementation */
>      private String adapterClass;
> +    
> +    private boolean forceNamespaceAware;
>  
>      /**
>       * This creates a new DOMOutputter which will attempt to first locate
> @@ -106,6 +108,15 @@
>      public DOMOutputter(String adapterClass) {
>          this.adapterClass = adapterClass;
>      }
> +    
> +    /**
> +     * Controls how NO_NAMESPACE nodes are handeled. If true the outputter always creates
> +     * namespace a aware DOM.
> +     * @param flag
> +     */
> +    public void setForceNamespaceAware(boolean flag) {
> +        this.forceNamespaceAware = flag;
> +    }
>  
>  
>      /**
> @@ -250,7 +261,9 @@
>              org.w3c.dom.Element domElement = null;
>              if (element.getNamespace() == Namespace.NO_NAMESPACE) {
>                  // No namespace, use createElement
> -                domElement = domDoc.createElement(element.getQualifiedName());
> +                domElement = forceNamespaceAware ?
> +                             domDoc.createElementNS(null, element.getQualifiedName())
> +                             : domDoc.createElement(element.getQualifiedName());
>              }
>              else {
>                  domElement = domDoc.createElementNS(
> @@ -308,8 +321,14 @@
>                  // Crimson doesn't like setAttributeNS() for non-NS attribs
>                  if (attribute.getNamespace() == Namespace.NO_NAMESPACE) {
>                      // No namespace, use setAttribute
> -                    domElement.setAttribute(attribute.getQualifiedName(),
> -                                            attribute.getValue());
> +                    if (forceNamespaceAware) {
> +                        domElement.setAttributeNS(null,
> +                                                  attribute.getQualifiedName(),
> +                                                  attribute.getValue());
> +                    } else {
> +                        domElement.setAttribute(attribute.getQualifiedName(),
> +                                                attribute.getValue());
> +                    }
>                  }
>                  else {
>                      domElement.setAttributeNS(attribute.getNamespaceURI(),
> @@ -392,7 +411,11 @@
>           try {
>               if (attribute.getNamespace() == Namespace.NO_NAMESPACE) {
>                   // No namespace, use createAttribute
> -                 domAttr = domDoc.createAttribute(attribute.getQualifiedName());
> +                 if (forceNamespaceAware) {
> +                     domAttr = domDoc.createAttributeNS(null, attribute.getQualifiedName());
> +                 } else {
> +                     domAttr = domDoc.createAttribute(attribute.getQualifiedName());
> +                 }
>               }
>               else {
>                   domAttr = domDoc.createAttributeNS(attribute.getNamespaceURI(),
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com


More information about the jdom-interest mailing list