KickJava   Java API By Example, From Geeks To Geeks.

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


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: XMLEntityReader.java,v 1.3 2005/11/03 17:02:21 jeffsuttor Exp $
23  * @(#)SAX2StAXBaseWriter.java 1.7 05/11/17
24  *
25  * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27  
28 package com.sun.org.apache.xalan.internal.xsltc.trax;
29
30 import java.util.Vector JavaDoc;
31
32 import javax.xml.stream.Location;
33 import javax.xml.stream.XMLReporter;
34 import javax.xml.stream.XMLStreamException;
35
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.Locator JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.SAXParseException JavaDoc;
40 import org.xml.sax.ext.LexicalHandler JavaDoc;
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
42
43
44 public abstract class SAX2StAXBaseWriter extends DefaultHandler JavaDoc
45         implements
46             LexicalHandler JavaDoc {
47
48
49     protected boolean isCDATA;
50
51     protected StringBuffer JavaDoc CDATABuffer;
52
53     protected Vector JavaDoc namespaces;
54
55     protected Locator JavaDoc docLocator;
56
57     protected XMLReporter reporter;
58
59     public SAX2StAXBaseWriter() {
60     }
61
62     public SAX2StAXBaseWriter(XMLReporter reporter) {
63         this.reporter = reporter;
64     }
65
66     public void setXMLReporter(XMLReporter reporter) {
67         this.reporter = reporter;
68     }
69
70     public void setDocumentLocator(Locator JavaDoc locator) {
71         this.docLocator = locator;
72     }
73
74
75     public Location getCurrentLocation() {
76         if (docLocator != null) {
77             return new SAXLocation(docLocator);
78         } else {
79             return null;
80         }
81
82     }
83
84     public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
85         reportException("ERROR", e);
86     }
87
88     public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
89         reportException("FATAL", e);
90     }
91
92     public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
93         reportException("WARNING", e);
94     }
95
96     public void startDocument() throws SAXException JavaDoc {
97                     namespaces = new Vector JavaDoc(2);
98     }
99
100     public void endDocument() throws SAXException JavaDoc {
101         namespaces = null;
102     }
103
104     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
105             Attributes JavaDoc attributes) throws SAXException JavaDoc {
106             namespaces = null;
107     }
108
109     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
110             throws SAXException JavaDoc {
111         namespaces = null;
112     }
113
114     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
115             throws SAXException JavaDoc {
116
117         if (prefix == null) {
118             prefix = "";
119         } else if (prefix.equals("xml")) {
120             return;
121         }
122
123         if (namespaces == null) {
124                 namespaces = new Vector JavaDoc(2);
125             }
126                 namespaces.addElement(prefix);
127                 namespaces.addElement(uri);
128         }
129             
130
131     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
132     }
133
134     public void startCDATA() throws SAXException JavaDoc {
135         isCDATA = true;
136         if (CDATABuffer == null) {
137             CDATABuffer = new StringBuffer JavaDoc();
138         } else {
139             CDATABuffer.setLength(0);
140         }
141     }
142
143     public void characters(char[] ch, int start, int length)
144             throws SAXException JavaDoc {
145         if (isCDATA) {
146             CDATABuffer.append(ch, start, length);
147         }
148     }
149
150     public void endCDATA() throws SAXException JavaDoc {
151         isCDATA = false;
152         CDATABuffer.setLength(0);
153     }
154
155     public void comment(char[] ch, int start, int length) throws SAXException JavaDoc {
156     }
157
158     public void endDTD() throws SAXException JavaDoc {
159     }
160
161     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
162     }
163
164     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
165             throws SAXException JavaDoc {
166     }
167
168     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
169     }
170
171     /**
172      * Used to report a {@link SAXException}to the {@link XMLReporter}
173      * registered with this handler.
174      */

175     protected void reportException(String JavaDoc type, SAXException JavaDoc e)
176             throws SAXException JavaDoc {
177
178         if (reporter != null) {
179             try {
180                 reporter.report(e.getMessage(), type, e, getCurrentLocation());
181             } catch (XMLStreamException e1) {
182                 throw new SAXException JavaDoc(e1);
183             }
184         }
185     }
186
187     /**
188      * Parses an XML qualified name, and places the resulting prefix and local
189      * name in the provided String array.
190      *
191      * @param qName The qualified name to parse.
192      * @param results An array where parse results will be placed. The prefix
193      * will be placed at <code>results[0]</code>, and the local
194      * part at <code>results[1]</code>
195      */

196     public static final void parseQName(String JavaDoc qName, String JavaDoc[] results) {
197
198         String JavaDoc prefix, local;
199         int idx = qName.indexOf(':');
200         if (idx >= 0) {
201             prefix = qName.substring(0, idx);
202             local = qName.substring(idx + 1);
203         } else {
204             prefix = "";
205             local = qName;
206         }
207         results[0] = prefix;
208         results[1] = local;
209     }
210
211     /**
212      * {@Link Location}implementation used to expose details from a SAX
213      * {@link Locator}.
214      *
215      * @author christian
216      * @version $Revision: 1.2 $
217      */

218     private static final class SAXLocation implements Location {
219
220         private int lineNumber;
221         private int columnNumber;
222         private String JavaDoc publicId;
223         private String JavaDoc systemId;
224         private SAXLocation(Locator JavaDoc locator) {
225             lineNumber = locator.getLineNumber();
226             columnNumber = locator.getColumnNumber();
227             publicId = locator.getPublicId();
228             systemId = locator.getSystemId();
229         }
230
231         public int getLineNumber() {
232             return lineNumber;
233         }
234
235         public int getColumnNumber() {
236             return columnNumber;
237         }
238
239         public int getCharacterOffset() {
240             return -1;
241         }
242
243         public String JavaDoc getPublicId() {
244             return publicId;
245         }
246
247         public String JavaDoc getSystemId() {
248             return systemId;
249         }
250     }
251 }
252
Popular Tags