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 net.sf.saxon.Configuration;
033: import net.sf.saxon.PreparedStylesheet;
034: import net.sf.saxon.TransformerFactoryImpl;
035:
036: import org.xml.sax.InputSource;
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 XsltSaxon2 implements XsltSupport {
045:
046: private static Configuration config = new Configuration();
047:
048: private TransformerFactory ifactory = new TransformerFactoryImpl(
049: config);
050:
051: private static final ThreadLocal<TransformerFactory> threadfactory = new ThreadLocal<TransformerFactory>();
052:
053: //TODO: replace by XSLT 2.0 version
054: // pretty-print script by M. Kay, see
055: // http://www.cafeconleche.org/books/xmljava/chapters/ch17s02.html#d0e32721
056: private static final String ID = "<?xml version='1.0'?>"
057: + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.1'>"
058: + " <xsl:output method='xml' indent='yes'/>"
059: + " <xsl:strip-space elements='*'/>"
060: + " <xsl:template match='/'>"
061: + " <xsl:copy-of select='.'/>" + " </xsl:template>"
062: + "</xsl:stylesheet>";
063:
064: private static Templates PP_XSLT;
065:
066: static {
067: Source src = new SAXSource(Xml.createXMLReader(),
068: new InputSource(new StringReader(ID)));
069: TransformerFactory factory = new TransformerFactoryImpl(config);
070: try {
071: PP_XSLT = factory.newTemplates(src);
072: } catch (TransformerConfigurationException e) {
073: throw new RuntimeException(e);
074: }
075: }
076:
077: public TransformerFactory getSharedTransformerFactory() {
078: return ifactory;
079: }
080:
081: public TransformerFactory getThreadTransformerFactory() {
082: TransformerFactory factory = threadfactory.get();
083: if (factory == null) {
084: // Create a new factory. As we have to use a new URIResolver
085: // for each transformation, we cannot reuse the same factory
086: // in other threads
087: factory = new TransformerFactoryImpl(config);
088: threadfactory.set(factory);
089: }
090: return factory;
091: }
092:
093: public Templates getPrettyPrinterTemplates() {
094: return PP_XSLT;
095: }
096:
097: public boolean isInternalTemplate(Templates templates) {
098: return templates instanceof PreparedStylesheet;
099: }
100:
101: public void doTracing(Transformer transformer, Writer traceWriter) {
102: //TODO: implementation
103: }
104:
105: }
|