Search in sources :

Example 91 with SAXSource

use of javax.xml.transform.sax.SAXSource in project opennms by OpenNMS.

the class RrdConvertUtils method dumpRrd.

/**
     * Dumps a RRD.
     *
     * @param sourceFile the source file
     * @return the RRD Object
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws RrdException the RRD exception
     */
public static RRDv3 dumpRrd(File sourceFile) throws IOException, RrdException {
    String rrdBinary = System.getProperty("rrd.binary");
    if (rrdBinary == null) {
        throw new IllegalArgumentException("rrd.binary property must be set");
    }
    try {
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        Process process = Runtime.getRuntime().exec(new String[] { rrdBinary, "dump", sourceFile.getAbsolutePath() });
        SAXSource source = new SAXSource(xmlReader, new InputSource(new InputStreamReader(process.getInputStream())));
        JAXBContext jc = JAXBContext.newInstance(RRDv3.class);
        Unmarshaller u = jc.createUnmarshaller();
        return (RRDv3) u.unmarshal(source);
    } catch (Exception e) {
        throw new RrdException("Can't parse RRD Dump", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) InputStreamReader(java.io.InputStreamReader) RRDv3(org.opennms.netmgt.rrd.model.v3.RRDv3) JAXBContext(javax.xml.bind.JAXBContext) RrdException(org.jrobin.core.RrdException) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) IOException(java.io.IOException) RrdException(org.jrobin.core.RrdException)

Example 92 with SAXSource

use of javax.xml.transform.sax.SAXSource in project uPortal by Jasig.

the class SpELDataTemplatingStrategy method processTemplates.

@Override
public Source processTemplates(Document data, String filename) {
    log.trace("Processing templates for document XML={}", data.asXML());
    for (String xpath : XPATH_EXPRESSIONS) {
        @SuppressWarnings("unchecked") List<Node> nodes = data.selectNodes(xpath);
        for (Node n : nodes) {
            String inpt, otpt;
            switch(n.getNodeType()) {
                case org.w3c.dom.Node.ATTRIBUTE_NODE:
                    Attribute a = (Attribute) n;
                    inpt = a.getValue();
                    otpt = processText(inpt);
                    if (otpt == null) {
                        throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename);
                    }
                    if (!otpt.equals(inpt)) {
                        a.setValue(otpt);
                    }
                    break;
                case org.w3c.dom.Node.TEXT_NODE:
                case org.w3c.dom.Node.CDATA_SECTION_NODE:
                    inpt = n.getText();
                    otpt = processText(inpt);
                    if (otpt == null) {
                        throw new RuntimeException("Invalid expression '" + inpt + "' in file " + filename);
                    }
                    if (!otpt.equals(inpt)) {
                        n.setText(otpt);
                    }
                    break;
                default:
                    String msg = "Unsupported node type:  " + n.getNodeTypeName();
                    throw new RuntimeException(msg);
            }
        }
    }
    final SAXSource rslt = new DocumentSource(data);
    // must be set, else import chokes
    rslt.setSystemId(filename);
    return rslt;
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) Attribute(org.dom4j.Attribute) DocumentSource(org.dom4j.io.DocumentSource) Node(org.dom4j.Node)

Example 93 with SAXSource

use of javax.xml.transform.sax.SAXSource in project jdk8u_jdk by JetBrains.

the class ValidationWarningsTest method doOneTestIteration.

//One iteration of xml validation test case. It will be called from each
//TestWorker task defined in WarningsTestBase class.
void doOneTestIteration() throws Exception {
    Source src = new StreamSource(new StringReader(xml));
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    SAXSource xsdSource = new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes())));
    Schema schema = schemaFactory.newSchema(xsdSource);
    Validator v = schema.newValidator();
    v.validate(src);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) StringReader(java.io.StringReader) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Example 94 with SAXSource

use of javax.xml.transform.sax.SAXSource in project jdk8u_jdk by JetBrains.

the class XSLTExFuncTest method transform.

void transform(TransformerFactory factory) throws TransformerConfigurationException, TransformerException {
    SAXSource xslSource = new SAXSource(new InputSource(xslFile));
    xslSource.setSystemId(xslFileId);
    Transformer transformer = factory.newTransformer(xslSource);
    StringWriter stringResult = new StringWriter();
    Result result = new StreamResult(stringResult);
    transformer.transform(new SAXSource(new InputSource(xmlFile)), result);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult)

Example 95 with SAXSource

use of javax.xml.transform.sax.SAXSource in project voltdb by VoltDB.

the class JDBCSQLXML method createSAXSource.

/**
     * Retrieves a new SAXSource for reading the XML value designated by this
     * SQLXML instance. <p>
     *
     * @param sourceClass The class of the source
     * @throws java.sql.SQLException if there is an error processing the XML
     *      value or if the given <tt>sourceClass</tt> is not supported.
     * @return a new SAXSource for reading the XML value designated by this
     *      SQLXML instance
     */
@SuppressWarnings("unchecked")
protected <T extends Source> T createSAXSource(Class<T> sourceClass) throws SQLException {
    SAXSource source = null;
    try {
        source = (sourceClass == null) ? new SAXSource() : (SAXSource) sourceClass.newInstance();
    } catch (SecurityException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (ClassCastException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    Reader reader = getCharacterStreamImpl();
    InputSource inputSource = new InputSource(reader);
    source.setInputSource(inputSource);
    return (T) source;
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) XMLEventReader(javax.xml.stream.XMLEventReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10