KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > tools > SAXEventSerializer


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.tools;
41
42 import java.io.IOException JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.OutputStreamWriter JavaDoc;
45 import java.io.Writer JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Stack JavaDoc;
49
50 import org.xml.sax.Attributes JavaDoc;
51 import org.xml.sax.SAXException JavaDoc;
52 import org.xml.sax.ext.LexicalHandler JavaDoc;
53 import org.xml.sax.helpers.DefaultHandler JavaDoc;
54 import com.sun.xml.fastinfoset.CommonResourceBundle;
55
56 public class SAXEventSerializer extends DefaultHandler JavaDoc
57         implements LexicalHandler JavaDoc {
58
59     private Writer JavaDoc _writer;
60     private boolean _charactersAreCDATA;
61     private StringBuffer JavaDoc _characters;
62     
63     private Stack JavaDoc _namespaceStack = new Stack JavaDoc();
64     protected List JavaDoc _namespaceAttributes;
65     
66     public SAXEventSerializer(OutputStream JavaDoc s) throws IOException JavaDoc {
67         _writer = new OutputStreamWriter JavaDoc(s);
68         _charactersAreCDATA = false;
69     }
70     
71     // -- ContentHandler interface ---------------------------------------
72

73     public void startDocument() throws SAXException JavaDoc {
74         try {
75             _writer.write("<sax xmlns=\"http://www.sun.com/xml/sax-events\">\n");
76             _writer.write("<startDocument/>\n");
77             _writer.flush();
78         }
79         catch (IOException JavaDoc e) {
80             throw new SAXException JavaDoc(e);
81         }
82     }
83
84     public void endDocument() throws SAXException JavaDoc {
85         try {
86             _writer.write("<endDocument/>\n");
87             _writer.write("</sax>");
88             _writer.flush();
89             _writer.close();
90         }
91         catch (IOException JavaDoc e) {
92             throw new SAXException JavaDoc(e);
93         }
94     }
95
96     
97     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
98         throws SAXException JavaDoc
99     {
100         if (_namespaceAttributes == null) {
101             _namespaceAttributes = new ArrayList JavaDoc();
102         }
103         
104         String JavaDoc qName = (prefix == "") ? "xmlns" : "xmlns" + prefix;
105         AttributeValueHolder attribute = new AttributeValueHolder(
106                 qName,
107                 prefix,
108                 uri,
109                 null,
110                 null);
111         _namespaceAttributes.add(attribute);
112     }
113     
114     public void endPrefixMapping(String JavaDoc prefix)
115         throws SAXException JavaDoc
116     {
117         /*
118         try {
119             outputCharacters();
120
121             _writer.write("<endPrefixMapping prefix=\"" +
122                 prefix + "\"/>\n");
123             _writer.flush();
124         }
125         catch (IOException e) {
126             throw new SAXException(e);
127         }
128          */

129     }
130
131     public void startElement(String JavaDoc uri, String JavaDoc localName,
132             String JavaDoc qName, Attributes JavaDoc attributes)
133         throws SAXException JavaDoc
134     {
135         try {
136             outputCharacters();
137
138             if (_namespaceAttributes != null) {
139                 
140                 AttributeValueHolder[] attrsHolder = new AttributeValueHolder[0];
141                 attrsHolder = (AttributeValueHolder[])_namespaceAttributes.toArray(attrsHolder);
142                         
143                 // Sort attributes
144
quicksort(attrsHolder, 0, attrsHolder.length - 1);
145
146                 for (int i = 0; i < attrsHolder.length; i++) {
147                     _writer.write("<startPrefixMapping prefix=\"" +
148                         attrsHolder[i].localName + "\" uri=\"" + attrsHolder[i].uri + "\"/>\n");
149                     _writer.flush();
150                 }
151                         
152                 _namespaceStack.push(attrsHolder);
153                 _namespaceAttributes = null;
154             } else {
155                 _namespaceStack.push(null);
156             }
157             
158             AttributeValueHolder[] attrsHolder =
159                 new AttributeValueHolder[attributes.getLength()];
160             for (int i = 0; i < attributes.getLength(); i++) {
161                 attrsHolder[i] = new AttributeValueHolder(
162                     attributes.getQName(i),
163                     attributes.getLocalName(i),
164                     attributes.getURI(i),
165                     attributes.getType(i),
166                     attributes.getValue(i));
167             }
168             
169             // Sort attributes
170
quicksort(attrsHolder, 0, attrsHolder.length - 1);
171
172             int attributeCount = 0;
173             for (int i = 0; i < attrsHolder.length; i++) {
174                 if (attrsHolder[i].uri.equals("http://www.w3.org/2000/xmlns/")) {
175                     // Ignore XMLNS attributes
176
continue;
177                 }
178                 attributeCount++;
179             }
180             
181             if (attributeCount == 0) {
182                 _writer.write("<startElement uri=\"" + uri
183                     + "\" localName=\"" + localName + "\" qName=\""
184                     + qName + "\"/>\n");
185                 return;
186             }
187             
188             _writer.write("<startElement uri=\"" + uri
189                 + "\" localName=\"" + localName + "\" qName=\""
190                 + qName + "\">\n");
191
192             // Serialize attributes as children
193
for (int i = 0; i < attrsHolder.length; i++) {
194                 if (attrsHolder[i].uri.equals("http://www.w3.org/2000/xmlns/")) {
195                     // Ignore XMLNS attributes
196
continue;
197                 }
198                 _writer.write(
199                     " <attribute qName=\"" + attrsHolder[i].qName +
200                     "\" localName=\"" + attrsHolder[i].localName +
201                     "\" uri=\"" + attrsHolder[i].uri +
202                     // "\" type=\"" + attrsHolder[i].type +
203
"\" value=\"" + attrsHolder[i].value +
204                     "\"/>\n");
205             }
206             
207             _writer.write("</startElement>\n");
208             _writer.flush();
209         }
210         catch (IOException JavaDoc e) {
211             throw new SAXException JavaDoc(e);
212         }
213     }
214
215     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
216             throws SAXException JavaDoc
217     {
218         try {
219             outputCharacters();
220
221             _writer.write("<endElement uri=\"" + uri
222                 + "\" localName=\"" + localName + "\" qName=\""
223                 + qName + "\"/>\n");
224             _writer.flush();
225
226             // Write out the end prefix here rather than waiting
227
// for the explicit events
228
AttributeValueHolder[] attrsHolder = (AttributeValueHolder[])_namespaceStack.pop();
229             if (attrsHolder != null) {
230                 for (int i = 0; i < attrsHolder.length; i++) {
231                     _writer.write("<endPrefixMapping prefix=\"" +
232                         attrsHolder[i].localName + "\"/>\n");
233                     _writer.flush();
234                 }
235             }
236             
237         }
238         catch (IOException JavaDoc e) {
239             throw new SAXException JavaDoc(e);
240         }
241     }
242
243     public void characters(char[] ch, int start, int length)
244             throws SAXException JavaDoc
245     {
246         if (length == 0) {
247             return;
248         }
249         
250         if (_characters == null) {
251             _characters = new StringBuffer JavaDoc();
252         }
253         
254         // Coalesce multiple character events
255
_characters.append(ch, start, length);
256         
257         /*
258         try {
259             _writer.write("<characters>" +
260                 (_charactersAreCDATA ? "<![CDATA[" : "") +
261                 new String(ch, start, length) +
262                 (_charactersAreCDATA ? "]]>" : "") +
263                 "</characters>\n");
264             _writer.flush();
265         }
266         catch (IOException e) {
267             throw new SAXException(e);
268         }
269          */

270     }
271
272     private void outputCharacters() throws SAXException JavaDoc {
273         if (_characters == null) {
274             return;
275         }
276         
277         try {
278             _writer.write("<characters>" +
279                 (_charactersAreCDATA ? "<![CDATA[" : "") +
280                 _characters +
281                 (_charactersAreCDATA ? "]]>" : "") +
282                 "</characters>\n");
283             _writer.flush();
284
285             _characters = null;
286         } catch (IOException JavaDoc e) {
287             throw new SAXException JavaDoc(e);
288         }
289     }
290     
291     public void ignorableWhitespace(char[] ch, int start, int length)
292             throws SAXException JavaDoc
293     {
294         // Report ignorable ws as characters (assumes validation off)
295
characters(ch, start, length);
296     }
297
298     public void processingInstruction(String JavaDoc target, String JavaDoc data)
299             throws SAXException JavaDoc
300     {
301         try {
302             outputCharacters();
303
304             _writer.write("<processingInstruction target=\"" + target
305                 + "\" data=\"" + data + "\"/>\n");
306             _writer.flush();
307         }
308         catch (IOException JavaDoc e) {
309             throw new SAXException JavaDoc(e);
310         }
311     }
312     
313     // -- LexicalHandler interface ---------------------------------------
314

315     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
316             throws SAXException JavaDoc {
317         // Not implemented
318
}
319
320     public void endDTD()
321             throws SAXException JavaDoc {
322         // Not implemented
323
}
324
325     public void startEntity(String JavaDoc name)
326             throws SAXException JavaDoc {
327         // Not implemented
328
}
329
330     public void endEntity(String JavaDoc name)
331             throws SAXException JavaDoc {
332         // Not implemented
333
}
334
335     public void startCDATA()
336             throws SAXException JavaDoc {
337         _charactersAreCDATA = true;
338     }
339
340     public void endCDATA()
341             throws SAXException JavaDoc {
342         _charactersAreCDATA = false;
343     }
344
345     public void comment(char[] ch, int start, int length)
346             throws SAXException JavaDoc
347     {
348         try {
349             outputCharacters();
350
351             _writer.write("<comment>" +
352                 new String JavaDoc(ch, start, length) +
353                 "</comment>\n");
354             _writer.flush();
355         }
356         catch (IOException JavaDoc e) {
357             throw new SAXException JavaDoc(e);
358         }
359     }
360     
361     // -- Utility methods ------------------------------------------------
362

363     private void quicksort(AttributeValueHolder[] attrs, int p, int r) {
364         while (p < r) {
365             final int q = partition(attrs, p, r);
366             quicksort(attrs, p, q);
367             p = q + 1;
368         }
369     }
370                                                                                                                          
371     private int partition(AttributeValueHolder[] attrs, int p, int r) {
372         AttributeValueHolder x = attrs[(p + r) >>> 1];
373         int i = p - 1;
374         int j = r + 1;
375         while (true) {
376             while (x.compareTo(attrs[--j]) < 0);
377             while (x.compareTo(attrs[++i]) > 0);
378             if (i < j) {
379                 final AttributeValueHolder t = attrs[i];
380                 attrs[i] = attrs[j];
381                 attrs[j] = t;
382             }
383             else {
384                 return j;
385             }
386         }
387     }
388     
389     public static class AttributeValueHolder implements Comparable JavaDoc {
390         public final String JavaDoc qName;
391         public final String JavaDoc localName;
392         public final String JavaDoc uri;
393         public final String JavaDoc type;
394         public final String JavaDoc value;
395
396         public AttributeValueHolder(String JavaDoc qName,
397             String JavaDoc localName,
398             String JavaDoc uri,
399             String JavaDoc type,
400             String JavaDoc value)
401         {
402             this.qName = qName;
403             this.localName = localName;
404             this.uri = uri;
405             this.type = type;
406             this.value = value;
407         }
408
409         public int compareTo(Object JavaDoc o) {
410             try {
411                 return qName.compareTo(((AttributeValueHolder) o).qName);
412             }
413             catch (Exception JavaDoc e) {
414                 throw new RuntimeException JavaDoc(CommonResourceBundle.getInstance().getString("message.AttributeValueHolderExpected"));
415             }
416         }
417     }
418
419 }
420
421
Popular Tags