KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > trax > StAXStream2SAX


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://jaxp.dev.java.net/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://jaxp.dev.java.net/CDDLv1.0.html
15  * If applicable add the following below this CDDL HEADER
16  * with the fields enclosed by brackets "[]" replaced with
17  * your own identifying information: Portions Copyright
18  * [year] [name of copyright owner]
19  */

20
21 /*
22  * $Id: StAXStream2SAX.java,v 1.7 2007/05/16 15:03:52 spericas Exp $
23  * @(#)StAXStream2SAX.java 1.7 07/05/29
24  *
25  * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.org.apache.xalan.internal.xsltc.trax;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Stack JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.xml.sax.DTDHandler JavaDoc;
39 import org.xml.sax.EntityResolver JavaDoc;
40 import org.xml.sax.ErrorHandler JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.Locator JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44 import org.xml.sax.SAXNotRecognizedException JavaDoc;
45 import org.xml.sax.SAXNotSupportedException JavaDoc;
46 import org.xml.sax.XMLReader JavaDoc;
47 import org.xml.sax.ext.LexicalHandler JavaDoc;
48 import org.xml.sax.ext.Locator2 JavaDoc;
49 import org.xml.sax.helpers.AttributesImpl JavaDoc;
50 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
51
52
53
54 import javax.xml.namespace.QName JavaDoc;
55 import javax.xml.stream.XMLStreamReader;
56 import javax.xml.stream.XMLStreamConstants;
57 import javax.xml.stream.XMLStreamException;
58 import javax.xml.stream.events.Attribute;
59 import javax.xml.stream.events.Characters;
60 import javax.xml.stream.events.EndElement;
61 import javax.xml.stream.events.Namespace;
62 import javax.xml.stream.events.ProcessingInstruction;
63 import javax.xml.stream.events.StartElement;
64 import javax.xml.stream.events.StartDocument;
65 import javax.xml.stream.events.XMLEvent;
66
67
68
69 /**
70  * @author Padmaja Vedula
71  * @author Sunitha Reddy
72  */

