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

http://dive4elements.wald.intevation.org