KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > SerializeTransformer


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend;
17
18 import org.apache.cocoon.transformation.AbstractTransformer;
19 import org.apache.cocoon.environment.SourceResolver;
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.Locator JavaDoc;
25
26 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
27 import javax.xml.transform.sax.TransformerHandler JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.TransformerConfigurationException JavaDoc;
30 import javax.xml.transform.OutputKeys JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.CharArrayWriter JavaDoc;
34
35 /**
36  * A transformer which serializes any content of a serialize element it encounters.
37  */

38 public class SerializeTransformer extends AbstractTransformer {
39     private int nestingLevel;
40     private int serializeNestingLevel;
41     private boolean inSerialize;
42     private TransformerHandler JavaDoc serializer;
43     private CharArrayWriter JavaDoc writer;
44     private static final String JavaDoc NAMESPACE = "http://outerx.org/daisywiki/1.0#serializer";
45     private static final String JavaDoc TRIGGER_EL = "serialize";
46
47     SAXTransformerFactory JavaDoc transformerFactory = (SAXTransformerFactory JavaDoc) SAXTransformerFactory.newInstance();
48
49     public void setup(SourceResolver sourceResolver, Map JavaDoc map, String JavaDoc s, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
50         this.nestingLevel = 0;
51     }
52
53     public void recycle() {
54         super.recycle();
55         this.writer = null;
56         this.serializer = null;
57     }
58
59     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
60         nestingLevel++;
61         if (namespaceURI.equals(NAMESPACE) && localName.equals(TRIGGER_EL)) {
62             inSerialize = true;
63             serializeNestingLevel = nestingLevel;
64
65             // some output properties
66
String JavaDoc method = atts.getValue("method");
67             if (method == null) method = "html";
68             String JavaDoc encoding = atts.getValue("encoding");
69             if (encoding == null) encoding = "UTF-8";
70
71             try {
72                 this.serializer = transformerFactory.newTransformerHandler();
73                 this.serializer.getTransformer().setOutputProperty(OutputKeys.METHOD, method);
74                 this.serializer.getTransformer().setOutputProperty(OutputKeys.ENCODING, encoding);
75             } catch (TransformerConfigurationException JavaDoc e) {
76                 throw new SAXException JavaDoc(e);
77             }
78             this.writer = new CharArrayWriter JavaDoc(5000);
79             serializer.setResult(new StreamResult JavaDoc(writer));
80             this.serializer.startDocument();
81         } else if (inSerialize) {
82             serializer.startElement(namespaceURI, localName, qName, atts);
83         } else {
84             super.startElement(namespaceURI, localName, qName, atts);
85         }
86     }
87
88     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
89         if (!inSerialize) {
90             super.endElement(namespaceURI, localName, qName);
91         } else {
92             if (nestingLevel == serializeNestingLevel && inSerialize && namespaceURI.equals(NAMESPACE) && localName.equals(TRIGGER_EL)) {
93                 inSerialize = false;
94                 this.serializer.endDocument();
95                 char[] text = writer.toCharArray();
96                 super.characters(text, 0, text.length);
97                 this.serializer = null;
98                 this.writer = null;
99             } else {
100                 serializer.endElement(namespaceURI, localName, qName);
101             }
102         }
103         nestingLevel--;
104     }
105
106     public void setDocumentLocator(Locator JavaDoc locator) {
107         if (!inSerialize)
108             super.setDocumentLocator(locator);
109     }
110
111     public void startDocument() throws SAXException JavaDoc {
112         if (!inSerialize)
113             super.startDocument();
114     }
115
116     public void endDocument() throws SAXException JavaDoc {
117         if (!inSerialize)
118         super.endDocument();
119     }
120
121     public void startPrefixMapping(String JavaDoc s, String JavaDoc s1) throws SAXException JavaDoc {
122         if (!inSerialize)
123             super.startPrefixMapping(s, s1);
124         else
125             serializer.startPrefixMapping(s, s1);
126     }
127
128     public void endPrefixMapping(String JavaDoc s) throws SAXException JavaDoc {
129         if (!inSerialize)
130             super.endPrefixMapping(s);
131         else
132             serializer.endPrefixMapping(s);
133     }
134
135     public void characters(char[] chars, int i, int i1) throws SAXException JavaDoc {
136         if (!inSerialize)
137             super.characters(chars, i, i1);
138         else
139             serializer.characters(chars, i, i1);
140     }
141
142     public void ignorableWhitespace(char[] chars, int i, int i1) throws SAXException JavaDoc {
143         if (!inSerialize)
144             super.ignorableWhitespace(chars, i, i1);
145     }
146
147     public void processingInstruction(String JavaDoc s, String JavaDoc s1) throws SAXException JavaDoc {
148         if (!inSerialize)
149             super.processingInstruction(s, s1);
150         else
151             serializer.processingInstruction(s, s1);
152     }
153
154     public void skippedEntity(String JavaDoc s) throws SAXException JavaDoc {
155         if (!inSerialize)
156             super.skippedEntity(s);
157     }
158
159     public void startDTD(String JavaDoc s, String JavaDoc s1, String JavaDoc s2) throws SAXException JavaDoc {
160         if (!inSerialize)
161             super.startDTD(s, s1, s2);
162     }
163
164     public void endDTD() throws SAXException JavaDoc {
165         if (!inSerialize)
166             super.endDTD();
167     }
168
169     public void startEntity(String JavaDoc s) throws SAXException JavaDoc {
170         if (!inSerialize)
171             super.startEntity(s);
172         else
173             serializer.startEntity(s);
174     }
175
176     public void endEntity(String JavaDoc s) throws SAXException JavaDoc {
177         if (!inSerialize)
178             super.endEntity(s);
179         else
180             serializer.endEntity(s);
181     }
182
183     public void startCDATA() throws SAXException JavaDoc {
184         if (!inSerialize)
185             super.startCDATA();
186         else
187             serializer.startCDATA();
188     }
189
190     public void endCDATA() throws SAXException JavaDoc {
191         if (!inSerialize)
192             super.endCDATA();
193         else
194             serializer.endCDATA();
195     }
196
197     public void comment(char[] chars, int i, int i1) throws SAXException JavaDoc {
198         if (!inSerialize)
199             super.comment(chars, i, i1);
200         else
201             serializer.comment(chars, i, i1);
202     }
203
204
205 }
206
Popular Tags