axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gdani...@apache.org
Subject cvs commit: xml-axis/java/src/org/apache/axis/utils DOM2Writer.java XMLUtils.java
Date Mon, 10 Sep 2001 19:50:42 GMT
gdaniels    01/09/10 12:50:42

  Modified:    java/src/org/apache/axis/utils DOM2Writer.java XMLUtils.java
  Log:
  Add option for prettyprinting XML when outputting.
  
  Revision  Changes    Path
  1.4       +41 -5     xml-axis/java/src/org/apache/axis/utils/DOM2Writer.java
  
  Index: DOM2Writer.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/DOM2Writer.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DOM2Writer.java	2001/07/12 15:04:54	1.3
  +++ DOM2Writer.java	2001/09/10 19:50:42	1.4
  @@ -99,18 +99,33 @@
       /**
        * Serialize this node into the writer as XML.
        */
  -    public static void serializeAsXML(Node node, Writer writer, 
  +    public static void serializeAsXML(Node node, Writer writer,
                                         boolean omitXMLDecl)
       {
           PrintWriter out = new PrintWriter(writer);
           if (!omitXMLDecl)
               out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  -        print(node, null, out);
  +        print(node, null, out, false, 0);
           out.flush();
       }
   
  +    /**
  +     * Serialize this node into the writer as XML.
  +     */
  +    public static void serializeAsXML(Node node, Writer writer,
  +                                      boolean omitXMLDecl,
  +                                      boolean pretty)
  +    {
  +        PrintWriter out = new PrintWriter(writer);
  +        if (!omitXMLDecl)
  +            out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  +        print(node, null, out, pretty, 0);
  +        out.flush();
  +    }
  +
       private static void print(Node node, NSStack namespaceStack,
  -                              PrintWriter out)
  +                              PrintWriter out, boolean pretty,
  +                              int indent)
       {
           if (node == null)
           {
  @@ -132,7 +147,8 @@
   
                       for (int i = 0; i < numChildren; i++)
                       {
  -                        print(children.item(i), namespaceStack, out);
  +                        print(children.item(i), namespaceStack, out,
  +                              pretty, indent);
                       }
                   }
                   break;
  @@ -142,6 +158,11 @@
               {
                   namespaceStack = new NSStack(namespaceStack);
   
  +                if (pretty) {
  +                    for (int i = 0; i < indent; i++)
  +                        out.print(' ');
  +                }
  +
                   out.print('<' + node.getNodeName());
   
                   String elPrefix = node.getPrefix();
  @@ -218,11 +239,14 @@
                       if (hasChildren)
                       {
                           out.print('>');
  +                        if (pretty)
  +                            out.print(lineSeparator);
                       }
   
                       for (int i = 0; i < numChildren; i++)
                       {
  -                        print(children.item(i), namespaceStack, out);
  +                        print(children.item(i), namespaceStack, out, pretty,
  +                              indent + 1);
                       }
                   }
                   else
  @@ -233,6 +257,8 @@
                   if (!hasChildren)
                   {
                       out.print("/>");
  +                    if (pretty)
  +                        out.print(lineSeparator);
                   }
                   break;
               }
  @@ -264,6 +290,8 @@
                   out.print("<!--");
                   out.print(node.getNodeValue());
                   out.print("-->");
  +                if (pretty)
  +                    out.print(lineSeparator);
                   break;
               }
   
  @@ -281,15 +309,23 @@
                   }
   
                   out.println("?>");
  +                if (pretty)
  +                    out.print(lineSeparator);
                   break;
               }
           }
   
           if (type == Node.ELEMENT_NODE && hasChildren == true)
           {
  +            if (pretty) {
  +                for (int i = 0; i < indent; i++)
  +                    out.print(' ');
  +            }
               out.print("</");
               out.print(node.getNodeName());
               out.print('>');
  +            if (pretty)
  +                out.print(lineSeparator);
               hasChildren = false;
           }
       }
  
  
  
  1.17      +12 -6     xml-axis/java/src/org/apache/axis/utils/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/XMLUtils.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- XMLUtils.java	2001/08/29 14:17:17	1.16
  +++ XMLUtils.java	2001/09/10 19:50:42	1.17
  @@ -216,21 +216,27 @@
       }
   
       public static void privateElementToWriter(Element element, Writer writer,
  -                                              boolean omitXMLDecl) {
  -        DOM2Writer.serializeAsXML(element, writer, omitXMLDecl);
  +                                              boolean omitXMLDecl,
  +                                              boolean pretty) {
  +        DOM2Writer.serializeAsXML(element, writer, omitXMLDecl, pretty);
       }
       
       public static void ElementToWriter(Element element, Writer writer) {
  -        privateElementToWriter(element, writer, true);
  +        privateElementToWriter(element, writer, true, false);
       }
       
       public static void DocumentToStream(Document doc, OutputStream out) {
           Writer writer = new OutputStreamWriter(out);
  -        privateElementToWriter(doc.getDocumentElement(), writer, false);
  +        privateElementToWriter(doc.getDocumentElement(), writer, false, false);
       }
  -    
  +
  +    public static void PrettyDocumentToStream(Document doc, OutputStream out) {
  +        Writer writer = new OutputStreamWriter(out);
  +        privateElementToWriter(doc.getDocumentElement(), writer, false, true);
  +    }
  +
       public static void DocumentToWriter(Document doc, Writer writer) {
  -        privateElementToWriter(doc.getDocumentElement(), writer, false);
  +        privateElementToWriter(doc.getDocumentElement(), writer, false, false);
       }
   
       public static String getInnerXMLString(Element element) {
  
  
  

Mime
View raw message