本文整理汇总了Java中javax.xml.transform.sax.SAXTransformerFactory.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java SAXTransformerFactory.newInstance方法的具体用法?Java SAXTransformerFactory.newInstance怎么用?Java SAXTransformerFactory.newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.sax.SAXTransformerFactory
的用法示例。
在下文中一共展示了SAXTransformerFactory.newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertWordDocumentIntoHtml
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Converts a .docx document into HTML markup. This code
* is based on <a href="http://stackoverflow.com/a/9053258/313554">this StackOverflow</a> answer.
*
* @param wordDocument The converted .docx document.
* @return
*/
public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) {
LOGGER.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename());
try {
InputStream input = wordDocument.getInputStream();
Parser parser = new OOXMLParser();
StringWriter sw = new StringWriter();
SAXTransformerFactory factory = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8");
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(new StreamResult(sw));
Metadata metadata = new Metadata();
metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8");
parser.parse(input, handler, metadata, new ParseContext());
return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString());
}
catch (IOException | SAXException | TransformerException | TikaException ex) {
LOGGER.error("Conversion failed because an exception was thrown", ex);
throw new DocumentConversionException(ex.getMessage(), ex);
}
}
示例2: buildSingleTransformerPipeline
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private Result buildSingleTransformerPipeline(Result result)
{
try
{
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
if (result != null)
{
transformerHandler.setResult(result);
}
return new SAXResult(transformerHandler);
}
catch (TransformerConfigurationException ex)
{
throw new FluentXmlConfigurationException(ex);
}
}
示例3: toSAXResult
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private SAXResult toSAXResult(Result result)
{
if (result instanceof SAXResult)
{
return (SAXResult) result;
}
try
{
SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
transformerHandler.setResult(result);
return new SAXResult(transformerHandler);
}
catch (TransformerConfigurationException ex)
{
throw new FluentXmlConfigurationException(ex);
}
}
示例4: test3
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
@Test
public void test3() throws Exception {
SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
Transformer t = sf.newTransformer();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml"));
DOMResult r = new DOMResult();
t.transform(new DOMSource(dom), r);
Assert.assertNotNull(r.getNode());
Node n = r.getNode().getFirstChild();
r.setNode(n);
t.transform(new DOMSource(dom), r);
Assert.assertNotNull(r.getNode());
Assert.assertSame(r.getNode(), n);
r.setNextSibling(r.getNode().getFirstChild());
t.transform(new DOMSource(dom), r);
Assert.assertNotNull(r.getNode());
Assert.assertSame(r.getNode(), n);
}
示例5: SimpleXMLWriter
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Builds a {@link SimpleXMLWriter} for the given display.
*
* @param display
* the display to build the writer for (not null).
* @throws TransformerConfigurationException
* if for some reason the TransformerHandler cannot be created.
*/
public SimpleXMLWriter(final Display display) throws TransformerConfigurationException {
Check.notNull(display, "display"); //$NON-NLS-1$
final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
transformerHandler = transformerFactory.newTransformerHandler();
transformer = transformerHandler.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$
// TODO Reference DTD or schema.
transformerHandler.setResult(new StreamResult(display.getPrintStream()));
}
示例6: JDependProcessorXML2
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Constructs a <code>JDepend</code> instance with the specified writer.
* Creates a new instance of JDependProcessorXML
*
*
* @param writer Writer.
*/
public JDependProcessorXML2(PrintWriter writer) {
super(writer);
formatter = NumberFormat.getInstance(Locale.ENGLISH);
formatter.setMaximumFractionDigits(2);
try {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.INDENT,"yes");
// use getWriter as transform result destination
hd.setResult(new StreamResult(this.getWriter()) );
// create the XmlEmitter
this.xmlEmitter = new XmlEmitter( hd );
} catch (TransformerConfigurationException tce) {
throw new XmlEmitterException(tce);
}
}
示例7: getHtmlFromFile
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Ritorna una vista HTML sul file processato. L'estrazione del testo fa
* perdere di fatto la formattazione contenuta nel word o nel PDF. La
* versione HTML è una versione processabile ma che mantiene il formato
*
* @param file file da parsare
* @return vista HTML formattato del contenuto del documento (privato delle
* immagini)
*/
public String getHtmlFromFile(File file) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
handler.setResult(new StreamResult(out));
ExpandedTitleContentHandler handler1 = new ExpandedTitleContentHandler(handler);
InputStream is = file.toURI().toURL().openStream();
adp.parse(is, handler1, new Metadata());
is.close();
return new String(out.toByteArray(), "UTF-8").replaceAll("<img .*?</img>", "").replaceAll("<img .*?/>", "");
} catch (TransformerConfigurationException | IllegalArgumentException | IOException | SAXException | TikaException ex) {
return "!ERROR: " + ex.getLocalizedMessage();
}
}
示例8: serialize
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Serializes a property list into a Cocoa XML Property List document.
* @param propertyList property list.
* @param file destination.
* @param comments comments to insert into document.
* @throws SAXException if exception during serialization.
* @throws TransformerConfigurationException if exception creating serializer.
*/
public static void serialize(final Map propertyList,
final List comments,
final File file)
throws IOException, SAXException,
TransformerConfigurationException {
SAXTransformerFactory sf = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = sf.newTransformerHandler();
FileOutputStream os = new FileOutputStream(file);
StreamResult result = new StreamResult(os);
handler.setResult(result);
handler.startDocument();
for(Iterator iter = comments.iterator(); iter.hasNext();) {
char[] comment = String.valueOf(iter.next()).toCharArray();
handler.comment(comment, 0, comment.length);
}
AttributesImpl attributes = new AttributesImpl();
handler.startElement(null, "plist", "plist", attributes);
serializeMap(propertyList, handler);
handler.endElement(null, "plist", "plist");
handler.endDocument();
}
示例9: performXslt
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private static void performXslt(TextFileRepos repos, StringBuilder sb) throws TransformerException {
for(TextFileRepo repo : repos.repos){
for(TextFileAndContents xml : repo.getXmlFiles()) {
SAXSource saxSource = new SAXSource(new InputSource( new StringReader(xml.textFromFile) ));
StringWriter writer = new StringWriter();
TransformerFactory factory = SAXTransformerFactory.newInstance();
Transformer transformer;
transformer = factory.newTransformer(new StreamSource(new StringReader(repo.getOnePerFolder().textFromFile)));
transformer.transform(saxSource, new StreamResult(writer));
sb.append("#Repo:" + repo.directory.getName() + " XMl file:" + xml.file.getName() + " XSL file: " + repo.getOnePerFolder().file.getName() + "\n");
sb.append(writer.toString());
sb.append("\n");
}
}
}
示例10: PvXMLWriter
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Creates a new instance of PvXMLWriter
*/
public PvXMLWriter()
{
try
{
factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
handler = factory.newTransformerHandler();
serializer = handler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
} catch (Exception e)
{
e.printStackTrace();
}
}
示例11: testXMLSerialisation
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
@Test
public void testXMLSerialisation() throws SAXException, IOException {
FSManifestIterable it = new FSManifestIterable(bundlerepo);
BundleRepoDescriptor repo = new BundleRepoDescriptor(bundlerepo.toURI(),
ExecutionEnvironmentProfileProvider.getInstance());
repo.populate(it.iterator());
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd;
try {
hd = tf.newTransformerHandler();
} catch (TransformerConfigurationException e) {
throw new BuildException("Sax configuration error: " + e.getMessage(), e);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult stream = new StreamResult(out);
hd.setResult(stream);
OBRXMLWriter.writeManifests(it, hd, false);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
BundleRepoDescriptor repo2 = OBRXMLParser.parse(bundlerepo.toURI(), in);
assertEquals(repo, repo2);
}
示例12: gen
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public String gen(String source, ArrayList<CoveredStatement> entries) throws Exception {
java.io.StringWriter sw = new StringWriter();
//PrintWriter out = new PrintWriter(System.out);
StreamResult streamResult = new StreamResult(sw);
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
//serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd");
//serializer.setOutputProperty(OutputKeys.INDENT, "yes");
hd.setResult(streamResult);
hd.startDocument();
AttributesImpl atts = new AttributesImpl();
hd.startElement("", "", "html", atts);
hd.startElement("", "", "head", atts);
hd.endElement("", "", "head");
hd.startElement("", "", "body", atts);
hd.startElement("", "", "pre", atts);
genTree(entries, hd, source);
hd.endElement("", "", "pre");
hd.endElement("", "", "body");
hd.endElement("", "", "html");
hd.endDocument();
return sw.toString();
}
示例13: downgrade
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
static
public byte[] downgrade(byte[] bytes, Version version) throws IOException, TransformerConfigurationException, SAXException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
SAXTransformerFactory transformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
TransformerHandler transformer = transformerFactory.newTransformerHandler();
transformer.setResult(new StreamResult(result));
ExportFilter exportFilter = new ExportFilter(XMLReaderFactory.createXMLReader(), version);
exportFilter.setContentHandler(transformer);
try(InputStream is = new ByteArrayInputStream(bytes)){
exportFilter.parse(new InputSource(is));
}
return result.toByteArray();
}
示例14: startRenderer
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/** {@inheritDoc} */
public void startRenderer(OutputStream outputStream)
throws IOException {
if (this.handler == null) {
SAXTransformerFactory factory
= (SAXTransformerFactory)SAXTransformerFactory.newInstance();
try {
TransformerHandler transformerHandler = factory.newTransformerHandler();
setContentHandler(transformerHandler);
StreamResult res = new StreamResult(outputStream);
transformerHandler.setResult(res);
} catch (TransformerConfigurationException tce) {
throw new RuntimeException(tce.getMessage());
}
this.out = outputStream;
}
try {
handler.startDocument();
} catch (SAXException saxe) {
handleSAXException(saxe);
}
}
示例15: XmlWriter
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public XmlWriter(PrintWriter out, String encoding) throws TransformerConfigurationException, SAXException {
StreamResult streamResult = new StreamResult(out);
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
transformerHandler = tf.newTransformerHandler();
Transformer serializer = transformerHandler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
transformerHandler.setResult(streamResult);
startedElementStack = new Stack<String>();
transformerHandler.startDocument();
elementAttributes = new AttributesImpl();
Pattern p = Pattern.compile("\\p{Cntrl}");
controlMatcher = p.matcher("");
}