use of org.firstinspires.ftc.robotcore.internal.system.Dom2XmlPullBuilder in project robotcode by OutoftheBoxFTC.
the class RobotConfigFileManager method getRobotConfigDescriptionTransform.
/**
* Returns a {@link Source} to the XSLT transformation that will transform robot config
* templates/configurations into human-readable descriptions.
*/
@NonNull
protected Source getRobotConfigDescriptionTransform() throws XmlPullParserException, IOException, TransformerConfigurationException, TransformerException {
// Load RobotConfigTaxonomy as a DOM
Reader xmlConfigTaxonomyReader = new InputStreamReader(context.getAssets().open(ROBOT_CONFIG_TAXONOMY_XML));
XmlPullParser xmlConfigTaxonomyParser = ReadXMLFileHandler.xmlPullParserFromReader(xmlConfigTaxonomyReader);
Dom2XmlPullBuilder builder = new Dom2XmlPullBuilder();
Element rootElement = builder.parseSubTree(xmlConfigTaxonomyParser);
Document document = rootElement.getOwnerDocument();
// Augment the DOM to add <Sensor>s corresponding to @I2cSensor elements that might be present
for (UserConfigurationType userConfigurationType : UserConfigurationTypeManager.getInstance().allUserTypes(UserConfigurationType.Flavor.I2C)) {
//
UserI2cSensorType userI2cSensorType = (UserI2cSensorType) userConfigurationType;
Element sensor = document.createElement("Sensor");
addChild(document, sensor, "XmlTag", userI2cSensorType.getXmlTag());
addChild(document, sensor, "Description", userI2cSensorType.getDescription());
addChild(document, sensor, "Bus", "i2cbus");
addChild(document, sensor, "BusDefault", context.getString(R.string.userSensorTypeBusDefault));
//
rootElement.appendChild(sensor);
}
// Turn that augmented taxonomy into a source
Source sourceConfigTaxonomy = new DOMSource(rootElement);
// Load the transform that will generate the transform we need
Source xsltGenerate = new StreamSource(context.getAssets().open(ROBOT_CONFIG_DESCRIPTION_GENERATE_XSLT));
// Transform the taxonomy to generate the description transformer
StringWriter xsltDescriptionWriter = new StringWriter();
Result transformerResult = new StreamResult(xsltDescriptionWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xsltGenerate);
transformer.transform(sourceConfigTaxonomy, transformerResult);
String xsltDescriptionTransform = xsltDescriptionWriter.toString().trim();
// Return that as a Source
StringReader xsltDescriptionTransformReader = new StringReader(xsltDescriptionTransform);
Source result = new StreamSource(xsltDescriptionTransformReader);
return result;
}
use of org.firstinspires.ftc.robotcore.internal.system.Dom2XmlPullBuilder in project robotcode by OutoftheBoxFTC.
the class RobotConfigFileManager method getSourceFromPullParser.
protected Source getSourceFromPullParser(@NonNull XmlPullParser xpp) throws XmlPullParserException, IOException {
Dom2XmlPullBuilder builder = new Dom2XmlPullBuilder();
Element rootElement = builder.parseSubTree(xpp);
return new DOMSource(rootElement);
}
Aggregations