[922] | 1 | package de.ugoe.cs.autoquest.plugin.jfc; |
---|
[806] | 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.IOException; |
---|
| 5 | import java.util.HashSet; |
---|
| 6 | import java.util.Set; |
---|
| 7 | |
---|
| 8 | import javax.xml.parsers.DocumentBuilder; |
---|
| 9 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
| 10 | import javax.xml.parsers.ParserConfigurationException; |
---|
| 11 | |
---|
| 12 | import org.w3c.dom.Document; |
---|
| 13 | import org.w3c.dom.Node; |
---|
| 14 | import org.w3c.dom.NodeList; |
---|
| 15 | import org.xml.sax.SAXException; |
---|
| 16 | |
---|
| 17 | public class JFCReplayIDValidator { |
---|
| 18 | private Set<String> validIds = null; |
---|
| 19 | |
---|
| 20 | public JFCReplayIDValidator(File guitarEfgFile) throws ParserConfigurationException, SAXException, IOException{ |
---|
| 21 | this.initialize(guitarEfgFile); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | public void initialize(File guitarGUIFile) throws ParserConfigurationException, SAXException, IOException{ |
---|
| 25 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
---|
| 26 | DocumentBuilder builder = factory.newDocumentBuilder(); |
---|
| 27 | Document freemindGUI = builder.parse(guitarGUIFile); |
---|
| 28 | |
---|
| 29 | validIds = new HashSet<String>(); |
---|
| 30 | |
---|
| 31 | NodeList guis = freemindGUI.getFirstChild().getChildNodes(); |
---|
| 32 | for (int i = 0; i < guis.getLength(); i++) |
---|
| 33 | extractIDs(guis.item(i)); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | private void extractIDs(Node node){ |
---|
| 37 | NodeList children = node.getChildNodes(); |
---|
| 38 | if (children != null){ |
---|
| 39 | for (int j = 0; j < children.getLength(); j++){ |
---|
| 40 | if (children.item(j).getNodeName().equals("Attributes")){ |
---|
| 41 | NodeList properties = children.item(j).getChildNodes(); |
---|
| 42 | for (int i = 0; i < properties.getLength(); i++){ |
---|
| 43 | NodeList property = properties.item(i).getChildNodes(); |
---|
| 44 | for (int k = 0; k < property.getLength(); k++){ |
---|
| 45 | if (property.item(k).getTextContent().equals("ID")){ |
---|
| 46 | validIds.add(property.item(k+2).getTextContent()); |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | else{ |
---|
| 52 | extractIDs(children.item(j)); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | public boolean validateReplayID(String replayID){ |
---|
| 60 | if (validIds == null || validIds.size() == 0) |
---|
| 61 | throw new IllegalStateException("There are no valid IDs known, call initialize first."); |
---|
| 62 | |
---|
| 63 | if (validIds.contains(replayID)) |
---|
| 64 | return true; |
---|
| 65 | |
---|
| 66 | return false; |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | } |
---|