Subversion Repositories Projects

Rev

Rev 1611 | Details | Compare with Previous | 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
import java.util.ArrayList;
6
import java.util.List;
7
 
8
import javax.xml.parsers.DocumentBuilder;
9
import javax.xml.parsers.DocumentBuilderFactory;
10
import javax.xml.parsers.ParserConfigurationException;
11
import javax.xml.xpath.XPath;
12
import javax.xml.xpath.XPathConstants;
13
import javax.xml.xpath.XPathExpressionException;
14
import javax.xml.xpath.XPathFactory;
15
 
16
import org.w3c.dom.Document;
17
import org.w3c.dom.Element;
18
import org.w3c.dom.NodeList;
19
import org.xml.sax.SAXException;
20
 
21
public class ParameterSet extends ConfigurationSet {
22
        public static final int NUMBER_OF_VARIABLES = 8;
23
        List<ParameterSection> declaredSections = new ArrayList<ParameterSection>();
24
        protected String name;
25
 
26
        protected int correctLengthToExcludeName(int length) {
27
                return length - 12;
28
        }
29
 
1615 - 30
        protected void setName(int[] data, int offset) {
1611 - 31
                StringBuilder sbname = new StringBuilder();
32
                for (int i = 0; i < 12; i++) {
33
                        if (data[offset] == 0)
34
                                break;
35
                        sbname.append((char) data[offset++]);
36
                }
37
                name = sbname.toString();
38
        }
39
 
40
        protected void outputName(StringBuilder sb) {
41
                sb.append(name + "\n");
42
        }
43
 
44
        public List<ParameterSection> getDeclaredSections() {
45
                return declaredSections;
46
        }
47
 
48
        public String toXML() {
49
                StringBuilder result = new StringBuilder();
50
                result.append("<parameterset eepromVersion=\"" + eepromVersion
51
                                + "\" length=\"" + getByteCount() + "\">\n");
52
                for (int i = 0; i < entries.size(); i++) {
53
                        ParameterEntry entry = entries.get(i);
54
                        entry.toXML(result);
55
                }
56
                result.append("</parameterset>\n");
57
                return result.toString();
58
        }
59
 
60
        public ParameterSet(int eepromVersion) {
61
                super();
62
                this.eepromVersion = eepromVersion;
63
        }
64
 
65
        // This parses only for the purpose of naming and typing parameter sets!
66
        // It does not parse the section and default information, which could be
67
        // useful for GUIs etc.
68
        public static ParameterSet parseXMLParameterSet(int version)
69
                        throws IOException {
70
                String fileName = "configsets/templates/parameterset_v" + version
71
                                + ".xml";
72
                File f = new File(fileName);
73
                DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance();
74
                saxfac.setValidating(false);
75
                try {
76
                        DocumentBuilder bldr = saxfac.newDocumentBuilder();
77
                        Document doc = bldr.parse(f);
78
                        XPath xpath = XPathFactory.newInstance().newXPath();
79
 
80
                        String s_eepromVersion = xpath.evaluate(
81
                                        "/parametertemplate/@eepromVersion", doc);
82
                        int eepromVersion = Integer.parseInt(s_eepromVersion);
83
 
84
                        String s_declaredLength = xpath.evaluate(
85
                                        "/parametertemplate/@length", doc);
86
                        int declaredLength = Integer.parseInt(s_declaredLength);
87
 
88
                        if (eepromVersion != version) {
89
                                throw new IOException(
90
                                                "Version mismatch between file name ("
91
                                                                + fileName
92
                                                                + ") and the version in the parametertemplate/@eepromVersion attribute("
93
                                                                + s_eepromVersion + ")");
94
                        }
95
 
96
                        ParameterSet result = new ParameterSet(eepromVersion);
97
 
98
                        NodeList sectionNodes = (NodeList) xpath.evaluate(
99
                                        "/parametertemplate/section", doc, XPathConstants.NODESET);
100
 
101
                        for (int i = 0; i < sectionNodes.getLength(); i++) {
102
                                Element sectionNode = (Element) sectionNodes.item(i);
103
                                ParameterSection section = new ParameterSection(
104
                                                sectionNode.getAttribute("name"),
105
                                                sectionNode.getAttribute("title"));
106
                                result.declaredSections.add(section);
107
 
108
                                NodeList parameterNodes = (NodeList) xpath.evaluate(
109
                                                "parameter", sectionNode, XPathConstants.NODESET);
110
 
111
                                for (int j = 0; j < parameterNodes.getLength(); j++) {
112
                                        Element parameterNode = (Element) parameterNodes.item(j);
113
 
114
                                        ParameterEntry entry;
115
 
116
                                        if (!sectionNode.hasAttribute("name")) {
117
                                                throw new IOException("A parameter element (the " + j
118
                                                                + "th in section "
119
                                                                + sectionNode.getAttribute("name")
120
                                                                + ") had no name attribute!");
121
                                        }
122
 
123
                                        String s_name = parameterNode.getAttribute("name");
124
                                        String s_type;
125
 
126
                                        if (parameterNode.hasAttribute("type")) {
127
                                                s_type = parameterNode.getAttribute("type");
128
                                        } else {
129
                                                s_type = "static";
130
                                        }
131
 
132
                                        if ("static".equals(s_type)) {
133
                                                entry = new StaticByteEntry(s_name);
134
                                        } else if ("dynamic".equals(s_type)) {
135
                                                DynamicByteEntry de = new DynamicByteEntry(s_name);
136
                                                if (parameterNode.hasAttribute("minValue")) {
137
                                                        de.setMinValue(Integer.parseInt(parameterNode
138
                                                                        .getAttribute("minValue")));
139
                                                }
140
                                                if (parameterNode.hasAttribute("maxValue")) {
141
                                                        de.setMinValue(Integer.parseInt(parameterNode
142
                                                                        .getAttribute("maxValue")));
143
                                                }
144
                                                if (parameterNode.hasAttribute("staticCodeName")) {
145
                                                        de.setStaticCodeName(parameterNode
146
                                                                        .getAttribute("staticCodeName"));
147
                                                } else
148
                                                        de.setStaticCodeName(de.getName());
149
                                                if (parameterNode.hasAttribute("dynamicCodeName")) {
150
                                                        de.setDynamicCodeName(parameterNode
151
                                                                        .getAttribute("dynamicCodeName"));
152
                                                } else
153
                                                        de.setDynamicCodeName(de.getName());
154
                                                entry = de;
155
                                        } else if ("bitset".equals(s_type)) {
156
                                                NodeList bitNodes = (NodeList) xpath.evaluate("bit",
157
                                                                parameterNode, XPathConstants.NODESET);
158
                                                String[] bitNames = new String[8];
159
                                                for (int k = 0; k < 8; k++) {
160
                                                        Element bitNode = (Element) bitNodes.item(k);
161
                                                        if (bitNode != null) {
162
                                                                bitNames[k] = bitNode.getAttribute("name");
163
                                                        } else {
164
                                                                bitNames[k] = "Unused";
165
                                                        }
166
                                                }
167
                                                entry = new BitSetEntry(s_name, bitNames);
168
                                        } else {
169
                                                throw new IOException("Unknown parameter type: "
170
                                                                + s_type);
171
                                        }
172
 
173
                                        result.entries.add(entry);
174
                                        section.addConfigEntry(entry);
175
                                }
176
                        }
177
                        result.name = "" + version;
178
                        if (result.getByteCount() != declaredLength) {
179
                                throw new IOException("The number of parameters in the set ("
180
                                                + result.getEntries().size()
181
                                                + ") was not equal to the declared length ("
182
                                                + declaredLength + ").");
183
                        }
184
                        return result;
185
                } catch (IOException ex) {
186
                        throw ex;
187
                } catch (XPathExpressionException ex) {
188
                        throw new IOException(ex);
189
                } catch (SAXException ex) {
190
                        throw new IOException(ex);
191
                } catch (ParserConfigurationException ex) {
192
                        throw new IOException(ex);
193
                }
194
        }
195
}