Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package dongfang.mkt.configuration;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ParameterSet
extends ConfigurationSet
{
public static final int NUMBER_OF_VARIABLES =
8;
List<ParameterSection
> declaredSections =
new ArrayList<ParameterSection
>();
protected String name
;
protected int correctLengthToExcludeName
(int length
) {
return length -
12;
}
protected void setName
(int[] data
) {
StringBuilder sbname =
new StringBuilder();
for (int i =
0; i
< 12; i++
) {
if (data
[offset
] ==
0)
break;
sbname.
append((char) data
[offset++
]);
}
name = sbname.
toString();
}
protected void outputName
(StringBuilder sb
) {
sb.
append(name +
"\n");
}
public List<ParameterSection
> getDeclaredSections
() {
return declaredSections
;
}
public String toXML
() {
StringBuilder result =
new StringBuilder();
result.
append("<parameterset eepromVersion=\"" + eepromVersion
+
"\" length=\"" + getByteCount
() +
"\">\n");
for (int i =
0; i
< entries.
size(); i++
) {
ParameterEntry entry = entries.
get(i
);
entry.
toXML(result
);
}
result.
append("</parameterset>\n");
return result.
toString();
}
public ParameterSet
(int eepromVersion
) {
super();
this.
eepromVersion = eepromVersion
;
}
// This parses only for the purpose of naming and typing parameter sets!
// It does not parse the section and default information, which could be
// useful for GUIs etc.
public static ParameterSet parseXMLParameterSet
(int version
)
throws IOException {
String fileName =
"configsets/templates/parameterset_v" + version
+
".xml";
File f =
new File(fileName
);
DocumentBuilderFactory saxfac =
DocumentBuilderFactory.
newInstance();
saxfac.
setValidating(false);
try {
DocumentBuilder bldr = saxfac.
newDocumentBuilder();
Document doc = bldr.
parse(f
);
XPath xpath =
XPathFactory.
newInstance().
newXPath();
String s_eepromVersion = xpath.
evaluate(
"/parametertemplate/@eepromVersion", doc
);
int eepromVersion =
Integer.
parseInt(s_eepromVersion
);
String s_declaredLength = xpath.
evaluate(
"/parametertemplate/@length", doc
);
int declaredLength =
Integer.
parseInt(s_declaredLength
);
if (eepromVersion
!= version
) {
throw new IOException(
"Version mismatch between file name ("
+ fileName
+
") and the version in the parametertemplate/@eepromVersion attribute("
+ s_eepromVersion +
")");
}
ParameterSet result =
new ParameterSet
(eepromVersion
);
NodeList sectionNodes =
(NodeList) xpath.
evaluate(
"/parametertemplate/section", doc,
XPathConstants.
NODESET);
for (int i =
0; i
< sectionNodes.
getLength(); i++
) {
Element sectionNode =
(Element) sectionNodes.
item(i
);
ParameterSection section =
new ParameterSection
(
sectionNode.
getAttribute("name"),
sectionNode.
getAttribute("title"));
result.
declaredSections.
add(section
);
NodeList parameterNodes =
(NodeList) xpath.
evaluate(
"parameter", sectionNode,
XPathConstants.
NODESET);
for (int j =
0; j
< parameterNodes.
getLength(); j++
) {
Element parameterNode =
(Element) parameterNodes.
item(j
);
ParameterEntry entry
;
if (!sectionNode.
hasAttribute("name")) {
throw new IOException("A parameter element (the " + j
+
"th in section "
+ sectionNode.
getAttribute("name")
+
") had no name attribute!");
}
String s_name = parameterNode.
getAttribute("name");
String s_type
;
if (parameterNode.
hasAttribute("type")) {
s_type = parameterNode.
getAttribute("type");
} else {
s_type =
"static";
}
if ("static".
equals(s_type
)) {
entry =
new StaticByteEntry
(s_name
);
} else if ("dynamic".
equals(s_type
)) {
DynamicByteEntry de =
new DynamicByteEntry
(s_name
);
if (parameterNode.
hasAttribute("minValue")) {
de.
setMinValue(Integer.
parseInt(parameterNode
.
getAttribute("minValue")));
}
if (parameterNode.
hasAttribute("maxValue")) {
de.
setMinValue(Integer.
parseInt(parameterNode
.
getAttribute("maxValue")));
}
if (parameterNode.
hasAttribute("staticCodeName")) {
de.
setStaticCodeName(parameterNode
.
getAttribute("staticCodeName"));
} else
de.
setStaticCodeName(de.
getName());
if (parameterNode.
hasAttribute("dynamicCodeName")) {
de.
setDynamicCodeName(parameterNode
.
getAttribute("dynamicCodeName"));
} else
de.
setDynamicCodeName(de.
getName());
entry = de
;
} else if ("bitset".
equals(s_type
)) {
NodeList bitNodes =
(NodeList) xpath.
evaluate("bit",
parameterNode,
XPathConstants.
NODESET);
String[] bitNames =
new String[8];
for (int k =
0; k
< 8; k++
) {
Element bitNode =
(Element) bitNodes.
item(k
);
if (bitNode
!=
null) {
bitNames
[k
] = bitNode.
getAttribute("name");
} else {
bitNames
[k
] =
"Unused";
}
}
entry =
new BitSetEntry
(s_name, bitNames
);
} else {
throw new IOException("Unknown parameter type: "
+ s_type
);
}
result.
entries.
add(entry
);
section.
addConfigEntry(entry
);
}
}
result.
name =
"" + version
;
if (result.
getByteCount() != declaredLength
) {
throw new IOException("The number of parameters in the set ("
+ result.
getEntries().
size()
+
") was not equal to the declared length ("
+ declaredLength +
").");
}
return result
;
} catch (IOException ex
) {
throw ex
;
} catch (XPathExpressionException ex
) {
throw new IOException(ex
);
} catch (SAXException ex
) {
throw new IOException(ex
);
} catch (ParserConfigurationException ex
) {
throw new IOException(ex
);
}
}
}