author | henryjen |
Mon, 22 Jan 2018 23:37:23 -0800 | |
changeset 48657 | 7e5fbd3a2254 |
parent 48577 | 8dff65f1d611 |
child 58022 | 12885822f0c5 |
permissions | -rw-r--r-- |
6 | 1 |
/* |
48577 | 2 |
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. |
6 | 3 |
*/ |
4 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
5 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* the License. You may obtain a copy of the License at |
6 | 11 |
* |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
6 | 13 |
* |
14 |
* Unless required by applicable law or agreed to in writing, software |
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17 |
* See the License for the specific language governing permissions and |
|
18 |
* limitations under the License. |
|
19 |
*/ |
|
20 |
||
21 |
package com.sun.org.apache.xalan.internal.xsltc.runtime.output; |
|
22 |
||
23 |
import java.io.IOException; |
|
24 |
import java.io.OutputStream; |
|
25 |
import java.io.Writer; |
|
26 |
||
27 |
import javax.xml.parsers.ParserConfigurationException; |
|
28 |
import javax.xml.stream.XMLEventWriter; |
|
29 |
import javax.xml.stream.util.XMLEventConsumer; |
|
30 |
import javax.xml.stream.XMLStreamWriter; |
|
31 |
||
32 |
import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2DOM; |
|
33 |
import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2StAXEventWriter; |
|
34 |
import com.sun.org.apache.xalan.internal.xsltc.trax.SAX2StAXStreamWriter; |
|
35 |
||
36 |
import com.sun.org.apache.xml.internal.serializer.ToHTMLSAXHandler; |
|
37 |
import com.sun.org.apache.xml.internal.serializer.ToHTMLStream; |
|
38 |
import com.sun.org.apache.xml.internal.serializer.ToTextSAXHandler; |
|
39 |
import com.sun.org.apache.xml.internal.serializer.ToTextStream; |
|
40 |
import com.sun.org.apache.xml.internal.serializer.ToUnknownStream; |
|
41 |
import com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler; |
|
42 |
import com.sun.org.apache.xml.internal.serializer.ToXMLStream; |
|
43 |
import com.sun.org.apache.xml.internal.serializer.SerializationHandler; |
|
44 |
import org.w3c.dom.Node; |
|
45 |
||
46 |
import org.xml.sax.ContentHandler; |
|
47 |
import org.xml.sax.ext.LexicalHandler; |
|
48 |
||
49 |
/** |
|
50 |
* @author Santiago Pericas-Geertsen |
|
48409 | 51 |
* @LastModified: Oct 2017 |
6 | 52 |
*/ |
53 |
public class TransletOutputHandlerFactory { |
|
54 |
||
55 |
public static final int STREAM = 0; |
|
56 |
public static final int SAX = 1; |
|
57 |
public static final int DOM = 2; |
|
58 |
public static final int STAX = 3; |
|
59 |
||
60 |
private String _encoding = "utf-8"; |
|
61 |
private String _method = null; |
|
62 |
private int _outputType = STREAM; |
|
63 |
private OutputStream _ostream = System.out; |
|
64 |
private Writer _writer = null; |
|
65 |
private Node _node = null; |
|
66 |
private Node _nextSibling = null; |
|
67 |
private XMLEventWriter _xmlStAXEventWriter = null; |
|
68 |
private XMLStreamWriter _xmlStAXStreamWriter = null; |
|
69 |
private int _indentNumber = -1; |
|
70 |
private ContentHandler _handler = null; |
|
71 |
private LexicalHandler _lexHandler = null; |
|
72 |
||
48577 | 73 |
private boolean _overrideDefaultParser; |
12458 | 74 |
|
6 | 75 |
static public TransletOutputHandlerFactory newInstance() { |
12458 | 76 |
return new TransletOutputHandlerFactory(true); |
77 |
} |
|
48577 | 78 |
static public TransletOutputHandlerFactory newInstance(boolean overrideDefaultParser) { |
79 |
return new TransletOutputHandlerFactory(overrideDefaultParser); |
|
6 | 80 |
} |
81 |
||
48577 | 82 |
public TransletOutputHandlerFactory(boolean overrideDefaultParser) { |
83 |
_overrideDefaultParser = overrideDefaultParser; |
|
12458 | 84 |
} |
6 | 85 |
public void setOutputType(int outputType) { |
86 |
_outputType = outputType; |
|
87 |
} |
|
88 |
||
89 |
public void setEncoding(String encoding) { |
|
90 |
if (encoding != null) { |
|
91 |
_encoding = encoding; |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
public void setOutputMethod(String method) { |
|
96 |
_method = method; |
|
97 |
} |
|
98 |
||
99 |
public void setOutputStream(OutputStream ostream) { |
|
100 |
_ostream = ostream; |
|
101 |
} |
|
102 |
||
103 |
public void setWriter(Writer writer) { |
|
104 |
_writer = writer; |
|
105 |
} |
|
106 |
||
107 |
public void setHandler(ContentHandler handler) { |
|
108 |
_handler = handler; |
|
109 |
} |
|
110 |
||
111 |
public void setLexicalHandler(LexicalHandler lex) { |
|
112 |
_lexHandler = lex; |
|
113 |
} |
|
114 |
||
115 |
public void setNode(Node node) { |
|
116 |
_node = node; |
|
117 |
} |
|
118 |
||
119 |
public Node getNode() { |
|
120 |
return (_handler instanceof SAX2DOM) ? ((SAX2DOM)_handler).getDOM() |
|
121 |
: null; |
|
122 |
} |
|
123 |
||
124 |
public void setNextSibling(Node nextSibling) { |
|
125 |
_nextSibling = nextSibling; |
|
126 |
} |
|
127 |
||
128 |
public XMLEventWriter getXMLEventWriter() { |
|
129 |
return (_handler instanceof SAX2StAXEventWriter) ? ((SAX2StAXEventWriter) _handler).getEventWriter() : null; |
|
130 |
} |
|
131 |
||
132 |
public void setXMLEventWriter(XMLEventWriter eventWriter) { |
|
133 |
_xmlStAXEventWriter = eventWriter; |
|
134 |
} |
|
135 |
||
136 |
public XMLStreamWriter getXMLStreamWriter() { |
|
137 |
return (_handler instanceof SAX2StAXStreamWriter) ? ((SAX2StAXStreamWriter) _handler).getStreamWriter() : null; |
|
138 |
} |
|
139 |
||
140 |
public void setXMLStreamWriter(XMLStreamWriter streamWriter) { |
|
141 |
_xmlStAXStreamWriter = streamWriter; |
|
142 |
} |
|
143 |
||
144 |
public void setIndentNumber(int value) { |
|
145 |
_indentNumber = value; |
|
146 |
} |
|
147 |
||
47477
115ed64c7822
8181155: Fix lint warnings in JAXP repo: fallthrough and static
joehw
parents:
47216
diff
changeset
|
148 |
@SuppressWarnings("fallthrough") // intentional at case STAX, SAX |
6 | 149 |
public SerializationHandler getSerializationHandler() |
150 |
throws IOException, ParserConfigurationException |
|
151 |
{ |
|
152 |
SerializationHandler result = null; |
|
153 |
switch (_outputType) |
|
154 |
{ |
|
155 |
case STREAM : |
|
156 |
||
157 |
if (_method == null) |
|
158 |
{ |
|
159 |
result = new ToUnknownStream(); |
|
160 |
} |
|
161 |
else if (_method.equalsIgnoreCase("xml")) |
|
162 |
{ |
|
163 |
||
164 |
result = new ToXMLStream(); |
|
165 |
||
166 |
} |
|
167 |
else if (_method.equalsIgnoreCase("html")) |
|
168 |
{ |
|
169 |
||
170 |
result = new ToHTMLStream(); |
|
171 |
||
172 |
} |
|
173 |
else if (_method.equalsIgnoreCase("text")) |
|
174 |
{ |
|
175 |
||
176 |
result = new ToTextStream(); |
|
177 |
||
178 |
} |
|
179 |
||
180 |
if (result != null && _indentNumber >= 0) |
|
181 |
{ |
|
182 |
result.setIndentAmount(_indentNumber); |
|
183 |
} |
|
184 |
||
185 |
result.setEncoding(_encoding); |
|
186 |
||
187 |
if (_writer != null) |
|
188 |
{ |
|
189 |
result.setWriter(_writer); |
|
190 |
} |
|
191 |
else |
|
192 |
{ |
|
193 |
result.setOutputStream(_ostream); |
|
194 |
} |
|
195 |
return result; |
|
196 |
||
197 |
case DOM : |
|
48577 | 198 |
_handler = (_node != null) ? |
199 |
new SAX2DOM(_node, _nextSibling, _overrideDefaultParser) : |
|
200 |
new SAX2DOM(_overrideDefaultParser); |
|
6 | 201 |
_lexHandler = (LexicalHandler) _handler; |
202 |
// falls through |
|
203 |
case STAX : |
|
204 |
if (_xmlStAXEventWriter != null) { |
|
205 |
_handler = new SAX2StAXEventWriter(_xmlStAXEventWriter); |
|
206 |
} else if (_xmlStAXStreamWriter != null) { |
|
207 |
_handler = new SAX2StAXStreamWriter(_xmlStAXStreamWriter); |
|
208 |
} |
|
209 |
_lexHandler = (LexicalHandler) _handler; |
|
210 |
// again falls through - Padmaja Vedula |
|
211 |
case SAX : |
|
212 |
if (_method == null) |
|
213 |
{ |
|
214 |
_method = "xml"; // default case |
|
215 |
} |
|
216 |
||
217 |
if (_method.equalsIgnoreCase("xml")) |
|
218 |
{ |
|
219 |
||
220 |
if (_lexHandler == null) |
|
221 |
{ |
|
222 |
result = new ToXMLSAXHandler(_handler, _encoding); |
|
223 |
} |
|
224 |
else |
|
225 |
{ |
|
226 |
result = |
|
227 |
new ToXMLSAXHandler( |
|
228 |
_handler, |
|
229 |
_lexHandler, |
|
230 |
_encoding); |
|
231 |
} |
|
232 |
||
233 |
} |
|
234 |
else if (_method.equalsIgnoreCase("html")) |
|
235 |
{ |
|
236 |
||
237 |
if (_lexHandler == null) |
|
238 |
{ |
|
239 |
result = new ToHTMLSAXHandler(_handler, _encoding); |
|
240 |
} |
|
241 |
else |
|
242 |
{ |
|
243 |
result = |
|
244 |
new ToHTMLSAXHandler( |
|
245 |
_handler, |
|
246 |
_lexHandler, |
|
247 |
_encoding); |
|
248 |
} |
|
249 |
||
250 |
} |
|
251 |
else if (_method.equalsIgnoreCase("text")) |
|
252 |
{ |
|
253 |
||
254 |
if (_lexHandler == null) |
|
255 |
{ |
|
256 |
result = new ToTextSAXHandler(_handler, _encoding); |
|
257 |
} |
|
258 |
else |
|
259 |
{ |
|
260 |
result = |
|
261 |
new ToTextSAXHandler( |
|
262 |
_handler, |
|
263 |
_lexHandler, |
|
264 |
_encoding); |
|
265 |
} |
|
266 |
||
267 |
} |
|
268 |
return result; |
|
269 |
} |
|
270 |
return null; |
|
271 |
} |
|
272 |
||
273 |
} |