1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.jfc; |
---|
16 | |
---|
17 | import java.io.File; |
---|
18 | import java.io.IOException; |
---|
19 | import java.util.HashSet; |
---|
20 | import java.util.Set; |
---|
21 | |
---|
22 | import javax.xml.parsers.DocumentBuilder; |
---|
23 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
24 | import javax.xml.parsers.ParserConfigurationException; |
---|
25 | |
---|
26 | import org.w3c.dom.Document; |
---|
27 | import org.w3c.dom.Node; |
---|
28 | import org.w3c.dom.NodeList; |
---|
29 | import org.xml.sax.SAXException; |
---|
30 | |
---|
31 | public class JFCReplayIDValidator { |
---|
32 | private Set<String> validIds = null; |
---|
33 | |
---|
34 | public JFCReplayIDValidator(File guitarEfgFile) throws ParserConfigurationException, SAXException, IOException{ |
---|
35 | this.initialize(guitarEfgFile); |
---|
36 | } |
---|
37 | |
---|
38 | public void initialize(File guitarGUIFile) throws ParserConfigurationException, SAXException, IOException{ |
---|
39 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
40 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
41 | Document freemindGUI = builder.parse(guitarGUIFile); |
---|
42 | |
---|
43 | validIds = new HashSet<String>(); |
---|
44 | |
---|
45 | NodeList guis = freemindGUI.getFirstChild().getChildNodes(); |
---|
46 | for (int i = 0; i < guis.getLength(); i++) |
---|
47 | extractIDs(guis.item(i)); |
---|
48 | } |
---|
49 | |
---|
50 | private void extractIDs(Node node){ |
---|
51 | NodeList children = node.getChildNodes(); |
---|
52 | if (children != null){ |
---|
53 | for (int j = 0; j < children.getLength(); j++){ |
---|
54 | if (children.item(j).getNodeName().equals("Attributes")){ |
---|
55 | NodeList properties = children.item(j).getChildNodes(); |
---|
56 | for (int i = 0; i < properties.getLength(); i++){ |
---|
57 | NodeList property = properties.item(i).getChildNodes(); |
---|
58 | for (int k = 0; k < property.getLength(); k++){ |
---|
59 | if (property.item(k).getTextContent().equals("ID")){ |
---|
60 | validIds.add(property.item(k+2).getTextContent()); |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | else{ |
---|
66 | extractIDs(children.item(j)); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | public boolean validateReplayID(String replayID){ |
---|
74 | if (validIds == null || validIds.size() == 0) |
---|
75 | throw new IllegalStateException("There are no valid IDs known, call initialize first."); |
---|
76 | |
---|
77 | if (validIds.contains(replayID)) |
---|
78 | return true; |
---|
79 | |
---|
80 | return false; |
---|
81 | |
---|
82 | } |
---|
83 | } |
---|