KickJava   Java API By Example, From Geeks To Geeks.

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


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: StAXEvent2SAX.java,v 1.4 2006/01/25 05:09:46 sunithareddy Exp $
23  * @(#)StAXEvent2SAX.java 1.8 06/04/07
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.Iterator JavaDoc;
32
33 import org.xml.sax.Attributes JavaDoc;
34 import org.xml.sax.ContentHandler JavaDoc;
35 import org.xml.sax.DTDHandler JavaDoc;
36 import org.xml.sax.EntityResolver JavaDoc;
37 import org.xml.sax.ErrorHandler JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.Locator JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41 import org.xml.sax.SAXNotRecognizedException JavaDoc;
42 import org.xml.sax.SAXNotSupportedException JavaDoc;
43 import org.xml.sax.XMLReader JavaDoc;
44 import org.xml.sax.ext.LexicalHandler JavaDoc;
45 import org.xml.sax.helpers.AttributesImpl JavaDoc;
46 import org.xml.sax.ext.Locator2 JavaDoc;
47 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl;
48
49 import javax.xml.namespace.QName JavaDoc;
50 import javax.xml.stream.XMLEventReader;
51 import javax.xml.stream.XMLStreamConstants;
52 import javax.xml.stream.XMLStreamException;
53 import javax.xml.stream.events.Attribute;
54 import javax.xml.stream.events.Characters;
55 import javax.xml.stream.events.EndElement;
56 import javax.xml.stream.events.Namespace;
57 import javax.xml.stream.events.ProcessingInstruction;
58 import javax.xml.stream.events.StartElement;
59 import javax.xml.stream.events.XMLEvent;
60 import javax.xml.stream.events.StartDocument;
61
62
63 /**
64  * @author Suresh Kumar
65  * @author Sunitha Reddy
66  * @since 1.6
67  */

68 public class StAXEvent2SAX implements XMLReader JavaDoc, Locator JavaDoc {
69
70     //private final static String EMPTYSTRING = "";
71
//private static final String XMLNS_PREFIX = "xmlns";
72

73     // StAX event source
74
private final XMLEventReader staxEventReader;
75     
76     //private Node _dom = null;
77
private ContentHandler JavaDoc _sax = null;
78     private LexicalHandler JavaDoc _lex = null;
79     private SAXImpl _saxImpl = null;
80     //private Hashtable _nsPrefixes = new Hashtable();
81
private String JavaDoc version = null;
82     private String JavaDoc encoding = null;
83         
84
85     public StAXEvent2SAX(XMLEventReader staxCore) {
86     staxEventReader = staxCore;
87     }
88
89     public ContentHandler JavaDoc getContentHandler() {
90     return _sax;
91     }
92
93     public void setContentHandler(ContentHandler JavaDoc handler) throws
94     NullPointerException JavaDoc
95     {
96     _sax = handler;
97     if (handler instanceof LexicalHandler JavaDoc) {
98         _lex = (LexicalHandler JavaDoc) handler;
99     }
100     
101     if (handler instanceof SAXImpl) {
102         _saxImpl = (SAXImpl)handler;
103     }
104     }
105
106     
107     public void parse(InputSource JavaDoc unused) throws IOException JavaDoc, SAXException JavaDoc {
108        try {
109             bridge();
110         } catch (XMLStreamException e) {
111             throw new SAXException JavaDoc(e);
112         }
113     }
114
115
116     //Main Work Starts Here.
117
public void parse() throws IOException JavaDoc, SAXException JavaDoc, XMLStreamException {
118         bridge();
119     }
120
121     
122     /* public void parse() throws IOException, SAXException {
123     if (_dom != null) {
124         boolean isIncomplete =
125         (_dom.getNodeType() != org.w3c.dom.Node.DOCUMENT_NODE);
126
127         if (isIncomplete) {
128         _sax.startDocument();
129         parse(_dom);
130         _sax.endDocument();
131         }
132         else {
133         parse(_dom);
134         }
135     }
136     }
137     */

138     
139     /*
140      * @see StAXReaderToContentHandler#bridge()
141      */

142     private void bridge() throws XMLStreamException {
143
144         try {
145             // remembers the nest level of elements to know when we are done.
146
int depth=0;
147
148             XMLEvent event = staxEventReader.peek();
149
150             if (!event.isStartDocument() && !event.isStartElement()) {
151                 throw new IllegalStateException JavaDoc();
152             }
153
154             // if the parser is on START_DOCUMENT, skip ahead to the first element
155
while( !event.isStartElement() ) {
156                 if (event.getEventType() == XMLStreamConstants.START_DOCUMENT){
157                     version = ((StartDocument)event).getVersion();
158                     if (((StartDocument)event).encodingSet())
159                         encoding = ((StartDocument)event).getCharacterEncodingScheme();
160                 }
161                 
162                 event = staxEventReader.nextEvent();
163             }
164            
165             handleStartDocument(event);
166
167             do {
168                 // These are all of the events listed in the javadoc for
169
// XMLEvent.
170
// The spec only really describes 11 of them.
171
switch (event.getEventType()) {
172                     case XMLStreamConstants.START_ELEMENT :
173                         depth++;
174                         handleStartElement(event.asStartElement());
175                         break;
176                     case XMLStreamConstants.END_ELEMENT :
177                         handleEndElement(event.asEndElement());
178                         depth--;
179                         break;
180                     case XMLStreamConstants.CHARACTERS :
181                         handleCharacters(event.asCharacters());
182                         break;
183                     case XMLStreamConstants.ENTITY_REFERENCE :
184                         handleEntityReference();
185                         break;
186                     case XMLStreamConstants.PROCESSING_INSTRUCTION :
187                         handlePI((ProcessingInstruction)event);
188                         break;
189                     case XMLStreamConstants.COMMENT :
190                         handleComment();
191                         break;
192                     case XMLStreamConstants.DTD :
193                         handleDTD();
194                         break;
195                     case XMLStreamConstants.ATTRIBUTE :
196                         handleAttribute();
197                         break;
198                     case XMLStreamConstants.NAMESPACE :
199                         handleNamespace();
200                         break;
201                     case XMLStreamConstants.CDATA :
202                         handleCDATA();
203                         break;
204                     case XMLStreamConstants.ENTITY_DECLARATION :
205                         handleEntityDecl();
206                         break;
207                     case XMLStreamConstants.NOTATION_DECLARATION :
208                         handleNotationDecl();
209                         break;
210                     case XMLStreamConstants.SPACE :
211                         handleSpace();
212                         break;
213                     default :
214                         throw new InternalError JavaDoc("processing event: " + event);
215                 }
216
217                 event=staxEventReader.nextEvent();
218             } while (depth!=0);
219
220             handleEndDocument();
221         } catch (SAXException JavaDoc e) {
222             throw new XMLStreamException(e);
223         }
224     }
225
226     
227     private void handleEndDocument() throws SAXException JavaDoc {
228         _sax.endDocument();
229     }
230
231     private void handleStartDocument(final XMLEvent event) throws SAXException JavaDoc {
232         _sax.setDocumentLocator(new Locator2 JavaDoc() {
233             public int getColumnNumber() {
234                 return event.getLocation().getColumnNumber();
235             }
236             public int getLineNumber() {
237                 return event.getLocation().getLineNumber();
238             }
239             public String JavaDoc getPublicId() {
240                 return event.getLocation().getPublicId();
241             }
242             public String JavaDoc getSystemId() {
243                 return event.getLocation().getSystemId();
244             }
245             public String JavaDoc getXMLVersion(){
246                 return version;
247             }
248             public String JavaDoc getEncoding(){
249                 return encoding;
250             }
251             
252         });
253         _sax.startDocument();
254     }
255
256     private void handlePI(ProcessingInstruction event)
257         throws XMLStreamException {
258         try {
259             _sax.processingInstruction(
260                 event.getTarget(),
261                 event.getData());
262         } catch (SAXException JavaDoc e) {
263             throw new XMLStreamException(e);
264         }
265     }
266
267     private void handleCharacters(Characters event) throws XMLStreamException {
268         try {
269             _sax.characters(
270                 event.getData().toCharArray(),
271                 0,
272                 event.getData().length());
273         } catch (SAXException JavaDoc e) {
274             throw new XMLStreamException(e);
275         }
276     }
277
278     private void handleEndElement(EndElement event) throws XMLStreamException {
279         QName JavaDoc qName = event.getName();
280         
281         //construct prefix:localName from qName
282
String JavaDoc qname = "";
283         if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){
284             qname = qName.getPrefix() + ":";
285         }
286         qname += qName.getLocalPart();
287         
288         try {
289             // fire endElement
290
_sax.endElement(
291                 qName.getNamespaceURI(),
292                 qName.getLocalPart(),
293                 qname);
294
295             // end namespace bindings
296
for( Iterator JavaDoc i = event.getNamespaces(); i.hasNext();) {
297                 String JavaDoc prefix = (String JavaDoc)i.next();
298                 if( prefix == null ) { // true for default namespace
299
prefix = "";
300                 }
301                 _sax.endPrefixMapping(prefix);
302             }
303         } catch (SAXException JavaDoc e) {
304             throw new XMLStreamException(e);
305         }
306     }
307
308     private void handleStartElement(StartElement event)
309         throws XMLStreamException {
310         try {
311             // start namespace bindings
312
for (Iterator JavaDoc i = event.getNamespaces(); i.hasNext();) {
313                 String JavaDoc prefix = ((Namespace)i.next()).getPrefix();
314                 if (prefix == null) { // true for default namespace
315
prefix = "";
316                 }
317                 _sax.startPrefixMapping(
318                     prefix,
319                     event.getNamespaceURI(prefix));
320             }
321
322             // fire startElement
323
QName JavaDoc qName = event.getName();
324             String JavaDoc prefix = qName.getPrefix();
325             String JavaDoc rawname;
326             if (prefix == null || prefix.length() == 0) {
327                 rawname = qName.getLocalPart();
328             } else {
329                 rawname = prefix + ':' + qName.getLocalPart();
330             }
331             
332             Attributes JavaDoc saxAttrs = getAttributes(event);
333             _sax.startElement(
334                 qName.getNamespaceURI(),
335                 qName.getLocalPart(),
336                 rawname,
337                 saxAttrs);
338         } catch (SAXException JavaDoc e) {
339             throw new XMLStreamException(e);
340         }
341     }
342
343     /**
344      * Get the attributes associated with the given START_ELEMENT StAXevent.
345      *
346      * @return the StAX attributes converted to an org.xml.sax.Attributes
347      */

348     private Attributes JavaDoc getAttributes(StartElement event) {
349         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
350
351         if ( !event.isStartElement() ) {
352             throw new InternalError JavaDoc(
353                 "getAttributes() attempting to process: " + event);
354         }
355         
356         // in SAX, namespace declarations are not part of attributes by default.
357
// (there's a property to control that, but as far as we are concerned
358
// we don't use it.) So don't add xmlns:* to attributes.
359

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

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

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

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

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

470     public void parse(String JavaDoc sysId) throws IOException JavaDoc, SAXException JavaDoc {
471     throw new IOException JavaDoc("This method is not yet implemented.");
472     }
473
474     /**
475      * This class is only used internally so this method should never
476      * be called.
477      */

478     public void setDTDHandler(DTDHandler JavaDoc handler) throws NullPointerException JavaDoc {
479     }
480
481     /**
482      * This class is only used internally so this method should never
483      * be called.
484      */

485     public void setEntityResolver(EntityResolver JavaDoc resolver) throws
486     NullPointerException JavaDoc
487     {
488     }
489
490     /**
491      * This class is only used internally so this method should never
492      * be called.
493      */

494     public EntityResolver JavaDoc getEntityResolver() {
495     return null;
496     }
497
498     /**
499      * This class is only used internally so this method should never
500      * be called.
501      */

502     public void setErrorHandler(ErrorHandler JavaDoc handler) throws
503     NullPointerException JavaDoc
504     {
505     }
506
507     /**
508      * This class is only used internally so this method should never
509      * be called.
510      */

511     public void setProperty(String JavaDoc name, Object JavaDoc value) throws
512     SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
513     }
514
515     /**
516      * This class is only used internally so this method should never
517      * be called.
518      */

519     public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc,
520     SAXNotSupportedException JavaDoc
521     {
522     return null;
523     }
524     
525     /**
526      * This class is only used internally so this method should never
527      * be called.
528      */

529     public int getColumnNumber() {
530     return 0;
531     }
532     
533     /**
534      * This class is only used internally so this method should never
535      * be called.
536      */

537     public int getLineNumber() {
538     return 0;
539     }
540
541     /**
542      * This class is only used internally so this method should never
543      * be called.
544      */

545     public String JavaDoc getPublicId() {
546     return null;
547     }
548
549     /**
550      * This class is only used internally so this method should never
551      * be called.
552      */

553     public String JavaDoc getSystemId() {
554     return null;
555     }
556 }
557
Popular Tags