286N/A * reserved comment block 286N/A * DO NOT REMOVE OR ALTER! 286N/A * Copyright 1999-2004 The Apache Software Foundation. 286N/A * Licensed under the Apache License, Version 2.0 (the "License"); 286N/A * you may not use this file except in compliance with the License. 286N/A * You may obtain a copy of the License at 286N/A * Unless required by applicable law or agreed to in writing, software 286N/A * distributed under the License is distributed on an "AS IS" BASIS, 286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 286N/A * See the License for the specific language governing permissions and 286N/A * limitations under the License. 286N/A/** <p>IncrementalSAXSource_Xerces takes advantage of the fact that Xerces1 286N/A * incremental mode is already a coroutine of sorts, and just wraps our 286N/A * IncrementalSAXSource API around it.</p> 286N/A * <p>Usage example: See main().</p> 286N/A * <p>Status: Passes simple main() unit-test. NEEDS JAVADOC.</p> 286N/A // Reflection. To allow this to compile with both Xerces1 and Xerces2, which 286N/A // require very different methods and objects, we need to avoid static 286N/A // references to those APIs. So until Xerces2 is pervasive and we're willing 286N/A // to make it a prerequisite, we will rely upon relection. 286N/A /** Create a IncrementalSAXSource_Xerces, and create a SAXParser 286N/A * to go with it. Xerces2 incremental parsing is only supported if 286N/A * this constructor is used, due to limitations in the Xerces2 API (as of 286N/A * Beta 3). If you don't like that restriction, tell the Xerces folks that 286N/A * there should be a simpler way to request incremental SAX parsing. 286N/A // Xerces-2 incremental parsing support (as of Beta 3) 286N/A // ContentHandlers still get set on fIncrementalParser (to get 286N/A // conversion from XNI events to SAX events), but 286N/A // _control_ for incremental parsing must be exercised via the config. 286N/A // At this time there's no way to read the existing config, only 286N/A // to assert a new one... and only when creating a brand-new parser. 286N/A // Reflection is used to allow us to continue to compile against 286N/A // Xerces1. If/when we can abandon the older versions of the parser, 286N/A // this will simplify significantly. 286N/A // If we can't get the magic constructor, no need to look further. 286N/A // Build the parser configuration object. StandardParserConfiguration 286N/A // happens to implement XMLPullParserConfiguration, which is the API 286N/A // we're going to want to use. 286N/A "com.sun.org.apache.xerces.internal.parsers.StandardParserConfiguration",
286N/A // Preload all the needed the configuration methods... I want to know they're 286N/A // all here before we commit to trying to use them, just in case the 286N/A // Fallback if this fails (implemented in createIncrementalSAXSource) is 286N/A // to attempt Xerces-1 incremental setup. Can't do tail-call in 286N/A // constructor, so create new, copy Xerces-1 initialization, 286N/A // then throw it away... Ugh. 286N/A /** Create a IncrementalSAXSource_Xerces wrapped around 286N/A * an existing SAXParser. Currently this works only for recent 286N/A * releases of Xerces-1. Xerces-2 incremental is currently possible 286N/A * only if we are allowed to create the parser instance, due to 286N/A * limitations in the API exposed by Xerces-2 Beta 3; see the 286N/A * no-args constructor for that code. 286N/A * @exception if the SAXParser class doesn't support the Xerces 286N/A * incremental parse operations. In that case, caller should 286N/A * fall back upon the IncrementalSAXSource_Filter approach. 286N/A // Reflection is used to allow us to compile against 286N/A // Xerces2. If/when we can abandon the older versions of the parser, 286N/A // Xerces2 incremental support is made available on previously 286N/A // constructed SAXParser instances. 286N/A // Fallback if this fails (implemented in createIncrementalSAXSource) is 286N/A // to use IncrementalSAXSource_Filter rather than Xerces-specific code. 286N/A // Xerces version mismatch; neither Xerces1 nor Xerces2 succeeded. 286N/A // Fall back on filtering solution. 286N/A // Xerces version mismatch; neither Xerces1 nor Xerces2 succeeded. 286N/A // Fall back on filtering solution. 286N/A // Register handler directly with the incremental parser 286N/A // Typecast required in Xerces2; SAXParser doesn't inheret XMLReader 286N/A // %OPT% Cast at asignment? 286N/A // Register handler directly with the incremental parser 286N/A // Not supported by all SAX2 parsers but should work in Xerces: 286N/A // Typecast required in Xerces2; SAXParser doesn't inheret XMLReader 286N/A // %OPT% Cast at asignment? 286N/A // Nothing we can do about it 286N/A // Nothing we can do about it 286N/A // Register handler directly with the incremental parser 286N/A // Typecast required in Xerces2; SAXParser doesn't inheret XMLReader 286N/A // %OPT% Cast at asignment? 286N/A //================================================================ 286N/A /** startParse() is a simple API which tells the IncrementalSAXSource 286N/A * to begin reading a document. 286N/A * @throws SAXException is parse thread is already in progress 286N/A * or parsing can not be started. 286N/A /** deliverMoreNodes() is a simple API which tells the coroutine 286N/A * parser that we need more nodes. This is intended to be called 286N/A * from one of our partner routines, and serves to encapsulate the 286N/A * details of how incremental parsing has been achieved. 286N/A * @param parsemore If true, tells the incremental parser to generate 286N/A * another chunk of output. If false, tells the parser that we're 286N/A * satisfied and it can terminate parsing of this document. 286N/A * @return Boolean.TRUE if the CoroutineParser believes more data may be available 286N/A * for further parsing. Boolean.FALSE if parsing ran to completion. 286N/A * Exception if the parser objected for some reason. 286N/A // Private methods -- conveniences to hide the reflection details 286N/A // Obtain input from SAX inputSource object, construct XNI version of 286N/A // that object. Logic adapted from Xerces2. 286N/A // Bugzilla5272 patch suggested by Sandy Gao. 286N/A // Has to be reflection to run with Xerces2 286N/A // after compilation against Xerces1. or vice 286N/A // versa, due to return type mismatches. 286N/A // %REVIEW% Do first pull. Should we instead just return true? 286N/A // Take next parsing step, return false iff parsing complete: 286N/A //================================================================ 286N/A /** Simple unit test. Attempt coroutine parsing of document indicated 286N/A * by first argument (as a URI), report progress. 286N/A // Use a serializer as our sample output 286N/A // Tell coroutine to begin parsing, run while parsing is in progress 286N/A // Special test: Terminate parsing early. 286N/A // System.out.println("\nParser threw exception:"); 286N/A // ((Exception)result).printStackTrace(); 286N/A}
// class IncrementalSAXSource_Xerces