Coverage Report - net.sf.practicalxml.builder.XmlBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlBuilder
87%
7/8
N/A
1
 
 1  
 // Copyright 2008-2014 severally by the contributors
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package net.sf.practicalxml.builder;
 16  
 
 17  
 
 18  
 /**
 19  
  *  A tool for building XML, avoiding the mistakes of simple text output and
 20  
  *  the hassle of the DOM API. Primarily intended for building small snippets
 21  
  *  of XML for unit tests, but usable for all types of output.
 22  
  *  <p>
 23  
  *  This class takes a declarative approach to creating XML, using static
 24  
  *  factory methods to create {@link Node} objects. These are a lightweight
 25  
  *  representation of the DOM <code>Node</code>, focused on creation rather
 26  
  *  than manipulation. You can also append children to nodes imperatively,
 27  
  *  using the methods in this class to create the node objects.
 28  
  *  <p>
 29  
  *  This is best explained by example. To create the following XML:
 30  
  *  <pre>
 31  
  *  </pre>
 32  
  *  You would have a program that looks like this:
 33  
  *  <pre>
 34  
  *  </pre>
 35  
  */
 36  0
 public class XmlBuilder
 37  
 {
 38  
     /**
 39  
      *  Creates an element, with optional namespace.
 40  
      *
 41  
      *  @param  nsUri       The namespace URI; ignored if null.
 42  
      *  @param  qname       Qualified name of the element.
 43  
      *  @param  children    Any children to be added to the element.
 44  
      */
 45  
     public static ElementNode element(String nsUri, String qname, Node... children)
 46  
     {
 47  2203
         return new ElementNode(nsUri, qname, children);
 48  
     }
 49  
 
 50  
 
 51  
     /**
 52  
      *  Creates an element that does not have a namespace.
 53  
      *
 54  
      *  @param  name        Name of the element.
 55  
      *  @param  children    Any children to be added to the element.
 56  
      */
 57  
     public static ElementNode element(String name, Node... children)
 58  
     {
 59  2197
         return element(null, name, children);
 60  
     }
 61  
 
 62  
 
 63  
     /**
 64  
      *  Creates a text node.
 65  
      */
 66  
     public static Node text(String content)
 67  
     {
 68  1626
         return new TextNode(content);
 69  
     }
 70  
 
 71  
 
 72  
     /**
 73  
      *  Creates a namespaced attribute.
 74  
      *
 75  
      *  @param  nsUri       The namespace URI; ignored if null.
 76  
      *  @param  qname       Qualified name of the attribute.
 77  
      *  @param  value       Value of the attribute.
 78  
      */
 79  
     public static Node attribute(String nsUri, String qname, String value)
 80  
     {
 81  876
         return new AttributeNode(nsUri, qname, value);
 82  
     }
 83  
 
 84  
 
 85  
     /**
 86  
      *  Creates an attribute without namespace.
 87  
      *
 88  
      *  @param  name        Name of the attribute.
 89  
      *  @param  value       Value of the attribute.
 90  
      */
 91  
     public static Node attribute(String name, String value)
 92  
     {
 93  16
         return new AttributeNode(null, name, value);
 94  
     }
 95  
 
 96  
 
 97  
     /**
 98  
      *  Creates a comment node.
 99  
      *  <p>
 100  
      *  <em>Warning:</em>
 101  
      *  Comment nodes are only reported to SAX content handlers that also
 102  
      *  implement <code>org.xml.sax.ext.LexicalHandler</code>.
 103  
      */
 104  
     public static Node comment(String text)
 105  
     {
 106  1
         return new CommentNode(text);
 107  
     }
 108  
 
 109  
 
 110  
     /**
 111  
      *  Creates a processing instruction node.
 112  
      *
 113  
      *  @since 1.0.2
 114  
      */
 115  
     public static Node processingInstruction(String target, String data)
 116  
     {
 117  1
         return new PINode(target, data);
 118  
     }
 119  
 }