XmlReader.java 10.4 KB
Newer Older
1
/* XmlReader.java --
Tom Tromey committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
   Copyright (C) 1999,2000,2001 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.xml.aelfred2;

import java.io.IOException;
import java.util.Locale;

import org.xml.sax.*;
import org.xml.sax.ext.*;

import gnu.xml.pipeline.EventFilter;
import gnu.xml.pipeline.ValidationConsumer;


/**
 * This SAX2 parser optionally layers a validator over the Ælfred2
 * SAX2 parser.  While this will not evaluate every XML validity constraint,
 * it does support all the validity constraints that are of any real utility
 * outside the strict SGML-compatible world.  See the documentation for the
 * SAXDriver class for information about the SAX2 features and properties
 * that are supported, and documentation for the ValidationConsumer for
 * information about what validity constraints may not be supported.
 * (Ælfred2 tests some of those, even in non-validating mode, to
 * achieve better conformance.)
 *
 * <p> Note that due to its internal construction, you can't change most
 * handlers until parse() returns.  This diverges slightly from SAX, which
 * expects later binding to be supported.  Early binding involves less
 * runtime overhead, which is an issue for event pipelines as used inside
 * this parser.  Rather than relying on the parser to handle late binding
 * to your own handlers, do it yourself.
 *
 * @see SAXDriver
 * @see gnu.xml.pipeline.ValidationConsumer
 *
 * @author David Brownell
 */
public final class XmlReader
  implements XMLReader
{

  static class FatalErrorHandler
    extends DefaultHandler2
  {
80

Tom Tromey committed
81 82 83 84 85
    public void error(SAXParseException e)
      throws SAXException
    {
      throw e;
    }
86

Tom Tromey committed
87
  }
88

Tom Tromey committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  private SAXDriver aelfred2 = new SAXDriver();
  private EventFilter filter = new EventFilter();
  private boolean isValidating;
  private boolean active;

  /**
   * Constructs a SAX Parser.
   */
  public XmlReader()
  {
  }

  /**
   * Constructs a SAX Parser, optionally treating validity errors
   * as if they were fatal errors.
   */
  public XmlReader(boolean invalidIsFatal)
  {
    if (invalidIsFatal)
      {
        setErrorHandler(new FatalErrorHandler());
      }
  }
112

Tom Tromey committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * <b>SAX2</b>: Returns the object used to report the logical
   * content of an XML document.
   */
  public ContentHandler getContentHandler()
  {
    return filter.getContentHandler();
  }

  /**
   * <b>SAX2</b>: Assigns the object used to report the logical
   * content of an XML document.
   * @exception IllegalStateException if called mid-parse
   */
  public void setContentHandler(ContentHandler handler)
  {
    if (active)
      {
        throw new IllegalStateException("already parsing");
      }
    filter.setContentHandler(handler);
  }

  /**
   * <b>SAX2</b>: Returns the object used to process declarations related
   * to notations and unparsed entities.
   */
  public DTDHandler getDTDHandler()
  {
    return filter.getDTDHandler();
  }

  /**
   * <b>SAX1</b> Assigns DTD handler
   * @exception IllegalStateException if called mid-parse
   */
  public void setDTDHandler(DTDHandler handler)
  {
    if (active)
      {
        throw new IllegalStateException("already parsing");
      }
    filter.setDTDHandler(handler);
  }
157

Tom Tromey committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  /**
   * <b>SAX2</b>: Returns the object used when resolving external
   * entities during parsing (both general and parameter entities).
   */
  public EntityResolver getEntityResolver()
  {
    return aelfred2.getEntityResolver();
  }

  /**
   * <b>SAX1</b> Assigns parser's entity resolver
   */
  public void setEntityResolver(EntityResolver handler)
  {
    aelfred2.setEntityResolver(handler);
  }

  /**
   * <b>SAX2</b>: Returns the object used to receive callbacks for XML
   * errors of all levels (fatal, nonfatal, warning); this is never null;
   */
  public ErrorHandler getErrorHandler()
  {
    return aelfred2.getErrorHandler();
  }

  /**
   * <b>SAX1</b> Assigns error handler
   * @exception IllegalStateException if called mid-parse
   */
  public void setErrorHandler(ErrorHandler handler)
  {
    if (active)
      {
        throw new IllegalStateException("already parsing");
      }
    aelfred2.setErrorHandler(handler);
  }

  /**
   * <b>SAX2</b>:  Assigns the specified property.
   * @exception IllegalStateException if called mid-parse
   */
  public void setProperty(String propertyId, Object value)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    if (active)
      {
        throw new IllegalStateException("already parsing");
      }
    if (getProperty(propertyId) != value)
      {
        filter.setProperty(propertyId, value);
      }
  }

  /**
   * <b>SAX2</b>:  Returns the specified property.
   */
  public Object getProperty(String propertyId)
    throws SAXNotRecognizedException
  {
    if ((SAXDriver.PROPERTY + "declaration-handler").equals(propertyId)
        || (SAXDriver.PROPERTY + "lexical-handler").equals(propertyId))
      {
        return filter.getProperty(propertyId);
      }
    throw new SAXNotRecognizedException(propertyId);
  }
