annotate flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java @ 3484:83845aa322ea

XMLFileFeatures: Cosmetics. flys-client/trunk@5181 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 09 Aug 2012 08:22:04 +0000
parents c64cad8dc772
children 3dfe7bbab70f
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;
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
7 import java.util.HashMap;
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
8 import java.util.LinkedList;
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9 import java.util.List;
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
10 import java.util.Map;
3476
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 javax.xml.xpath.XPathConstants;
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.apache.log4j.Logger;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
15
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
16 import org.w3c.dom.Document;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17 import org.w3c.dom.Element;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
18 import org.w3c.dom.Node;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
19 import org.w3c.dom.NodeList;
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 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
22
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 public class XMLFileFeatures implements Features {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
25 private static final Logger logger =
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
26 Logger.getLogger(XMLFileFeatures.class);
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
27
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
28 private Map<String, List<String>> featuremap =
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
29 new HashMap<String, List<String>>();
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
31 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
32 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
33
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
34 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
35 FileInputStream finput = new FileInputStream(filename);
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 try {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
38 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
39
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
40 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
41 doc,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
42 XPATH_ROLES,
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
43 XPathConstants.NODESET,
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
44 FeaturesNamespaceContext.INSTANCE);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
46 for(int i = 0, m = roles.getLength(); i < m; i++) {
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
47 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
48
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
49 String name = rolenode.getAttributeNS(
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
50 FeaturesNamespaceContext.NAMESPACE_URI, "name");
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
51
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
52 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
53
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
54 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
55 rolenode,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
56 XPATH_FEATURES,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
57 XPathConstants.NODESET,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
58 FeaturesNamespaceContext.INSTANCE);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
59
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
60 if (features == null) {
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
61 continue;
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
62 }
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
63
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
64 int N = features.getLength();
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
65
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
66 if (N > 0) {
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
67 List<String> allowed = new ArrayList<String>(N);
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
68 for (int j = 0; j < N; j++) {
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
69 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
70 String featurename = featurenode.getNodeValue();
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
71
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
72 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
73
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
74 allowed.add(featurename);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
75 }
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
76 featuremap.put(name, allowed);
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
77 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
78 }
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
79 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
80 }
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
81 finally {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
82 finput.close();
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
83 }
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
84 }
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 @Override
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
87 public List<String> getFeatures(List<String> roles) {
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
88 List<String> features = new ArrayList<String>();
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
89
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
90 for (String role: roles) {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
91 List<String> allowed = this.featuremap.get(role);
3484
83845aa322ea XMLFileFeatures: Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3482
diff changeset
92 if (allowed != null) {
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
93 features.addAll(allowed);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
94 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
95 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
96 return features;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
97 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
98 }

http://dive4elements.wald.intevation.org