From be6b4608bdea970657d0257460ab92874314ca9b Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Fri, 26 Sep 2014 11:06:51 +0200 Subject: Various problems found by FindBugs --- .../java/com/c2kernel/utils/XmlElementParser.java | 172 ++++++++++----------- 1 file changed, 86 insertions(+), 86 deletions(-) (limited to 'src/main/java/com/c2kernel/utils/XmlElementParser.java') diff --git a/src/main/java/com/c2kernel/utils/XmlElementParser.java b/src/main/java/com/c2kernel/utils/XmlElementParser.java index 2e86763..7601731 100644 --- a/src/main/java/com/c2kernel/utils/XmlElementParser.java +++ b/src/main/java/com/c2kernel/utils/XmlElementParser.java @@ -6,6 +6,7 @@ import java.util.Vector; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -13,93 +14,92 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; -public class XmlElementParser -{ - public static String[] parse(String data, String xpath) - { - Vector returnData = new Vector(); - String[] returnArray = new String[0]; - try - { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - StringReader is = new StringReader(data); - Document doc = builder.parse(new InputSource(is)); - StringTokenizer pathTokens = new StringTokenizer(xpath, "/"); - int taille = pathTokens.countTokens(); - String[] pathElements = new String[taille]; - int i=taille; - while (pathTokens.hasMoreElements()) - pathElements[--i] = pathTokens.nextToken(); +public class XmlElementParser { + public static String[] parse(String data, String xpath) { + Vector returnData = new Vector(); + String[] returnArray = new String[0]; + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + try { + builder = factory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + Logger.error(e); + throw new RuntimeException("Could not create XML Document Builder"); + } + StringReader is = new StringReader(data); + Document doc; + try { + doc = builder.parse(new InputSource(is)); + } catch (Exception e) { + Logger.error(e); + throw new RuntimeException("Parser malfunction"); + } + StringTokenizer pathTokens = new StringTokenizer(xpath, "/"); + int taille = pathTokens.countTokens(); + String[] pathElements = new String[taille]; + int i = taille; + while (pathTokens.hasMoreElements()) + pathElements[--i] = pathTokens.nextToken(); - if (Logger.doLog(6)) { - Logger.msg(6, "Path elements:"); - for (String pathElement : pathElements) - Logger.debug(6, pathElement); - } + if (Logger.doLog(6)) { + Logger.msg(6, "Path elements:"); + for (String pathElement : pathElements) + Logger.debug(6, pathElement); + } - Logger.msg(6, "Looking for attribute "+pathElements[0]+" in "+pathElements[1]); - NodeList nl = doc.getElementsByTagName(pathElements[1]); - for (int j = 0; j < nl.getLength(); j++) - { - Logger.msg(6, "Found one"); - Element e = (Element)nl.item(j); - boolean match=true; - Node child=e; - for (int k=2;k