227

Tom Tromey committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  private void forceValidating()
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
                        true);
    aelfred2.setFeature(SAXDriver.FEATURE + "external-general-entities",
                        true);
    aelfred2.setFeature(SAXDriver.FEATURE + "external-parameter-entities",
                        true);
  }

  /**
   * <b>SAX2</b>:  Sets the state of features supported in this parser.
   * Note that this parser requires reporting of namespace prefixes when
   * validating.
   */
  public void setFeature(String featureId, boolean state)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    boolean value = getFeature(featureId);

    if (state == value)
      {
        return;
      }

    if ((SAXDriver.FEATURE + "validation").equals(featureId))
      {
        if (active)
          {
            throw new SAXNotSupportedException("already parsing");
          }
        if (state)
          {
            forceValidating();
          }
        isValidating = state;
      }
    else
      {
        aelfred2.setFeature(featureId, state);
      }
  }

  /**
   * <b>SAX2</b>: Tells whether this parser supports the specified feature.
   * At this time, this directly parallels the underlying SAXDriver,
   * except that validation is optionally supported.
   *
   * @see SAXDriver
   */
  public boolean getFeature(String featureId)
    throws SAXNotRecognizedException, SAXNotSupportedException
  {
    if ((SAXDriver.FEATURE + "validation").equals(featureId))
      {
        return isValidating;
      }

    return aelfred2.getFeature(featureId);
  }

  /**
   * <b>SAX1</b>: Sets the locale used for diagnostics; currently,
   * only locales using the English language are supported.
   * @param locale The locale for which diagnostics will be generated
   */
  public void setLocale(Locale locale)
    throws SAXException
  {
    aelfred2.setLocale(locale);
  }

  /**
   * <b>SAX1</b>: Preferred API to parse an XML document, using a
   * system identifier (URI).
     */
  public void parse(String systemId)
    throws SAXException, IOException
  {
    parse(new InputSource(systemId));
  }

  /**
   * <b>SAX1</b>: Underlying API to parse an XML document, used
   * directly when no URI is available.  When this is invoked,
   * and the parser is set to validate, some features will be
   * automatically reset to appropriate values:  for reporting
   * namespace prefixes, and incorporating external entities.
   *
   * @param source The XML input source.
   *
   * @exception IllegalStateException if called mid-parse
   * @exception SAXException The handlers may throw any SAXException,
   *  and the parser normally throws SAXParseException objects.
   * @exception IOException IOExceptions are normally through through
   *  the parser if there are problems reading the source document.
   */
  public void parse(InputSource source)
    throws SAXException, IOException
  {
    EventFilter next;
    boolean nsdecls;

    synchronized (aelfred2)
      {
        if (active)
          {
            throw new IllegalStateException("already parsing");
          }
        active = true;
      }

    // set up the output pipeline
    if (isValidating)
      {
        forceValidating();
        next = new ValidationConsumer(filter);
      }
    else
      {
        next = filter;
      }

    // connect pipeline and error handler
    // don't let _this_ call to bind() affect xmlns* attributes
    nsdecls = aelfred2.getFeature(SAXDriver.FEATURE + "namespace-prefixes");
    EventFilter.bind(aelfred2, next);
    if (!nsdecls)
      {
        aelfred2.setFeature(SAXDriver.FEATURE + "namespace-prefixes",
                            false);
      }

    // parse, clean up
    try
      {
        aelfred2.parse(source);
      }
    finally
      {
        active = false;
      }
  }

}