001: package net.sf.saxon;
002:
003: import net.sf.saxon.event.CommentStripper;
004: import net.sf.saxon.event.ReceivingContentHandler;
005: import net.sf.saxon.event.StartTagBuffer;
006: import net.sf.saxon.style.StyleNodeFactory;
007: import net.sf.saxon.style.StylesheetStripper;
008: import net.sf.saxon.style.UseWhenFilter;
009: import net.sf.saxon.trans.XPathException;
010: import net.sf.saxon.tree.DocumentImpl;
011: import net.sf.saxon.tree.TreeBuilder;
012: import org.xml.sax.Locator;
013:
014: import javax.xml.transform.Templates;
015: import javax.xml.transform.sax.TemplatesHandler;
016:
017: /**
018: * <b>TemplatesHandlerImpl</b> implements the javax.xml.transform.sax.TemplatesHandler
019: * interface. It acts as a ContentHandler which receives a stream of
020: * SAX events representing a stylesheet, and returns a Templates object that
021: * represents the compiled form of this stylesheet.
022: * @author Michael H. Kay
023: */
024:
025: public class TemplatesHandlerImpl extends ReceivingContentHandler
026: implements TemplatesHandler {
027:
028: private TreeBuilder builder;
029: private StyleNodeFactory nodeFactory;
030: private Templates templates;
031: private String systemId;
032:
033: /**
034: * Create a TemplatesHandlerImpl and initialise variables. The constructor is protected, because
035: * the Filter should be created using newTemplatesHandler() in the SAXTransformerFactory
036: * class
037: */
038:
039: protected TemplatesHandlerImpl(Configuration config) {
040:
041: setPipelineConfiguration(config.makePipelineConfiguration());
042:
043: nodeFactory = new StyleNodeFactory(config);
044:
045: builder = new TreeBuilder();
046: builder.setPipelineConfiguration(getPipelineConfiguration());
047: builder.setNodeFactory(nodeFactory);
048: builder.setLineNumbering(true);
049:
050: StartTagBuffer startTagBuffer = new StartTagBuffer();
051:
052: UseWhenFilter useWhenFilter = new UseWhenFilter(startTagBuffer);
053: useWhenFilter.setUnderlyingReceiver(builder);
054: useWhenFilter
055: .setPipelineConfiguration(getPipelineConfiguration());
056:
057: startTagBuffer.setUnderlyingReceiver(useWhenFilter);
058: startTagBuffer
059: .setPipelineConfiguration(getPipelineConfiguration());
060:
061: StylesheetStripper styleStripper = new StylesheetStripper();
062: styleStripper.setStylesheetRules(config.getNamePool());
063: styleStripper.setUnderlyingReceiver(startTagBuffer);
064: styleStripper
065: .setPipelineConfiguration(getPipelineConfiguration());
066:
067: CommentStripper commentStripper = new CommentStripper();
068: commentStripper.setUnderlyingReceiver(styleStripper);
069: commentStripper
070: .setPipelineConfiguration(getPipelineConfiguration());
071:
072: this .setReceiver(commentStripper);
073:
074: }
075:
076: /**
077: * Get the Templates object to used for a transformation
078: */
079:
080: public Templates getTemplates() {
081: if (templates == null) {
082: DocumentImpl doc = (DocumentImpl) builder.getCurrentRoot();
083: if (doc == null) {
084: return null;
085: }
086: PreparedStylesheet sheet = new PreparedStylesheet(
087: getConfiguration());
088: try {
089: sheet.setStylesheetDocument(doc, nodeFactory);
090: templates = sheet;
091: } catch (XPathException tce) {
092: // don't know why we aren't allowed to just throw it!
093: throw new UnsupportedOperationException(tce
094: .getMessage());
095: }
096: }
097:
098: return templates;
099: }
100:
101: /**
102: * Set the SystemId of the document
103: */
104:
105: public void setSystemId(String url) {
106: systemId = url;
107: builder.setSystemId(url);
108: }
109:
110: /**
111: * Callback interface for SAX: not for application use
112: */
113:
114: public void setDocumentLocator(Locator locator) {
115: super .setDocumentLocator(locator);
116: setSystemId(locator.getSystemId());
117: }
118:
119: /**
120: * Get the systemId of the document
121: */
122:
123: public String getSystemId() {
124: return systemId;
125: }
126:
127: }
128:
129: //
130: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
131: // you may not use this file except in compliance with the License. You may obtain a copy of the
132: // License at http://www.mozilla.org/MPL/
133: //
134: // Software distributed under the License is distributed on an "AS IS" basis,
135: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
136: // See the License for the specific language governing rights and limitations under the License.
137: //
138: // The Original Code is: all this file.
139: //
140: // The Initial Developer of the Original Code is Michael H. Kay.
141: //
142: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
143: //
144: // Contributor(s): None
145: //
|