diff flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java @ 3476:4a6321dd5186

Implement a class representation of features corresponding to roles flys-client/trunk@5171 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Wed, 08 Aug 2012 12:51:18 +0000
parents
children e59588ea27bd
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java	Wed Aug 08 12:51:18 2012 +0000
@@ -0,0 +1,84 @@
+package de.intevation.flys.client.server.features;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import java.util.Hashtable;
+import java.util.List;
+import java.util.LinkedList;
+
+import javax.xml.xpath.XPathConstants;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+public class XMLFileFeatures implements Features {
+
+    private static final Logger logger = Logger.getLogger(XMLFileFeatures.class);
+
+    private Hashtable<String, List<String>> featuremap = new Hashtable<String, List<String>>();
+
+    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);
+
+            String name = XMLUtils.xpathString(
+                rolenode, "@name", FeaturesNamespaceContext.INSTANCE);
+
+            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 LinkedList<String>();
+                for (int j=0; j < features.getLength(); j++) {
+                    Node featurenode = features.item(j);
+                    String featurename = featurenode.getNodeValue();
+
+                    logger.debug("found feature: " + featurename);
+
+                    allowed.add(featurename);
+                }
+                featuremap.put(name, allowed);
+            }
+        }
+        logger.debug("Loaded all features");
+
+        finput.close();
+    }
+
+    @Override
+    public List<String> getFeatures(List<String> roles) {
+        List<String> features = new LinkedList<String>();
+
+        for (String role: roles) {
+            List<String> allowed = this.featuremap.get(role);
+            if (!allowed.isEmpty()) {
+                features.addAll(allowed);
+            }
+        }
+        return features;
+    }
+}

http://dive4elements.wald.intevation.org