# HG changeset patch # User Bjoern Ricks # Date 1344433175 0 # Node ID e59588ea27bd963edbe7f76a5fe322650a129671 # Parent 14d37be8541e0e61c335790986fd065c6ecc0e86 Some optimization of the code mentioned by Sascha T. flys-client/trunk@5176 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 14d37be8541e -r e59588ea27bd flys-client/ChangeLog --- a/flys-client/ChangeLog Wed Aug 08 13:10:30 2012 +0000 +++ b/flys-client/ChangeLog Wed Aug 08 13:39:35 2012 +0000 @@ -1,3 +1,8 @@ +2012-08-08 Björn Ricks + * src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java: + Always close FileInputStream, improve for loops and avoid XPath for + getting a xml attribute. + 2012-08-08 Björn Ricks * src/main/java/de/intevation/flys/client/server/CapabilitiesParser.java, src/main/java/de/intevation/flys/client/server/BaseServlet.java, diff -r 14d37be8541e -r e59588ea27bd flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java Wed Aug 08 13:10:30 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java Wed Aug 08 13:39:35 2012 +0000 @@ -24,49 +24,53 @@ private Hashtable> featuremap = new Hashtable>(); + private final static String XPATH_FEATURES = "ftr:feature/child::text()"; + private final static String XPATH_ROLES = "/ftr:features/ftr:role"; + public XMLFileFeatures(String filename) throws IOException { FileInputStream finput = new FileInputStream(filename); - Document doc = XMLUtils.parseDocument(finput); - - String XPATH_FEATURES = "ftr:feature/child::text()"; - String XPATH_ROLES = "/ftr:features/ftr:role"; - - NodeList roles = (NodeList) XMLUtils.xpath( - doc, - XPATH_ROLES, - XPathConstants.NODESET, - FeaturesNamespaceContext.INSTANCE); - for(int i=0; i < roles.getLength(); i++) { - Node rolenode = roles.item(i); + try { + Document doc = XMLUtils.parseDocument(finput); - String name = XMLUtils.xpathString( - rolenode, "@name", FeaturesNamespaceContext.INSTANCE); - - logger.debug("Found role: " + name); - - NodeList features = (NodeList) XMLUtils.xpath( - rolenode, - XPATH_FEATURES, + NodeList roles = (NodeList) XMLUtils.xpath( + doc, + XPATH_ROLES, XPathConstants.NODESET, FeaturesNamespaceContext.INSTANCE); - if (features.getLength() > 0) { - List allowed = new LinkedList(); - for (int j=0; j < features.getLength(); j++) { - Node featurenode = features.item(j); - String featurename = featurenode.getNodeValue(); - - logger.debug("found feature: " + featurename); + for(int i=0, m = roles.getLength(); i < m; i++) { + Element rolenode = (Element)roles.item(i); - allowed.add(featurename); + String name = rolenode.getAttributeNS( + FeaturesNamespaceContext.NAMESPACE_URI, "name"); + + logger.debug("Found role: " + name); + + NodeList features = (NodeList) XMLUtils.xpath( + rolenode, + XPATH_FEATURES, + XPathConstants.NODESET, + FeaturesNamespaceContext.INSTANCE); + + if (features.getLength() > 0) { + List allowed = new ArrayList(features.getLength()); + for (int j=0, l = features.getLength(); j < l; j++) { + Node featurenode = features.item(j); + String featurename = featurenode.getNodeValue(); + + logger.debug("Found feature: " + featurename); + + allowed.add(featurename); + } + featuremap.put(name, allowed); } - featuremap.put(name, allowed); } + logger.debug("Loaded all features"); } - logger.debug("Loaded all features"); - - finput.close(); + finally { + finput.close(); + } } @Override