annotate flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java @ 3482:c64cad8dc772

Add missing import of ArrayList flys-client/trunk@5177 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Wed, 08 Aug 2012 13:41:58 +0000
parents e59588ea27bd
children 83845aa322ea
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
3482
c64cad8dc772 Add missing import of ArrayList
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3481
diff changeset
6 import java.util.ArrayList;
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
7 import java.util.Hashtable;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
8 import java.util.List;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9 import java.util.LinkedList;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11 import javax.xml.xpath.XPathConstants;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
13 import org.apache.log4j.Logger;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
14
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
15 import org.w3c.dom.Document;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
16 import org.w3c.dom.Element;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17 import org.w3c.dom.Node;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
18 import org.w3c.dom.NodeList;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
19
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 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
21
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 public class XMLFileFeatures implements Features {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24 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
25
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
26 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
27
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
28 private final static String XPATH_FEATURES = "ftr:feature/child::text()";
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
29 private final static String XPATH_ROLES = "/ftr:features/ftr:role";
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
30
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
31 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
32 FileInputStream finput = new FileInputStream(filename);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
33
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
34 try {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
35 Document doc = XMLUtils.parseDocument(finput);
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
36
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
37 NodeList roles = (NodeList) XMLUtils.xpath(
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
38 doc,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
39 XPATH_ROLES,
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40 XPathConstants.NODESET,
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41 FeaturesNamespaceContext.INSTANCE);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
42
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
43 for(int i=0, m = roles.getLength(); i < m; i++) {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
44 Element rolenode = (Element)roles.item(i);
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
46 String name = rolenode.getAttributeNS(
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
47 FeaturesNamespaceContext.NAMESPACE_URI, "name");
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
48
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
49 logger.debug("Found role: " + name);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
50
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
51 NodeList features = (NodeList) XMLUtils.xpath(
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
52 rolenode,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
53 XPATH_FEATURES,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
54 XPathConstants.NODESET,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
55 FeaturesNamespaceContext.INSTANCE);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
56
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
57 if (features.getLength() > 0) {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
58 List<String> allowed = new ArrayList<String>(features.getLength());
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
59 for (int j=0, l = features.getLength(); j < l; j++) {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
60 Node featurenode = features.item(j);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
61 String featurename = featurenode.getNodeValue();
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
62
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
63 logger.debug("Found feature: " + featurename);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
64
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
65 allowed.add(featurename);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
66 }
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
67 featuremap.put(name, allowed);
3476
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 }
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
70 logger.debug("Loaded all features");
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
71 }
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
72 finally {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
73 finput.close();
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
74 }
3476
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
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
77 @Override
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
78 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
79 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
80
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81 for (String role: roles) {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
82 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
83 if (!allowed.isEmpty()) {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
84 features.addAll(allowed);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
85 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
86 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
87 return features;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
88 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
89 }

http://dive4elements.wald.intevation.org