KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Enumeration JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.xml.sax.AttributeList JavaDoc;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.xml.sax.DocumentHandler JavaDoc;
25 import org.xml.sax.Locator JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.helpers.AttributesImpl JavaDoc;
28
29 /**
30  * This class is an utility class "adapting" a SAX version 1.0
31  * <code>DocumentHandler</code>, to SAX version 2 <code>ContentHandler</code>.
32  * <br>
33  * This class fully supports XML namespaces, converting <code>xmlns</code> and
34  * <code>xmlns:...</code> element attributes into appropriate
35  * <code>startPrefixMapping(...)</code> and <code>endPrefixMapping(...)</code>
36  * calls.
37  *
38  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
39  * (Apache Software Foundation)
40  * @version CVS $Id: DocumentHandlerAdapter.java 30932 2004-07-29 17:35:38Z vgritsenko $
41  */

42 public class DocumentHandlerAdapter extends AbstractXMLProducer
43 implements DocumentHandler JavaDoc {
44
45     /** The element-oriented namespace-uri stacked mapping table. */
46     private Hashtable JavaDoc stackedNS=new Hashtable JavaDoc();
47     /** The current namespaces table. */
48     private NamespacesTable namespaces=new NamespacesTable();
49     /** The current stack depth.*/
50     private int stack=0;
51
52     /**
53      * Create a new <code>DocumentHandlerAdapter</code> instance.
54      */

55     public DocumentHandlerAdapter() {
56         super();
57     }
58
59     /**
60      * Create a new <code>DocumentHandlerAdapter</code> instance.
61      */

62     public DocumentHandlerAdapter(XMLConsumer consumer) {
63         this();
64         super.setConsumer(consumer);
65     }
66
67     /**
68      * Create a new <code>DocumentHandlerAdapter</code> instance.
69      */

70     public DocumentHandlerAdapter(ContentHandler JavaDoc content) {
71         this();
72         super.setContentHandler(content);
73     }
74
75     /**
76      * Receive an object for locating the origin of SAX document events.
77      */

78     public void setDocumentLocator (Locator JavaDoc locator) {
79         if (super.contentHandler==null) return;
80         else super.contentHandler.setDocumentLocator(locator);
81     }
82
83     /**
84      * Receive notification of the beginning of a document.
85      */

86     public void startDocument ()
87         throws SAXException JavaDoc {
88         if (super.contentHandler==null)
89             throw new SAXException JavaDoc("ContentHandler not set");
90         super.contentHandler.startDocument();
91     }
92
93     /**
94      * Receive notification of the end of a document.
95      */

96     public void endDocument ()
97         throws SAXException JavaDoc {
98         if (super.contentHandler==null)
99             throw new SAXException JavaDoc("ContentHandler not set");
100         super.contentHandler.endDocument();
101     }
102
103     /**
104      * Receive notification of the beginning of an element.
105      */

106     public void startElement (String JavaDoc name, AttributeList JavaDoc a)
107         throws SAXException JavaDoc {
108         if (super.contentHandler==null)
109             throw new SAXException JavaDoc("ContentHandler not set");
110         // Check for namespace declarations (two loops because we're not sure
111
// about attribute ordering.
112
AttributesImpl a2=new AttributesImpl();
113         Vector JavaDoc nslist=new Vector JavaDoc();
114         for (int x=0; x<a.getLength(); x++) {
115             String JavaDoc att=a.getName(x);
116             String JavaDoc uri=a.getValue(x);
117             if (att.equals("xmlns") || att.startsWith("xmlns:")) {
118                 String JavaDoc pre="";
119                 if (att.length()>5) pre=att.substring(6);
120                 this.namespaces.addDeclaration(pre,uri);
121                 nslist.addElement(pre);
122                 super.contentHandler.startPrefixMapping(pre,uri);
123             }
124         }
125         if (nslist.size()>0) this.stackedNS.put(new Integer JavaDoc(this.stack),nslist);
126         // Resolve the element namespaced name
127
NamespacesTable.Name w=this.namespaces.resolve(null,name,null,null);
128         // Second loop through attributes to fill AttributesImpl
129
for (int x=0; x<a.getLength(); x++) {
130             String JavaDoc att=a.getName(x);
131             if (att.equals("xmlns") || att.startsWith("xmlns:")) continue;
132             // We have something different from a namespace declaration
133
NamespacesTable.Name k=this.namespaces.resolve(null,att,null,null);
134             String JavaDoc val=a.getValue(x);
135             String JavaDoc typ=a.getType(x);
136             String JavaDoc uri=k.getPrefix().length()==0?"":k.getUri();
137             a2.addAttribute(uri,k.getLocalName(),k.getQName(),typ,val);
138         }
139         // Notify the contentHandler
140
super.contentHandler.startElement(w.getUri(),w.getLocalName(),
141                                           w.getQName(),a2);
142         // Forward on the stack
143
this.stack++;
144     }
145
146     /**
147      * Receive notification of the end of an element.
148      */

149     public void endElement (String JavaDoc name)
150         throws SAXException JavaDoc {
151         if (super.contentHandler==null)
152             throw new SAXException JavaDoc("ContentHandler not set");
153             // Get back on the stack
154
this.stack--;
155         // Notify the contentHandler
156
NamespacesTable.Name w=this.namespaces.resolve(null,name,null,null);
157         super.contentHandler.endElement(w.getUri(),w.getLocalName(),
158                                         w.getQName());
159         // Undeclare namespaces
160
Vector JavaDoc nslist=(Vector JavaDoc)this.stackedNS.remove(new Integer JavaDoc(this.stack));
161         if (nslist==null) return;
162         if (nslist.size()==0) return;
163         Enumeration JavaDoc e=nslist.elements();
164         while (e.hasMoreElements()) {
165             String JavaDoc prefix=(String JavaDoc)e.nextElement();
166             NamespacesTable.Declaration d=namespaces.removeDeclaration(prefix);
167             super.contentHandler.endPrefixMapping(d.getPrefix());
168         }
169     }
170
171
172     /**
173      * Receive notification of character data.
174      */

175     public void characters (char ch[], int start, int len)
176         throws SAXException JavaDoc {
177         if (super.contentHandler==null)
178             throw new SAXException JavaDoc("ContentHandler not set");
179         super.contentHandler.characters(ch,start,len);
180     }
181
182
183     /**
184      * Receive notification of ignorable whitespace in element content.
185      */

186     public void ignorableWhitespace (char ch[], int start, int len)
187         throws SAXException JavaDoc {
188         if (super.contentHandler==null)
189             throw new SAXException JavaDoc("ContentHandler not set");
190         super.contentHandler.ignorableWhitespace(ch,start,len);
191     }
192
193
194     /**
195      * Receive notification of a processing instruction.
196      */

197     public void processingInstruction (String JavaDoc target, String JavaDoc data)
198         throws SAXException JavaDoc {
199         if (super.contentHandler==null)
200             throw new SAXException JavaDoc("ContentHandler not set");
201         super.contentHandler.processingInstruction(target,data);
202     }
203 }
204
Popular Tags