KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > SAX2XNI


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package com.sun.org.apache.xerces.internal.util;
58
59 import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
60 import com.sun.org.apache.xerces.internal.jaxp.validation.WrappedSAXException;
61 import com.sun.org.apache.xerces.internal.xni.QName;
62 import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
63 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
64 import com.sun.org.apache.xerces.internal.xni.XMLLocator;
65 import com.sun.org.apache.xerces.internal.xni.XMLString;
66 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
67 import org.xml.sax.Attributes JavaDoc;
68 import org.xml.sax.ContentHandler JavaDoc;
69 import org.xml.sax.Locator JavaDoc;
70 import org.xml.sax.SAXException JavaDoc;
71
72 /**
73  * Receves SAX {@link ContentHandler} events
74  * and produces the equivalent {@link XMLDocumentHandler} events.
75  *
76  * @author
77  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
78  */

79 public class SAX2XNI implements ContentHandler JavaDoc, XMLDocumentSource {
80     public SAX2XNI( XMLDocumentHandler core ) {
81         this.fCore = core;
82     }
83     
84     private XMLDocumentHandler fCore;
85
86     private final NamespaceSupport nsContext = new NamespaceSupport();
87     private final SymbolTable symbolTable = new SymbolTable();
88     
89
90     public void setDocumentHandler(XMLDocumentHandler handler) {
91         fCore = handler;
92     }
93
94     public XMLDocumentHandler getDocumentHandler() {
95         return fCore;
96     }
97     
98     
99     //
100
//
101
// ContentHandler implementation
102
//
103
//
104
public void startDocument() throws SAXException {
105         try {
106             nsContext.reset();
107             
108             XMLLocator xmlLocator;
109             if(locator==null)
110                 // some SAX source doesn't provide a locator,
111
// in which case we assume no line information is available
112
// and use a dummy locator. With this, downstream components
113
// can always assume that they will get a non-null Locator.
114
xmlLocator=new SimpleLocator(null,null,-1,-1);
115             else
116                 xmlLocator=new LocatorWrapper(locator);
117             
118             fCore.startDocument(
119                     xmlLocator,
120                     null,
121                     nsContext,
122                     null);
123         } catch( WrappedSAXException e ) {
124             throw e.exception;
125         }
126     }
127     
128     public void endDocument() throws SAXException {
129         try {
130             fCore.endDocument(null);
131         } catch( WrappedSAXException e ) {
132             throw e.exception;
133         }
134     }
135     
136     public void startElement( String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes att ) throws SAXException {
137         try {
138             fCore.startElement(createQName(uri,local,qname),createAttributes(att),null);
139         } catch( WrappedSAXException e ) {
140             throw e.exception;
141         }
142     }
143     
144     public void endElement( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException {
145         try {
146             fCore.endElement(createQName(uri,local,qname),null);
147         } catch( WrappedSAXException e ) {
148             throw e.exception;
149         }
150     }
151     
152     public void characters( char[] buf, int offset, int len ) throws SAXException {
153         try {
154             fCore.characters(new XMLString(buf,offset,len),null);
155         } catch( WrappedSAXException e ) {
156             throw e.exception;
157         }
158     }
159     
160     public void ignorableWhitespace( char[] buf, int offset, int len ) throws SAXException {
161         try {
162             fCore.ignorableWhitespace(new XMLString(buf,offset,len),null);
163         } catch( WrappedSAXException e ) {
164             throw e.exception;
165         }
166     }
167     
168     public void startPrefixMapping( String JavaDoc prefix, String JavaDoc uri ) {
169         nsContext.pushContext();
170         nsContext.declarePrefix(prefix,uri);
171     }
172     
173     public void endPrefixMapping( String JavaDoc prefix ) {
174         nsContext.popContext();
175     }
176     
177     public void processingInstruction( String JavaDoc target, String JavaDoc data ) throws SAXException {
178         try {
179             fCore.processingInstruction(
180                     symbolize(target),createXMLString(data),null);
181         } catch( WrappedSAXException e ) {
182             throw e.exception;
183         }
184     }
185     
186     public void skippedEntity( String JavaDoc name ) {
187     }
188     
189     private Locator locator;
190     public void setDocumentLocator( Locator _loc ) {
191         this.locator = _loc;
192     }
193     
194     /** Creates a QName object. */
195     private QName createQName(String JavaDoc uri, String JavaDoc local, String JavaDoc raw) {
196
197         int idx = raw.indexOf(':');
198         
199         if( local.length()==0 ) {
200             // if naemspace processing is turned off, local could be "".
201
// in that case, treat everything to be in the no namespace.
202
uri = "";
203             if(idx<0)
204                 local = raw;
205             else
206                 local = raw.substring(idx+1);
207         }
208         
209         String JavaDoc prefix;
210         if (idx < 0)
211             prefix = null;
212         else
213             prefix = raw.substring(0, idx);
214         
215         if (uri != null && uri.length() == 0)
216             uri = null; // XNI uses null whereas SAX uses the empty string
217

218         return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri));
219     }
220     
221     /** Symbolizes the specified string. */
222     private String JavaDoc symbolize(String JavaDoc s) {
223         if (s == null)
224             return null;
225         else
226             return symbolTable.addSymbol(s);
227     }
228     
229     private XMLString createXMLString(String JavaDoc str) {
230         // with my patch
231
// return new XMLString(str);
232

233         // for now
234
return new XMLString(str.toCharArray(), 0, str.length());
235     }
236     
237     
238     /** only one instance of XMLAttributes is used. */
239     private final XMLAttributes xa = new XMLAttributesImpl();
240     
241     /** Creates an XMLAttributes object. */
242     private XMLAttributes createAttributes(Attributes att) {
243         xa.removeAllAttributes();
244         int len = att.getLength();
245         for (int i = 0; i < len; i++)
246             xa.addAttribute(
247                     createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)),
248                     att.getType(i),
249                     att.getValue(i));
250         return xa;
251     }
252 }
253
Popular Tags