Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1611 | - | 1 | package dongfang.mkt.configuration; |
2 | |||
3 | import java.io.File; |
||
4 | import java.io.IOException; |
||
5 | |||
6 | import javax.xml.parsers.DocumentBuilder; |
||
7 | import javax.xml.parsers.DocumentBuilderFactory; |
||
8 | import javax.xml.parsers.ParserConfigurationException; |
||
9 | import javax.xml.xpath.XPath; |
||
10 | import javax.xml.xpath.XPathConstants; |
||
11 | import javax.xml.xpath.XPathExpressionException; |
||
12 | import javax.xml.xpath.XPathFactory; |
||
13 | |||
14 | import org.w3c.dom.Document; |
||
15 | import org.w3c.dom.Element; |
||
16 | import org.w3c.dom.NodeList; |
||
17 | import org.xml.sax.SAXException; |
||
18 | |||
19 | public class IMUConfiguration extends ConfigurationSet { |
||
20 | protected int correctLengthToExcludeName(int length) { |
||
21 | return length; |
||
22 | } |
||
23 | |||
24 | protected void setName(int[] data, int length) {} |
||
25 | |||
26 | protected void outputName(StringBuilder sb) { |
||
27 | } |
||
28 | |||
29 | public String toXML() { |
||
30 | StringBuilder result = new StringBuilder(); |
||
31 | result.append("<imuconfiguration eepromVersion=\"" + eepromVersion |
||
32 | + "\" length=\"" + getByteCount() + "\">\n"); |
||
33 | for (int i = 0; i < entries.size(); i++) { |
||
34 | ParameterEntry entry = entries.get(i); |
||
35 | entry.toXML(result); |
||
36 | } |
||
37 | result.append("</imuconfiguration>\n"); |
||
38 | return result.toString(); |
||
39 | } |
||
40 | |||
41 | public IMUConfiguration(int eepromVersion) { |
||
42 | super(); |
||
43 | this.eepromVersion = eepromVersion; |
||
44 | } |
||
45 | |||
46 | // This parses only for the purpose of naming and typing parameter sets! |
||
47 | // It does not parse the section and default information, which could be |
||
48 | // useful for GUIs etc. |
||
49 | public static IMUConfiguration parseXMLIMUConfiguration(int version) |
||
50 | throws IOException { |
||
51 | String fileName = "configsets/templates/imuconfiguration_v" + version |
||
52 | + ".xml"; |
||
53 | File f = new File(fileName); |
||
54 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
||
55 | saxfac.setValidating(false); |
||
56 | try { |
||
57 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
58 | Document doc = bldr.parse(f); |
||
59 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
60 | |||
61 | String s_eepromVersion = xpath.evaluate( |
||
62 | "/imuconfigurationtemplate/@eepromVersion", doc); |
||
63 | int eepromVersion = Integer.parseInt(s_eepromVersion); |
||
64 | |||
65 | String s_declaredLength = xpath.evaluate( |
||
66 | "/imuconfigurationtemplate/@length", doc); |
||
67 | int declaredLength = Integer.parseInt(s_declaredLength); |
||
68 | |||
69 | if (eepromVersion != version) { |
||
70 | throw new IOException( |
||
71 | "Version mismatch between file name (" |
||
72 | + fileName |
||
73 | + ") and the version in the imuconfigurationtemplate/@eepromVersion attribute(" |
||
74 | + s_eepromVersion + ")"); |
||
75 | } |
||
76 | |||
77 | IMUConfiguration result = new IMUConfiguration(eepromVersion); |
||
78 | |||
79 | NodeList parameterNodes = (NodeList) xpath.evaluate( |
||
80 | "/imuconfigurationtemplate/parameter", doc, |
||
81 | XPathConstants.NODESET); |
||
82 | |||
83 | for (int j = 0; j < parameterNodes.getLength(); j++) { |
||
84 | Element parameterNode = (Element) parameterNodes.item(j); |
||
85 | |||
86 | ParameterEntry entry; |
||
87 | |||
88 | String s_name = parameterNode.getAttribute("name"); |
||
89 | String s_type; |
||
90 | |||
91 | if (parameterNode.hasAttribute("type")) { |
||
92 | s_type = parameterNode.getAttribute("type"); |
||
93 | } else { |
||
94 | s_type = "static"; |
||
95 | } |
||
96 | |||
97 | if ("static".equals(s_type)) { |
||
98 | entry = new StaticByteEntry(s_name); |
||
99 | } else if ("dynamic".equals(s_type)) { |
||
100 | throw new IOException("Dynamic parameters are not supported in IMU config."); |
||
101 | } else if ("bitset".equals(s_type)) { |
||
102 | NodeList bitNodes = (NodeList) xpath.evaluate("bit", |
||
103 | parameterNode, XPathConstants.NODESET); |
||
104 | String[] bitNames = new String[8]; |
||
105 | for (int k = 0; k < 8; k++) { |
||
106 | Element bitNode = (Element) bitNodes.item(k); |
||
107 | if (bitNode != null) { |
||
108 | bitNames[k] = bitNode.getAttribute("name"); |
||
109 | } else { |
||
110 | bitNames[k] = "Unused"; |
||
111 | } |
||
112 | } |
||
113 | entry = new BitSetEntry(s_name, bitNames); |
||
114 | } else { |
||
115 | throw new IOException("Unknown parameter type: " + s_type); |
||
116 | } |
||
117 | |||
118 | result.entries.add(entry); |
||
119 | } |
||
120 | //result.name = "" + version; |
||
121 | if (result.getByteCount() != declaredLength) { |
||
122 | throw new IOException("The number of parameters in the set (" |
||
123 | + result.getEntries().size() |
||
124 | + ") was not equal to the declared length (" |
||
125 | + declaredLength + ")."); |
||
126 | } |
||
127 | return result; |
||
128 | } catch (IOException ex) { |
||
129 | throw ex; |
||
130 | } catch (XPathExpressionException ex) { |
||
131 | throw new IOException(ex); |
||
132 | } catch (SAXException ex) { |
||
133 | throw new IOException(ex); |
||
134 | } catch (ParserConfigurationException ex) { |
||
135 | throw new IOException(ex); |
||
136 | } |
||
137 | } |
||
138 | } |