[jdom-interest] Visibility patch: org.jdom.input

Jason Hunter jhunter at acm.org
Tue May 27 15:39:50 PDT 2003


Here's a visibility patch for org.jdom.input.

* Made SAXBuilder.factory private.  However, I now expose it with a new 
public getFactory() method.

* It also seemed to make sense to add a lot of new public getters on 
SAXBuilder.  Any subclass will need to know how to behave so these will 
expose that info, plus it's just nice to have getters for setters.  I 
didn't write getters for features and properties.

getDriverClass()
getValidation()
getErrorHandler()
getEntityResolver()
getDTDHandler()
getXMLFilter()
getIgnoringElementContentWhitespace()
getReuseParser()
getExpandEntities()

* Removed the SAXBuilder utility method fileToURL() which we used in JDK 
1.1 because it exists now in JDK 1.2.  It was protected, now gone.

* While I was in SAXBuilder I made the parser reuse flag default to 
true.  I kept the setReuseParser() method so you can turn it off if you 
hit a parser reuse problem.

* In SAXHandler I changed several fields from protected to private: 
currentElement, atRoot, inDTD, inInternalSubset, previousCDATA, inCDATA, 
suppress, declaredNamespaces.

* Also changed flushCharacters(String) and appendExternalId() to private.

* Added a new protected method SAXHandler.setAlternateRoot() for use 
when you want to build under an existing root element, as JDOMResult 
needs.  I'd appreciate Laurent especially giving this method a look.

* Made the protected getCurrentElement() method public.  It seems common 
for subclasses to want to see the current element being operated upon 
and so we might as well expose it publicly.

* Reordered the JDOMParseException constructor to be more logical. 
Deprecated the old model just in case anyone's relying on it.

* Changed the visibility on TextBuffer methods to be package protected 
to match the class's visibility.

-jh-
-------------- next part --------------
Index: JDOMParseException.java
===================================================================
RCS file: /home/cvs/jdom/src/java/org/jdom/input/JDOMParseException.java,v
retrieving revision 1.4
diff -u -r1.4 JDOMParseException.java
--- JDOMParseException.java	30 Apr 2003 09:55:13 -0000	1.4
+++ JDOMParseException.java	27 May 2003 23:06:54 -0000
@@ -1,36 +1,36 @@
-/*-- 
+/*--
 
  $Id: JDOMParseException.java,v 1.4 2003/04/30 09:55:13 jhunter Exp $
 
  Copyright (C) 2000 Jason Hunter & Brett McLaughlin.
  All rights reserved.
- 
+
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
- 
+
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions, and the following disclaimer.
- 
+
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions, and the disclaimer that follows 
-    these conditions in the documentation and/or other materials 
+    notice, this list of conditions, and the disclaimer that follows
+    these conditions in the documentation and/or other materials
     provided with the distribution.
 
  3. The name "JDOM" must not be used to endorse or promote products
     derived from this software without prior written permission.  For
     written permission, please contact <request_AT_jdom_DOT_org>.
- 
+
  4. Products derived from this software may not be called "JDOM", nor
     may "JDOM" appear in their name, without prior written permission
     from the JDOM Project Management <request_AT_jdom_DOT_org>.
- 
- In addition, we request (but do not require) that you include in the 
- end-user documentation provided with the redistribution and/or in the 
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
  software itself an acknowledgement equivalent to the following:
      "This product includes software developed by the
       JDOM Project (http://www.jdom.org/)."
- Alternatively, the acknowledgment may be graphical using the logos 
+ Alternatively, the acknowledgment may be graphical using the logos
  available at http://www.jdom.org/images/logos.
 
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
@@ -46,12 +46,12 @@
  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGE.
 
- This software consists of voluntary contributions made by many 
- individuals on behalf of the JDOM Project and was originally 
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  Brett McLaughlin <brett_AT_jdom_DOT_org>.  For more information
  on the JDOM Project, please see <http://www.jdom.org/>.
- 
+
  */
 
 package org.jdom.input;
