Commit | Line | Data |
---|---|---|
69e26a06 MS |
1 | package org.ufcspa.simdecs.entities; |
2 | ||
3 | import java.io.InputStream; | |
4 | import java.io.IOException; | |
5 | import java.util.ArrayList; | |
6 | ||
7 | import javax.xml.parsers.DocumentBuilder; | |
8 | import javax.xml.parsers.DocumentBuilderFactory; | |
9 | import javax.xml.parsers.ParserConfigurationException; | |
10 | ||
11 | import org.w3c.dom.Document; | |
12 | import org.w3c.dom.Element; | |
13 | import org.w3c.dom.NodeList; | |
14 | import org.xml.sax.SAXException; | |
15 | ||
16 | public class BayesianNetworkParser { | |
17 | ||
18 | private Document dom; | |
19 | ||
20 | public void parseBayesianNetwork(String filename) { | |
21 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
22 | try { | |
23 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
24 | dom = db.parse(filename); | |
25 | } catch(ParserConfigurationException pce) { | |
26 | pce.printStackTrace(); | |
27 | } catch(SAXException se) { | |
28 | se.printStackTrace(); | |
29 | } catch(IOException ioe) { | |
30 | ioe.printStackTrace(); | |
31 | } | |
32 | } | |
33 | ||
34 | public void parseBayesianNetwork(InputStream input) { | |
35 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
36 | try { | |
37 | DocumentBuilder db = dbf.newDocumentBuilder(); | |
38 | dom = db.parse(input); | |
39 | } catch(ParserConfigurationException pce) { | |
40 | pce.printStackTrace(); | |
41 | } catch(SAXException se) { | |
42 | se.printStackTrace(); | |
43 | } catch(IOException ioe) { | |
44 | ioe.printStackTrace(); | |
45 | } | |
46 | } | |
47 | ||
48 | public String getBayesianNetworkName() throws Exception { | |
49 | String bayesianNetworkName = ""; | |
50 | Element docElement = dom.getDocumentElement(); | |
51 | ||
52 | NodeList headerElements = docElement.getElementsByTagName("xbifns:header"); | |
53 | if(headerElements != null && headerElements.getLength() > 0) { | |
54 | Element header = (Element)headerElements.item(0); | |
55 | NodeList nameElements = header.getElementsByTagName("xbifns:name"); | |
56 | if (nameElements != null && nameElements.getLength() > 0) { | |
57 | Element name = (Element)nameElements.item(0); | |
58 | bayesianNetworkName = name.getTextContent(); | |
59 | } else { | |
60 | throw new Exception("Document not well formed."); | |
61 | } | |
62 | } | |
63 | else { | |
64 | throw new Exception("Document not well formed."); | |
65 | } | |
66 | return bayesianNetworkName; | |
67 | } | |
68 | ||
69 | public ArrayList<String> getBayesianNetworkNodes() throws Exception { | |
70 | ArrayList<String> namesList = new ArrayList<String>(); | |
71 | Element docElement = dom.getDocumentElement(); | |
72 | ||
73 | NodeList variablesElements = docElement.getElementsByTagName("xbifns:variables"); | |
74 | if(variablesElements != null && variablesElements.getLength() > 0) { | |
75 | Element variablesElement = (Element)variablesElements.item(0); | |
76 | NodeList variables = variablesElement.getElementsByTagName("xbifns:variable"); | |
77 | if (variables != null && variables.getLength() > 0) { | |
78 | for (int i = 0; i < variables.getLength(); i++){ | |
79 | Element variable = (Element)variables.item(i); | |
80 | namesList.add(variable.getAttribute("name")); | |
81 | } | |
82 | } else { | |
83 | throw new Exception("Document not well formed."); | |
84 | } | |
85 | } | |
86 | else { | |
87 | throw new Exception("Document not well formed."); | |
88 | } | |
89 | return namesList; | |
90 | } | |
91 | ||
92 | /* Testing */ | |
93 | public static void main(String[] args) throws Exception { | |
94 | BayesianNetworkParser parser = new BayesianNetworkParser(); | |
95 | parser.parseBayesianNetwork("test_bn.xml"); | |
96 | String bn = parser.getBayesianNetworkName(); | |
97 | ArrayList<String> nodes = parser.getBayesianNetworkNodes(); | |
98 | System.out.println(bn); | |
99 | System.out.println(nodes); | |
100 | } | |
101 | } |