KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > DocumentHandlerWrapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.cocoon.xml;
17
18 import org.apache.avalon.framework.logger.LogEnabled;
19 import org.apache.avalon.framework.logger.Logger;
20
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.DocumentHandler JavaDoc;
23 import org.xml.sax.Locator JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.AttributeListImpl JavaDoc;
26
27 import java.util.Vector JavaDoc;
28
29 /**
30  * This class is an utility class "wrapping" around a SAX version 1.0
31  * <code>DocumentHandler</code> and forwarding it those events received throug
32  * its <code>XMLConsumers</code> interface.
33  * <br>
34  * This class fully supports XML namespaces, converting
35  * <code>startPrefixMapping(...)</code> and <code>endPrefixMapping(...)</code>
36  * calls into appropriate <code>xmlns</code> and <code>xmlns:...</code> element
37  * attributes.
38  *
39  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
40  * (Apache Software Foundation)
41  * @version CVS $Id: DocumentHandlerWrapper.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public class DocumentHandlerWrapper extends AbstractXMLConsumer implements LogEnabled /*, Recyclable*/ {
44
45     protected Logger log;
46
47     /** The current namespaces table. */
48     private NamespacesTable namespaces=new NamespacesTable();
49     /** The vector of namespaces declarations to include in the next element. */
50     private Vector JavaDoc undecl=new Vector JavaDoc();
51
52     /** The current <code>DocumentHandler</code>. */
53     protected DocumentHandler JavaDoc documentHandler=null;
54
55     /**
56      * Create a new <code>DocumentHandlerWrapper</code> instance.
57      */

58     public DocumentHandlerWrapper() {
59         super();
60      }
61
62     /**
63      * Create a new <code>DocumentHandlerWrapper</code> instance.
64      */

65     public DocumentHandlerWrapper(DocumentHandler JavaDoc document) {
66         this();
67         this.setDocumentHandler(document);
68     }
69
70     /**
71      * Provide component with a logger.
72      *
73      * @param logger the logger
74      */

75     public void enableLogging(Logger logger) {
76         if (this.log == null) {
77             this.log = logger;
78         }
79     }
80
81     /**
82      * Implementation of the recycle method
83      */

84     public void recycle() {
85         this.documentHandler = null;
86     }
87
88     /**
89      * Set the <code>DocumentHandler</code> that will receive XML data.
90      *
91      * @exception IllegalStateException If the <code>DocumentHandler</code>
92      * was already set.
93      */

94     public void setDocumentHandler(DocumentHandler JavaDoc document)
95     throws IllegalStateException JavaDoc {
96         if (this.documentHandler!=null) throw new IllegalStateException JavaDoc();
97         this.documentHandler=document;
98     }
99
100     /**
101      * Receive an object for locating the origin of SAX document events.
102      */

103     public void setDocumentLocator (Locator JavaDoc locator) {
104         if (this.documentHandler==null) return;
105         else this.documentHandler.setDocumentLocator(locator);
106     }
107
108     /**
109      * Receive notification of the beginning of a document.
110      */

111     public void startDocument ()
112     throws SAXException JavaDoc {
113         if (this.documentHandler==null)
114             throw new SAXException JavaDoc("DocumentHandler not set");
115         this.documentHandler.startDocument();
116     }
117
118     /**
119      * Receive notification of the end of a document.
120      */

121     public void endDocument ()
122     throws SAXException JavaDoc {
123         if (this.documentHandler==null)
124             throw new SAXException JavaDoc("DocumentHandler not set");
125         this.documentHandler.endDocument();
126     }
127
128     /**
129      * Begin the scope of a prefix-URI Namespace mapping.
130      */

131     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
132     throws SAXException JavaDoc {
133         this.undecl.addElement(this.namespaces.addDeclaration(prefix,uri));
134     }
135
136     /**
137      * End the scope of a prefix-URI mapping.
138      */

139     public void endPrefixMapping(String JavaDoc prefix)
140     throws SAXException JavaDoc {
141         if (namespaces.removeDeclaration(prefix)==null)
142             throw new SAXException JavaDoc("Namespace prefix \""+prefix+
143                                    "\" never declared");
144     }
145
146     /**
147      * Receive notification of the beginning of an element.
148      */

149     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
150     throws SAXException JavaDoc {
151         if (this.documentHandler==null)
152             throw new SAXException JavaDoc("DocumentHandler not set");
153         NamespacesTable.Name name=this.namespaces.resolve(uri,raw,null,loc);
154         // Create the AttributeList
155
AttributeListImpl JavaDoc a2=new AttributeListImpl JavaDoc();
156         // Set the xmlns:...="..." attributes
157
if (this.undecl.size()>0) {
158             for (int x=0; x<this.undecl.size(); x++) {
159                 NamespacesTable.Declaration dec=null;
160                 dec=(NamespacesTable.Declaration)this.undecl.elementAt(x);
161                 String JavaDoc aname="xmlns";
162                 if (dec.getPrefix().length()>0) aname="xmlns:"+dec.getPrefix();
163                 a2.addAttribute(aname,"CDATA",dec.getUri());
164             }
165             this.undecl.clear();
166         }
167         // Set the real attributes
168
for (int x=0; x<a.getLength(); x++) {
169             NamespacesTable.Name aname=namespaces.resolve(a.getURI(x),
170                                                           a.getQName(x),
171                                                           null,
172                                                           a.getLocalName(x));
173             a2.addAttribute(aname.getQName(),a.getType(x),a.getValue(x));
174         }
175         // Call the document handler startElement() method.
176
this.documentHandler.startElement(name.getQName(),a2);
177     }
178
179
180     /**
181      * Receive notification of the end of an element.
182      */

183     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
184     throws SAXException JavaDoc {
185         if (this.documentHandler==null)
186             throw new SAXException JavaDoc("DocumentHandler not set");
187         NamespacesTable.Name name=this.namespaces.resolve(uri,raw,null,loc);
188         this.documentHandler.endElement(name.getQName());
189     }
190
191     /**
192      * Receive notification of character data.
193      */

194     public void characters(char ch[], int start, int len)
195     throws SAXException JavaDoc {
196         if (this.documentHandler==null)
197             throw new SAXException JavaDoc("DocumentHandler not set");
198         this.documentHandler.characters(ch,start,len);
199     }
200
201     /**
202      * Receive notification of ignorable whitespace in element content.
203      */

204     public void ignorableWhitespace(char ch[], int start, int len)
205     throws SAXException JavaDoc {
206         if (this.documentHandler==null)
207             throw new SAXException JavaDoc("DocumentHandler not set");
208         this.documentHandler.ignorableWhitespace(ch,start,len);
209     }
210
211     /**
212      * Receive notification of a processing instruction.
213      */

214     public void processingInstruction(String JavaDoc target, String JavaDoc data)
215     throws SAXException JavaDoc {
216         if (this.documentHandler==null)
217             throw new SAXException JavaDoc("DocumentHandler not set");
218         this.documentHandler.processingInstruction(target,data);
219     }
220 }
221
Popular Tags