ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject cvs commit: ant/src/main/org/apache/tools/ant/util DOMElementWriter.java
Date Tue, 24 May 2005 17:54:47 GMT
mbenson     2005/05/24 10:54:47

  Modified:    src/testcases/org/apache/tools/ant/taskdefs/optional
                        EchoPropertiesTest.java
               src/main/org/apache/tools/ant/util DOMElementWriter.java
  Log:
  go ahead and close elements with no children
  
  Revision  Changes    Path
  1.12      +1 -1      ant/src/testcases/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
  
  Index: EchoPropertiesTest.java
  ===================================================================
  RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- EchoPropertiesTest.java	11 Mar 2004 23:40:59 -0000	1.11
  +++ EchoPropertiesTest.java	24 May 2005 17:54:47 -0000	1.12
  @@ -114,7 +114,7 @@
               BufferedReader br = new BufferedReader( fr );
               String read = null;
               while ( (read = br.readLine()) != null) {
  -                if (read.indexOf("<property name=\"test.property\" value=\""+TEST_VALUE+"\"></property>")
>= 0) {
  +                if (read.indexOf("<property name=\"test.property\" value=\""+TEST_VALUE+"\"
/>") >= 0) {
                       // found the property we set - it's good.
                       return;
                   }
  
  
  
  1.28      +77 -52    ant/src/main/org/apache/tools/ant/util/DOMElementWriter.java
  
  Index: DOMElementWriter.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/util/DOMElementWriter.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- DOMElementWriter.java	14 Mar 2005 20:08:32 -0000	1.27
  +++ DOMElementWriter.java	24 May 2005 17:54:47 -0000	1.28
  @@ -77,67 +77,67 @@
                         String indentWith)
           throws IOException {
   
  -        openElement(element, out, indent, indentWith);
  -
           // Write child elements and text
  -        boolean hasChildren = false;
           NodeList children = element.getChildNodes();
  -        for (int i = 0; i < children.getLength(); i++) {
  -            Node child = children.item(i);
  -
  -            switch (child.getNodeType()) {
  -
  -            case Node.ELEMENT_NODE:
  -                if (!hasChildren) {
  -                    out.write(lSep);
  -                    hasChildren = true;
  -                }
  -                write((Element) child, out, indent + 1, indentWith);
  -                break;
  -
  -            case Node.TEXT_NODE:
  -                out.write(encode(child.getNodeValue()));
  -                break;
  -
  -            case Node.COMMENT_NODE:
  -                out.write("<!--");
  -                out.write(encode(child.getNodeValue()));
  -                out.write("-->");
  -                break;
  +        boolean hasChildren = (children.getLength() > 0);
  +        openElement(element, out, indent, indentWith, hasChildren);
   
  -            case Node.CDATA_SECTION_NODE:
  -                out.write("<![CDATA[");
  -                out.write(encodedata(((Text) child).getData()));
  -                out.write("]]>");
  -                break;
  -
  -            case Node.ENTITY_REFERENCE_NODE:
  -                out.write('&');
  -                out.write(child.getNodeName());
  -                out.write(';');
  -                break;
  -
  -            case Node.PROCESSING_INSTRUCTION_NODE:
  -                out.write("<?");
  -                out.write(child.getNodeName());
  -                String data = child.getNodeValue();
  -                if (data != null && data.length() > 0) {
  -                    out.write(' ');
  -                    out.write(data);
  +        if (hasChildren) {
  +            for (int i = 0; i < children.getLength(); i++) {
  +                Node child = children.item(i);
  +    
  +                switch (child.getNodeType()) {
  +    
  +                case Node.ELEMENT_NODE:
  +                    if (i == 0) {
  +                        out.write(lSep);
  +                    }
  +                    write((Element) child, out, indent + 1, indentWith);
  +                    break;
  +    
  +                case Node.TEXT_NODE:
  +                    out.write(encode(child.getNodeValue()));
  +                    break;
  +    
  +                case Node.COMMENT_NODE:
  +                    out.write("<!--");
  +                    out.write(encode(child.getNodeValue()));
  +                    out.write("-->");
  +                    break;
  +    
  +                case Node.CDATA_SECTION_NODE:
  +                    out.write("<![CDATA[");
  +                    out.write(encodedata(((Text) child).getData()));
  +                    out.write("]]>");
  +                    break;
  +    
  +                case Node.ENTITY_REFERENCE_NODE:
  +                    out.write('&');
  +                    out.write(child.getNodeName());
  +                    out.write(';');
  +                    break;
  +    
  +                case Node.PROCESSING_INSTRUCTION_NODE:
  +                    out.write("<?");
  +                    out.write(child.getNodeName());
  +                    String data = child.getNodeValue();
  +                    if (data != null && data.length() > 0) {
  +                        out.write(' ');
  +                        out.write(data);
  +                    }
  +                    out.write("?>");
  +                    break;
  +                default:
  +                    // Do nothing
                   }
  -                out.write("?>");
  -                break;
  -            default:
  -                // Do nothing
               }
  +            closeElement(element, out, indent, indentWith, true);
           }
  -
  -        closeElement(element, out, indent, indentWith, hasChildren);
       }
   
       /**
        * Writes the opening tag - including all attributes -
  -     * correspondong to a DOM element.
  +     * corresponding to a DOM element.
        *
        * @param element the DOM element to write
        * @param out where to send the output
  @@ -149,6 +149,25 @@
       public void openElement(Element element, Writer out, int indent,
                               String indentWith)
           throws IOException {
  +        openElement(element, out, indent, indentWith, true);
  +    }
  +
  +    /**
  +     * Writes the opening tag - including all attributes -
  +     * corresponding to a DOM element.
  +     *
  +     * @param element the DOM element to write
  +     * @param out where to send the output
  +     * @param indent number of
  +     * @param indentWith string that should be used to indent the
  +     * corresponding tag.
  +     * @param hasChildren whether this element has children.
  +     * @throws IOException if an error happens while writing to the stream.
  +     * @since Ant 1.7
  +     */
  +    public void openElement(Element element, Writer out, int indent,
  +                            String indentWith, boolean hasChildren)
  +        throws IOException {
           // Write indent characters
           for (int i = 0; i < indent; i++) {
               out.write(indentWith);
  @@ -168,7 +187,13 @@
               out.write(encode(attr.getValue()));
               out.write("\"");
           }
  -        out.write(">");
  +        if (hasChildren) {
  +            out.write(">");
  +        } else {
  +            out.write(" />");
  +            out.write(lSep);
  +            out.flush();
  +        }
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message