Search in sources :

Example 1 with Dom2XmlPullBuilder

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) InputStreamReader(java.io.InputStreamReader) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) XmlPullParser(org.xmlpull.v1.XmlPullParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StringWriter(java.io.StringWriter) Dom2XmlPullBuilder(org.firstinspires.ftc.robotcore.internal.system.Dom2XmlPullBuilder) StringReader(java.io.StringReader) UserConfigurationType(com.qualcomm.robotcore.hardware.configuration.UserConfigurationType) UserI2cSensorType(com.qualcomm.robotcore.hardware.configuration.UserI2cSensorType) NonNull(android.support.annotation.NonNull)

Example 2 with Dom2XmlPullBuilder

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);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Dom2XmlPullBuilder(org.firstinspires.ftc.robotcore.internal.system.Dom2XmlPullBuilder) Element(org.w3c.dom.Element)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)2 Dom2XmlPullBuilder (org.firstinspires.ftc.robotcore.internal.system.Dom2XmlPullBuilder)2 Element (org.w3c.dom.Element)2 NonNull (android.support.annotation.NonNull)1 UserConfigurationType (com.qualcomm.robotcore.hardware.configuration.UserConfigurationType)1 UserI2cSensorType (com.qualcomm.robotcore.hardware.configuration.UserI2cSensorType)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Result (javax.xml.transform.Result)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Document (org.w3c.dom.Document)1 XmlPullParser (org.xmlpull.v1.XmlPullParser)1