73 public class StAXStream2SAX implements XMLReader JavaDoc, Locator JavaDoc {
74
75     //private final static String EMPTYSTRING = "";
76
//private static final String XMLNS_PREFIX = "xmlns";
77

78     // StAX Stream source
79
private final XMLStreamReader staxStreamReader;
80     
81     //private Node _dom = null;
82
private ContentHandler JavaDoc _sax = null;
83     private LexicalHandler JavaDoc _lex = null;
84     private SAXImpl _saxImpl = null;
85     //private Hashtable _nsPrefixes = new Hashtable();
86

87     public StAXStream2SAX(XMLStreamReader staxSrc) {
88             staxStreamReader = staxSrc;
89     }
90
91     public ContentHandler JavaDoc getContentHandler() {
92     return _sax;
93     }
94
95     public void setContentHandler(ContentHandler JavaDoc handler) throws
96     NullPointerException JavaDoc
97     {
98     _sax = handler;
99     if (handler instanceof LexicalHandler JavaDoc) {
100         _lex = (LexicalHandler JavaDoc) handler;
101     }
102     
103     if (handler instanceof SAXImpl) {
104         _saxImpl = (SAXImpl)handler;
105     }
106     }
107
108     
109     public void parse(InputSource JavaDoc unused) throws IOException JavaDoc, SAXException JavaDoc {
110         try {
111             bridge();
112         } catch (XMLStreamException e) {
113             throw new SAXException JavaDoc(e);
114         }
115     }
116
117
118     //Main Work Starts Here.
119
public void parse() throws IOException JavaDoc, SAXException JavaDoc, XMLStreamException {
120         bridge();
121     }
122
123     
124    /**
125      * This class is only used internally so this method should never
126      * be called.
127      */

128     public void parse(String JavaDoc sysId) throws IOException JavaDoc, SAXException JavaDoc {
129     throw new IOException JavaDoc("This method is not yet implemented.");
130     }
131     
132
133    public void bridge() throws XMLStreamException {
134
135         try {
136             // remembers the nest level of elements to know when we are done.
137
int depth=0;
138
139             // skip over START_DOCUMENT
140
int event = staxStreamReader.getEventType();
141             if (event == XMLStreamConstants.START_DOCUMENT) {
142                 event = staxStreamReader.next();
143             }
144
145             // If not a START_ELEMENT (e.g., a DTD), skip to next tag
146
if (event != XMLStreamConstants.START_ELEMENT) {
147                 event = staxStreamReader.nextTag();
148                 // An error if a START_ELEMENT isn't found now
149
if (event != XMLStreamConstants.START_ELEMENT) {
150                     throw new IllegalStateException JavaDoc("The current event is " +
151                             "not START_ELEMENT\n but" + event);
152                 }
153             }
154                   
155             handleStartDocument();
156
157             do {
158                 // These are all of the events listed in the javadoc for
159
// XMLEvent.
160
// The spec only really describes 11 of them.
161
switch (event) {
162                     case XMLStreamConstants.START_ELEMENT :
163                         depth++;
164                         handleStartElement();
165                         break;
166                     case XMLStreamConstants.END_ELEMENT :
167                         handleEndElement();
168                         depth--;
169                         break;
170                     case XMLStreamConstants.CHARACTERS :
171                         handleCharacters();
172                         break;
173                     case XMLStreamConstants.ENTITY_REFERENCE :
174                         handleEntityReference();
175                         break;
176                     case XMLStreamConstants.PROCESSING_INSTRUCTION :
177                         handlePI();
178                         break;
179                     case XMLStreamConstants.COMMENT :
180                         handleComment();
181                         break;
182                     case XMLStreamConstants.DTD :
183                         handleDTD();
184                         break;
185                     case XMLStreamConstants.ATTRIBUTE :
186                         handleAttribute();
187                         break;
188                     case XMLStreamConstants.NAMESPACE :
189                         handleNamespace();
190                         break;
191                     case XMLStreamConstants.CDATA :
192                         handleCDATA();
193                         break;
194                     case XMLStreamConstants.ENTITY_DECLARATION :
195                         handleEntityDecl();
196                         break;
197                     case XMLStreamConstants.NOTATION_DECLARATION :
198                         handleNotationDecl();
199                         break;
200                     case XMLStreamConstants.SPACE :
201                         handleSpace();
202                         break;
203                     default :
204                         throw new InternalError JavaDoc("processing event: " + event);
205                 }
206                 
207                 event=staxStreamReader.next();
208             } while (depth!=0);
209
210             handleEndDocument();
211         } catch (SAXException JavaDoc e) {
212             throw new XMLStreamException(e);
213         }
214     }
215
216     private void handleEndDocument() throws SAXException JavaDoc {
217         _sax.endDocument();
218     }
219
220     private void handleStartDocument() throws SAXException JavaDoc {
221         _sax.setDocumentLocator(new Locator2 JavaDoc() {
222             public int getColumnNumber() {
223                 return staxStreamReader.getLocation().getColumnNumber();
224             }
225             public int getLineNumber() {
226                 return staxStreamReader.getLocation().getLineNumber();
227             }
228             public String JavaDoc getPublicId() {
229                 return staxStreamReader.getLocation().getPublicId();
230             }
231             public String JavaDoc getSystemId() {
232                 return staxStreamReader.getLocation().getSystemId();
233             }
234             public String JavaDoc getXMLVersion() {
235                 return staxStreamReader.getVersion();
236             }
237             public String JavaDoc getEncoding() {
238                 return staxStreamReader.getEncoding();
239             }
240          });
241         _sax.startDocument();
242     }
243
244     private void handlePI() throws XMLStreamException {
245         try {
246             _sax.processingInstruction(
247                 staxStreamReader.getPITarget(),
248                 staxStreamReader.getPIData());
249         } catch (SAXException JavaDoc e) {
250             throw new XMLStreamException(e);
251         }
252     }
253
254     private void handleCharacters() throws XMLStreamException {
255
256         // workaround for bugid 5046319 - switch over to commented section
257
// below when it is fixed.
258
int textLength = staxStreamReader.getTextLength();
259         char[] chars = new char[textLength];
260
261         staxStreamReader.getTextCharacters(0, chars, 0, textLength);
262
263         try {
264             _sax.characters(chars, 0, chars.length);
265         } catch (SAXException JavaDoc e) {
266             throw new XMLStreamException(e);
267         }
268
269         
270 // int start = 0;
271
// int len;
272
// do {
273
// len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length);
274
// start += len;
275
// try {
276
// _sax.characters(buf, 0, len);
277
// } catch (SAXException e) {
278
// throw new XMLStreamException(e);
279
// }
280
// } while (len == buf.length);
281
}
282
283     private void handleEndElement() throws XMLStreamException {
284         QName JavaDoc qName = staxStreamReader.getName();
285
286         try {
287             //construct prefix:localName from qName
288
String JavaDoc qname = "";
289             if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){
290                 qname = qName.getPrefix() + ":";
291             }
292             qname += qName.getLocalPart();
293             
294             // fire endElement
295
_sax.endElement(
296                 qName.getNamespaceURI(),
297                 qName.getLocalPart(),
298                 qname);
299
300             // end namespace bindings
301
int nsCount = staxStreamReader.getNamespaceCount();
302             for (int i = nsCount - 1; i >= 0; i--) {
303                 String JavaDoc prefix = staxStreamReader.getNamespacePrefix(i);
304                 if (prefix == null) { // true for default namespace
305
prefix = "";
306                 }
307                 _sax.endPrefixMapping(prefix);
308             }
309         } catch (SAXException JavaDoc e) {
310             throw new XMLStreamException(e);
311         }
312     }
313
314     private void handleStartElement() throws XMLStreamException {
315
316         try {
317             // start namespace bindings
318
int nsCount = staxStreamReader.getNamespaceCount();
319             for (int i = 0; i < nsCount; i++) {
320                 String JavaDoc prefix = staxStreamReader.getNamespacePrefix(i);
321                 if (prefix == null) { // true for default namespace
322
prefix = "";
323                 }
324                 _sax.startPrefixMapping(
325                     prefix,
326                     staxStreamReader.getNamespaceURI(i));
327             }
328
329             // fire startElement
330
QName JavaDoc qName = staxStreamReader.getName();
331             String JavaDoc prefix = qName.getPrefix();
332             String JavaDoc rawname;
333             if(prefix==null || prefix.length()==0)
334                 rawname = qName.getLocalPart();
335             else
336                 rawname = prefix + ':' + qName.getLocalPart();
337             Attributes JavaDoc attrs = getAttributes();
338             _sax.startElement(
339                 qName.getNamespaceURI(),
340                 qName.getLocalPart(),
341                 rawname,
342                 attrs);
343         } catch (SAXException JavaDoc e) {
344             throw new XMLStreamException(e);
345         }
346     }
347
348     /**
349      * Get the attributes associated with the given START_ELEMENT or ATTRIBUTE
350      * StAXevent.
351      *
352      * @return the StAX attributes converted to an org.xml.sax.Attributes
353      */

