use of com.sun.org.apache.xml.internal.serialize.OutputFormat 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");
}
use of com.sun.org.apache.xml.internal.serialize.OutputFormat 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;
}
use of com.sun.org.apache.xml.internal.serialize.OutputFormat 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();
}
use of com.sun.org.apache.xml.internal.serialize.OutputFormat 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);
}
}
}
use of com.sun.org.apache.xml.internal.serialize.OutputFormat 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();
}
Aggregations