KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > LogicsheetCodeGenerator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.language.markup;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.cocoon.components.source.SourceUtil;
22 import org.apache.cocoon.xml.AbstractXMLPipe;
23 import org.apache.cocoon.util.TraxErrorHandler;
24 import org.apache.excalibur.source.Source;
25
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 import javax.xml.transform.OutputKeys JavaDoc;
30 import javax.xml.transform.TransformerConfigurationException JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.sax.SAXResult JavaDoc;
33 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
34 import javax.xml.transform.sax.TransformerHandler JavaDoc;
35 import javax.xml.transform.stream.StreamResult JavaDoc;
36 import java.io.StringWriter JavaDoc;
37 import java.util.Properties JavaDoc;
38
39 /**
40  * A logicsheet-based implementation of <code>MarkupCodeGenerator</code>
41  *
42  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
43  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
44  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
45  * @version CVS $Id: LogicsheetCodeGenerator.java 30932 2004-07-29 17:35:38Z vgritsenko $
46  */

47 public class LogicsheetCodeGenerator extends AbstractLogEnabled implements MarkupCodeGenerator {
48
49     private ContentHandler JavaDoc serializerContentHandler;
50
51     private AbstractXMLPipe end;
52
53     private TransformerHandler JavaDoc currentParent;
54
55     private StringWriter JavaDoc writer;
56
57     /** The trax TransformerFactory */
58     private SAXTransformerFactory JavaDoc tfactory = null;
59
60     /**
61      * Initialize the LogicsheetCodeGenerator.
62      */

63     public void initialize() {
64         Properties JavaDoc format = new Properties JavaDoc();
65         try {
66             // Set the serializer which would act as ContentHandler for the last transformer
67
// FIXME (SSA) change a home made content handler, that extract the PCDATA
68
// from the last remaining element
69
TransformerHandler JavaDoc handler = getTransformerFactory().newTransformerHandler();
70
71             // Set the output properties
72
format.put(OutputKeys.METHOD,"text");
73             // FIXME (SSA) remove the nice identing. For debug purpose only.
74
format.put(OutputKeys.INDENT,"yes");
75             handler.getTransformer().setOutputProperties(format);
76
77             this.writer = new StringWriter JavaDoc();
78             handler.setResult(new StreamResult JavaDoc(writer));
79             this.serializerContentHandler = handler;
80         } catch (TransformerConfigurationException JavaDoc tce) {
81             getLogger().error("LogicsheetCodeGenerator: unable to get TransformerHandler", tce);
82         }
83     }
84
85     /**
86      * Helper for TransformerFactory.
87      */

88     private SAXTransformerFactory JavaDoc getTransformerFactory()
89     {
90         if(tfactory == null) {
91             tfactory = (SAXTransformerFactory JavaDoc) TransformerFactory.newInstance();
92             tfactory.setErrorListener(new TraxErrorHandler(getLogger()));
93         }
94         return tfactory;
95     }
96
97     /**
98      * Add a logicsheet to the logicsheet list
99      *
100      * @param logicsheet The logicsheet to be added
101      */

102     public void addLogicsheet(Logicsheet logicsheet) throws ProcessingException {
103         if (this.currentParent == null) {
104             // Setup the first transformer of the chain.
105
this.currentParent = logicsheet.getTransformerHandler();
106
107             // the parent is the rootReader
108
this.end.setContentHandler(this.currentParent);
109
110             // Set content handler for the end of the chain : serializer
111
this.currentParent.setResult(new SAXResult JavaDoc(this.serializerContentHandler));
112         } else {
113             // Build the transformer chain on the fly
114
TransformerHandler JavaDoc newParent = logicsheet.getTransformerHandler();
115
116             // the currentParent is the parent of the new logicsheet filter
117
this.currentParent.setResult(new SAXResult JavaDoc(newParent));
118
119             // reset the new parent and the contentHanlder
120
this.currentParent = newParent;
121             this.currentParent.setResult(new SAXResult JavaDoc(this.serializerContentHandler));
122         }
123     }
124
125     /**
126      * Generate source code from the given source. Filename information is
127      * ignored in the logicsheet-based code generation approach.
128      *
129      * @param source The source of the markup
130      * @return The generated source code
131      * @exception Exception If an error occurs during code generation
132      */

133     public String JavaDoc generateCode(Source source, AbstractXMLPipe filter)
134             throws Exception JavaDoc {
135         try {
136             // set the root XMLReader of the transformer chain
137
this.end = filter;
138             // start the parsing
139
SourceUtil.toSAX(source, filter);
140             return this.writer.toString();
141         } catch (SAXException JavaDoc e) {
142             if(e.getException() != null) {
143                 getLogger().debug("Got SAXException; Rethrowing cause exception", e);
144                 throw e.getException();
145             }
146             getLogger().debug("Got SAXException", e);
147             throw e;
148         }
149     }
150 }
151
Popular Tags