本文整理汇总了Java中javax.xml.transform.TransformerFactory类的典型用法代码示例。如果您正苦于以下问题:Java TransformerFactory类的具体用法?Java TransformerFactory怎么用?Java TransformerFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransformerFactory类属于javax.xml.transform包,在下文中一共展示了TransformerFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRedirect
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* @bug 8165116
* Verifies that redirect works properly when extension function is enabled
*
* @param xml the XML source
* @param xsl the stylesheet that redirect output to a file
* @param output the output file
* @param redirect the redirect file
* @throws Exception if the test fails
**/
@Test(dataProvider = "redirect")
public void testRedirect(String xml, String xsl, String output, String redirect) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
Transformer t = tf.newTransformer(new StreamSource(new StringReader(xsl)));
//Transform the xml
tryRunWithTmpPermission(
() -> t.transform(new StreamSource(new StringReader(xml)), new StreamResult(new StringWriter())),
new FilePermission(output, "write"), new FilePermission(redirect, "write"));
// Verifies that the output is redirected successfully
String userDir = getSystemProperty("user.dir");
Path pathOutput = Paths.get(userDir, output);
Path pathRedirect = Paths.get(userDir, redirect);
Assert.assertTrue(Files.exists(pathOutput));
Assert.assertTrue(Files.exists(pathRedirect));
System.out.println("Output to " + pathOutput + " successful.");
System.out.println("Redirect to " + pathRedirect + " successful.");
Files.deleteIfExists(pathOutput);
Files.deleteIfExists(pathRedirect);
}
示例2: writeOut
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
private static void writeOut(Document doc) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
DOMSource source = new DOMSource(doc);
File f = new File("splFile.xml");
f.delete();
StreamResult result = new StreamResult(f);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
}
示例3: testStAXSource2
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
@Test
public final void testStAXSource2() throws XMLStreamException {
XMLInputFactory ifactory = XMLInputFactory.newInstance();
ifactory.setProperty("javax.xml.stream.supportDTD", Boolean.TRUE);
StAXSource ss = new StAXSource(ifactory.createXMLStreamReader(getClass().getResource("5368141.xml").toString(),
getClass().getResourceAsStream("5368141.xml")));
DOMResult dr = new DOMResult();
TransformerFactory tfactory = TransformerFactory.newInstance();
try {
Transformer transformer = tfactory.newTransformer();
transformer.transform(ss, dr);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
示例4: doTransform
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
private void doTransform(String sXSL, OutputStream os) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException {
// create the transformerfactory & transformer instance
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
if (null == sXSL) {
t.transform(new DOMSource(doc), new StreamResult(os));
return;
}
try {
Transformer finalTransformer = tf.newTransformer(new StreamSource(sXSL));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
t.transform(new DOMSource(doc), new StreamResult(bos));
bos.flush();
StreamSource xmlSource = new StreamSource(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())));
finalTransformer.transform(xmlSource, new StreamResult(os));
} catch (IOException ioe) {
throw new javax.xml.transform.TransformerException(ioe);
}
}
示例5: xmlFormat
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* xml 格式化
*
* @param xml
* @return
*/
public static String xmlFormat(String xml) {
if (TextUtils.isEmpty(xml)) {
return "Empty/Null xml content";
}
String message;
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
message = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (TransformerException e) {
message = xml;
}
return message;
}
示例6: childNodeToString
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* Converts the Node/Element instance to XML payload.
*
* @param node the node/element instance to convert
* @return the XML payload representing the node/element
* @throws AlipayApiException problem converting XML to string
*/
public static String childNodeToString(Node node) throws AlipayApiException {
String payload = null;
try {
Transformer tf = TransformerFactory.newInstance().newTransformer();
Properties props = tf.getOutputProperties();
props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
tf.setOutputProperties(props);
StringWriter writer = new StringWriter();
tf.transform(new DOMSource(node), new StreamResult(writer));
payload = writer.toString();
payload = payload.replaceAll(REG_INVALID_CHARS, " ");
} catch (TransformerException e) {
throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
}
return payload;
}
示例7: writeWebXml
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* Writes the modified web.xml back out to war file
*
* @param doc
* The application.xml DOM Document
* @throws org.apache.tools.ant.DeployException
* in case of any problems
*/
protected void writeWebXml(final Document doc, final OutputStream outputStream) throws DeployException {
try {
doc.normalize();
// Prepare the DOM document for writing
DOMSource source = new DOMSource(doc);
// Prepare the output file
StreamResult result = new StreamResult(outputStream);
// Write the DOM document to the file
// Get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
// Write to a file
xformer.transform(source, result);
} catch (TransformerException tex) {
throw new DeployException("Error writing out modified web xml ", tex);
}
}
示例8: write
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
public static void write(Document doc, OutputStream out) throws IOException {
// XXX note that this may fail to write out namespaces correctly if the document
// is created with namespaces and no explicit prefixes; however no code in
// this package is likely to be doing so
try {
Transformer t = TransformerFactory.newInstance().newTransformer(
new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
DocumentType dt = doc.getDoctype();
if (dt != null) {
String pub = dt.getPublicId();
if (pub != null) {
t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
}
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
}
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
Source source = new DOMSource(doc);
Result result = new StreamResult(out);
t.transform(source, result);
} catch (Exception | TransformerFactoryConfigurationError e) {
throw new IOException(e);
}
}
示例9: PomModifier
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
public PomModifier(final Path projectDirectory, final Path gitDirectory) {
if (builderFactory == null) {
builderFactory = DocumentBuilderFactory.newInstance();
transformerFactory = TransformerFactory.newInstance();
try {
builder = builderFactory.newDocumentBuilder();
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
} catch (ParserConfigurationException | TransformerConfigurationException e) {
throw new IllegalStateException(e);
}
}
this.projectPomFile = gitDirectory.resolve("pom.xml");
this.projectDirectory = projectDirectory;
this.gitDirectory = gitDirectory;
}
示例10: testCheckElementContentWhitespace
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* Test for the isIgnoringElementContentWhitespace and the
* setIgnoringElementContentWhitespace. The xml file has all kinds of
* whitespace,tab and newline characters, it uses the MyNSContentHandler
* which does not invoke the characters callback when this
* setIgnoringElementContentWhitespace is set to true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckElementContentWhitespace() throws Exception {
String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
String outputFile = USER_DIR + "dbfactory02.out";
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
assertFalse(dbf.isIgnoringElementContentWhitespace());
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
assertFalse(eh.isErrorOccured());
DOMSource domSource = new DOMSource(doc);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
SAXResult saxResult = new SAXResult();
try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
saxResult.setHandler(handler);
transformer.transform(domSource, saxResult);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例11: readMode
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
public static String readMode(FileObject fo) throws IOException {
final InputStream is = fo.getInputStream();
try {
StringWriter w = new StringWriter();
Source t = new StreamSource(DesignSupport.class.getResourceAsStream("polishing.xsl")); // NOI18N
Transformer tr = TransformerFactory.newInstance().newTransformer(t);
Source s = new StreamSource(is);
Result r = new StreamResult(w);
tr.transform(s, r);
return w.toString();
} catch (TransformerException ex) {
throw new IOException(ex);
} finally {
is.close();
}
}
示例12: makeVerificationResult
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* Builds a {@link VerificationResult} from a GeTeTa {@link Message}.
*
* @param source the original top-level XML node of the verification result
* @param importedMessage the JAXB-converted GeTeTa {@link Message} object
* @return the imported result
* @throws ImportException if an error occurs while importing
*/
private VerificationResult makeVerificationResult(Node source, Message importedMessage)
throws ImportException {
try {
/* Write log to file */
File logFile = File.createTempFile("log-verification-", ".xml");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(source);
StreamResult result = new StreamResult(logFile);
transformer.transform(domSource, result);
/* Return appropriate VerificationResult */
switch (importedMessage.getReturncode()) {
case RETURN_CODE_SUCCESS:
return new VerificationSuccess(logFile);
case RETURN_CODE_NOT_VERIFIED:
return new edu.kit.iti.formal.stvs.model.verification.Counterexample(
parseCounterexample(importedMessage), logFile);
default:
return new VerificationError(VerificationError.Reason.ERROR, logFile);
}
} catch (TransformerException | IOException exception) {
throw new ImportException(exception);
}
}
示例13: xmlToHtml
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* Transforms the XML content to XHTML/HTML format string with the XSL.
*
* @param payload the XML payload to convert
* @param xsltFile the XML stylesheet file
* @return the transformed XHTML/HTML format string
* @throws AlipayApiException problem converting XML to HTML
*/
public static String xmlToHtml(String payload, File xsltFile)
throws AlipayApiException {
String result = null;
try {
Source template = new StreamSource(xsltFile);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(template);
Properties props = transformer.getOutputProperties();
props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
transformer.setOutputProperties(props);
StreamSource source = new StreamSource(new StringReader(payload));
StreamResult sr = new StreamResult(new StringWriter());
transformer.transform(source, sr);
result = sr.getWriter().toString();
} catch (TransformerException e) {
throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
}
return result;
}
示例14: marshalSamlXmlObject
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
/**
* Marshal the saml xml object to raw xml.
*
* @param object the object
* @param writer the writer
* @return the xml string
*/
public String marshalSamlXmlObject(final XMLObject object, final StringWriter writer) {
try {
final MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
final Marshaller marshaller = marshallerFactory.getMarshaller(object);
if (marshaller == null) {
throw new IllegalArgumentException("Cannot obtain marshaller for object " + object.getElementQName());
}
final Element element = marshaller.marshall(object);
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", SAMLConstants.SAML20_NS);
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#");
final TransformerFactory transFactory = TransformerFactory.newInstance();
final Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(element), new StreamResult(writer));
return writer.toString();
} catch (final Exception e) {
throw new IllegalStateException("An error has occurred while marshalling SAML object to xml", e);
}
}
示例15: domSourceToString
import javax.xml.transform.TransformerFactory; //导入依赖的package包/类
protected String domSourceToString() throws SQLException {
try {
DOMSource source = new DOMSource(this.asDOMResult.getNode());
Transformer identity = TransformerFactory.newInstance().newTransformer();
StringWriter stringOut = new StringWriter();
Result result = new StreamResult(stringOut);
identity.transform(source, result);
return stringOut.toString();
} catch (Throwable t) {
SQLException sqlEx = SQLError.createSQLException(t.getMessage(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
sqlEx.initCause(t);
throw sqlEx;
}
}