Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
3475:9b29facddbd1 | 3476:4a6321dd5186 |
---|---|
1 package de.intevation.flys.client.server.features; | |
2 | |
3 import java.io.FileInputStream; | |
4 import java.io.IOException; | |
5 | |
6 import java.util.Hashtable; | |
7 import java.util.List; | |
8 import java.util.LinkedList; | |
9 | |
10 import javax.xml.xpath.XPathConstants; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import org.w3c.dom.Document; | |
15 import org.w3c.dom.Element; | |
16 import org.w3c.dom.Node; | |
17 import org.w3c.dom.NodeList; | |
18 | |
19 import de.intevation.artifacts.common.utils.XMLUtils; | |
20 | |
21 public class XMLFileFeatures implements Features { | |
22 | |
23 private static final Logger logger = Logger.getLogger(XMLFileFeatures.class); | |
24 | |
25 private Hashtable<String, List<String>> featuremap = new Hashtable<String, List<String>>(); | |
26 | |
27 public XMLFileFeatures(String filename) throws IOException { | |
28 FileInputStream finput = new FileInputStream(filename); | |
29 Document doc = XMLUtils.parseDocument(finput); | |
30 | |
31 String XPATH_FEATURES = "ftr:feature/child::text()"; | |
32 String XPATH_ROLES = "/ftr:features/ftr:role"; | |
33 | |
34 NodeList roles = (NodeList) XMLUtils.xpath( | |
35 doc, | |
36 XPATH_ROLES, | |
37 XPathConstants.NODESET, | |
38 FeaturesNamespaceContext.INSTANCE); | |
39 | |
40 for(int i=0; i < roles.getLength(); i++) { | |
41 Node rolenode = roles.item(i); | |
42 | |
43 String name = XMLUtils.xpathString( | |
44 rolenode, "@name", FeaturesNamespaceContext.INSTANCE); | |
45 | |
46 logger.debug("Found role: " + name); | |
47 | |
48 NodeList features = (NodeList) XMLUtils.xpath( | |
49 rolenode, | |
50 XPATH_FEATURES, | |
51 XPathConstants.NODESET, | |
52 FeaturesNamespaceContext.INSTANCE); | |
53 | |
54 if (features.getLength() > 0) { | |
55 List<String> allowed = new LinkedList<String>(); | |
56 for (int j=0; j < features.getLength(); j++) { | |
57 Node featurenode = features.item(j); | |
58 String featurename = featurenode.getNodeValue(); | |
59 | |
60 logger.debug("found feature: " + featurename); | |
61 | |
62 allowed.add(featurename); | |
63 } | |
64 featuremap.put(name, allowed); | |
65 } | |
66 } | |
67 logger.debug("Loaded all features"); | |
68 | |
69 finput.close(); | |
70 } | |
71 | |
72 @Override | |
73 public List<String> getFeatures(List<String> roles) { | |
74 List<String> features = new LinkedList<String>(); | |
75 | |
76 for (String role: roles) { | |
77 List<String> allowed = this.featuremap.get(role); | |
78 if (!allowed.isEmpty()) { | |
79 features.addAll(allowed); | |
80 } | |
81 } | |
82 return features; | |
83 } | |
84 } |