Search in sources :

Example 41 with DOMSource

use of javax.xml.transform.dom.DOMSource in project jphp by jphp-compiler.

the class Debugger method write.

protected void write(CommandArguments args, AbstractCommand command) {
    try {
        Document document = xmlBuilder.newDocument();
        command.run(this, args, document);
        StringWriter xmlWriter = new StringWriter();
        StreamResult xmlResult = new StreamResult(xmlWriter);
        transformer.transform(new DOMSource(document), xmlResult);
        byte[] xmlBytes = xmlWriter.toString().getBytes();
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        out.write(String.valueOf(xmlBytes.length).getBytes());
        out.write("\0".getBytes());
        out.write(xmlBytes);
        out.write("\0".getBytes());
        out.flush();
    } catch (IOException | TransformerException e) {
        throw new DebuggerException(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document)

Example 42 with DOMSource

use of javax.xml.transform.dom.DOMSource in project head by mifos.

the class ChartOfAccountsConfig method load.

/**
     * Factory method which loads the Chart of Accounts configuration from the
     * given filename. Given XML filename will be validated against
     * {@link FilePaths#CHART_OF_ACCOUNTS_SCHEMA}.
     *
     * @param chartOfAccountsXml
     *            a relative path to the Chart of Accounts configuration file.
     */
public static ChartOfAccountsConfig load(String chartOfAccountsXml) throws ConfigurationException {
    ChartOfAccountsConfig instance = null;
    Document document = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = dbf.newDocumentBuilder();
        if (FilePaths.CHART_OF_ACCOUNTS_DEFAULT.equals(chartOfAccountsXml)) {
            // default chart of accounts
            document = parser.parse(MifosResourceUtil.getClassPathResourceAsStream(chartOfAccountsXml));
        } else {
            // custom chart of accounts
            document = parser.parse(MifosResourceUtil.getFile(chartOfAccountsXml));
        }
        // create a SchemaFactory capable of understanding XML schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load an XML schema
        ClassPathResource schemaFileResource = new ClassPathResource(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
        Source schemaFile = null;
        if (schemaFileResource.exists()) {
            InputStream in = ChartOfAccountsConfig.class.getClassLoader().getResourceAsStream(FilePaths.CHART_OF_ACCOUNTS_SCHEMA);
            schemaFile = new StreamSource(in);
        } else {
            schemaFile = new StreamSource(MifosResourceUtil.getFile(FilePaths.CHART_OF_ACCOUNTS_SCHEMA));
        }
        Schema schema = factory.newSchema(schemaFile);
        // create a Validator instance and validate document
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (IOException e) {
        throw new ConfigurationException(e);
    } catch (SAXException e) {
        throw new ConfigurationException(e);
    } catch (ParserConfigurationException e) {
        throw new ConfigurationException(e);
    }
    instance = new ChartOfAccountsConfig();
    instance.coaDocument = document;
    return instance;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) Document(org.w3c.dom.Document) ClassPathResource(org.springframework.core.io.ClassPathResource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Validator(javax.xml.validation.Validator)

Example 43 with DOMSource

use of javax.xml.transform.dom.DOMSource in project openblocks by mikaelhg.

the class WorkspaceController method validate.

/**
     * Validates the code blocks document against the schema
     * @param document The document to check
     * @throws RuntimeException If the validation failed
     */
private void validate(Document document) {
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        URL schemaUrl = ClassLoader.getSystemResource("edu/mit/blocks/codeblocks/codeblocks.xsd");
        Schema schema = schemaFactory.newSchema(schemaUrl);
        Validator validator = schema.newValidator();
        validator.validate(new DOMSource(document));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) MalformedURLException(java.net.MalformedURLException) Schema(javax.xml.validation.Schema) IOException(java.io.IOException) URL(java.net.URL) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 44 with DOMSource

use of javax.xml.transform.dom.DOMSource in project openhab1-addons by openhab.

the class Helper method nodeToString.

/***
     * Converts a xml Node into String
     *
     * @param node to convert
     * @return converted string
     */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        logger.warn("nodeToString Transformer Exception", te);
    }
    return sw.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 45 with DOMSource

use of javax.xml.transform.dom.DOMSource in project robovm by robovm.

the class TransformerFactoryImpl method newTemplates.

/**
   * Process the source into a Templates object, which is likely
   * a compiled representation of the source. This Templates object
   * may then be used concurrently across multiple threads.  Creating
   * a Templates object allows the TransformerFactory to do detailed
   * performance optimization of transformation instructions, without
   * penalizing runtime transformation.
   *
   * @param source An object that holds a URL, input stream, etc.
   * @return A Templates object capable of being used for transformation purposes.
   *
   * @throws TransformerConfigurationException May throw this during the parse when it
   *            is constructing the Templates object and fails.
   */
public Templates newTemplates(Source source) throws TransformerConfigurationException {
    String baseID = source.getSystemId();
    if (null != baseID) {
        baseID = SystemIDResolver.getAbsoluteURI(baseID);
    }
    if (source instanceof DOMSource) {
        DOMSource dsource = (DOMSource) source;
        Node node = dsource.getNode();
        if (null != node)
            return processFromNode(node, baseID);
        else {
            String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
            throw new IllegalArgumentException(messageStr);
        }
    }
    TemplatesHandler builder = newTemplatesHandler();
    builder.setSystemId(baseID);
    try {
        InputSource isource = SAXSource.sourceToInputSource(source);
        isource.setSystemId(baseID);
        XMLReader reader = null;
        if (source instanceof SAXSource)
            reader = ((SAXSource) source).getXMLReader();
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (m_isSecureProcessing) {
                    try {
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (org.xml.sax.SAXException se) {
                    }
                }
                javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
                reader = jaxpParser.getXMLReader();
            } catch (javax.xml.parsers.ParserConfigurationException ex) {
                throw new org.xml.sax.SAXException(ex);
            } catch (javax.xml.parsers.FactoryConfigurationError ex1) {
                throw new org.xml.sax.SAXException(ex1.toString());
            } catch (NoSuchMethodError ex2) {
            } catch (AbstractMethodError ame) {
            }
        }
        if (null == reader)
            reader = XMLReaderFactory.createXMLReader();
        // If you set the namespaces to true, we'll end up getting double 
        // xmlns attributes.  Needs to be fixed.  -sb
        // reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        reader.setContentHandler(builder);
        reader.parse(isource);
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(se.getMessage(), se);
        }
    } catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
                return null;
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(e.getMessage(), e);
        }
    }
    return builder.getTemplates();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35