Search in sources :

Example 51 with Source

use of javax.xml.transform.Source in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testSchema.

@Test
public void testSchema() throws Exception {
    String xmlFileName = "hazelcast-client-discovery-spi-test.xml";
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = ClientDiscoverySpiTest.class.getClassLoader().getResource("hazelcast-client-config-3.9.xsd");
    Schema schema = factory.newSchema(schemaResource);
    InputStream xmlResource = ClientDiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    Source source = new StreamSource(xmlResource);
    Validator validator = schema.newValidator();
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 52 with Source

use of javax.xml.transform.Source in project hazelcast by hazelcast.

the class ConfigXmlGenerator method format.

private String format(final String input, int indent) {
    if (!formatted) {
        return input;
    }
    StreamResult xmlOutput = null;
    try {
        final Source xmlInput = new StreamSource(new StringReader(input));
        xmlOutput = new StreamResult(new StringWriter());
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        /* Older versions of Xalan still use this method of setting indent values.
            * Attempt to make this work but don't completely fail if it's a problem.
            */
        try {
            transformerFactory.setAttribute("indent-number", indent);
        } catch (IllegalArgumentException e) {
            if (LOGGER.isFinestEnabled()) {
                LOGGER.finest("Failed to set indent-number attribute; cause: " + e.getMessage());
            }
        }
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        /* Newer versions of Xalan will look for a fully-qualified output property in order to specify amount of
            * indentation to use.  Attempt to make this work as well but again don't completely fail if it's a problem.
            */
        try {
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
        } catch (IllegalArgumentException e) {
            if (LOGGER.isFinestEnabled()) {
                LOGGER.finest("Failed to set indent-amount property; cause: " + e.getMessage());
            }
        }
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        LOGGER.warning(e);
        return input;
    } finally {
        if (xmlOutput != null) {
            closeResource(xmlOutput.getWriter());
        }
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 53 with Source

use of javax.xml.transform.Source in project gephi by gephi.

the class LayoutPresetPersistence method savePreset.

public void savePreset(String name, Layout layout) {
    Preset preset = addPreset(new Preset(name, layout));
    FileOutputStream fos = null;
    try {
        //Create file if dont exist
        FileObject folder = FileUtil.getConfigFile("layoutpresets");
        if (folder == null) {
            folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
        }
        File presetFile = new File(FileUtil.toFile(folder), name + ".xml");
        //Create doc
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();
        document.setXmlVersion("1.0");
        document.setXmlStandalone(true);
        //Write doc
        preset.writeXML(document);
        //Write XML file
        fos = new FileOutputStream(presetFile);
        Source source = new DOMSource(document);
        Result result = new StreamResult(fos);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
            }
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 54 with Source

use of javax.xml.transform.Source in project gephi by gephi.

the class ExporterGraphML method transform.

private void transform(Document document) throws TransformerConfigurationException, TransformerException {
    Source source = new DOMSource(document);
    Result result = new StreamResult(writer);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 55 with Source

use of javax.xml.transform.Source in project j2objc by google.

the class ProcessorInclude method startElement.

/**
   * Receive notification of the start of an xsl:include element.
   *
   * @param handler The calling StylesheetHandler/TemplatesBuilder.
   * @param uri The Namespace URI, or the empty string if the
   *        element has no Namespace URI or if Namespace
   *        processing is not being performed.
   * @param localName The local name (without prefix), or the
   *        empty string if Namespace processing is not being
   *        performed.
   * @param rawName The raw XML 1.0 name (with prefix), or the
   *        empty string if raw names are not available.
   * @param attributes The attributes attached to the element.  If
   *        there are no attributes, it shall be an empty
   *        Attributes object.
   *
   * @throws org.xml.sax.SAXException Any SAX exception, possibly
   *            wrapping another exception.
   */
public void startElement(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
    setPropertiesFromAttributes(handler, rawName, attributes, this);
    try {
        // Get the Source from the user's URIResolver (if any).
        Source sourceFromURIResolver = getSourceFromUriResolver(handler);
        // Get the system ID of the included/imported stylesheet module
        String hrefUrl = getBaseURIOfIncludedStylesheet(handler, sourceFromURIResolver);
        if (handler.importStackContains(hrefUrl)) {
            throw new org.xml.sax.SAXException(XSLMessages.createMessage(getStylesheetInclErr(), //"(StylesheetHandler) "+hrefUrl+" is directly or indirectly importing itself!");
            new Object[] { hrefUrl }));
        }
        // Push the system ID and corresponding Source
        // on some stacks for later retrieval during parse() time.
        handler.pushImportURL(hrefUrl);
        handler.pushImportSource(sourceFromURIResolver);
        int savedStylesheetType = handler.getStylesheetType();
        handler.setStylesheetType(this.getStylesheetType());
        handler.pushNewNamespaceSupport();
        try {
            parse(handler, uri, localName, rawName, attributes);
        } finally {
            handler.setStylesheetType(savedStylesheetType);
            handler.popImportURL();
            handler.popImportSource();
            handler.popNamespaceSupport();
        }
    } catch (TransformerException te) {
        handler.error(te.getMessage(), te);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26