Blame |
Last modification |
View Log
| RSS feed
package dongfang.mkt.configuration;
import java.io.File;
import java.io.IOException;
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 IMUConfiguration
extends ConfigurationSet
{
protected int correctLengthToExcludeName
(int length
) {
return length
;
}
protected void setName
(int[] data,
int length
) {}
protected void outputName
(StringBuilder sb
) {
}
public String toXML
() {
StringBuilder result =
new StringBuilder();
result.
append("<imuconfiguration eepromVersion=\"" + eepromVersion
+
"\" length=\"" + getByteCount
() +
"\">\n");
for (int i =
0; i
< entries.
size(); i++
) {
ParameterEntry entry = entries.
get(i
);
entry.
toXML(result
);
}
result.
append("</imuconfiguration>\n");
return result.
toString();
}
public IMUConfiguration
(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 IMUConfiguration parseXMLIMUConfiguration
(int version
)
throws IOException {
String fileName =
"configsets/templates/imuconfiguration_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(
"/imuconfigurationtemplate/@eepromVersion", doc
);
int eepromVersion =
Integer.
parseInt(s_eepromVersion
);
String s_declaredLength = xpath.
evaluate(
"/imuconfigurationtemplate/@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 imuconfigurationtemplate/@eepromVersion attribute("
+ s_eepromVersion +
")");
}
IMUConfiguration result =
new IMUConfiguration
(eepromVersion
);
NodeList parameterNodes =
(NodeList) xpath.
evaluate(
"/imuconfigurationtemplate/parameter", doc,
XPathConstants.
NODESET);
for (int j =
0; j
< parameterNodes.
getLength(); j++
) {
Element parameterNode =
(Element) parameterNodes.
item(j
);
ParameterEntry entry
;
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
)) {
throw new IOException("Dynamic parameters are not supported in IMU config.");
} 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
);
}
//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
);
}
}
}