1 /*
2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
3  * @LastModified: Oct 2017
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package com.sun.org.apache.xalan.internal.xsltc.trax;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 import javax.xml.stream.Location;
32 import javax.xml.stream.XMLReporter;
33 import javax.xml.stream.XMLStreamException;
34 import org.xml.sax.Attributes;
35 import org.xml.sax.Locator;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.SAXParseException;
38 import org.xml.sax.ext.LexicalHandler;
39 import org.xml.sax.helpers.DefaultHandler;
40 
41 
42 public abstract class SAX2StAXBaseWriter extends DefaultHandler
43                 implements
44                         LexicalHandler {
45 
46 
47         protected boolean isCDATA;
48 
49         protected StringBuffer CDATABuffer;
50 
51         protected List<String> namespaces;
52 
53         protected Locator docLocator;
54 
55         protected XMLReporter reporter;
56 
SAX2StAXBaseWriter()57         public SAX2StAXBaseWriter() {
58         }
59 
SAX2StAXBaseWriter(XMLReporter reporter)60         public SAX2StAXBaseWriter(XMLReporter reporter) {
61                 this.reporter = reporter;
62         }
63 
setXMLReporter(XMLReporter reporter)64         public void setXMLReporter(XMLReporter reporter) {
65                 this.reporter = reporter;
66         }
67 
setDocumentLocator(Locator locator)68         public void setDocumentLocator(Locator locator) {
69                 this.docLocator = locator;
70         }
71 
72 
getCurrentLocation()73         public Location getCurrentLocation() {
74                 if (docLocator != null) {
75                         return new SAXLocation(docLocator);
76                 } else {
77                         return null;
78                 }
79 
80         }
81 
error(SAXParseException e)82         public void error(SAXParseException e) throws SAXException {
83                 reportException("ERROR", e);
84         }
85 
fatalError(SAXParseException e)86         public void fatalError(SAXParseException e) throws SAXException {
87                 reportException("FATAL", e);
88         }
89 
warning(SAXParseException e)90         public void warning(SAXParseException e) throws SAXException {
91                 reportException("WARNING", e);
92         }
93 
startDocument()94         public void startDocument() throws SAXException {
95                     namespaces = new ArrayList<>(2);
96         }
97 
endDocument()98         public void endDocument() throws SAXException {
99                 namespaces = null;
100         }
101 
startElement(String uri, String localName, String qName, Attributes attributes)102         public void startElement(String uri, String localName, String qName,
103                         Attributes attributes) throws SAXException {
104                         namespaces = null;
105         }
106 
endElement(String uri, String localName, String qName)107         public void endElement(String uri, String localName, String qName)
108                         throws SAXException {
109                 namespaces = null;
110         }
111 
startPrefixMapping(String prefix, String uri)112         public void startPrefixMapping(String prefix, String uri)
113                         throws SAXException {
114 
115                 if (prefix == null) {
116                         prefix = "";
117                 } else if (prefix.equals("xml")) {
118                         return;
119                 }
120 
121                 if (namespaces == null) {
122                     namespaces = new ArrayList<>(2);
123                 }
124                 namespaces.add(prefix);
125                 namespaces.add(uri);
126         }
127 
128 
endPrefixMapping(String prefix)129         public void endPrefixMapping(String prefix) throws SAXException {
130         }
131 
startCDATA()132         public void startCDATA() throws SAXException {
133                 isCDATA = true;
134                 if (CDATABuffer == null) {
135                         CDATABuffer = new StringBuffer();
136                 } else {
137                         CDATABuffer.setLength(0);
138                 }
139         }
140 
characters(char[] ch, int start, int length)141         public void characters(char[] ch, int start, int length)
142                         throws SAXException {
143                 if (isCDATA) {
144                         CDATABuffer.append(ch, start, length);
145                 }
146         }
147 
endCDATA()148         public void endCDATA() throws SAXException {
149                 isCDATA = false;
150                 CDATABuffer.setLength(0);
151         }
152 
comment(char[] ch, int start, int length)153         public void comment(char[] ch, int start, int length) throws SAXException {
154         }
155 
endDTD()156         public void endDTD() throws SAXException {
157         }
158 
endEntity(String name)159         public void endEntity(String name) throws SAXException {
160         }
161 
startDTD(String name, String publicId, String systemId)162         public void startDTD(String name, String publicId, String systemId)
163                         throws SAXException {
164         }
165 
startEntity(String name)166         public void startEntity(String name) throws SAXException {
167         }
168 
169         /**
170          * Used to report a {@link SAXException}to the {@link XMLReporter}
171          * registered with this handler.
172          */
reportException(String type, SAXException e)173         protected void reportException(String type, SAXException e)
174                         throws SAXException {
175 
176                 if (reporter != null) {
177                         try {
178                                 reporter.report(e.getMessage(), type, e, getCurrentLocation());
179                         } catch (XMLStreamException e1) {
180                                 throw new SAXException(e1);
181                         }
182                 }
183         }
184 
185         /**
186          * Parses an XML qualified name, and places the resulting prefix and local
187          * name in the provided String array.
188          *
189          * @param qName The qualified name to parse.
190          * @param results An array where parse results will be placed. The prefix
191          *            will be placed at <code>results[0]</code>, and the local
192          *            part at <code>results[1]</code>
193          */
parseQName(String qName, String[] results)194         public static final void parseQName(String qName, String[] results) {
195 
196                 String prefix, local;
197                 int idx = qName.indexOf(':');
198                 if (idx >= 0) {
199                         prefix = qName.substring(0, idx);
200                         local = qName.substring(idx + 1);
201                 } else {
202                         prefix = "";
203                         local = qName;
204                 }
205                 results[0] = prefix;
206                 results[1] = local;
207         }
208 
209         /**
210          * {@Link Location}implementation used to expose details from a SAX
211          * {@link Locator}.
212          *
213          * @author christian
214          */
215         private static final class SAXLocation implements Location {
216 
217                 private int lineNumber;
218                 private int columnNumber;
219                 private String publicId;
220                 private String systemId;
SAXLocation(Locator locator)221                 private SAXLocation(Locator locator) {
222                         lineNumber = locator.getLineNumber();
223                         columnNumber = locator.getColumnNumber();
224                         publicId = locator.getPublicId();
225                         systemId = locator.getSystemId();
226                 }
227 
getLineNumber()228                 public int getLineNumber() {
229                         return lineNumber;
230                 }
231 
getColumnNumber()232                 public int getColumnNumber() {
233                         return columnNumber;
234                 }
235 
getCharacterOffset()236                 public int getCharacterOffset() {
237                         return -1;
238                 }
239 
getPublicId()240                 public String getPublicId() {
241                         return publicId;
242                 }
243 
getSystemId()244                 public String getSystemId() {
245                         return systemId;
246                 }
247         }
248 }
249