Search in sources :

Example 1 with XMLSerializer

use of com.sun.org.apache.xml.internal.serialize.XMLSerializer in project tdi-studio-se by Talend.

the class GenerateSpagoBIXML method createSpagoBIXML.

private void createSpagoBIXML() {
    if (file != null) {
        try {
            Project project = ((RepositoryContext) CorePlugin.getContext().getProperty(Context.REPOSITORY_CONTEXT_KEY)).getProject();
            final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
            fabrique.setValidating(true);
            final DocumentBuilder analyseur = fabrique.newDocumentBuilder();
            analyseur.setErrorHandler(new ErrorHandler() {

                public void error(final SAXParseException exception) throws SAXException {
                    throw exception;
                }

                public void fatalError(final SAXParseException exception) throws SAXException {
                    throw exception;
                }

                public void warning(final SAXParseException exception) throws SAXException {
                    throw exception;
                }
            });
            Document document = analyseur.newDocument();
            //$NON-NLS-1$
            Element spagobi = document.createElement("etl");
            document.appendChild(spagobi);
            // ///////////////////add job element.
            //$NON-NLS-1$
            Element projectElement = document.createElement("job");
            spagobi.appendChild(projectElement);
            //$NON-NLS-1$
            Attr attr = document.createAttribute("project");
            attr.setNodeValue(project.getEmfProject().getLabel());
            projectElement.setAttributeNode(attr);
            //$NON-NLS-1$
            attr = document.createAttribute("jobName");
            attr.setNodeValue(item.getProperty().getLabel());
            projectElement.setAttributeNode(attr);
            //$NON-NLS-1$
            attr = document.createAttribute("context");
            attr.setNodeValue(contextName);
            projectElement.setAttributeNode(attr);
            //$NON-NLS-1$
            attr = document.createAttribute("language");
            attr.setNodeValue(project.getLanguage().getName());
            projectElement.setAttributeNode(attr);
            XMLSerializer serializer = new XMLSerializer();
            OutputFormat outputFormat = new OutputFormat();
            outputFormat.setIndenting(true);
            serializer.setOutputFormat(outputFormat);
            serializer.setOutputCharStream(new java.io.FileWriter(file));
            serializer.serialize(document);
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        }
    }
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) RepositoryContext(org.talend.core.context.RepositoryContext) XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) Project(org.talend.core.model.general.Project) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException)

Example 2 with XMLSerializer

use of com.sun.org.apache.xml.internal.serialize.XMLSerializer in project tdi-studio-se by Talend.

the class ExpressionFileOperation method saveExpressionToFile.

/**
     * yzhang Comment method "savingExpression".
     * 
     * @return
     * @throws IOException
     * @throws ParserConfigurationException
     */
public boolean saveExpressionToFile(File file, List<Variable> variables, String expressionContent) throws IOException, ParserConfigurationException {
    final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
    final Bundle b = Platform.getBundle(PLUGIN_ID);
    final URL url = FileLocator.toFileURL(FileLocator.find(b, new Path(SCHEMA_XSD), null));
    final File schema = new File(url.getPath());
    //$NON-NLS-1$
    fabrique.setAttribute(SCHEMA_LANGUAGE, "http://www.w3.org/2001/XMLSchema");
    fabrique.setAttribute(SCHEMA_VALIDATOR, schema);
    fabrique.setValidating(true);
    final DocumentBuilder analyseur = fabrique.newDocumentBuilder();
    analyseur.setErrorHandler(new ErrorHandler() {

        public void error(final SAXParseException exception) throws SAXException {
            throw exception;
        }

        public void fatalError(final SAXParseException exception) throws SAXException {
            throw exception;
        }

        public void warning(final SAXParseException exception) throws SAXException {
            throw exception;
        }
    });
    Document document = analyseur.newDocument();
    //$NON-NLS-1$
    Element expressionElement = document.createElement("expression");
    document.appendChild(expressionElement);
    //$NON-NLS-1$
    Attr content = document.createAttribute(Messages.getString("ExpressionFileOperation.content"));
    content.setNodeValue(expressionContent);
    expressionElement.setAttributeNode(content);
    for (Variable variable : variables) {
        //$NON-NLS-1$
        Element variableElement = document.createElement("variable");
        expressionElement.appendChild(variableElement);
        //$NON-NLS-1$
        Attr name = document.createAttribute(Messages.getString("ExpressionFileOperation.name"));
        name.setNodeValue(variable.getName());
        variableElement.setAttributeNode(name);
        //$NON-NLS-1$
        Attr value = document.createAttribute(Messages.getString("ExpressionFileOperation.value"));
        value.setNodeValue(variable.getValue());
        variableElement.setAttributeNode(value);
        //$NON-NLS-1$
        Attr talendType = document.createAttribute(Messages.getString("ExpressionFileOperation.talendType"));
        //$NON-NLS-1$
        talendType.setNodeValue(variable.getTalendType());
        variableElement.setAttributeNode(talendType);
        //$NON-NLS-1$
        Attr nullable = document.createAttribute(Messages.getString("ExpressionFileOperation.nullable"));
        //$NON-NLS-1$
        nullable.setNodeValue(String.valueOf(variable.isNullable()));
        variableElement.setAttributeNode(nullable);
    }
    // use specific Xerces class to write DOM-data to a file:
    XMLSerializer serializer = new XMLSerializer();
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setIndenting(true);
    serializer.setOutputFormat(outputFormat);
    FileWriter writer = new FileWriter(file);
    serializer.setOutputCharStream(writer);
    serializer.serialize(document);
    writer.close();
    return true;
}
Also used : Path(org.eclipse.core.runtime.Path) ErrorHandler(org.xml.sax.ErrorHandler) XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) Bundle(org.osgi.framework.Bundle) Element(org.w3c.dom.Element) FileWriter(java.io.FileWriter) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) URL(java.net.URL) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File)

