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
57 public SAX2StAXBaseWriter() {
58 }
59
60 public SAX2StAXBaseWriter(XMLReporter reporter) {
61 this.reporter = reporter;
62 }
63
64 public void setXMLReporter(XMLReporter reporter) {
65 this.reporter = reporter;
66 }
67
68 public void setDocumentLocator(Locator locator) {
69 this.docLocator = locator;
70 }
71
72
73 public Location getCurrentLocation() {
74 if (docLocator != null) {
75 return new SAXLocation(docLocator);
76 } else {
77 return null;
78 }
79
80 }
81
82 public void error(SAXParseException e) throws SAXException {
83 reportException("ERROR", e);
84 }
85
86 public void fatalError(SAXParseException e) throws SAXException {
87 reportException("FATAL", e);
88 }
89
90 public void warning(SAXParseException e) throws SAXException {
91 reportException("WARNING", e);
92 }
93
94 public void startDocument() throws SAXException {
95 namespaces = new ArrayList<>(2);
96 }
97
98 public void endDocument() throws SAXException {
99 namespaces = null;
108 throws SAXException {
109 namespaces = null;
110 }
111
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
129 public void endPrefixMapping(String prefix) throws SAXException {
130 }
131
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
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
148 public void endCDATA() throws SAXException {
|
1 /*
2 * Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.org.apache.xalan.internal.xsltc.trax;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import javax.xml.stream.Location;
31 import javax.xml.stream.XMLReporter;
32 import javax.xml.stream.XMLStreamException;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.Locator;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.SAXParseException;
37 import org.xml.sax.ext.LexicalHandler;
38 import org.xml.sax.ext.Locator2;
39 import org.xml.sax.helpers.DefaultHandler;
40
41 public abstract class SAX2StAXBaseWriter extends DefaultHandler
42 implements
43 LexicalHandler {
44
45 protected boolean isCDATA;
46
47 protected StringBuffer CDATABuffer;
48
49 protected List<String> namespaces;
50
51 protected Locator docLocator;
52
53 protected XMLReporter reporter;
54
55 String xmlVersion = null, encoding = null;
56
57 public SAX2StAXBaseWriter() {
58 }
59
60 public SAX2StAXBaseWriter(XMLReporter reporter) {
61 this.reporter = reporter;
62 }
63
64 public void setXMLReporter(XMLReporter reporter) {
65 this.reporter = reporter;
66 }
67
68 public void setDocumentLocator(Locator locator) {
69 this.docLocator = locator;
70 }
71
72 private void updateVersionAndEncoding() {
73 if (docLocator != null && docLocator instanceof Locator2) {
74 Locator2 l2 = (Locator2) docLocator;
75 xmlVersion = l2.getXMLVersion();
76 encoding = l2.getEncoding();
77 }
78 }
79
80 public void setXmlVersion(String version) {
81 this.xmlVersion = version;
82 }
83
84 public void setEncoding(String encoding) {
85 this.encoding = encoding;
86 }
87
88 void writeStartDocument() throws SAXException {
89 updateVersionAndEncoding();
90 }
91
92 public Location getCurrentLocation() {
93 if (docLocator != null) {
94 return new SAXLocation(docLocator);
95 } else {
96 return null;
97 }
98 }
99
100 public void error(SAXParseException e) throws SAXException {
101 reportException("ERROR", e);
102 }
103
104 public void fatalError(SAXParseException e) throws SAXException {
105 reportException("FATAL", e);
106 }
107
108 public void warning(SAXParseException e) throws SAXException {
109 reportException("WARNING", e);
110 }
111
112 public void startDocument() throws SAXException {
113 namespaces = new ArrayList<>(2);
114 }
115
116 public void endDocument() throws SAXException {
117 namespaces = null;
126 throws SAXException {
127 namespaces = null;
128 }
129
130 public void startPrefixMapping(String prefix, String uri)
131 throws SAXException {
132
133 if (prefix == null) {
134 prefix = "";
135 } else if (prefix.equals("xml")) {
136 return;
137 }
138
139 if (namespaces == null) {
140 namespaces = new ArrayList<>(2);
141 }
142 namespaces.add(prefix);
143 namespaces.add(uri);
144 }
145
146 public void endPrefixMapping(String prefix) throws SAXException {
147 }
148
149 public void startCDATA() throws SAXException {
150 isCDATA = true;
151 if (CDATABuffer == null) {
152 CDATABuffer = new StringBuffer();
153 } else {
154 CDATABuffer.setLength(0);
155 }
156 }
157
158 public void characters(char[] ch, int start, int length)
159 throws SAXException {
160 if (isCDATA) {
161 CDATABuffer.append(ch, start, length);
162 }
163 }
164
165 public void endCDATA() throws SAXException {
|