Clover coverage report - Maven Clover report
Coverage timestamp: Thu Oct 11 2007 08:41:48 CEST
file stats: LOC: 119   Methods: 5
NCLOC: 91   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLHelper.java 30% 35.9% 40% 34.4%
coverage coverage
 1    /*
 2    * Copyright (C) 2007 Eric MIGNOT - mignots.eric@free.fr
 3    *
 4    * This library is free software; you can redistribute it and/or
 5    * modify it under the terms of the GNU Lesser General Public
 6    * License as published by the Free Software Foundation; either
 7    * version 2.1 of the License, or (at your option) any later version.
 8    *
 9    * This library is distributed in the hope that it will be useful,
 10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 12    * Lesser General Public License for more details.
 13    *
 14    * You should have received a copy of the GNU Lesser General Public
 15    * License along with this library; if not, write to the Free Software
 16    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17    */
 18   
 19    package org.ericmignot.modeler.util.xml;
 20   
 21    import java.io.*;
 22    import javax.xml.parsers.*;
 23    import org.w3c.dom.*;
 24   
 25    /**
 26    * Convenience XML Methods
 27    */
 28    public class XMLHelper
 29    {
 30  0 public static Element getFirstElementByName(Element element, String tagName)
 31    {
 32  0 return toto(element, tagName);
 33    }
 34   
 35  106 public static Element getFirstElementByName(String xml, String tagName)
 36    {
 37  106 try
 38    {
 39  106 DocumentBuilderFactory l_Factory =
 40    DocumentBuilderFactory.newInstance();
 41  106 DocumentBuilder l_DocumentBuilder = l_Factory.newDocumentBuilder();
 42  106 Document l_Document = l_DocumentBuilder.parse(
 43    new ByteArrayInputStream(xml.getBytes()));
 44  106 Element l_Element = l_Document.getDocumentElement();
 45   
 46  106 return toto(l_Element, tagName);
 47    }
 48    catch (Exception e)
 49    {
 50  0 e.printStackTrace();
 51  0 return null;
 52    }
 53    }
 54   
 55  106 private static Element toto(Element p_CurrentElement, String p_TagName)
 56    {
 57  106 if (p_CurrentElement != null)
 58    {
 59  106 if (p_CurrentElement.getNodeName().equalsIgnoreCase(p_TagName))
 60    {
 61  105 return p_CurrentElement;
 62    }
 63  1 NodeList l_Childs = p_CurrentElement.getChildNodes();
 64  1 for (int i = 0; i < l_Childs.getLength(); i ++)
 65    {
 66  1 Node l_Child = l_Childs.item(i);
 67  1 if (l_Child instanceof Element)
 68    {
 69  0 Element found = toto((Element) l_Child, p_TagName);
 70  0 if (found != null)
 71    {
 72  0 return found;
 73    }
 74    }
 75    }
 76    }
 77  1 return null;
 78    }
 79   
 80  0 public static String formatXML(String source)
 81    {
 82  0 String toReturn = "";
 83  0 int tabNumber = 0;
 84  0 for (int i=0; i<source.length(); i++)
 85    {
 86  0 if ("<".equalsIgnoreCase(source.substring(i, i+1)))
 87    {
 88  0 if ((i<(source.length()-2)) && "</".equalsIgnoreCase(source.substring(i, i+2)))
 89    {
 90  0 tabNumber --;
 91  0 toReturn += "\n";
 92  0 toReturn = addTabs(toReturn, tabNumber);
 93    }
 94    else
 95    {
 96  0 tabNumber ++;
 97  0 toReturn += "\n";
 98  0 toReturn = addTabs(toReturn, tabNumber);
 99    }
 100    }
 101  0 if ((i<(source.length()-2)) && "/>".equalsIgnoreCase(source.substring(i, i+2)))
 102    {
 103  0 tabNumber --;
 104    }
 105  0 toReturn += source.substring(i, i+1);
 106    }
 107  0 return toReturn;
 108    }
 109   
 110  0 public static String addTabs(String source, int nbTab)
 111    {
 112  0 String toReturn = new String(source);
 113  0 for (int j=0; j<nbTab; j++)
 114    {
 115  0 toReturn += "\t";
 116    }
 117  0 return toReturn;
 118    }
 119    }