本文整理匯總了Java中javax.xml.transform.sax.TransformerHandler.getTransformer方法的典型用法代碼示例。如果您正苦於以下問題:Java TransformerHandler.getTransformer方法的具體用法?Java TransformerHandler.getTransformer怎麽用?Java TransformerHandler.getTransformer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.transform.sax.TransformerHandler
的用法示例。
在下文中一共展示了TransformerHandler.getTransformer方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createTransformerHandler
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
public static TransformerHandler createTransformerHandler(final ErrorListener errorListener,
final boolean indentOutput) throws TransformerConfigurationException {
final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactoryFactory.newInstance();
if (errorListener != null) {
stf.setErrorListener(errorListener);
}
final TransformerHandler th = stf.newTransformerHandler();
final Transformer transformer = th.getTransformer();
setCommonOutputProperties(transformer, indentOutput);
if (errorListener != null) {
transformer.setErrorListener(errorListener);
}
return th;
}
示例2: JDependProcessorXML2
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的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);
}
}
示例3: newRAFilter
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
@Override
public TransformerHandler newRAFilter(double min, double max) throws TransformerConfigurationException, SAXException, ParserConfigurationException,
IOException {
TransformerHandler retval = getTransformerHandler(getRAXsl());
Transformer xformer = retval.getTransformer();
xformer.setParameter("ra_min_hr", String.valueOf(min));
xformer.setParameter("ra_max_hr", String.valueOf(max));
return retval;
}
示例4: newDECFilter
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
@Override
public TransformerHandler newDECFilter(double min, double max) throws TransformerConfigurationException, SAXException, ParserConfigurationException,
IOException {
TransformerHandler retval = getTransformerHandler(getDECXsl());
Transformer xformer = retval.getTransformer();
xformer.setParameter("dec_min_deg", String.valueOf(min));
xformer.setParameter("dec_max_deg", String.valueOf(max));
return retval;
}
示例5: newRADECFilter
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
@Override
public TransformerHandler newRADECFilter(double rmin, double rmax, double dmin, double dmax) throws TransformerConfigurationException, SAXException,
ParserConfigurationException, IOException {
TransformerHandler retval = getTransformerHandler(getRADECXsl());
Transformer xformer = retval.getTransformer();
xformer.setParameter("ra_min_hr", String.valueOf(rmin));
xformer.setParameter("ra_max_hr", String.valueOf(rmax));
xformer.setParameter("dec_min_deg", String.valueOf(dmin));
xformer.setParameter("dec_max_deg", String.valueOf(dmax));
return retval;
}
示例6: newStellarTypeFilter
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
@Override
public TransformerHandler newStellarTypeFilter(String type) throws TransformerConfigurationException, SAXException, ParserConfigurationException,
IOException {
TransformerHandler retval = getTransformerHandler(getStellarXsl());
Transformer xformer = retval.getTransformer();
xformer.setParameter("type", type);
return retval;
}
示例7: getTransformHandlerList
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
protected LinkedList<TransformerHandler> getTransformHandlerList(MCRParameterCollector parameterCollector)
throws TransformerConfigurationException, SAXException {
checkTemplateUptodate();
LinkedList<TransformerHandler> xslSteps = new LinkedList<>();
//every transformhandler shares the same ErrorListener instance
MCRErrorListener errorListener = MCRErrorListener.getInstance();
for (Templates template : templates) {
TransformerHandler handler = tFactory.newTransformerHandler(template);
parameterCollector.setParametersTo(handler.getTransformer());
handler.getTransformer().setErrorListener(errorListener);
if (TRACE_LISTENER_ENABLED) {
TransformerImpl transformer = (TransformerImpl) handler.getTransformer();
TraceManager traceManager = transformer.getTraceManager();
try {
traceManager.addTraceListener(TRACE_LISTENER);
} catch (TooManyListenersException e) {
LOGGER.warn("Could not add MCRTraceListener.", e);
}
}
if (!xslSteps.isEmpty()) {
Result result = new SAXResult(handler);
xslSteps.getLast().setResult(result);
}
xslSteps.add(handler);
}
return xslSteps;
}
示例8: formatFile
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
private void formatFile(final Path in, final Path out) {
try {
final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactoryFactory.newInstance();
final TransformerHandler transformerHandler = stf.newTransformerHandler();
final Transformer serializer = transformerHandler.getTransformer();
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
transformerHandler.setResult(new StreamResult(Files.newOutputStream(out)));
final SAXParser parser = PARSER_FACTORY.newSAXParser();
final WhiteSpaceStripper stripper = new WhiteSpaceStripper();
stripper.setLexicalHandler(transformerHandler);
stripper.setContentHandler(transformerHandler);
final XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(stripper);
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", stripper);
// Do some final replacement on some text.
String xsd = new String(Files.readAllBytes(in));
// Strip out empty annotations.
xsd = xsd.replaceAll("<xs:annotation\\s*/>", "");
xmlReader.parse(new InputSource(new StringReader(xsd)));
} catch (final Exception e) {
System.out.println("Error processing file: " + in.toAbsolutePath().toString());
e.printStackTrace();
}
}
示例9: outputAll
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
private void outputAll(Jar ajar[]) throws Exception {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
Transformer serializer = hd.getTransformer();
serializer.setOutputProperty(OutputKeys.INDENT,"yes");
hd.setResult(new StreamResult(this.writer));
XmlEmitter xmlEmitter = new XmlEmitter( hd, ajar );
xmlEmitter.emit();
this.writer.flush();
this.writer.close();
}
示例10: prepareTh
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
/**
* Prepare TH instance for work This involves setting TrAX parameters and all
* other stuff if needed
*
* @param th
* @param params
*/
protected void prepareTh (final TransformerHandler th, final Hashtable <String, Object> params)
{
if (CSTX.DEBUG)
log.debug ("prepareTh()");
final Transformer tr = th.getTransformer ();
// make sure old parameters are cleaned
tr.clearParameters ();
// set transformation parameters
if (!params.isEmpty ())
{
for (final Enumeration <String> e = params.keys (); e.hasMoreElements ();)
{
final String key = e.nextElement ();
if (CSTX.DEBUG)
log.debug ("prepareTh(): set parameter " + key + "=" + params.get (key));
if (!key.startsWith (tmp_TRAX_ATTR_NS) && !key.startsWith (tmp_FILTER_ATTR_NS))
{
// ordinary parameter, set it to the TrAX object
tr.setParameter (key, params.get (key));
}
}
}
}
示例11: createTransformerHandler
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
/**
* Creates a {@link TransformerHandler}.
*
* @param commentHeader the comment header
* @param rootTag the root tag
* @param streamResult stream result
*/
public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag,
StreamResult streamResult, Charset charset)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
try {
tf.setAttribute("indent-number", new Integer(2));
} catch (Exception e) {
// ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510
}
TransformerHandler transformerHandler = tf.newTransformerHandler();
Transformer serializer = transformerHandler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, charset.name());
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
transformerHandler.setResult(streamResult);
transformerHandler.startDocument();
String newline = System.getProperty("line.separator");
if (newline == null) {
newline = "\n";
}
commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " ");
transformerHandler.characters("\n".toCharArray(), 0, 1);
transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length);
transformerHandler.characters("\n".toCharArray(), 0, 1);
if (rootTag.length() > 0) {
transformerHandler.startElement("", "", rootTag, null);
}
return transformerHandler;
}
示例12: openWriter
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
public void openWriter()
throws SAXException {
LOGGER.debug("openWriter()");
if (xmlWriter != null) {
throw new SAXException("Writer still open");
} else {
if (metaData == null) {
throw new SAXException("Attempt to start writing XML without processing meta data to indicate which feed the data is for " + processFilePath);
}
String feed = metaData.get(StroomHeaderArguments.FEED);
if (feed == null) {
throw new StroomStreamException(StroomStatusCode.FEED_MUST_BE_SPECIFIED);
}
filePartCount ++;
try {
StroomZipOutputStream stroomZipOutputStream = feedToStroomZipOutputStreamMap.get(feed);
if (stroomZipOutputStream == null) {
stroomZipOutputStream = stroomZipRepository.getStroomZipOutputStream();
LOGGER.debug("openWriter() - %s - Opened %s for feed %s", processFilePath, stroomZipOutputStream.getFinalFile(),feed);
feedToStroomZipOutputStreamMap.put(feed, stroomZipOutputStream);
fileCount ++;
}
// Close off last entry?
OutputStream outputStream = feedToOutputStreamMap.get(feed);
if (outputStream != null) {
outputStream.close();
}
final SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory
.newInstance();
final TransformerHandler th = stf.newTransformerHandler();
serializer = th.getTransformer();
serializer.setOutputProperty(OutputKeys.METHOD, XML);
serializer.setOutputProperty(OutputKeys.ENCODING, UTF_8);
serializer.setOutputProperty(OutputKeys.INDENT, YES);
metaData.write(stroomZipOutputStream.addEntry(new StroomZipEntry(null, String.valueOf(filePartCount), StroomZipFileType.Meta)), true);
OutputStream dataStream = stroomZipOutputStream.addEntry(new StroomZipEntry(null, String.valueOf(filePartCount), StroomZipFileType.Data));
feedToOutputStreamMap.put(feed, dataStream);
th.setResult(new StreamResult(dataStream));
xmlWriter = th;
xmlWriter.startDocument();
for (Map.Entry<String, String> prefix : startPrefixMapping.entrySet()) {
xmlWriter.startPrefixMapping(prefix.getKey(), prefix.getValue());
}
xmlWriter.startElement(startElementUri, startElementLocalName, startElementQName, startElementAttributes);
} catch (TransformerConfigurationException sax) {
throw new SAXException(sax);
} catch (IOException ioEx) {
throw new SAXException(ioEx);
}
}
}
示例13: createXml
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
@Override
public void createXml(List<User> list, String fileName) throws Exception {
Result resultXml = new StreamResult(new FileOutputStream(fileName)); // 輸出到person.xml
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
TransformerHandler th = sff.newTransformerHandler();
th.setResult(resultXml);
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 編碼格式是UTF-8
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 換行
// 開始xml文檔
th.startDocument();
th.startElement("", "", "users", null); // 定義users節點
for (User user : list) {
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "", "id", "", String.valueOf(user.getId()));
th.startElement("", "", "user", attrs); // 定義user節點
// 定義name節點
th.startElement("", "", "name", null);
th.characters(user.getName().toCharArray(), 0, user.getName()
.length());
th.endElement("", "", "name");
// 定義age節點
th.startElement("", "", "age", null);
th.characters(String.valueOf(user.getAge()).toCharArray(), 0,
String.valueOf(user.getAge()).toCharArray().length);
th.endElement("", "", "age");
// 定義sex節點
th.startElement("", "", "sex", null);
th.characters(user.getSex().toCharArray(), 0, user.getSex()
.length());
th.endElement("", "", "sex");
th.endElement("", "", "user"); // 結束user節點
}
th.endElement("", "", "users"); // 結束users節點
// 結束xml文檔
th.endDocument();
}
示例14: generate
import javax.xml.transform.sax.TransformerHandler; //導入方法依賴的package包/類
public void generate(Values values) throws IOException {
File dimenFile = values.dimenFile;
FileOutputStream fos = new FileOutputStream(dimenFile);
StreamResult result = new StreamResult(fos);
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler th = null;
try {
th = sff.newTransformerHandler();
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(result);
th.startDocument();
AttributesImpl attr = new AttributesImpl();
char[] space = "\n".toCharArray();
th.characters(space, 0, space.length);
th.startElement("", "", "resources", attr);
List<Dimen> dimens = values.dimens;
char[] spaceChar = "\n ".toCharArray();
for (Dimen dimen : dimens) {
//white space
th.characters(spaceChar, 0, spaceChar.length);
//name attr
attr.addAttribute("", "", "name", String.class.getName(), dimen.name);
th.startElement("", "", "dimen", attr);
char[] valueChars = String.format("%spx", dimen.value).toCharArray();
th.characters(valueChars, 0, valueChars.length);
th.endElement("", "", "dimen");
}
th.endElement("", "", "resources");
th.endDocument();
} catch (TransformerConfigurationException | SAXException e) {
e.printStackTrace();
} finally {
fos.close();
}
}