Coverage Report - com.aurea.maven.plugins.sonic.utils.Xpp3Utils
 
Classes in this File Line Coverage Branch Coverage Complexity
Xpp3Utils
0%
0/116
0%
0/64
3.533
 
 1  
 package com.aurea.maven.plugins.sonic.utils;
 2  
 
 3  
 import java.io.ByteArrayInputStream;
 4  
 import java.io.File;
 5  
 import java.util.ArrayList;
 6  
 import java.util.Enumeration;
 7  
 import java.util.HashMap;
 8  
 import java.util.List;
 9  
 import java.util.Properties;
 10  
 
 11  
 import javax.xml.namespace.QName;
 12  
 import javax.xml.parsers.DocumentBuilder;
 13  
 import javax.xml.parsers.DocumentBuilderFactory;
 14  
 import javax.xml.xpath.XPath;
 15  
 import javax.xml.xpath.XPathConstants;
 16  
 import javax.xml.xpath.XPathExpression;
 17  
 import javax.xml.xpath.XPathFactory;
 18  
 
 19  
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 20  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 21  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 22  
 import org.w3c.dom.Document;
 23  
 import org.w3c.dom.NodeList;
 24  
 
 25  
 /**
 26  
  *
 27  
  */
 28  0
 public class Xpp3Utils {
 29  
   
 30  
 
 31  
   /**
 32  
    * 
 33  
    */
 34  
   public static final String PROPERTY_SEPARATOR = ";";
 35  
   
 36  
   /**
 37  
    * @param _element
 38  
    *          element
 39  
    */
 40  
   public static void removeAllChildren(final Xpp3Dom _element) {
 41  
 
 42  0
     if (_element != null) {
 43  0
       while (_element.getChildCount() > 0) {
 44  0
         _element.removeChild(0);
 45  
       }
 46  
     }
 47  0
   }
 48  
   
 49  
   /**
 50  
    * @param _element
 51  
    *          element
 52  
    * @param _path
 53  
    *          path
 54  
    */
 55  
   public static void removeAllChildren(final Xpp3Dom _element, final String _path) {
 56  
 
 57  0
     List<Xpp3Dom> tbdParents = collectElements(_element, _path);
 58  0
     for (Xpp3Dom tbdParent : tbdParents) {
 59  0
       while (tbdParent.getChildCount() > 0) {
 60  0
         tbdParent.removeChild(0);
 61  
       }
 62  0
     }
 63  0
   }
 64  
   
 65  
   /**
 66  
    * @param _element
 67  
    *          element
 68  
    * @param _path
 69  
    *          _path
 70  
    * @return List&lt;Xpp3Dom&gt;
 71  
    */
 72  
   public static List<Xpp3Dom> collectElements(final Xpp3Dom _element, final String _path) {
 73  
 
 74  0
     List<Xpp3Dom> result = new ArrayList<Xpp3Dom>();
 75  
     
 76  0
     if (_element != null) {
 77  0
       if (_path != null && !"".equals(_path)) {
 78  0
         buildElementList(_element, _path.split("/"), result);
 79  
       } else {
 80  0
         for (Xpp3Dom child : _element.getChildren()) {
 81  0
           result.add(child);
 82  
         }
 83  
       }
 84  
     }
 85  
     
 86  0
     return result;
 87  
   }
 88  
   
 89  
   private static void buildElementList(final Xpp3Dom _element, final String[] _path, final List<Xpp3Dom> _result) {
 90  
 
 91  0
     if (_path.length == 0) {
 92  0
       return;
 93  
     } else {
 94  0
       List<Xpp3Dom> children = collectImmediateChilds(_element, _path[0]);
 95  0
       if (_path.length == 1) {
 96  0
         _result.addAll(children);
 97  
       } else {
 98  0
         String[] tail = new String[_path.length - 1];
 99  0
         for (int i = 1; i < _path.length; i++) {
 100  0
           tail[i - 1] = _path[i];
 101  
         }
 102  0
         for (Xpp3Dom child : children) {
 103  0
           buildElementList(child, tail, _result);
 104  0
         }
 105  
       }
 106  
     }
 107  0
   }
 108  
   
 109  
   private static List<Xpp3Dom> collectImmediateChilds(final Xpp3Dom _element, final String _path) {
 110  
 
 111  0
     List<Xpp3Dom> result = new ArrayList<Xpp3Dom>();
 112  
     
 113  0
     if (_element != null && _path != null) {
 114  0
       for (Xpp3Dom child : _element.getChildren()) {
 115  0
         if (child.getName().matches(_path)) {
 116  0
           result.add(child);
 117  
         }
 118  
       }
 119  
     }
 120  0
     return result;
 121  
   }
 122  
   
 123  
   /**
 124  
    * @param _baseDir
 125  
    *          _baseDir
 126  
    * @param _extension
 127  
    *          _extension
 128  
    * @return List&lt;Xpp3Dom&gt;
 129  
    * @throws Exception
 130  
    *           Exception
 131  
    */
 132  
   public static List<Xpp3Dom> collectElements(final String _baseDir, final String _extension) throws Exception {
 133  
 
 134  0
     return collectElements(_baseDir, _extension, ".*");
 135  
   }
 136  
   
 137  
   /**
 138  
    * @param _baseDir
 139  
    *          _baseDir
 140  
    * @param _extension
 141  
    *          _extension
 142  
    * @param _path
 143  
    *          _path
 144  
    * @return List&lt;Xpp3Dom&gt;
 145  
    * @throws Exception
 146  
    *           Exception
 147  
    */
 148  
   public static List<Xpp3Dom> collectElements(final String _baseDir, final String _extension, final String _path)
 149  
     throws Exception {
 150  
 
 151  0
     String path = _path;
 152  
     
 153  0
     if (path == null) {
 154  0
       path = ".*";
 155  
     }
 156  
     
 157  0
     ArrayList<Xpp3Dom> result = new ArrayList<Xpp3Dom>();
 158  
     
 159  0
     File baseDirFile = new File(_baseDir);
 160  0
     if (baseDirFile.exists() && baseDirFile.isDirectory()) {
 161  0
       for (String fileName : FileUtilities.getFileList(_baseDir, _extension, null)) {
 162  0
         File queuesFile = new File(_baseDir, fileName);
 163  0
         XmlStreamReader reader = new XmlStreamReader(queuesFile);
 164  0
         Xpp3Dom dom = Xpp3DomBuilder.build(reader);
 165  0
         result.addAll(collectElements(dom, path));
 166  
       }
 167  
     }
 168  0
     return result;
 169  
   }
 170  
   
 171  
   /**
 172  
    * @param _dom
 173  
    *          _dom
 174  
    * @param _childName
 175  
    *          _childName
 176  
    */
 177  
   public static void removeChildByName(final Xpp3Dom _dom, final String _childName) {
 178  
 
 179  0
     Xpp3Dom[] children = _dom.getChildren();
 180  0
     for (int i = 0; i < children.length; i++) {
 181  0
       if (children[i].getName().equals(_childName)) {
 182  0
         _dom.removeChild(i);
 183  0
         return;
 184  
       }
 185  
     }
 186  0
   }
 187  
   
 188  
   /**
 189  
    * @param _tagName
 190  
    *          _tagName
 191  
    * @return Xpp3Dom
 192  
    */
 193  
   public static Xpp3Dom createXpp3Dom(final String _tagName) {
 194  
 
 195  0
     return createXpp3Dom(_tagName, null, null);
 196  
   }
 197  
   
 198  
   /**
 199  
    * @param _tagName
 200  
    *          _tagName
 201  
    * @param _content
 202  
    *          _content
 203  
    * @return Xpp3Dom
 204  
    */
 205  
   public static Xpp3Dom createXpp3Dom(final String _tagName, final String _content) {
 206  
 
 207  0
     return createXpp3Dom(_tagName, _content, null);
 208  
   }
 209  
   
 210  
   /**
 211  
    * @param _tagName
 212  
    *          _tagName
 213  
    * @param _content
 214  
    *          _content
 215  
    * @param _attributes
 216  
    *          _attributes
 217  
    * @return Xpp3Dom
 218  
    */
 219  
   public static Xpp3Dom createXpp3Dom(final String _tagName, final String _content, final String _attributes) {
 220  
 
 221  0
     Xpp3Dom result = new Xpp3Dom(_tagName);
 222  0
     if (_content != null) {
 223  0
       result.setValue(_content);
 224  
     }
 225  0
     if (_attributes != null) {
 226  0
       Properties props = stringToProperties(_attributes);
 227  0
       for (Enumeration<Object> en = props.keys(); en.hasMoreElements();) {
 228  0
         String propName = (String) en.nextElement();
 229  0
         result.setAttribute(propName, props.getProperty(propName));
 230  0
       }
 231  
     }
 232  0
     return result;
 233  
   }
 234  
   
 235  
   private static Properties stringToProperties(final String _attributes) {
 236  
 
 237  0
     Properties result = new Properties();
 238  
     
 239  0
     if (_attributes != null && !"".equals(_attributes)) {
 240  0
       String[] props = _attributes.split(";");
 241  0
       for (String prop : props) {
 242  0
         String[] nvPair = prop.split("=");
 243  0
         if (nvPair.length == 2) {
 244  0
           result.put(nvPair[0], nvPair[1]);
 245  
         }
 246  
       }
 247  
     }
 248  0
     return result;
 249  
   }
 250  
   
 251  
   /**
 252  
    * @param _original
 253  
    *          _original
 254  
    * @param _keyExpression
 255  
    *          _keyExpression
 256  
    * @return List&lt;Xpp3Dom&gt;
 257  
    */
 258  
   public static List<Xpp3Dom> unifyXpp3List(final List<Xpp3Dom> _original, final String _keyExpression) {
 259  
 
 260  0
     HashMap<String, Xpp3Dom> map = new HashMap<String, Xpp3Dom>();
 261  0
     for (Xpp3Dom doc : _original) {
 262  
       try {
 263  0
         NodeList nl = (NodeList) (evaluateXPath(doc, _keyExpression));
 264  0
         if (nl.getLength() != 1) {
 265  0
           throw new RuntimeException("Xpath Expression received " + nl.getLength() + " nodes instead of one");
 266  
         }
 267  0
         String key = nl.item(0).getTextContent();
 268  0
         if (!map.containsKey(key)) {
 269  0
           map.put(key, doc);
 270  
         }
 271  0
       } catch (Exception e) {
 272  0
         throw new RuntimeException(e);
 273  0
       }
 274  0
     }
 275  
     
 276  0
     List<Xpp3Dom> result = new ArrayList<Xpp3Dom>();
 277  0
     result.addAll(map.values());
 278  
     
 279  0
     return result;
 280  
   }
 281  
   
 282  
   /**
 283  
    * @param _dom
 284  
    *          _dom
 285  
    * @param _xPathExpression
 286  
    *          _xPathExpression
 287  
    * @return Object
 288  
    */
 289  
   public static Object evaluateXPath(final Xpp3Dom _dom, final String _xPathExpression) {
 290  
 
 291  0
     Object result = null;
 292  
     
 293  
     try {
 294  0
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 295  0
       dbf.setNamespaceAware(true);
 296  0
       DocumentBuilder builder = dbf.newDocumentBuilder();
 297  0
       Document doc = builder.parse(new ByteArrayInputStream(_dom.toString().getBytes()));
 298  
       
 299  0
       XPathFactory xpFactory = XPathFactory.newInstance();
 300  0
       XPath xpath = xpFactory.newXPath();
 301  0
       XPathExpression expr = xpath.compile(_xPathExpression);
 302  
       
 303  0
       result = expr.evaluate(doc, XPathConstants.NODESET);
 304  0
     } catch (Exception e) {
 305  0
       e.printStackTrace();
 306  0
     }
 307  
     
 308  0
     return result;
 309  
   }
 310  
   
 311  
   /**
 312  
    * @param _dom
 313  
    *          _dom
 314  
    * @param _xPathExpression
 315  
    *          _xPathExpression
 316  
    * @return Object
 317  
    */
 318  
   public static Object evaluateXPath(final Xpp3Dom _dom, final String _xPathExpression, final QName returnType) {
 319  
 
 320  0
     Object result = null;
 321  
     
 322  
     try {
 323  0
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 324  0
       dbf.setNamespaceAware(true);
 325  0
       DocumentBuilder builder = dbf.newDocumentBuilder();
 326  0
       Document doc = builder.parse(new ByteArrayInputStream(_dom.toString().getBytes()));
 327  
       
 328  0
       XPathFactory xpFactory = XPathFactory.newInstance();
 329  0
       XPath xpath = xpFactory.newXPath();
 330  0
       XPathExpression expr = xpath.compile(_xPathExpression);
 331  
       
 332  0
       result = expr.evaluate(doc, returnType);
 333  0
     } catch (Exception e) {
 334  0
       e.printStackTrace();
 335  0
     }
 336  
     
 337  0
     return result;
 338  
   }
 339  
 }