354     private Attributes JavaDoc getAttributes() {
355         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
356
357         int eventType = staxStreamReader.getEventType();
358         if (eventType != XMLStreamConstants.ATTRIBUTE
359             && eventType != XMLStreamConstants.START_ELEMENT) {
360             throw new InternalError JavaDoc(
361                 "getAttributes() attempting to process: " + eventType);
362         }
363         
364         // in SAX, namespace declarations are not part of attributes by default.
365
// (there's a property to control that, but as far as we are concerned
366
// we don't use it.) So don't add xmlns:* to attributes.
367

368         // gather non-namespace attrs
369
for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
370             String JavaDoc uri = staxStreamReader.getAttributeNamespace(i);
371             if(uri==null) uri="";
372             String JavaDoc localName = staxStreamReader.getAttributeLocalName(i);
373             String JavaDoc prefix = staxStreamReader.getAttributePrefix(i);
374             String JavaDoc qName;
375             if(prefix==null || prefix.length()==0)
376                 qName = localName;
377             else
378                 qName = prefix + ':' + localName;
379             String JavaDoc type = staxStreamReader.getAttributeType(i);
380             String JavaDoc value = staxStreamReader.getAttributeValue(i);
381
382             attrs.addAttribute(uri, localName, qName, type, value);
383         }
384
385         return attrs;
386     }
387
388     private void handleNamespace() {
389         // no-op ???
390
// namespace events don't normally occur outside of a startElement
391
// or endElement
392
}
393
394     private void handleAttribute() {
395         // no-op ???
396
// attribute events don't normally occur outside of a startElement
397
// or endElement
398
}
399
400     private void handleDTD() {
401         // no-op ???
402
// it seems like we need to pass this info along, but how?
403
}
404
405     private void handleComment() {
406         // no-op ???
407
}
408
409     private void handleEntityReference() {
410         // no-op ???
411
}
412
413     private void handleSpace() {
414         // no-op ???
415
// this event is listed in the javadoc, but not in the spec.
416
}
417
418     private void handleNotationDecl() {
419         // no-op ???
420
// this event is listed in the javadoc, but not in the spec.
421
}
422
423     private void handleEntityDecl() {
424         // no-op ???
425
// this event is listed in the javadoc, but not in the spec.
426
}
427
428     private void handleCDATA() {
429         // no-op ???
430
// this event is listed in the javadoc, but not in the spec.
431
}
432     
433     
434     /**
435      * This class is only used internally so this method should never
436      * be called.
437      */

