001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.util.xsltimpl;
021:
022: import java.io.StringReader;
023: import java.io.Writer;
024:
025: import javax.xml.transform.Source;
026: import javax.xml.transform.Templates;
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerConfigurationException;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.sax.SAXSource;
031:
032: import org.xml.sax.InputSource;
033:
034: import com.icl.saxon.Controller;
035: import com.icl.saxon.PreparedStyleSheet;
036: import com.icl.saxon.TransformerFactoryImpl;
037:
038: import de.schlund.pfixxml.util.Xml;
039: import de.schlund.pfixxml.util.XsltSupport;
040:
041: /**
042: * @author mleidig@schlund.de
043: */
044: public class XsltSaxon1 implements XsltSupport {
045:
046: private TransformerFactory ifactory = new TransformerFactoryImpl();
047:
048: private static final ThreadLocal<TransformerFactory> threadfactory = new ThreadLocal<TransformerFactory>();
049:
050: // pretty-print script by M. Kay, see
051: // http://www.cafeconleche.org/books/xmljava/chapters/ch17s02.html#d0e32721
052: private static final String ID = "<?xml version='1.0'?>"
053: + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.1'>"
054: + " <xsl:output method='xml' indent='yes'/>"
055: + " <xsl:strip-space elements='*'/>"
056: + " <xsl:template match='/'>"
057: + " <xsl:copy-of select='.'/>" + " </xsl:template>"
058: + "</xsl:stylesheet>";
059:
060: private static Templates PP_XSLT;
061:
062: static {
063: Source src = new SAXSource(Xml.createXMLReader(),
064: new InputSource(new StringReader(ID)));
065: TransformerFactory factory = new TransformerFactoryImpl();
066: try {
067: PP_XSLT = factory.newTemplates(src);
068: } catch (TransformerConfigurationException e) {
069: throw new RuntimeException(e);
070: }
071: }
072:
073: public TransformerFactory getSharedTransformerFactory() {
074: return ifactory;
075: }
076:
077: public TransformerFactory getThreadTransformerFactory() {
078: TransformerFactory factory = threadfactory.get();
079: if (factory == null) {
080: // Create a new factory. As we have to use a new URIResolver
081: // for each transformation, we cannot reuse the same factory
082: // in other threads
083: factory = new TransformerFactoryImpl();
084: threadfactory.set(factory);
085: }
086: return factory;
087: }
088:
089: public Templates getPrettyPrinterTemplates() {
090: return PP_XSLT;
091: }
092:
093: public boolean isInternalTemplate(Templates templates) {
094: return templates instanceof PreparedStyleSheet;
095: }
096:
097: public void doTracing(Transformer transformer, Writer traceWriter) {
098: Saxon1TraceListener tl = new Saxon1TraceListener(
099: Saxon1TraceListener.Format.VERBOSE, traceWriter);
100: Controller c = (Controller) transformer;
101: c.setTraceListener(tl);
102: c.setLineNumbering(true);
103: }
104:
105: }
|