Rev 1688 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1688 | - | 1 | package dongfang.mkt.configuration; |
2 | |||
3 | import java.io.IOException; |
||
4 | import java.io.InputStream; |
||
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 MotorMixer { |
||
1689 | - | 20 | public static final int EEPROMVERSION = 1; |
1688 | - | 21 | |
22 | public static final int MAX_MOTORS = 12; |
||
23 | public static final int MIX_PITCH = 0; |
||
24 | public static final int MIX_ROLL = 1; |
||
25 | public static final int MIX_THROTTLE = 2; |
||
26 | public static final int MIX_YAW = 3; |
||
27 | public static final int MIX_OPPOSITE_MOTOR = 4; |
||
28 | |||
29 | void rowToXML(int[] row, StringBuilder out) { |
||
1689 | - | 30 | out.append("<motor throttlePart=\"" + row[MIX_THROTTLE] + "\" pitchPart=\"" + row[MIX_PITCH] + "\" rollPart=\"" + row[MIX_ROLL] + "\" yawPart=\"" + row[MIX_YAW] + "\" oppositeMotor=\"" + row[MIX_OPPOSITE_MOTOR] + "\" />"); |
1688 | - | 31 | } |
1689 | - | 32 | |
33 | private String name; |
||
34 | |||
1688 | - | 35 | private int[][] matrix = new int[MAX_MOTORS][]; |
36 | |||
37 | public void setMatrix(int[][] matrix) { |
||
38 | this.matrix = matrix; |
||
39 | } |
||
40 | |||
1689 | - | 41 | public String getName() { |
42 | return name; |
||
43 | } |
||
44 | |||
45 | public void setName(String name) { |
||
46 | this.name = name; |
||
47 | } |
||
48 | |||
1688 | - | 49 | public int[][] toBinary() { |
50 | return matrix; |
||
51 | } |
||
52 | |||
53 | public String toXML() { |
||
54 | StringBuilder result = new StringBuilder(); |
||
1689 | - | 55 | result.append("<motorMixer eepromVersion=\"" + EEPROMVERSION + "\" name=\"" + name + "\">\n"); |
1688 | - | 56 | for (int i = 0; i < MAX_MOTORS; i++) { |
57 | int[] row = matrix[i]; |
||
58 | result.append(" "); |
||
59 | this.rowToXML(row, result); |
||
60 | result.append("\n"); |
||
61 | } |
||
62 | result.append("</motorMixer>\n"); |
||
63 | return result.toString(); |
||
64 | } |
||
65 | |||
1689 | - | 66 | public void parseXML(InputStream input) throws IOException { |
1688 | - | 67 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
68 | saxfac.setValidating(false); |
||
69 | try { |
||
70 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
71 | Document doc = bldr.parse(input); |
||
72 | |||
73 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
74 | String s_eepromVersion = xpath.evaluate("/motorMixer/@eepromVersion", doc); |
||
75 | int eepromVersion; |
||
76 | |||
77 | try { |
||
78 | eepromVersion = Integer.parseInt(s_eepromVersion); |
||
79 | } catch (NumberFormatException ex) { |
||
80 | System.err |
||
81 | .println("The motorMixer element must have an 'eepromVersion' attribute with a numerical value (eg.<motorMixer eepromVersion='1'>)"); |
||
82 | System.exit(-1); |
||
83 | } |
||
84 | |||
1689 | - | 85 | this.name = xpath.evaluate("/motorMixer/@name", doc); |
86 | |||
1688 | - | 87 | NodeList rowNodes = (NodeList) xpath.evaluate( |
88 | "/motorMixer/motor", doc, XPathConstants.NODESET); |
||
89 | |||
90 | for (int i = 0; i < rowNodes.getLength(); i++) { |
||
91 | Element e = (Element) rowNodes.item(i); |
||
92 | int[] row = new int[5]; |
||
93 | |||
1689 | - | 94 | String s_value = e.getAttribute("throttlePart"); |
1688 | - | 95 | row[MIX_THROTTLE] = Integer.parseInt(s_value); |
96 | |||
1689 | - | 97 | s_value = e.getAttribute("pitchPart"); |
1688 | - | 98 | row[MIX_PITCH] = Integer.parseInt(s_value); |
99 | |||
1689 | - | 100 | s_value = e.getAttribute("rollPart"); |
1688 | - | 101 | row[MIX_ROLL] = Integer.parseInt(s_value); |
102 | |||
1689 | - | 103 | s_value = e.getAttribute("yawPart"); |
1688 | - | 104 | row[MIX_YAW] = Integer.parseInt(s_value); |
105 | |||
106 | s_value = e.getAttribute("oppositeMotor"); |
||
107 | row[MIX_OPPOSITE_MOTOR] = Integer.parseInt(s_value); |
||
108 | |||
109 | matrix[i] = row; |
||
110 | } |
||
111 | input.close(); |
||
112 | } catch (ParserConfigurationException ex) { |
||
113 | // Should never happen. |
||
114 | throw new RuntimeException(ex); |
||
115 | } catch (SAXException ex) { |
||
116 | System.err.println(ex); |
||
117 | System.err |
||
118 | .println("There is something screwed with your XML document. It is not well-formed and won't parse."); |
||
119 | throw new RuntimeException("Parse error."); |
||
120 | } catch (XPathExpressionException ex) { |
||
121 | // Should never happen. |
||
122 | throw new RuntimeException(ex); |
||
123 | } |
||
124 | } |
||
125 | } |