6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
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 |
* $Id: IncrementalSAXSource_Xerces.java,v 1.2.4.1 2005/09/15 08:15:08 suresh_emailid Exp $
|
|
22 |
*/
|
|
23 |
|
|
24 |
package com.sun.org.apache.xml.internal.dtm.ref;
|
|
25 |
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.lang.reflect.Constructor;
|
|
28 |
import java.lang.reflect.Method;
|
|
29 |
|
|
30 |
import com.sun.org.apache.xerces.internal.parsers.SAXParser;
|
|
31 |
import com.sun.org.apache.xml.internal.res.XMLErrorResources;
|
|
32 |
import com.sun.org.apache.xml.internal.res.XMLMessages;
|
12458
|
33 |
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
|
6
|
34 |
|
|
35 |
import org.xml.sax.InputSource;
|
|
36 |
import org.xml.sax.SAXException;
|
|
37 |
import org.xml.sax.XMLReader;
|
|
38 |
|
|
39 |
|
|
40 |
/** <p>IncrementalSAXSource_Xerces takes advantage of the fact that Xerces1
|
|
41 |
* incremental mode is already a coroutine of sorts, and just wraps our
|
|
42 |
* IncrementalSAXSource API around it.</p>
|
|
43 |
*
|
|
44 |
* <p>Usage example: See main().</p>
|
|
45 |
*
|
|
46 |
* <p>Status: Passes simple main() unit-test. NEEDS JAVADOC.</p>
|
|
47 |
* */
|
|
48 |
public class IncrementalSAXSource_Xerces
|
|
49 |
implements IncrementalSAXSource
|
|
50 |
{
|
|
51 |
//
|
|
52 |
// Reflection. To allow this to compile with both Xerces1 and Xerces2, which
|
|
53 |
// require very different methods and objects, we need to avoid static
|
|
54 |
// references to those APIs. So until Xerces2 is pervasive and we're willing
|
|
55 |
// to make it a prerequisite, we will rely upon relection.
|
|
56 |
//
|
|
57 |
Method fParseSomeSetup=null; // Xerces1 method
|
|
58 |
Method fParseSome=null; // Xerces1 method
|
|
59 |
Object fPullParserConfig=null; // Xerces2 pull control object
|
|
60 |
Method fConfigSetInput=null; // Xerces2 method
|
|
61 |
Method fConfigParse=null; // Xerces2 method
|
|
62 |
Method fSetInputSource=null; // Xerces2 pull control method
|
|
63 |
Constructor fConfigInputSourceCtor=null; // Xerces2 initialization method
|
|
64 |
Method fConfigSetByteStream=null; // Xerces2 initialization method
|
|
65 |
Method fConfigSetCharStream=null; // Xerces2 initialization method
|
|
66 |
Method fConfigSetEncoding=null; // Xerces2 initialization method
|
|
67 |
Method fReset=null; // Both Xerces1 and Xerces2, but diff. signatures
|
|
68 |
|
|
69 |
//
|
|
70 |
// Data
|
|
71 |
//
|
|
72 |
SAXParser fIncrementalParser;
|
|
73 |
private boolean fParseInProgress=false;
|
|
74 |
|
|
75 |
//
|
|
76 |
// Constructors
|
|
77 |
//
|
|
78 |
|
|
79 |
/** Create a IncrementalSAXSource_Xerces, and create a SAXParser
|
|
80 |
* to go with it. Xerces2 incremental parsing is only supported if
|
|
81 |
* this constructor is used, due to limitations in the Xerces2 API (as of
|
|
82 |
* Beta 3). If you don't like that restriction, tell the Xerces folks that
|
|
83 |
* there should be a simpler way to request incremental SAX parsing.
|
|
84 |
* */
|
|
85 |
public IncrementalSAXSource_Xerces()
|
|
86 |
throws NoSuchMethodException
|
|
87 |
{
|
|
88 |
try
|
|
89 |
{
|
31497
|
90 |
// This should be cleaned up and the use of reflection
|
|
91 |
// removed - see JDK-8129880
|
|
92 |
|
6
|
93 |
// Xerces-2 incremental parsing support (as of Beta 3)
|
|
94 |
// ContentHandlers still get set on fIncrementalParser (to get
|
|
95 |
// conversion from XNI events to SAX events), but
|
|
96 |
// _control_ for incremental parsing must be exercised via the config.
|
|
97 |
//
|
|
98 |
// At this time there's no way to read the existing config, only
|
|
99 |
// to assert a new one... and only when creating a brand-new parser.
|
|
100 |
//
|
|
101 |
// Reflection is used to allow us to continue to compile against
|
|
102 |
// Xerces1. If/when we can abandon the older versions of the parser,
|
|
103 |
// this will simplify significantly.
|
|
104 |
|
|
105 |
// If we can't get the magic constructor, no need to look further.
|
|
106 |
Class xniConfigClass=ObjectFactory.findProviderClass(
|
|
107 |
"com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration",
|
12458
|
108 |
true);
|
6
|
109 |
Class[] args1={xniConfigClass};
|
|
110 |
Constructor ctor=SAXParser.class.getConstructor(args1);
|
|
111 |
|
|
112 |
// Build the parser configuration object. StandardParserConfiguration
|
|
113 |
// happens to implement XMLPullParserConfiguration, which is the API
|
|
114 |
// we're going to want to use.
|
|
115 |
Class xniStdConfigClass=ObjectFactory.findProviderClass(
|
|
116 |
"com.sun.org.apache.xerces.internal.parsers.StandardParserConfiguration",
|
12458
|
117 |
true);
|
6
|
118 |
fPullParserConfig=xniStdConfigClass.newInstance();
|
|
119 |
Object[] args2={fPullParserConfig};
|
|
120 |
fIncrementalParser = (SAXParser)ctor.newInstance(args2);
|
|
121 |
|
|
122 |
// Preload all the needed the configuration methods... I want to know they're
|
|
123 |
// all here before we commit to trying to use them, just in case the
|
|
124 |
// API changes again.
|
|
125 |
Class fXniInputSourceClass=ObjectFactory.findProviderClass(
|
|
126 |
"com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource",
|
12458
|
127 |
true);
|
6
|
128 |
Class[] args3={fXniInputSourceClass};
|
|
129 |
fConfigSetInput=xniStdConfigClass.getMethod("setInputSource",args3);
|
|
130 |
|
|
131 |
Class[] args4={String.class,String.class,String.class};
|
|
132 |
fConfigInputSourceCtor=fXniInputSourceClass.getConstructor(args4);
|
|
133 |
Class[] args5={java.io.InputStream.class};
|
|
134 |
fConfigSetByteStream=fXniInputSourceClass.getMethod("setByteStream",args5);
|
|
135 |
Class[] args6={java.io.Reader.class};
|
|
136 |
fConfigSetCharStream=fXniInputSourceClass.getMethod("setCharacterStream",args6);
|
|
137 |
Class[] args7={String.class};
|
|
138 |
fConfigSetEncoding=fXniInputSourceClass.getMethod("setEncoding",args7);
|
|
139 |
|
|
140 |
Class[] argsb={Boolean.TYPE};
|
|
141 |
fConfigParse=xniStdConfigClass.getMethod("parse",argsb);
|
|
142 |
Class[] noargs=new Class[0];
|
|
143 |
fReset=fIncrementalParser.getClass().getMethod("reset",noargs);
|
|
144 |
}
|
|
145 |
catch(Exception e)
|
|
146 |
{
|
|
147 |
// Fallback if this fails (implemented in createIncrementalSAXSource) is
|
|
148 |
// to attempt Xerces-1 incremental setup. Can't do tail-call in
|
|
149 |
// constructor, so create new, copy Xerces-1 initialization,
|
|
150 |
// then throw it away... Ugh.
|
|
151 |
IncrementalSAXSource_Xerces dummy=new IncrementalSAXSource_Xerces(new SAXParser());
|
|
152 |
this.fParseSomeSetup=dummy.fParseSomeSetup;
|
|
153 |
this.fParseSome=dummy.fParseSome;
|
|
154 |
this.fIncrementalParser=dummy.fIncrementalParser;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
/** Create a IncrementalSAXSource_Xerces wrapped around
|
|
159 |
* an existing SAXParser. Currently this works only for recent
|
|
160 |
* releases of Xerces-1. Xerces-2 incremental is currently possible
|
|
161 |
* only if we are allowed to create the parser instance, due to
|
|
162 |
* limitations in the API exposed by Xerces-2 Beta 3; see the
|
|
163 |
* no-args constructor for that code.
|
|
164 |
*
|
|
165 |
* @exception if the SAXParser class doesn't support the Xerces
|
|
166 |
* incremental parse operations. In that case, caller should
|
|
167 |
* fall back upon the IncrementalSAXSource_Filter approach.
|
|
168 |
* */
|
|
169 |
public IncrementalSAXSource_Xerces(SAXParser parser)
|
|
170 |
throws NoSuchMethodException
|
|
171 |
{
|
|
172 |
// Reflection is used to allow us to compile against
|
|
173 |
// Xerces2. If/when we can abandon the older versions of the parser,
|
|
174 |
// this constructor will simply have to fail until/unless the
|
|
175 |
// Xerces2 incremental support is made available on previously
|
|
176 |
// constructed SAXParser instances.
|
|
177 |
fIncrementalParser=parser;
|
|
178 |
Class me=parser.getClass();
|
|
179 |
Class[] parms={InputSource.class};
|
|
180 |
fParseSomeSetup=me.getMethod("parseSomeSetup",parms);
|
|
181 |
parms=new Class[0];
|
|
182 |
fParseSome=me.getMethod("parseSome",parms);
|
|
183 |
// Fallback if this fails (implemented in createIncrementalSAXSource) is
|
|
184 |
// to use IncrementalSAXSource_Filter rather than Xerces-specific code.
|
|
185 |
}
|
|
186 |
|
|
187 |
//
|
|
188 |
// Factories
|
|
189 |
//
|
|
190 |
static public IncrementalSAXSource createIncrementalSAXSource()
|
|
191 |
{
|
|
192 |
try
|
|
193 |
{
|
|
194 |
return new IncrementalSAXSource_Xerces();
|
|
195 |
}
|
|
196 |
catch(NoSuchMethodException e)
|
|
197 |
{
|
|
198 |
// Xerces version mismatch; neither Xerces1 nor Xerces2 succeeded.
|
|
199 |
// Fall back on filtering solution.
|
|
200 |
IncrementalSAXSource_Filter iss=new IncrementalSAXSource_Filter();
|
|
201 |
iss.setXMLReader(new SAXParser());
|
|
202 |
return iss;
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
static public IncrementalSAXSource
|
|
207 |
createIncrementalSAXSource(SAXParser parser) {
|
|
208 |
try
|
|
209 |
{
|
|
210 |
return new IncrementalSAXSource_Xerces(parser);
|
|
211 |
}
|
|
212 |
catch(NoSuchMethodException e)
|
|
213 |
{
|
|
214 |
// Xerces version mismatch; neither Xerces1 nor Xerces2 succeeded.
|
|
215 |
// Fall back on filtering solution.
|
|
216 |
IncrementalSAXSource_Filter iss=new IncrementalSAXSource_Filter();
|
|
217 |
iss.setXMLReader(parser);
|
|
218 |
return iss;
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
//
|
|
223 |
// Public methods
|
|
224 |
//
|
|
225 |
|
|
226 |
// Register handler directly with the incremental parser
|
|
227 |
public void setContentHandler(org.xml.sax.ContentHandler handler)
|
|
228 |
{
|
|
229 |
// Typecast required in Xerces2; SAXParser doesn't inheret XMLReader
|
|
230 |
// %OPT% Cast at asignment?
|
|
231 |
((XMLReader)fIncrementalParser).setContentHandler(handler);
|
|
232 |
}
|
|
233 |
|
|
234 |
// Register handler directly with the incremental parser
|
|
235 |
public void setLexicalHandler(org.xml.sax.ext.LexicalHandler handler)
|
|
236 |
{
|
|
237 |
// Not supported by all SAX2 parsers but should work in Xerces:
|
|
238 |
try
|
|
239 |
{
|
|
240 |
// Typecast required in Xerces2; SAXParser doesn't inheret XMLReader
|
|
241 |
// %OPT% Cast at asignment?
|
|
242 |
((XMLReader)fIncrementalParser).setProperty("http://xml.org/sax/properties/lexical-handler",
|
|
243 |
handler);
|
|
244 |
}
|
|
245 |
catch(org.xml.sax.SAXNotRecognizedException e)
|
|
246 |
{
|
|
247 |
// Nothing we can do about it
|
|
248 |
}
|
|
249 |
catch(org.xml.sax.SAXNotSupportedException e)
|
|
250 |
{
|
|
251 |
// Nothing we can do about it
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
// Register handler directly with the incremental parser
|
|
256 |
public void setDTDHandler(org.xml.sax.DTDHandler handler)
|
|
257 |
{
|
|
258 |
// Typecast required in Xerces2; SAXParser doesn't inheret XMLReader
|
|
259 |
// %OPT% Cast at asignment?
|
|
260 |
((XMLReader)fIncrementalParser).setDTDHandler(handler);
|
|
261 |
}
|
|
262 |
|
|
263 |
//================================================================
|
|
264 |
/** startParse() is a simple API which tells the IncrementalSAXSource
|
|
265 |
* to begin reading a document.
|
|
266 |
*
|
|
267 |
* @throws SAXException is parse thread is already in progress
|
|
268 |
* or parsing can not be started.
|
|
269 |
* */
|
|
270 |
public void startParse(InputSource source) throws SAXException
|
|
271 |
{
|
|
272 |
if (fIncrementalParser==null)
|
|
273 |
throw new SAXException(XMLMessages.createXMLMessage(XMLErrorResources.ER_STARTPARSE_NEEDS_SAXPARSER, null)); //"startParse needs a non-null SAXParser.");
|
|
274 |
if (fParseInProgress)
|
|
275 |
throw new SAXException(XMLMessages.createXMLMessage(XMLErrorResources.ER_STARTPARSE_WHILE_PARSING, null)); //"startParse may not be called while parsing.");
|
|
276 |
|
|
277 |
boolean ok=false;
|
|
278 |
|
|
279 |
try
|
|
280 |
{
|
|
281 |
ok = parseSomeSetup(source);
|
|
282 |
}
|
|
283 |
catch(Exception ex)
|
|
284 |
{
|
|
285 |
throw new SAXException(ex);
|
|
286 |
}
|
|
287 |
|
|
288 |
if(!ok)
|
|
289 |
throw new SAXException(XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_INIT_PARSER, null)); //"could not initialize parser with");
|
|
290 |
}
|
|
291 |
|
|
292 |
|
|
293 |
/** deliverMoreNodes() is a simple API which tells the coroutine
|
|
294 |
* parser that we need more nodes. This is intended to be called
|
|
295 |
* from one of our partner routines, and serves to encapsulate the
|
|
296 |
* details of how incremental parsing has been achieved.
|
|
297 |
*
|
|
298 |
* @param parsemore If true, tells the incremental parser to generate
|
|
299 |
* another chunk of output. If false, tells the parser that we're
|
|
300 |
* satisfied and it can terminate parsing of this document.
|
|
301 |
* @return Boolean.TRUE if the CoroutineParser believes more data may be available
|
|
302 |
* for further parsing. Boolean.FALSE if parsing ran to completion.
|
|
303 |
* Exception if the parser objected for some reason.
|
|
304 |
* */
|
|
305 |
public Object deliverMoreNodes (boolean parsemore)
|
|
306 |
{
|
|
307 |
if(!parsemore)
|
|
308 |
{
|
|
309 |
fParseInProgress=false;
|
|
310 |
return Boolean.FALSE;
|
|
311 |
}
|
|
312 |
|
|
313 |
Object arg;
|
|
314 |
try {
|
|
315 |
boolean keepgoing = parseSome();
|
|
316 |
arg = keepgoing ? Boolean.TRUE : Boolean.FALSE;
|
|
317 |
} catch (SAXException ex) {
|
|
318 |
arg = ex;
|
|
319 |
} catch (IOException ex) {
|
|
320 |
arg = ex;
|
|
321 |
} catch (Exception ex) {
|
|
322 |
arg = new SAXException(ex);
|
|
323 |
}
|
|
324 |
return arg;
|
|
325 |
}
|
|
326 |
|
|
327 |
// Private methods -- conveniences to hide the reflection details
|
|
328 |
private boolean parseSomeSetup(InputSource source)
|
|
329 |
throws SAXException, IOException, IllegalAccessException,
|
|
330 |
java.lang.reflect.InvocationTargetException,
|
|
331 |
java.lang.InstantiationException
|
|
332 |
{
|
|
333 |
if(fConfigSetInput!=null)
|
|
334 |
{
|
|
335 |
// Obtain input from SAX inputSource object, construct XNI version of
|
|
336 |
// that object. Logic adapted from Xerces2.
|
|
337 |
Object[] parms1={source.getPublicId(),source.getSystemId(),null};
|
|
338 |
Object xmlsource=fConfigInputSourceCtor.newInstance(parms1);
|
|
339 |
Object[] parmsa={source.getByteStream()};
|
|
340 |
fConfigSetByteStream.invoke(xmlsource,parmsa);
|
|
341 |
parmsa[0]=source.getCharacterStream();
|
|
342 |
fConfigSetCharStream.invoke(xmlsource,parmsa);
|
|
343 |
parmsa[0]=source.getEncoding();
|
|
344 |
fConfigSetEncoding.invoke(xmlsource,parmsa);
|
|
345 |
|
|
346 |
// Bugzilla5272 patch suggested by Sandy Gao.
|
|
347 |
// Has to be reflection to run with Xerces2
|
|
348 |
// after compilation against Xerces1. or vice
|
|
349 |
// versa, due to return type mismatches.
|
|
350 |
Object[] noparms=new Object[0];
|
|
351 |
fReset.invoke(fIncrementalParser,noparms);
|
|
352 |
|
|
353 |
parmsa[0]=xmlsource;
|
|
354 |
fConfigSetInput.invoke(fPullParserConfig,parmsa);
|
|
355 |
|
|
356 |
// %REVIEW% Do first pull. Should we instead just return true?
|
|
357 |
return parseSome();
|
|
358 |
}
|
|
359 |
else
|
|
360 |
{
|
|
361 |
Object[] parm={source};
|
|
362 |
Object ret=fParseSomeSetup.invoke(fIncrementalParser,parm);
|
|
363 |
return ((Boolean)ret).booleanValue();
|
|
364 |
}
|
|
365 |
}
|
|
366 |
// Would null work???
|
|
367 |
private static final Object[] noparms=new Object[0];
|
|
368 |
private static final Object[] parmsfalse={Boolean.FALSE};
|
|
369 |
private boolean parseSome()
|
|
370 |
throws SAXException, IOException, IllegalAccessException,
|
|
371 |
java.lang.reflect.InvocationTargetException
|
|
372 |
{
|
|
373 |
// Take next parsing step, return false iff parsing complete:
|
|
374 |
if(fConfigSetInput!=null)
|
|
375 |
{
|
|
376 |
Object ret=(Boolean)(fConfigParse.invoke(fPullParserConfig,parmsfalse));
|
|
377 |
return ((Boolean)ret).booleanValue();
|
|
378 |
}
|
|
379 |
else
|
|
380 |
{
|
|
381 |
Object ret=fParseSome.invoke(fIncrementalParser,noparms);
|
|
382 |
return ((Boolean)ret).booleanValue();
|
|
383 |
}
|
|
384 |
}
|
|
385 |
|
|
386 |
|
|
387 |
//================================================================
|
|
388 |
/** Simple unit test. Attempt coroutine parsing of document indicated
|
|
389 |
* by first argument (as a URI), report progress.
|
|
390 |
*/
|
|
391 |
public static void _main(String args[])
|
|
392 |
{
|
|
393 |
System.out.println("Starting...");
|
|
394 |
|
|
395 |
CoroutineManager co = new CoroutineManager();
|
|
396 |
int appCoroutineID = co.co_joinCoroutineSet(-1);
|
|
397 |
if (appCoroutineID == -1)
|
|
398 |
{
|
|
399 |
System.out.println("ERROR: Couldn't allocate coroutine number.\n");
|
|
400 |
return;
|
|
401 |
}
|
|
402 |
IncrementalSAXSource parser=
|
|
403 |
createIncrementalSAXSource();
|
|
404 |
|
|
405 |
// Use a serializer as our sample output
|
|
406 |
com.sun.org.apache.xml.internal.serialize.XMLSerializer trace;
|
|
407 |
trace=new com.sun.org.apache.xml.internal.serialize.XMLSerializer(System.out,null);
|
|
408 |
parser.setContentHandler(trace);
|
|
409 |
parser.setLexicalHandler(trace);
|
|
410 |
|
|
411 |
// Tell coroutine to begin parsing, run while parsing is in progress
|
|
412 |
|
|
413 |
for(int arg=0;arg<args.length;++arg)
|
|
414 |
{
|
|
415 |
try
|
|
416 |
{
|
|
417 |
InputSource source = new InputSource(args[arg]);
|
|
418 |
Object result=null;
|
|
419 |
boolean more=true;
|
|
420 |
parser.startParse(source);
|
|
421 |
for(result = parser.deliverMoreNodes(more);
|
|
422 |
result==Boolean.TRUE;
|
|
423 |
result = parser.deliverMoreNodes(more))
|
|
424 |
{
|
|
425 |
System.out.println("\nSome parsing successful, trying more.\n");
|
|
426 |
|
|
427 |
// Special test: Terminate parsing early.
|
|
428 |
if(arg+1<args.length && "!".equals(args[arg+1]))
|
|
429 |
{
|
|
430 |
++arg;
|
|
431 |
more=false;
|
|
432 |
}
|
|
433 |
|
|
434 |
}
|
|
435 |
|
|
436 |
if (result instanceof Boolean && ((Boolean)result)==Boolean.FALSE)
|
|
437 |
{
|
|
438 |
System.out.println("\nParser ended (EOF or on request).\n");
|
|
439 |
}
|
|
440 |
else if (result == null) {
|
|
441 |
System.out.println("\nUNEXPECTED: Parser says shut down prematurely.\n");
|
|
442 |
}
|
|
443 |
else if (result instanceof Exception) {
|
|
444 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException((Exception)result);
|
|
445 |
// System.out.println("\nParser threw exception:");
|
|
446 |
// ((Exception)result).printStackTrace();
|
|
447 |
}
|
|
448 |
|
|
449 |
}
|
|
450 |
|
|
451 |
catch(SAXException e)
|
|
452 |
{
|
|
453 |
e.printStackTrace();
|
|
454 |
}
|
|
455 |
}
|
|
456 |
|
|
457 |
}
|
|
458 |
|
|
459 |
|
|
460 |
} // class IncrementalSAXSource_Xerces
|