You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by sa...@apache.org on 2002/01/30 00:16:44 UTC
cvs commit: xml-xerces/java/src/org/apache/xerces/xni/parser XMLParserConfiguration.java XMLPullParserConfiguration.java
sandygao 02/01/29 15:16:44
Modified: java/src/org/apache/xerces/impl XMLEntityManager.java
java/src/org/apache/xerces/parsers AbstractSAXParser.java
DOMASBuilderImpl.java DOMBuilderImpl.java
DOMParser.java StandardParserConfiguration.java
java/src/org/apache/xerces/xni/parser
XMLParserConfiguration.java
XMLPullParserConfiguration.java
Log:
Fixing three problems:
1. Close all streams/readers even seen by the parser after the parsing is finished.
2. Provide a way to explicitly close streams/readers if a pull-parser decides to terminate parsing before the document is fully parsed.
3. Provide line/column number even if the system id is not set.
Revision Changes Path
1.23 +33 -9 xml-xerces/java/src/org/apache/xerces/impl/XMLEntityManager.java
Index: XMLEntityManager.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/XMLEntityManager.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- XMLEntityManager.java 29 Jan 2002 01:15:09 -0000 1.22
+++ XMLEntityManager.java 29 Jan 2002 23:16:42 -0000 1.23
@@ -68,6 +68,7 @@
import java.net.URL;
import java.util.Hashtable;
import java.util.Stack;
+import java.util.Vector;
import org.apache.xerces.impl.XMLErrorReporter;
import org.apache.xerces.impl.io.ASCIIReader;
@@ -114,7 +115,7 @@
* @author Andy Clark, IBM
* @author Arnaud Le Hors, IBM
*
- * @version $Id: XMLEntityManager.java,v 1.22 2002/01/29 01:15:09 lehors Exp $
+ * @version $Id: XMLEntityManager.java,v 1.23 2002/01/29 23:16:42 sandygao Exp $
*/
public class XMLEntityManager
implements XMLComponent, XMLEntityResolver {
@@ -792,6 +793,10 @@
//reader = new OneCharReader(reader);
}
+ // we've seen a new Reader. put it in a list, so that
+ // we can close it later.
+ fOwnReaders.addElement(reader);
+
// push entity on stack
if (fCurrentEntity != null) {
fEntityStack.push(fCurrentEntity);
@@ -815,6 +820,25 @@
return fEntityScanner;
} // getEntityScanner():XMLEntityScanner
+ // a list of Readers ever seen
+ protected Vector fOwnReaders = new Vector();
+
+ /**
+ * Close all opened InputStreams and Readers opened by this parser.
+ */
+ public void closeReaders() {
+ // close all readers
+ for (int i = fOwnReaders.size()-1; i >= 0; i--) {
+ try {
+ ((Reader)fOwnReaders.elementAt(i)).close();
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ // and clear the list
+ fOwnReaders.removeAllElements();
+ }
+
//
// XMLComponent methods
//
@@ -1127,6 +1151,10 @@
}
// pop stack
+ // REVISIT: we are done with the current entity, should close
+ // the associated reader
+ //fCurrentEntity.reader.close();
+ // Now we close all readers after we finish parsing
fCurrentEntity = fEntityStack.size() > 0
? (ScannedEntity)fEntityStack.pop() : null;
if (DEBUG_BUFFER) {
@@ -3055,9 +3083,8 @@
* @return The line number, or -1 if none is available.
*/
public int getLineNumber() {
- //return fCurrentEntity != null ? fCurrentEntity.lineNumber : -1;
if (fCurrentEntity != null) {
- if (fCurrentEntity.entityLocation != null && fCurrentEntity.entityLocation.getLiteralSystemId() != null ) {
+ if (fCurrentEntity.isExternal()) {
return fCurrentEntity.lineNumber;
}
else {
@@ -3065,8 +3092,7 @@
int size = fEntityStack.size();
for (int i=size-1; i>0 ; i--) {
ScannedEntity firstExternalEntity = (ScannedEntity)fEntityStack.elementAt(i);
-
- if (firstExternalEntity.entityLocation != null && firstExternalEntity.entityLocation.getLiteralSystemId() != null) {
+ if (firstExternalEntity.isExternal()) {
return firstExternalEntity.lineNumber;
}
}
@@ -3100,9 +3126,8 @@
* @return The column number, or -1 if none is available.
*/
public int getColumnNumber() {
- //return fCurrentEntity != null ? fCurrentEntity.columnNumber : -1;
if (fCurrentEntity != null) {
- if (fCurrentEntity.entityLocation != null && fCurrentEntity.entityLocation.getLiteralSystemId() != null ) {
+ if (fCurrentEntity.isExternal()) {
return fCurrentEntity.columnNumber;
}
else {
@@ -3110,8 +3135,7 @@
int size = fEntityStack.size();
for (int i=size-1; i>0 ; i--) {
ScannedEntity firstExternalEntity = (ScannedEntity)fEntityStack.elementAt(i);
-
- if (firstExternalEntity.entityLocation != null && firstExternalEntity.entityLocation.getLiteralSystemId() != null) {
+ if (firstExternalEntity.isExternal()) {
return firstExternalEntity.columnNumber;
}
}
1.22 +1 -20 xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java
Index: AbstractSAXParser.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/AbstractSAXParser.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- AbstractSAXParser.java 29 Jan 2002 20:44:02 -0000 1.21
+++ AbstractSAXParser.java 29 Jan 2002 23:16:43 -0000 1.22
@@ -112,7 +112,7 @@
* @author Arnaud Le Hors, IBM
* @author Andy Clark, IBM
*
- * @version $Id: AbstractSAXParser.java,v 1.21 2002/01/29 20:44:02 neilg Exp $
+ * @version $Id: AbstractSAXParser.java,v 1.22 2002/01/29 23:16:43 sandygao Exp $
*/
public abstract class AbstractSAXParser
extends AbstractXMLDocumentParser
@@ -1130,25 +1130,6 @@
throw (IOException)ex;
}
throw new SAXException(ex);
- }
-
- // close stream opened by the parser
- finally {
- try {
- Reader reader = source.getCharacterStream();
- if (reader != null) {
- reader.close();
- }
- else {
- InputStream is = source.getByteStream();
- if (is != null) {
- is.close();
- }
- }
- }
- catch (IOException e) {
- // ignore
- }
}
} // parse(String)
1.8 +31 -4 xml-xerces/java/src/org/apache/xerces/parsers/DOMASBuilderImpl.java
Index: DOMASBuilderImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/DOMASBuilderImpl.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DOMASBuilderImpl.java 29 Jan 2002 01:15:17 -0000 1.7
+++ DOMASBuilderImpl.java 29 Jan 2002 23:16:43 -0000 1.8
@@ -58,6 +58,7 @@
package org.apache.xerces.parsers;
import java.util.Vector;
+import java.io.IOException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
@@ -104,7 +105,7 @@
*
* @author Pavani Mukthipudi, Sun Microsystems Inc.
* @author Neil Graham, IBM
- * @version $Id: DOMASBuilderImpl.java,v 1.7 2002/01/29 01:15:17 lehors Exp $
+ * @version $Id: DOMASBuilderImpl.java,v 1.8 2002/01/29 23:16:43 sandygao Exp $
*
*/
@@ -253,8 +254,16 @@
*/
public ASModel parseASURI(String uri)
throws DOMASException, Exception {
- // need to wrap the uri with an XMLInputSource
- return parseASInputSource(new XMLInputSource(null, uri, null));
+ XMLInputSource source = new XMLInputSource(null, uri, null);
+ try {
+ return parseASInputSource(source);
+ }
+
+ catch (XNIException e) {
+ Exception ex = e.getException();
+ throw ex;
+ }
+
}
/**
@@ -286,7 +295,25 @@
throws DOMASException, Exception {
// need to wrap the DOMInputSource with an XMLInputSource
XMLInputSource xis = this.dom2xmlInputSource(is);
- return parseASInputSource(xis);
+ try {
+ return parseASInputSource(xis);
+ }
+
+ catch (XNIException e) {
+ Exception ex = e.getException();
+ throw ex;
+ }
+ finally {
+ // if we created a StringReader from the string data, need to
+ // close such reader
+ if (is.getStringData() != null) {
+ try {
+ xis.getCharacterStream().close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
}
ASModel parseASInputSource(XMLInputSource is) throws Exception {
1.7 +0 -18 xml-xerces/java/src/org/apache/xerces/parsers/DOMBuilderImpl.java
Index: DOMBuilderImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/DOMBuilderImpl.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DOMBuilderImpl.java 29 Jan 2002 21:21:50 -0000 1.6
+++ DOMBuilderImpl.java 29 Jan 2002 23:16:43 -0000 1.7
@@ -586,24 +586,6 @@
throw ex;
}
- // close stream opened by the parser
- finally {
- try {
- Reader reader = source.getCharacterStream();
- if (reader != null) {
- reader.close();
- }
- else {
- InputStream is = source.getByteStream();
- if (is != null) {
- is.close();
- }
- }
- }
- catch (IOException e) {
- // ignore
- }
- }
return getDocument();
}
1.60 +2 -21 xml-xerces/java/src/org/apache/xerces/parsers/DOMParser.java
Index: DOMParser.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/DOMParser.java,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -r1.59 -r1.60
--- DOMParser.java 25 Jan 2002 17:49:46 -0000 1.59
+++ DOMParser.java 29 Jan 2002 23:16:43 -0000 1.60
@@ -93,7 +93,7 @@
* @author Arnaud Le Hors, IBM
* @author Andy Clark, IBM
*
- * @version $Id: DOMParser.java,v 1.59 2002/01/25 17:49:46 elena Exp $
+ * @version $Id: DOMParser.java,v 1.60 2002/01/29 23:16:43 sandygao Exp $
*/
public class DOMParser
extends AbstractDOMParser {
@@ -196,25 +196,6 @@
throw new SAXException(ex);
}
- // close stream opened by the parser
- finally {
- try {
- Reader reader = source.getCharacterStream();
- if (reader != null) {
- reader.close();
- }
- else {
- InputStream is = source.getByteStream();
- if (is != null) {
- is.close();
- }
- }
- }
- catch (IOException e) {
- // ignore
- }
- }
-
} // parse(String)
/**
@@ -275,7 +256,7 @@
}
throw new SAXException(ex);
}
-
+
} // parse(InputSource)
/**
1.18 +13 -2 xml-xerces/java/src/org/apache/xerces/parsers/StandardParserConfiguration.java
Index: StandardParserConfiguration.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/parsers/StandardParserConfiguration.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- StandardParserConfiguration.java 29 Jan 2002 01:15:17 -0000 1.17
+++ StandardParserConfiguration.java 29 Jan 2002 23:16:43 -0000 1.18
@@ -122,7 +122,7 @@
* @author Arnaud Le Hors, IBM
* @author Andy Clark, IBM
*
- * @version $Id: StandardParserConfiguration.java,v 1.17 2002/01/29 01:15:17 lehors Exp $
+ * @version $Id: StandardParserConfiguration.java,v 1.18 2002/01/29 23:16:43 sandygao Exp $
*/
public class StandardParserConfiguration
extends BasicParserConfiguration
@@ -439,7 +439,7 @@
* specified locale.
*/
public void setLocale(Locale locale) throws XNIException {
- super.setLocale(locale);
+ super.setLocale(locale);
fErrorReporter.setLocale(locale);
} // setLocale(Locale)
@@ -547,6 +547,15 @@
} // parse(boolean):boolean
+ /**
+ * If the application decides to terminate parsing before the xml document
+ * is fully parsed, the application should call this method to free any
+ * resource allocated during parsing. For example, close all opened streams.
+ */
+ public void cleanup() {
+ fEntityManager.closeReaders();
+ }
+
//
// XMLParserConfiguration methods
//
@@ -588,6 +597,8 @@
}
finally {
fParseInProgress = false;
+ // close all streams opened by xerces
+ this.cleanup();
}
} // parse(InputSource)
1.5 +5 -2 xml-xerces/java/src/org/apache/xerces/xni/parser/XMLParserConfiguration.java
Index: XMLParserConfiguration.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/xni/parser/XMLParserConfiguration.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XMLParserConfiguration.java 29 Jan 2002 01:15:19 -0000 1.4
+++ XMLParserConfiguration.java 29 Jan 2002 23:16:43 -0000 1.5
@@ -112,7 +112,7 @@
* @author Arnaud Le Hors, IBM
* @author Andy Clark, IBM
*
- * @version $Id: XMLParserConfiguration.java,v 1.4 2002/01/29 01:15:19 lehors Exp $
+ * @version $Id: XMLParserConfiguration.java,v 1.5 2002/01/29 23:16:43 sandygao Exp $
*/
public interface XMLParserConfiguration
extends XMLComponentManager {
@@ -137,7 +137,10 @@
* This method is synchronous: it will not return until parsing
* has ended. If a client application wants to terminate
* parsing early, it should throw an exception.
- *
+ * <p>
+ * When this method returns, all characters streams and byte streams
+ * opened by the parser are closed.
+ *
* @param source The input source for the top-level of the
* XML document.
*
1.4 +8 -1 xml-xerces/java/src/org/apache/xerces/xni/parser/XMLPullParserConfiguration.java
Index: XMLPullParserConfiguration.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/xni/parser/XMLPullParserConfiguration.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- XMLPullParserConfiguration.java 29 Jan 2002 01:15:19 -0000 1.3
+++ XMLPullParserConfiguration.java 29 Jan 2002 23:16:44 -0000 1.4
@@ -81,7 +81,7 @@
*
* @author Andy Clark, IBM
*
- * @version $Id: XMLPullParserConfiguration.java,v 1.3 2002/01/29 01:15:19 lehors Exp $
+ * @version $Id: XMLPullParserConfiguration.java,v 1.4 2002/01/29 23:16:44 sandygao Exp $
*/
public interface XMLPullParserConfiguration
extends XMLParserConfiguration {
@@ -125,4 +125,11 @@
*/
public boolean parse(boolean complete) throws XNIException, IOException;
+ /**
+ * If the application decides to terminate parsing before the xml document
+ * is fully parsed, the application should call this method to free any
+ * resource allocated during parsing. For example, close all opened streams.
+ */
+ public void cleanup();
+
} // interface XMLPullParserConfiguration
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org