abstractparser.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,070 行 · 第 1/2 页

JAVA
1,070
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source 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 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the *   Free SoftwareFoundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.xml;import com.caucho.server.util.CauchoSystem;import com.caucho.util.L10N;import com.caucho.vfs.Path;import com.caucho.vfs.ReadStream;import com.caucho.vfs.Vfs;import com.caucho.vfs.VfsStream;import org.w3c.dom.Document;import org.xml.sax.*;import org.xml.sax.ext.LexicalHandler;import javax.xml.parsers.DocumentBuilderFactory;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.Hashtable;import java.util.Locale;import java.util.logging.Logger;abstract public class AbstractParser implements XMLReader, Parser{  static final Logger log = Logger.getLogger(AbstractParser.class.getName());  static final L10N L = new L10N(AbstractParser.class);  static Hashtable<String,String> _attrTypes = new Hashtable<String,String>();  static Entities _xmlEntities = new XmlEntities();  Policy _policy;  boolean _isCoalescing = true;    boolean _optionalTags = true;  boolean _skipWhitespace;  boolean _skipComments;  boolean _strictComments;  boolean _strictAttributes;  boolean _entitiesAsText = false;  boolean _expandEntities = true;  boolean _strictCharacters;  boolean _strictXml;  boolean _singleTopElement;  boolean _normalizeWhitespace = false;  boolean _forgiving;  boolean _extraForgiving;  boolean _switchToXml = false;  boolean _doResinInclude = false;  boolean _isNamespaceAware = true;  boolean _isNamespacePrefixes = true;  boolean _isSAXNamespaces = false;    boolean _isXmlnsPrefix;  boolean _isXmlnsAttribute;    boolean _isValidating = false;  boolean _isJsp;  boolean _isStaticEncoding = false;  String _defaultEncoding = "UTF-8";  // sax stuff  ContentHandler _contentHandler;  EntityResolver _entityResolver;  DTDHandler _dtdHandler;  LexicalHandler _lexicalHandler;  ErrorHandler _errorHandler;  Locale _locale;    Entities _entities;  QDocument _owner;  QDocumentType _dtd;  DOMBuilder _builder;  Path _searchPath;    String _publicId;  String _systemId;  String _filename;  int _line = 1;  /**   * Creates a new parser with the XmlPolicy and a new dtd.   */  AbstractParser()  {    this(new XmlPolicy(), null);    _policy.strictComments = true;    _policy.strictAttributes = true;    _policy.strictCharacters = true;    _policy.strictXml = true;    _policy.singleTopElement = true;    _policy.optionalTags = false;  }  /**   * Creates a new parser with a given policy and dtd.   *   * @param policy the parsing policy, handling optional tags.   * @param dtd the parser's dtd.   */  AbstractParser(Policy policy, QDocumentType dtd)  {    _policy = policy;        if (dtd == null)      dtd = new QDocumentType(null);    _dtd = dtd;    _entities = _xmlEntities;    if (policy instanceof HtmlPolicy)      _entities = HtmlEntities.create(4.0);  }  void clear()  {    _isCoalescing = true;    _isNamespaceAware = true;    _isSAXNamespaces = false;    _isNamespacePrefixes = false;    _optionalTags = true;    _skipWhitespace = false;    _skipComments = false;    _strictComments = false;    _strictAttributes = false;    _entitiesAsText = false;    _expandEntities = true;    _strictCharacters = false;    _strictXml = false;    _singleTopElement = false;    _normalizeWhitespace = false;    _forgiving = false;    _extraForgiving = false;    _switchToXml = false;    _doResinInclude = false;    _isJsp = false;    _defaultEncoding = "UTF-8";    _isStaticEncoding = false;  }  void init()  {    /*    _isXmlnsPrefix = (_isNamespaceAware ||		      _isSAXNamespaces ||		      _isNamespacePrefixes);    */    _isXmlnsPrefix = _isNamespaceAware || _isNamespacePrefixes;    _isXmlnsAttribute = _isNamespacePrefixes || ! _isNamespaceAware;  }  /**   * Sets the owner.   */  public void setOwner(QDocument doc)  {    _owner = doc;  }  public void setFilename(String filename)  {    _filename = filename;  }  /**   * Sets the configuration for a document builder.   */  public void setConfig(DocumentBuilderFactory factory)  {    if (_builder == null)      _builder = new DOMBuilder();    _isCoalescing = factory.isCoalescing();    setExpandEntities(factory.isExpandEntityReferences());    setSkipComments(factory.isIgnoringComments());    setSkipWhitespace(factory.isIgnoringElementContentWhitespace());    setNamespaceAware(factory.isNamespaceAware());    setNamespacePrefixes(false);    setValidating(factory.isValidating());  }      public void setEntitiesAsText(boolean entitiesAsText)  {    _entitiesAsText = entitiesAsText;  }  public boolean getEntitiesAsText()  {    return _entitiesAsText;  }  public void setExpandEntities(boolean expandEntities)  {    _expandEntities = expandEntities;    _policy.expandEntities = expandEntities;  }  /**   * Set to true if comments should be skipped.  If false events will be   * generated for the comments.   */  public void setSkipComments(boolean skipComments)  {    _skipComments = skipComments;  }  /**   * Set to true if ignorable-whitespace should be skipped.   */  public void setSkipWhitespace(boolean skipWhitespace)  {    _skipWhitespace = skipWhitespace;  }  /**   * Returns true if text and cdata nodes will be combined.   */  public boolean isCoalescing()  {    return _isCoalescing;  }  /**   * Set true if text and cdata nodes should be combined.   */  public void setCoalescing(boolean isCoalescing)  {    _isCoalescing = isCoalescing;  }  /**   * Returns true if the XML should be validated   */  public boolean isValidating()  {    return _isValidating;  }  /**   * Set true if the XML should be validated   */  public void setValidating(boolean isValidating)  {    _isValidating = isValidating;  }  /**   * Returns true if the parsing is namespace aware.   */  public boolean isNamespaceAware()  {    return _isNamespaceAware;  }  /**   * Set true if the parsing is namespace aware.   */  public void setNamespaceAware(boolean isNamespaceAware)  {    _isNamespaceAware = isNamespaceAware;  }  /**   * Returns true if the parsing uses sax namespaces   */  public boolean isSAXNamespaces()  {    return _isSAXNamespaces;  }  /**   * Set true if the parsing uses sax namespaces   */  public void setSAXNamespaces(boolean isNamespaces)  {    _isSAXNamespaces = isNamespaces;  }  /**   * Returns true if the parsing uses namespace prefixes   */  public boolean isNamespacePrefixes()  {    return _isNamespacePrefixes;  }  /**   * Set true if the parsing uses sax namespaces   */  public void setNamespacePrefixes(boolean isNamespaces)  {    _isNamespacePrefixes = isNamespaces;  }  /**   * If true, normalizes HTML tags to lower case.   */  public void setToLower(boolean toLower)  {    if (_policy instanceof HtmlPolicy)      ((HtmlPolicy) _policy).setToLower(toLower);  }  public boolean getSkipComments()  {    return _skipComments;  }  /**   * Sets the parser as a forgiving parser, allowing some non-strict   * XML.   *   * @param forgiving if true, forgives non-strict XML.   */  public void setForgiving(boolean forgiving)  {    _forgiving = forgiving;  }  /**   * Returns true if the parser is forgiving.   *   * @return true if the parser forgives non-strict XML.   */  public boolean getForgiving()  {    return _forgiving;  }  /**   * Set true if the parser should switch from HTML to XML if it detects   * the &lt;?xml ?> header.   */  public void setAutodetectXml(boolean autodetectXml)  {    _switchToXml = autodetectXml;  }    /**   * Sets the parser to handle special JSP forgiveness.   *   * @param isJsp if true, handles special JSP forgiveness.   */  public void setJsp(boolean isJsp)  {    _isJsp = isJsp;    if (_policy instanceof HtmlPolicy)      ((HtmlPolicy) _policy).setJsp(isJsp);  }    /**   * Returns true if the parser should handle special JSP forgiveness.   *   * @return true if handles special JSP forgiveness.   */  public boolean getJsp()  {    return _isJsp;  }  /**   * Sets the search path for included documents.  MergePaths are often   * used.   *   * @param path the path to search   */  public void setSearchPath(Path path)  {    _searchPath = path;  }  /**   * Gets the search path for included documents.  MergePaths are often   * used.   *   * @return the path to search   */  public Path getSearchPath()  {    return _searchPath;  }  /**   * Sets the default encoding if none is specified.   *   * @param encoding the default encoding   */  public void setDefaultEncoding(String encoding)  {    _defaultEncoding = encoding;  }  /**   * Gets the default encoding if none is specified.   */  public String getDefaultEncoding()  {    return _defaultEncoding;  }  /**   * Enables including of other XML documents with resin:include.   *   * @param doResinInclude if true, enables the include.   */  public void setResinInclude(boolean doResinInclude)  {    _doResinInclude = doResinInclude;  }  /**   * Returns true if resin:include will include other XML documents.   *   * @param doResinInclude if true, enables the include.   */  public boolean getResinInclude()  {    return _doResinInclude;  }  public Object getProperty(String name)    throws SAXNotRecognizedException  {    if (name.equals("http://xml.org/sax/properties/lexical-handler"))      return _lexicalHandler;    else if (name.equals("http://xml.org/sax/properties/dom-node"))      return null;    else if (name.equals("http://xml.org/sax/properties/xml-string"))      return null;    else      throw new SAXNotRecognizedException(name);  }  public void setProperty(String name, Object obj)    throws SAXNotSupportedException  {    if (name.equals("http://xml.org/sax/properties/lexical-handler"))      _lexicalHandler = (LexicalHandler) obj;    else if (name.equals("http://xml.org/sax/handlers/LexicalHandler"))      _lexicalHandler = (LexicalHandler) obj;    else      throw new SAXNotSupportedException(name);  }  public boolean getFeature(String name)    throws SAXNotRecognizedException  {    if (name.equals("http://xml.org/sax/features/namespaces"))      return _isSAXNamespaces;    else if (name.equals("http://xml.org/sax/features/namespace-prefixes"))      return _isNamespacePrefixes;    else if (name.equals("http://xml.org/sax/features/string-interning"))      return true;    else if (name.equals("http://xml.org/sax/features/validation"))      return _isValidating;    else if (name.equals("http://xml.org/sax/features/external-general-entities"))      return true;    else if (name.equals("http://xml.org/sax/features/external-parameter-entities"))      return false;    else if (name.equals("http://caucho.com/xml/features/skip-comments"))      return _skipComments;    else if (name.equals("http://caucho.com/xml/features/resin-include"))      return _doResinInclude;    else      throw new SAXNotRecognizedException(name);  }  public void setFeature(String name, boolean value)    throws SAXNotSupportedException  {    if (name.equals("http://xml.org/sax/features/namespaces")) {      _isNamespaceAware = value;    }    else if (name.equals("http://xml.org/sax/features/namespace-prefixes")) {      // setting namespace-prefixes, even if false, sets namespace-aware      // see xml/032b      _isNamespacePrefixes = value;      _isNamespaceAware = true;    }    else if (name.equals("http://caucho.com/xml/features/skip-comments")) {      _skipComments = value;    }    else if (name.equals("http://caucho.com/xml/features/resin-include"))      _doResinInclude = value;    else if (name.equals("http://xml.org/sax/features/validation"))      _isValidating = value;    else      throw new SAXNotSupportedException(name);  }  public void setLexicalHandler(LexicalHandler handler)  {    _lexicalHandler = handler;  }  /**   * Sets the callback object to find files.   *   * @param resolver the object to find files.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?