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

Some optimization of the code mentioned by Sascha T. flys-client/trunk@5176 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Wed, 08 Aug 2012 13:39:35 +0000
parents 4a6321dd5186
children c64cad8dc772
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
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
27 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
28 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
29
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30 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
31 FileInputStream finput = new FileInputStream(filename);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
33 try {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
34 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
35
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
36 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
37 doc,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
38 XPATH_ROLES,
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
39 XPathConstants.NODESET,
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40 FeaturesNamespaceContext.INSTANCE);
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
42 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
43 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
44
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
45 String name = rolenode.getAttributeNS(
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
46 FeaturesNamespaceContext.NAMESPACE_URI, "name");
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
47
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
48 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
49
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
50 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
51 rolenode,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
52 XPATH_FEATURES,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
53 XPathConstants.NODESET,
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
54 FeaturesNamespaceContext.INSTANCE);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
55
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
56 if (features.getLength() > 0) {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
57 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
58 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
59 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
60 String featurename = featurenode.getNodeValue();
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
61
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
62 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
63
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
64 allowed.add(featurename);
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
65 }
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
66 featuremap.put(name, allowed);
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
67 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
68 }
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
69 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
70 }
3481
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
71 finally {
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
72 finput.close();
e59588ea27bd Some optimization of the code mentioned by Sascha T.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 3476
diff changeset
73 }
3476
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
74 }
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 @Override
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
77 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
78 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
79
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
80 for (String role: roles) {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81 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
82 if (!allowed.isEmpty()) {
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
83 features.addAll(allowed);
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 return features;
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
87 }
4a6321dd5186 Implement a class representation of features corresponding to roles
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
88 }

http://dive4elements.wald.intevation.org