438     public DTDHandler JavaDoc getDTDHandler() {
439     return null;
440     }
441
442     /**
443      * This class is only used internally so this method should never
444      * be called.
445      */

446     public ErrorHandler JavaDoc getErrorHandler() {
447     return null;
448     }
449
450     /**
451      * This class is only used internally so this method should never
452      * be called.
453      */

454     public boolean getFeature(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
455     SAXNotSupportedException JavaDoc
456     {
457     return false;
458     }
459
460     /**
461      * This class is only used internally so this method should never
462      * be called.
463      */

464     public void setFeature(String JavaDoc name, boolean value) throws
465     SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc
466     {
467     }
468
469     /**
470      * This class is only used internally so this method should never
471      * be called.
472      */

473     public void setDTDHandler(DTDHandler JavaDoc handler) throws NullPointerException JavaDoc {
474     }
475
476     /**
477      * This class is only used internally so this method should never
478      * be called.
479      */

480     public void setEntityResolver(EntityResolver JavaDoc resolver) throws
481     NullPointerException JavaDoc
482     {
483     }
484
485     /**
486      * This class is only used internally so this method should never
487      * be called.
488      */

489     public EntityResolver JavaDoc getEntityResolver() {
490     return null;
491     }
492
493     /**
494      * This class is only used internally so this method should never
495      * be called.
496      */

497     public void setErrorHandler(ErrorHandler JavaDoc handler) throws
498     NullPointerException JavaDoc
499     {
500     }
501
502     /**
503      * This class is only used internally so this method should never
504      * be called.
505      */

506     public void setProperty(String JavaDoc name, Object JavaDoc value) throws
507     SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
508     }
509
510     /**
511      * This class is only used internally so this method should never
512      * be called.
513      */

514     public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
515     SAXNotSupportedException JavaDoc
516     {
517     return null;
518     }
519     
520     /**
521      * This class is only used internally so this method should never
522      * be called.
523      */

524     public int getColumnNumber() {
525     return 0;
526     }
527     
528     /**
529      * This class is only used internally so this method should never
530      * be called.
531      */

532     public int getLineNumber() {
533     return 0;
534     }
535
536     /**
537      * This class is only used internally so this method should never
538      * be called.
539      */

540     public String JavaDoc getPublicId() {
541     return null;
542     }
543
544     /**
545      * This class is only used internally so this method should never
546      * be called.
547      */

548     public String JavaDoc getSystemId() {
549     return null;
550     }
551     
552 }
553
Popular Tags