changeset 3481:e59588ea27bd

Some optimization of the code mentioned by Sascha T. flys-client/trunk@5176 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Wed, 08 Aug 2012 13:39:35 +0000
parents 14d37be8541e
children c64cad8dc772
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java
diffstat 2 files changed, 41 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- 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 <bjoern.ricks@intevation.de>
+	* 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 <bjoern.ricks@intevation.de>
 	* src/main/java/de/intevation/flys/client/server/CapabilitiesParser.java,
 	  src/main/java/de/intevation/flys/client/server/BaseServlet.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<String, List<String>> featuremap = new Hashtable<String, List<String>>();
 
+    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<String> allowed = new LinkedList<String>();
-                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<String> allowed = new ArrayList<String>(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

http://dive4elements.wald.intevation.org