Example usage for javax.xml.transform.stream StreamSource StreamSource

List of usage examples for javax.xml.transform.stream StreamSource StreamSource

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamSource StreamSource.

Prototype

public StreamSource(File f) 

Source Link

Document

Construct a StreamSource from a File.

Usage

From source file:Main.java

public static boolean doesXMLMatchXSD(String xmlUrl) throws SAXException, FileNotFoundException, IOException {

    boolean doesMatchXSD = false;

    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    SAXSource sourceXSD = new SAXSource(new InputSource(new FileInputStream(new File(xsdUrl))));
    Schema schema = factory.newSchema(sourceXSD);
    Validator validator = (Validator) schema.newValidator();
    validator.validate(new StreamSource(new File(xmlUrl)));

    doesMatchXSD = true;//from  w  w w. j  a va2 s .  co  m
    return doesMatchXSD;
}

From source file:Main.java

/**
 * Convert XSLT file to a Source/*from   ww w . ja v  a2  s  .  c  o m*/
 * Note that no actual file access or parsing occurs, so no exceptions are thrown.
 *
 * @param XSLTFile
 * @return Source
 */
public static Source XSLTFileToSource(IPath XSLTFile) {
    return new StreamSource(XSLTFile.toFile());
}

From source file:Main.java

public static String pretty(String xml) {
    try {/*from  w ww .j a va 2 s  .  c o  m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult result = new StreamResult(new StringWriter());
        Source source = new StreamSource(new StringReader(xml));
        transformer.transform(source, result);
        return result.getWriter().toString().replace("\r\n", "\n").replace("\n\r", "\n").replace("\r", "\n");
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Creates an XML Source from the given XML String.
 * @param s//from www .ja v  a2s  . c  o  m
 *      XML String.
 * @return
 *      XML Source.
 */
public static Source source(String s) {
    return new StreamSource(new StringReader(s));
}

From source file:Main.java

public static Schema loadSchema(final InputStream... fromStreams) {
    Source[] sources = new Source[fromStreams.length];
    int i = 0;//w w  w .  j  a  va2  s .  c  om
    for (InputStream stream : fromStreams) {
        sources[i++] = new StreamSource(stream);
    }

    try {
        return SCHEMA_FACTORY.newSchema(sources);
    } catch (SAXException e) {
        throw new IllegalStateException("Failed to instantiate XML schema", e);
    }
}

From source file:Main.java

public static String transform(String xml, String stylesheet) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    transformer.transform(new StreamSource(xml), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

/**
 * Validate xml file using specific schema
 * @param sourceXmlBytes// w ww  .  j  ava2 s.c  om
 *        the byte array reading from xml file
 * @param schemaUrl   
 *         schema url used for validation
 * @return  byte[]
 */
public static void validate(byte[] sourceXmlBytes, URL schemaUrl) throws IOException, SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        Schema schema = factory.newSchema(schemaUrl);
        Validator validator = schema.newValidator();
        Source source = new StreamSource(new ByteArrayInputStream(sourceXmlBytes));
        validator.validate(source);
    } catch (SAXException ex) {
        throw ex;
    } catch (IOException e) {
        throw e;
    }
}

From source file:Main.java

public static String formatXML(String unformatted)
        throws SAXException, IOException, TransformerException, ParserConfigurationException {

    if (unformatted == null)
        return null;

    // remove whitespaces between xml tags
    String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><");

    // Instantiate transformer input
    Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces));
    StreamResult xmlOutput = new StreamResult(new StringWriter());

    // Configure transformer
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    transformer.transform(xmlInput, xmlOutput);
    String formatted = xmlOutput.getWriter().toString();

    return formatted;
}

From source file:Main.java

private static StreamSource asStreamSource(String xmlString) {
    return new StreamSource(new StringReader(xmlString));
}

From source file:Main.java

/**
 * @param xml/*from   w w w  .  j a  v  a  2  s.c o  m*/
 * @return pretty xml
 */
public static String prettyXml(String xml) {
    Transformer serializer;
    try {
        Source source = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();

        serializer = transFactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (Exception e) {
        return xml;
    }
}