@@ -62,13 +62,13 @@
 /**
  * Thrown during parse errors, with information about where the parse error
  * occurred as well as access to the partially built document.
- * 
+ *
  * @version $Revision: 1.4 $, $Date: 2003/04/30 09:55:13 $
  * @author  Laurent Bihanic
  */
 public class JDOMParseException extends JDOMException {
 
-    private static final String CVS_ID = 
+    private static final String CVS_ID =
       "@(#) $RCSfile: JDOMParseException.java,v $ $Revision: 1.4 $ $Date: 2003/04/30 09:55:13 $ $Name:  $";
 
     /**
@@ -88,7 +88,7 @@
      *              to be thrown.
      */
     public JDOMParseException(String message, Throwable cause)  {
-        this(message, null, cause);
+        this(message, cause, null);
     }
 
     /**
@@ -98,23 +98,32 @@
      *
      * @param message <code>String</code> message indicating
      *                the problem that occurred.
+     * @param cause <code>Throwable</code> that caused this
+     *              to be thrown.
      * @param partialDocument <code>Document</code> the portion of
      *                        the input XML document that was
      *                        successfully built.
-     * @param cause <code>Throwable</code> that caused this
-     *              to be thrown.
      */
-    public JDOMParseException(String message, Document partialDocument,
-                                              Throwable cause)  {
+    public JDOMParseException(String message, Throwable cause,
+                              Document partialDocument)  {
         super(message, cause);
         this.partialDocument = partialDocument;
     }
 
     /**
-     * Returns the partial document that was successfully built before 
+     * @deprecated Use {@link JDOMParseException(String, Throwable, Document)}
+     * instead.
+     */
+    public JDOMParseException(String message, Document partialDocument,
+                              Throwable cause) {
+        this(message, cause, partialDocument);
+    }
+
+    /**
+     * Returns the partial document that was successfully built before
      * the error occurred.
      *
-     * @return the partial document.
+     * @return the partial document or null if none.
      */
     public Document getPartialDocument() {
         return partialDocument;
Index: SAXBuilder.java
===================================================================
RCS file: /home/cvs/jdom/src/java/org/jdom/input/SAXBuilder.java,v
retrieving revision 1.77
diff -u -r1.77 SAXBuilder.java
--- SAXBuilder.java	30 Apr 2003 21:58:19 -0000	1.77
+++ SAXBuilder.java	27 May 2003 23:06:58 -0000
@@ -120,9 +120,9 @@
 
     /** XMLFilter instance to use */
     private XMLFilter saxXMLFilter = null;
- 
+
     /** The factory for creating new JDOM objects */
-    protected JDOMFactory factory = null;
+    private JDOMFactory factory = new DefaultJDOMFactory();
 
     /** Whether to ignore ignorable whitespace */
     private boolean ignoringWhite = false;
@@ -135,9 +135,9 @@
 
     /**
      * Whether parser reuse is allowed.
-     * <p>Default: <code>false</code></p>
+     * <p>Default: <code>true</code></p>
      */
-    private boolean reuseParser = false;
+    private boolean reuseParser = true;
 
     /** The current SAX parser, if parser reuse has been activated. */
     private XMLReader saxParser = null;
@@ -190,6 +190,23 @@
         this.validate = validate;
     }
 
+    /**
+     * Returns the driver class assigned in the constructor, or null if none.
+     *
+     * @return the driver class assigned in the constructor
+     */
+    public String getDriverClass() {
+        return saxDriverClass;
+    }
+
+    /**
+     * Returns the current {@link JDOMFactory} in use.
+     * @return the factory in use
+     */
+    public JDOMFactory getFactory() {
+        return factory;
+    }
+
     /*
      * This sets a custom JDOMFactory for the builder.  Use this to build
      * the tree with your own subclasses of the JDOM classes.
@@ -201,6 +218,15 @@
     }
 
     /**
+     * Returns whether validation is to be performed during the build.
+     *
+     * @return whether validation is to be performed during the build
+     */
+    public boolean getValidation() {
+        return validate;
+    }
+
+    /**
      * This sets validation for the builder.
      *
      * @param validate <code>boolean</code> indicating whether validation 
@@ -211,6 +237,14 @@
     }
 
     /**
+     * Returns the {@link ErrorHandler} assigned, or null if none.
+     * @return
+     */
+    public ErrorHandler getErrorHandler() {
+        return saxErrorHandler;
+    }
+
+    /**
      * This sets custom ErrorHandler for the <code>Builder</code>.
      *
      * @param errorHandler <code>ErrorHandler</code>
@@ -220,6 +254,15 @@
     }
 
     /**
+     * Returns the {@link EntityResolver} assigned, or null if none.
+     *
+     * @return the EntityResolver assigned
+     */
+    public EntityResolver getEntityResolver() {
+        return saxEntityResolver;
+    }
+
+    /**
      * This sets custom EntityResolver for the <code>Builder</code>.
      *
      * @param entityResolver <code>EntityResolver</code>
@@ -229,6 +272,15 @@
     }
 
     /**
+     * Returns the {@link DTDHandler} assigned, or null if none.
+     *
+     * @return the DTDHandler assigned
+     */
+    public DTDHandler getDTDHandler() {
+        return saxDTDHandler;
+    }
+
+    /**
      * This sets custom DTDHandler for the <code>Builder</code>.
      *
      * @param dtdHandler <code>DTDHandler</code>
@@ -238,14 +290,34 @@
     }
 
     /**
-     * This sets custom XMLFilter for the <code>Builder</code>.
+     * Returns the {@link XMLFilter} used during parsing, or null if none.
      *
-     * @param xmlFilter <code>XMLFilter</code>
+     * @return the XMLFilter used during parsing
+     */
+    public XMLFilter getXMLFilter() {
+        return saxXMLFilter;
+    }
+
+    /**
+     * This sets a custom {@link org.xml.sax.XMLFilter} for the builder.
+     *
+     * @param xmlFilter the filter to use
      */
     public void setXMLFilter(XMLFilter xmlFilter) {
         saxXMLFilter = xmlFilter;
     }
- 
+
+    /**
+     * Returns whether element content whitespace is to be ignored during the
+     * build.
+     *
+     * @return whether element content whitespace is to be ignored during the
+     * build
+     */
+    public boolean getIgnoringElementContentWhitespace() {
+        return ignoringWhite;
+    }
+
     /**
      * Specifies whether or not the parser should elminate whitespace in 
      * element content (sometimes known as "ignorable whitespace") when
@@ -262,11 +334,22 @@
     }
 
     /**
+     * Returns whether the contained SAX parser instance is reused across
+     * multiple parses.  The default is true.
+     *
+     * @return whether the contained SAX parser instance is reused across
+     * multiple parses
+     */
+    public boolean getReuseParser() {
+        return reuseParser;
+    }
+
+    /**
      * Specifies whether this builder shall reuse the same SAX parser
      * when performing subsequent parses or allocate a new parser for
      * each parse.  The default value of this setting is
-     * <code>false</code> (no parser reuse).  Setting the value to
-     * <code>true</code> can result in a performance improvement when parsing
+     * <code>true</code> (parser reuse).  Setting the value to
+     * <code>false</code> can result in a performance improvement when parsing
      * large numbers of files in sequence.
      * <p>
      * <strong>Note</strong>: As SAX parser instances are not thread safe,
@@ -380,16 +463,16 @@
             if (systemId != null) {
                 throw new JDOMParseException("Error on line " + 
                     e.getLineNumber() + " of document " + systemId,
-                    contentHandler.getDocument(), e);
+                    e, contentHandler.getDocument());
             } else {
                 throw new JDOMParseException("Error on line " +
-                    e.getLineNumber(),
-                    contentHandler.getDocument(), e);
+                    e.getLineNumber(), e,
+                    contentHandler.getDocument());
             }
         }
         catch (SAXException e) {
             throw new JDOMParseException("Error in building: " + 
-                e.getMessage(), contentHandler.getDocument(), e);
+                e.getMessage(), e, contentHandler.getDocument());
         }
         finally {
             // Explicitly nullify the handler to encourage GC
@@ -703,7 +786,7 @@
     public Document build(File file) 
         throws JDOMException, IOException {
         try {
-            URL url = fileToURL(file);
+            URL url = file.toURL();
             return build(url);
         } catch (MalformedURLException e) {
             throw new JDOMException("Error in building", e);
@@ -807,27 +890,37 @@
         return build(new InputSource(systemId));
     }
 
+//    /**
+//     * Imitation of File.toURL(), a JDK 1.2 method, reimplemented
+//     * here to work with JDK 1.1.
+//     *
+//     * @see java.io.File
+//     *
+//     * @param f the file to convert
+//     * @return the file path converted to a file: URL
+//     */
+//    protected URL fileToURL(File f) throws MalformedURLException {
+//        String path = f.getAbsolutePath();
+//        if (File.separatorChar != '/') {
+//            path = path.replace(File.separatorChar, '/');
+//        }
+//        if (!path.startsWith("/")) {
+//            path = "/" + path;
+//        }
+//        if (!path.endsWith("/") && f.isDirectory()) {
+//            path = path + "/";
+//        }
+//        return new URL("file", "", path);
+//    }
+
     /**
-     * Imitation of File.toURL(), a JDK 1.2 method, reimplemented 
-     * here to work with JDK 1.1.
-     *
-     * @see java.io.File
+     * Returns whether or not entities are being expanded into normal text
+     * content.
      *
-     * @param f the file to convert
-     * @return the file path converted to a file: URL
+     * @return whether entities are being expanded
      */
-    protected URL fileToURL(File f) throws MalformedURLException {
-        String path = f.getAbsolutePath();
-        if (File.separatorChar != '/') {
-            path = path.replace(File.separatorChar, '/');
-        }
-        if (!path.startsWith("/")) {
-            path = "/" + path;
-        }
-        if (!path.endsWith("/") && f.isDirectory()) {
-            path = path + "/";
-        }
-        return new URL("file", "", path);
+    public boolean getExpandEntities() {
+        return expand;
     }
 
     /**
Index: SAXHandler.java
===================================================================
RCS file: /home/cvs/jdom/src/java/org/jdom/input/SAXHandler.java,v
retrieving revision 1.57
diff -u -r1.57 SAXHandler.java
--- SAXHandler.java	23 May 2003 21:59:28 -0000	1.57
+++ SAXHandler.java	27 May 2003 23:07:01 -0000
@@ -87,38 +87,38 @@
     private Document document;
 
     /** <code>Element</code> object being built */
-    protected Element currentElement;
+    private Element currentElement;
 
     /** Indicator of where in the document we are */
-    protected boolean atRoot;
+    private boolean atRoot;
 
     /** Indicator of whether we are in the DocType. Note that the DTD consists
      * of both the internal subset (inside the <!DOCTYPE> tag) and the
       * external subset (in a separate .dtd file). */
-    protected boolean inDTD = false;
+    private boolean inDTD = false;
 
     /** Indicator of whether we are in the internal subset */
-    protected boolean inInternalSubset = false;
+    private boolean inInternalSubset = false;
 
     /** Indicator of whether we previously were in a CDATA */
-    protected boolean previousCDATA = false;
+    private boolean previousCDATA = false;
 
     /** Indicator of whether we are in a CDATA */
-    protected boolean inCDATA = false;
+    private boolean inCDATA = false;
 
     /** Indicator of whether we should expand entities */
     private boolean expand = true;
 
     /** Indicator of whether we are actively suppressing (non-expanding) a
         current entity */
-    protected boolean suppress = false;
+    private boolean suppress = false;
 
     /** How many nested entities we're currently within */
     private int entityDepth = 0;  // XXX may not be necessary anymore?
 
     /** Temporary holder for namespaces that have been declared with
       * startPrefixMapping, but are not yet available on the element */
-    protected List declaredNamespaces;
+    private List declaredNamespaces;
 
     /** Temporary holder for the internal subset */
     private StringBuffer internalSubset = new StringBuffer();
@@ -209,6 +209,17 @@
     }
 
     /**
+     * Builds content under a dummy root element.  Useful for building
+     * content that would otherwise be a non-well formed document.
+     *
+     * @param root root element under which content will be built
+     */
+    protected void setAlternateRoot(Element root) {
+        this.currentElement = root;
+        this.atRoot = false;
+    }
+
+    /**
      * Returns the document.  Should be called after parsing is complete.
      *
      * @return <code>Document</code> - Document that was built
@@ -602,7 +613,7 @@
         textBuffer.clear();
     }
 
-    protected void flushCharacters(String data) throws SAXException {
+    private void flushCharacters(String data) throws SAXException {
         if (data.length() == 0) {
             previousCDATA = inCDATA;
             return;
@@ -854,7 +865,7 @@
      * @param publicID the public ID
      * @param systemID the system ID
      */
-    protected void appendExternalId(String publicID, String systemID) {
+    private void appendExternalId(String publicID, String systemID) {
         if (publicID != null) {
             internalSubset.append(" PUBLIC \"")
                   .append(publicID)
@@ -878,7 +889,7 @@
      *
      * @return <code>Element</code> - element being built.
      */
-    protected Element getCurrentElement() throws SAXException {
+    public Element getCurrentElement() throws SAXException {
         if (currentElement == null) {
             throw new SAXException(
                 "Ill-formed XML document (multiple root elements detected)");
Index: TextBuffer.java
===================================================================
RCS file: /home/cvs/jdom/src/java/org/jdom/input/TextBuffer.java,v
retrieving revision 1.6
diff -u -r1.6 TextBuffer.java
--- TextBuffer.java	30 Apr 2003 09:55:13 -0000	1.6
+++ TextBuffer.java	27 May 2003 23:07:01 -0000
@@ -91,13 +91,13 @@
     private int arraySize;
 
     /** Constructor */
-    public TextBuffer() {
+    TextBuffer() {
         array = new char[4096]; // initial capacity
         arraySize = 0;
     }
 
     /** Append the specified text to the text value of this buffer. */
-    public void append(char[] source, int start, int count) {
+    void append(char[] source, int start, int count) {
         if (prefixString == null) {
             // This is the first chunk, so we'll store it in the prefix string
             prefixString = new String(source, start, count);
@@ -111,7 +111,7 @@
     }
 
     /** Returns the size of the text value. */
-    public int size() {
+    int size() {
         if (prefixString == null) {
             return 0;
         }
@@ -121,7 +121,7 @@
     }
 
     /** Clears the text value and prepares the TextBuffer for reuse. */
-    public void clear() {
+    void clear() {
         arraySize = 0;
         prefixString = null;
     }


More information about the jdom-interest mailing list