Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/znerd/xmlenc/sax/SAXEventReceiver.java


1   /*
2    * $Id: SAXEventReceiver.java,v 1.6 2005/09/12 08:40:06 znerd Exp $
3    */
4   package org.znerd.xmlenc.sax;
5   
6   import java.io.IOException;
7   import java.io.UnsupportedEncodingException;
8   import java.io.Writer;
9   import org.xml.sax.Attributes;
10  import org.xml.sax.ContentHandler;
11  import org.xml.sax.Locator;
12  import org.xml.sax.SAXException;
13  import org.znerd.xmlenc.XMLEventListener;
14  
15  /**
16   * SAX handler that receives SAX events and transforms them to <em>xmlenc</em>
17   * events.
18   *
19   * @version $Revision: 1.6 $ $Date: 2005/09/12 08:40:06 $
20   * @author Ernst de Haan (<a href="mailto:wfe.dehaan@gmail.com">wfe.dehaan@gmail.com</a>)
21   *
22   * @since xmlenc 0.31
23   */
24  public class SAXEventReceiver
25  extends Object
26  implements ContentHandler {
27  
28     //-------------------------------------------------------------------------
29     // Class functions
30     //-------------------------------------------------------------------------
31  
32     //-------------------------------------------------------------------------
33     // Class fields
34     //-------------------------------------------------------------------------
35  
36     //-------------------------------------------------------------------------
37     // Constructor
38     //-------------------------------------------------------------------------
39  
40     /**
41      * Constructs a new <code>SAXEventReceiver</code> that sends events to the
42      * specified <code>XMLEventListener</code>.
43      *
44      * @param eventListener
45      *    the {@link XMLEventListener} that should be used, cannot be
46      *    <code>null</code>.
47      *
48      * @throws IllegalArgumentException
49      *    if <code>eventListener == null</code>.
50      */
51     public SAXEventReceiver(XMLEventListener eventListener)
52     throws IllegalArgumentException {
53        if (eventListener == null) {
54           throw new IllegalArgumentException("eventListener == null");
55        }
56  
57        _eventListener = eventListener;
58     }
59  
60  
61     //-------------------------------------------------------------------------
62     // Fields
63     //-------------------------------------------------------------------------
64  
65     /**
66      * The <code>XMLOutputter</code> that is used.
67      */
68     private final XMLEventListener _eventListener;
69  
70  
71     //-------------------------------------------------------------------------
72     // Methods
73     //-------------------------------------------------------------------------
74  
75     public void setDocumentLocator(Locator locator) {
76        // XXX: What should we do here?
77     }
78  
79     public void startDocument() throws SAXException {
80        // XXX: Print declaration here?
81     }
82  
83     public void endDocument() throws SAXException {
84        try {
85           _eventListener.endDocument();
86        } catch (IOException ioException) {
87           throw new SAXException(ioException);
88        }
89     }
90  
91     public void startPrefixMapping(String prefix, String uri)
92     throws SAXException {
93        // XXX: What should we do here?
94     }
95  
96     public void endPrefixMapping(String prefix)
97     throws SAXException {
98        // XXX: What should we do here?
99     }
100 
101    public void startElement(String uri, String localName, String qName, Attributes atts)
102    throws SAXException {
103       try {
104          _eventListener.startTag(qName);
105          int attributeCount = atts.getLength();
106          for (int i = 0; i < attributeCount; i++) {
107             _eventListener.attribute(atts.getQName(i), atts.getValue(i));
108          }
109       } catch (IOException ioException) {
110          throw new SAXException(ioException);
111       }
112    }
113 
114    public void endElement(String uri, String localName, String qName)
115    throws SAXException {
116       try {
117          _eventListener.endTag();
118       } catch (IOException ioException) {
119          throw new SAXException(ioException);
120       }
121    }
122 
123    public void characters(char[] ch, int start, int length)
124    throws SAXException {
125       try {
126          _eventListener.pcdata(ch, start, length);
127       } catch (IOException ioException) {
128          throw new SAXException(ioException);
129       }
130    }
131 
132    public void ignorableWhitespace(char[] ch, int start, int length)
133    throws SAXException {
134       try {
135          _eventListener.whitespace(ch, start, length);
136       } catch (IOException ioException) {
137          throw new SAXException(ioException);
138       }
139    }
140 
141    public void processingInstruction(String target, String data)
142    throws SAXException {
143       try {
144          _eventListener.pi(target, data);
145       } catch (IOException ioException) {
146          throw new SAXException(ioException);
147       }
148    }
149 
150    public void skippedEntity(String name)
151    throws SAXException {
152       // XXX: Should we do anything here?
153    }
154 }