Search in sources :

Example 36 with SAXSource

use of javax.xml.transform.sax.SAXSource in project asterixdb by apache.

the class TestSuiteParser method parse.

public org.apache.asterix.testframework.xml.TestSuite parse(File testSuiteCatalog) throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setXIncludeAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "file");
    JAXBContext ctx = JAXBContext.newInstance(org.apache.asterix.testframework.xml.TestSuite.class);
    Unmarshaller um = ctx.createUnmarshaller();
    return (org.apache.asterix.testframework.xml.TestSuite) um.unmarshal(new SAXSource(saxParser.getXMLReader(), new InputSource(testSuiteCatalog.toURI().toString())));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 37 with SAXSource

use of javax.xml.transform.sax.SAXSource in project geode by apache.

the class ManagedEntityConfigXmlGenerator method generate.

/////////////////////// Instance Methods ///////////////////////
/**
   * Generates XML and writes it to the given <code>PrintWriter</code>
   */
private void generate(PrintWriter pw) {
    // XML text
    try {
        Source src = new SAXSource(this, new InputSource());
        Result res = new StreamResult(pw);
        TransformerFactory xFactory = TransformerFactory.newInstance();
        Transformer xform = xFactory.newTransformer();
        xform.setOutputProperty(OutputKeys.METHOD, "xml");
        xform.setOutputProperty(OutputKeys.INDENT, "yes");
        xform.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, SYSTEM_ID);
        xform.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, PUBLIC_ID);
        xform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xform.transform(src, res);
        pw.flush();
    } catch (Exception ex) {
        RuntimeException ex2 = new RuntimeException(LocalizedStrings.ManagedEntityConfigXmlGenerator_EXCEPTION_THROWN_WHILE_GENERATING_XML.toLocalizedString());
        ex2.initCause(ex);
        throw ex2;
    }
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult)

Example 38 with SAXSource

use of javax.xml.transform.sax.SAXSource in project karaf by apache.

the class JaxbUtil method unmarshalNoValidate.

private static Features unmarshalNoValidate(String uri, InputStream stream) {
    try {
        Unmarshaller unmarshaller = FEATURES_CONTEXT.createUnmarshaller();
        NoSourceAndNamespaceFilter xmlFilter = new NoSourceAndNamespaceFilter(XmlUtils.xmlReader());
        xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
        InputSource is = new InputSource(uri);
        if (stream != null) {
            is.setByteStream(stream);
        }
        SAXSource source = new SAXSource(xmlFilter, is);
        Features features = (Features) unmarshaller.unmarshal(source);
        features.setNamespace(xmlFilter.getNamespace());
        return features;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("Unable to load " + uri, e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Unmarshaller(javax.xml.bind.Unmarshaller) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Example 39 with SAXSource

use of javax.xml.transform.sax.SAXSource in project sling by apache.

the class XmlUtil method prettyPrint.

public static String prettyPrint(InputSource is) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        // initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        Source source = new SAXSource(is);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (Exception e) {
        // Catch generic error as panel should still work if xml apis are
        // not
        // resolved
        log.warn("Error occurred while transforming xml", e);
    } finally {
        Util.close(is);
    }
    return "Source not found";
}
Also used : Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Source(javax.xml.transform.Source)

Example 40 with SAXSource

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

the class BaseXsltDataUpgraderTest method loadSchema.

private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
    Assert.notEmpty(resources, "No resources given");
    Assert.hasLength(schemaLanguage, "No schema language provided");
    Source[] schemaSources = new Source[resources.length];
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    for (int i = 0; i < resources.length; i++) {
        Assert.notNull(resources[i], "Resource is null");
        Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
        InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
        schemaSources[i] = new SAXSource(xmlReader, inputSource);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
    return schemaFactory.newSchema(schemaSources);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader)

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