Coverage Report - yarfraw.utils.XMLUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLUtils
28% 
47% 
0
 
 1  
 package yarfraw.utils;
 2  
 
 3  
 import java.io.ByteArrayOutputStream;
 4  
 import java.io.IOException;
 5  
 import java.io.InputStream;
 6  
 import java.io.StringReader;
 7  
 import java.util.ArrayList;
 8  
 import java.util.List;
 9  
 
 10  
 import javax.xml.namespace.QName;
 11  
 import javax.xml.parsers.DocumentBuilderFactory;
 12  
 import javax.xml.parsers.ParserConfigurationException;
 13  
 import javax.xml.transform.Result;
 14  
 import javax.xml.transform.Source;
 15  
 import javax.xml.transform.Transformer;
 16  
 import javax.xml.transform.TransformerConfigurationException;
 17  
 import javax.xml.transform.TransformerException;
 18  
 import javax.xml.transform.TransformerFactory;
 19  
 import javax.xml.transform.stream.StreamResult;
 20  
 import javax.xml.transform.stream.StreamSource;
 21  
 
 22  
 import org.apache.commons.collections.CollectionUtils;
 23  
 import org.apache.commons.lang.ObjectUtils;
 24  
 import org.apache.commons.lang.StringUtils;
 25  
 import org.w3c.dom.Document;
 26  
 import org.w3c.dom.Element;
 27  
 import org.w3c.dom.NamedNodeMap;
 28  
 import org.w3c.dom.Node;
 29  
 import org.w3c.dom.NodeList;
 30  
 import org.xml.sax.InputSource;
 31  
 import org.xml.sax.SAXException;
 32  
 
 33  
 import yarfraw.core.datamodel.YarfrawException;
 34  
 
 35  
 public class XMLUtils{
 36  0
   private XMLUtils(){}
 37  
   /**
 38  
    * Parses an xml string to a Document object.
 39  
    * 
 40  
    * @param xml - xml to be parsed
 41  
    * @param validating - see {@link DocumentBuilderFactory}
 42  
    * @param ignoringComments - see {@link DocumentBuilderFactory}
 43  
    * @return a W3 DOM object representation of the input string
 44  
    * @throws SAXException
 45  
    * @throws IOException
 46  
    * @throws ParserConfigurationException
 47  
    */
 48  
   public static Document parseXml(String xml, boolean validating, boolean ignoringComments) 
 49  
   throws SAXException, IOException, ParserConfigurationException{
 50  120
    return parseXml(new InputSource(new StringReader(xml)), validating, ignoringComments); 
 51  
   }
 52  
   
 53  
   /**
 54  
    * Parses an xml stream to a Document object.
 55  
    * 
 56  
    * @param stream - xml to be parsed
 57  
    * @param validating - see {@link DocumentBuilderFactory}
 58  
    * @param ignoringComments - see {@link DocumentBuilderFactory}
 59  
    * @return a W3 DOM object representation of the input string
 60  
    * @throws SAXException
 61  
    * @throws IOException
 62  
    * @throws ParserConfigurationException
 63  
    */
 64  
   public static Document parseXml(InputStream stream, boolean validating, boolean ignoringComments) 
 65  
   throws SAXException, IOException, ParserConfigurationException{
 66  0
    return parseXml(new InputSource(stream), validating, ignoringComments);
 67  
   }
 68  
   /**
 69  
    * Parses xml to a Document object.
 70  
    * 
 71  
    * @param source - xml source to be parsed
 72  
    * @param validating - see {@link DocumentBuilderFactory}
 73  
    * @param ignoringComments - see {@link DocumentBuilderFactory}
 74  
    * @return a W3 DOM object representation of the input string
 75  
    * @throws SAXException
 76  
    * @throws IOException
 77  
    * @throws ParserConfigurationException
 78  
    */
 79  
   public static Document parseXml(InputSource source, boolean validating, boolean ignoringComments)
 80  
   throws SAXException, IOException, ParserConfigurationException{
 81  120
     DocumentBuilderFactory factory = 
 82  
       DocumentBuilderFactory.newInstance();
 83  120
    factory.setValidating(validating);
 84  120
    factory.setIgnoringComments(ignoringComments);
 85  120
    factory.setNamespaceAware(true);
 86  120
    return factory.newDocumentBuilder().parse(source);
 87  
   }
 88  
   
 89  
   /**
 90  
    * Search through the input element list and return the first element that matches
 91  
    * both input the namespaceURI and the localName.
 92  
    * 
 93  
    * @param namespaceURI - namespaceURI of the element to be search for
 94  
    * @param localName - localName of the element
 95  
    * @return - null if no matching element is found,
 96  
    * the matching element otherwise.
 97  
    */
 98  
   public static Element getElementByNS(List<Element> elements, String namespaceURI, String localName){
 99  78
     if(CollectionUtils.isEmpty(elements)){
 100  0
       return null;
 101  
     }
 102  78
     for(Element e : elements){
 103  141
       if(ObjectUtils.equals(localName, e.getLocalName()) && 
 104  
               ObjectUtils.equals(namespaceURI, emptyIfNull(e.getNamespaceURI()))){
 105  78
         return e;
 106  
       }
 107  
     }    
 108  0
     return null;
 109  
   }
 110  
 
 111  
   /**
 112  
    * Search through the input element list and return the FIRST element that matches
 113  
    * the localName.
 114  
    * 
 115  
    * @param localName - localName of the element
 116  
    * @return - null if no matching element is found,
 117  
    * the matching element otherwise.
 118  
    */
 119  
   public static Element getElementByLocalName(List<Element> elements, String localName){
 120  12
     if(CollectionUtils.isEmpty(elements)){
 121  0
       return null;
 122  
     }
 123  12
     for(Element e : elements){
 124  12
       if(ObjectUtils.equals(localName, e.getLocalName())){
 125  12
         return e;
 126  
       }
 127  
     }    
 128  0
     return null;
 129  
   }
 130  
   
 131  
   public static boolean same(QName qn1, QName qn2){
 132  244056
     return ObjectUtils.equals(qn1, qn2);
 133  
   }
 134  
 
 135  
   
 136  
   /**
 137  
    * Return empty string if input is null.
 138  
    * @param str
 139  
    * @return
 140  
    */
 141  
   public static String emptyIfNull(String str){
 142  78
     return str == null?StringUtils.EMPTY:str;
 143  
   }
 144  
 
 145  
   
 146  
   /**
 147  
    * Get the attribute value of the attribute that matches the specified {@link QName}.
 148  
    * @param element - the element to be searched on.
 149  
    * @param name - 
 150  
    * @return - the value of the specified attribute, null if the attribute is not found.
 151  
    */
 152  
   public static String getAttributeValue(Node element, QName name){
 153  0
     return getAttributeValue(element, name.getLocalPart(), name.getNamespaceURI());
 154  
   }
 155  
   
 156  
   /**
 157  
    * Get the attribute value of the attribute that matches the specified localName.
 158  
    * @param element - the element to be searched on.
 159  
    * @param name - 
 160  
    * @return - the value of the specified attribute, null if the attribute is not found.
 161  
    */
 162  
   public static String getAttributeValue(Node element, String attributeLocalName){
 163  0
     return getAttributeValue(element, attributeLocalName, null);
 164  
   }
 165  
   /**
 166  
    * Get the attribute value of the attribute that matches the specified localName and NamespaceUri
 167  
    * @param element - the element to be searched on.
 168  
    * @param name - 
 169  
    * @return - the value of the specified attribute, null if the attribute is not found.
 170  
    */
 171  
   public static String getAttributeValue(Node element, String attributeLocalName, String namespaceUri){
 172  0
     NamedNodeMap attr = element.getAttributes();
 173  0
     if(attr == null){
 174  0
       return null;
 175  
     }
 176  0
     for(int i=0; i<attr.getLength(); i++){
 177  0
       Node a = attr.item(i);
 178  0
       boolean sameUri = namespaceUri == null? true: namespaceUri.equals(a.getNamespaceURI());
 179  0
       if(sameUri && attributeLocalName.equals(a.getLocalName())){
 180  0
         return a.getNodeValue();
 181  
       }
 182  
     }
 183  0
     return null;
 184  
   }
 185  
      
 186  
   /**
 187  
    * Construct a {@link QName} object using a node's localName and NamespaceUri
 188  
    * @param n - a DOM node
 189  
    * @return - a new {@link QName} object that has input localName as it's localName and
 190  
    * input NamespaceUri as its NamespaceUri.
 191  
    */
 192  
   public static QName getQName(Node n){
 193  0
     return new QName(emptyIfNull(n.getNamespaceURI()), n.getLocalName());
 194  
   }
 195  
   
 196  
   
 197  
   /**
 198  
    * Get a list of Children nodes of the input parent using a localName
 199  
    * @param parent - the parent node
 200  
    * @param localName - local name of the children to be searched for
 201  
    * @return - all children nodes that matches the input localName. 
 202  
    */
 203  
   public static List<Node> getChildrenNodesByName(Node parent, String localName){
 204  0
     return getChildrenByName(parent, localName, false);
 205  
   }
 206  
   
 207  
   /**
 208  
    * Get a single Children node of the input parent using a localName
 209  
    * @param parent - the parent node
 210  
    * @param localName - local name of the children to be searched for
 211  
    * @return - the FIRST children node that matches the input localName. 
 212  
    */
 213  
   public static Node getChildrenNodeByName(Node parent, String localName){
 214  0
     List<Node> result = getChildrenByName(parent, localName, true);
 215  0
     return result.size() > 0? result.get(0) : null;
 216  
   }
 217  
   
 218  
 
 219  
   public static String transformWithXsl(String xslt, String xml) throws YarfrawException{
 220  0
     Source source = new StreamSource(xml);
 221  0
     ByteArrayOutputStream out = new ByteArrayOutputStream();
 222  0
     Result res = new StreamResult(out);  
 223  0
     TransformerFactory transFact = TransformerFactory.newInstance();
 224  
     Transformer trans;
 225  
     
 226  
     try {
 227  0
       trans = transFact.newTransformer(new StreamSource(xslt));
 228  0
       trans.transform(source, res);
 229  0
     } catch (TransformerConfigurationException e) {
 230  0
       throw new YarfrawException("Transformer config exception", e);
 231  0
     } catch (TransformerException e) {
 232  0
       throw new YarfrawException("Transform exception", e);
 233  0
     }
 234  
     
 235  0
     return out.toString();
 236  
   }
 237  
   
 238  
   private static List<Node> getChildrenByName(Node parent, String localName, boolean single){
 239  0
     List<Node> nodes = new ArrayList<Node>();
 240  0
     NodeList list = parent.getChildNodes();
 241  0
     for(int i =0; i< list.getLength(); i++){
 242  0
       if(localName.equals(list.item(i).getLocalName())){
 243  0
         nodes.add(list.item(i));
 244  0
         if(single){
 245  0
           return nodes;
 246  
         }
 247  
       }
 248  
     }
 249  0
     return nodes;
 250  
   }
 251  
 
 252  
 }