Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

Jason Hunter

2006-05-26

Replies:

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
> @@(protected) @@
>
>    /** 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
> @@(protected) @@
>    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;
> +   }
>
>
>    /**
> @@(protected) @@
>         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(
> @@(protected) @@
>            // 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(),
> @@(protected) @@
>       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@(protected)
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.