Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/features/XMLFileFeatures.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/server/features/XMLFileFeatures.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.server.features; | |
2 | |
3 import java.io.FileInputStream; | |
4 import java.io.IOException; | |
5 | |
6 import java.util.ArrayList; | |
7 import java.util.HashMap; | |
8 import java.util.List; | |
9 import java.util.Map; | |
10 | |
11 import javax.xml.xpath.XPathConstants; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 import org.w3c.dom.Document; | |
16 import org.w3c.dom.Element; | |
17 import org.w3c.dom.Node; | |
18 import org.w3c.dom.NodeList; | |
19 | |
20 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
21 | |
22 public class XMLFileFeatures implements Features { | |
23 | |
24 private static final Logger logger = | |
25 Logger.getLogger(XMLFileFeatures.class); | |
26 | |
27 private Map<String, List<String>> featuremap = | |
28 new HashMap<String, List<String>>(); | |
29 | |
30 private final static String XPATH_FEATURES = "ftr:feature/child::text()"; | |
31 private final static String XPATH_ROLES = "/ftr:features/ftr:role"; | |
32 | |
33 public XMLFileFeatures(String filename) throws IOException { | |
34 FileInputStream finput = new FileInputStream(filename); | |
35 logger.debug("XMLFileFeatures: " + filename); | |
36 try { | |
37 Document doc = XMLUtils.parseDocument(finput); | |
38 | |
39 NodeList roles = (NodeList) XMLUtils.xpath( | |
40 doc, | |
41 XPATH_ROLES, | |
42 XPathConstants.NODESET, | |
43 FeaturesNamespaceContext.INSTANCE); | |
44 | |
45 for(int i = 0, m = roles.getLength(); i < m; i++) { | |
46 Element rolenode = (Element)roles.item(i); | |
47 | |
48 String name = rolenode.getAttribute("name"); | |
49 | |
50 logger.debug("Found role: " + name); | |
51 | |
52 NodeList features = (NodeList) XMLUtils.xpath( | |
53 rolenode, | |
54 XPATH_FEATURES, | |
55 XPathConstants.NODESET, | |
56 FeaturesNamespaceContext.INSTANCE); | |
57 | |
58 if (features == null) { | |
59 continue; | |
60 } | |
61 | |
62 int N = features.getLength(); | |
63 | |
64 if (N > 0) { | |
65 List<String> allowed = new ArrayList<String>(N); | |
66 for (int j = 0; j < N; j++) { | |
67 Node featurenode = features.item(j); | |
68 String featurename = featurenode.getNodeValue(); | |
69 | |
70 logger.debug("Found feature: " + featurename); | |
71 | |
72 allowed.add(featurename); | |
73 } | |
74 featuremap.put(name, allowed); | |
75 } | |
76 } | |
77 logger.debug("Loaded all features"); | |
78 } | |
79 finally { | |
80 finput.close(); | |
81 } | |
82 } | |
83 | |
84 @Override | |
85 public List<String> getFeatures(List<String> roles) { | |
86 List<String> features = new ArrayList<String>(); | |
87 | |
88 for (String role: roles) { | |
89 List<String> allowed = this.featuremap.get(role); | |
90 if (allowed != null) { | |
91 features.addAll(allowed); | |
92 } | |
93 } | |
94 return features; | |
95 } | |
96 } |