Example 3 with XMLSerializer

use of com.sun.org.apache.xml.internal.serialize.XMLSerializer in project tdi-studio-se by Talend.

the class ExportProjectSettings method saveDocumentByEncoding.

private static void saveDocumentByEncoding(Document document, File file) throws IOException {
    if (document == null || file == null) {
        return;
    }
    XMLSerializer serializer = new XMLSerializer();
    OutputFormat outputFormat = new OutputFormat();
    outputFormat.setIndenting(true);
    serializer.setOutputFormat(outputFormat);
    //$NON-NLS-1$
    OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
    serializer.setOutputCharStream(output);
    serializer.serialize(document);
    output.close();
}
Also used : XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) FileOutputStream(java.io.FileOutputStream) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with XMLSerializer

use of com.sun.org.apache.xml.internal.serialize.XMLSerializer in project jdk8u_jdk by JetBrains.

the class TransformerTest method testBug8162598.

/*
     * @bug 8162598
     * @summary Test XSLTC handling of namespaces, especially empty namespace definitions to reset the
     *          default namespace
     */
@Test
public final void testBug8162598() throws IOException, TransformerException {
    final String LINE_SEPARATOR = System.getProperty("line.separator");
    final String xsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR + "    <xsl:template match=\"/\">" + LINE_SEPARATOR + "        <root xmlns=\"ns1\">" + LINE_SEPARATOR + "            <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR + "        </root>" + LINE_SEPARATOR + "    </xsl:template>" + LINE_SEPARATOR + "    <xsl:template name=\"transform\">" + LINE_SEPARATOR + "        <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR + "        <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR + "        <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR + "        <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR + "        <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR + "        <test6 xmlns=\"\"/>" + LINE_SEPARATOR + "    </xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>";
    final String sourceXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR;
    System.out.println("Stylesheet:");
    System.out.println("=============================");
    System.out.println(xsl);
    System.out.println();
    System.out.println("Source before transformation:");
    System.out.println("=============================");
    System.out.println(sourceXml);
    System.out.println();
    System.out.println("Result after transformation:");
    System.out.println("============================");
    Document document = transformInputStreamToDocument(createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), new ByteArrayInputStream(sourceXml.getBytes()));
    OutputFormat format = new OutputFormat();
    format.setIndenting(true);
    new XMLSerializer(System.out, format).serialize(document);
    System.out.println();
    checkNodeNS8162598(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null);
    checkNodeNS8162598(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null);
    checkNodeNS8162598(document.getElementsByTagName("test3").item(0), null, null, null);
    checkNodeNS8162598(document.getElementsByTagName("test4").item(0), null, null, null);
    checkNodeNS8162598(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null);
    Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(), "unexpected namespace for test6");
}
Also used : XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) Test(org.testng.annotations.Test)

Example 5 with XMLSerializer

use of com.sun.org.apache.xml.internal.serialize.XMLSerializer in project tdi-studio-se by Talend.

the class SoapExecutor method dumpSoapMessage.

/**
     * Dumps the SOAPMessage to String
     * @param msg the SOAP message
     * @return the String representation of the message
     * @throws IOException  IO issue
     * @throws SOAPException SOAP issue
     */
public String dumpSoapMessage(SOAPMessage msg) throws IOException, SOAPException {
    Document doc = msg.getSOAPBody().getOwnerDocument();
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    StringWriter s = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(s, format);
    serializer.serialize(doc);
    return s.toString();
}
Also used : XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) StringWriter(java.io.StringWriter) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document)

Aggregations

OutputFormat (com.sun.org.apache.xml.internal.serialize.OutputFormat)6 XMLSerializer (com.sun.org.apache.xml.internal.serialize.XMLSerializer)6 Document (org.w3c.dom.Document)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 Element (org.w3c.dom.Element)3 ErrorHandler (org.xml.sax.ErrorHandler)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Attr (org.w3c.dom.Attr)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Path (org.eclipse.core.runtime.Path)1 Bundle (org.osgi.framework.Bundle)1