示例#1
1
  /**
   * Parse an XML document from a location identified by an URI reference. If the URI contains a
   * fragment identifier (see section 4.1 in ), the behavior is not defined by this specification.
   */
  public Document parseURI(String uri) throws LSException {

    // If DOMParser insstance is already busy parsing another document when this
    // method is called, then raise INVALID_STATE_ERR according to DOM L3 LS spec
    if (fBusy) {
      String msg =
          DOMMessageFormatter.formatMessage(
              DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null);
      throw new DOMException(DOMException.INVALID_STATE_ERR, msg);
    }

    XMLInputSource source = new XMLInputSource(null, uri, null, false);
    try {
      currentThread = Thread.currentThread();
      fBusy = true;
      parse(source);
      fBusy = false;
      if (abortNow && currentThread.isInterrupted()) {
        // reset interrupt state
        abortNow = false;
        Thread.interrupted();
      }
    } catch (Exception e) {
      fBusy = false;
      if (abortNow && currentThread.isInterrupted()) {
        Thread.interrupted();
      }
      if (abortNow) {
        abortNow = false;
        restoreHandlers();
        return null;
      }
      // Consume this exception if the user
      // issued an interrupt or an abort.
      if (e != Abort.INSTANCE) {
        if (!(e instanceof XMLParseException) && fErrorHandler != null) {
          DOMErrorImpl error = new DOMErrorImpl();
          error.fException = e;
          error.fMessage = e.getMessage();
          error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
          fErrorHandler.getErrorHandler().handleError(error);
        }
        if (DEBUG) {
          e.printStackTrace();
        }
        throw (LSException) DOMUtil.createLSException(LSException.PARSE_ERR, e).fillInStackTrace();
      }
    }
    Document doc = getDocument();
    dropDocumentReferences();
    return doc;
  }
示例#2
1
  /** Parse an XML document from a resource identified by an <code>LSInput</code>. */
  public Document parse(LSInput is) throws LSException {

    // need to wrap the LSInput with an XMLInputSource
    XMLInputSource xmlInputSource = dom2xmlInputSource(is);
    if (fBusy) {
      String msg =
          DOMMessageFormatter.formatMessage(
              DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null);
      throw new DOMException(DOMException.INVALID_STATE_ERR, msg);
    }

    try {
      currentThread = Thread.currentThread();
      fBusy = true;
      parse(xmlInputSource);
      fBusy = false;
      if (abortNow && currentThread.isInterrupted()) {
        // reset interrupt state
        abortNow = false;
        Thread.interrupted();
      }
    } catch (Exception e) {
      fBusy = false;
      if (abortNow && currentThread.isInterrupted()) {
        Thread.interrupted();
      }
      if (abortNow) {
        abortNow = false;
        restoreHandlers();
        return null;
      }
      // Consume this exception if the user
      // issued an interrupt or an abort.
      if (e != Abort.INSTANCE) {
        if (!(e instanceof XMLParseException) && fErrorHandler != null) {
          DOMErrorImpl error = new DOMErrorImpl();
          error.fException = e;
          error.fMessage = e.getMessage();
          error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
          fErrorHandler.getErrorHandler().handleError(error);
        }
        if (DEBUG) {
          e.printStackTrace();
        }
        throw (LSException) DOMUtil.createLSException(LSException.PARSE_ERR, e).fillInStackTrace();
      }
    }
    Document doc = getDocument();
    dropDocumentReferences();
    return doc;
  }
示例#3
0
  /**
   * Initialize upon first access. Will load all the HTML character references into a list that is
   * accessible by name or character value and is optimized for character substitution. This method
   * may be called any number of times but will execute only once.
   */
  private static void initialize() {
    InputStream is = null;
    BufferedReader reader = null;
    int index;
    String name;
    String value;
    int code;
    String line;

    // Make sure not to initialize twice.
    if (_byName != null) return;
    try {
      _byName = new Hashtable();
      _byChar = new Hashtable();
      is = HTMLdtd.class.getResourceAsStream(ENTITIES_RESOURCE);
      if (is == null) {
        throw new RuntimeException(
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.SERIALIZER_DOMAIN,
                "ResourceNotFound",
                new Object[] {ENTITIES_RESOURCE}));
      }
      reader = new BufferedReader(new InputStreamReader(is, "ASCII"));
      line = reader.readLine();
      while (line != null) {
        if (line.length() == 0 || line.charAt(0) == '#') {
          line = reader.readLine();
          continue;
        }
        index = line.indexOf(' ');
        if (index > 1) {
          name = line.substring(0, index);
          ++index;
          if (index < line.length()) {
            value = line.substring(index);
            index = value.indexOf(' ');
            if (index > 0) value = value.substring(0, index);
            code = Integer.parseInt(value);
            defineEntity(name, (char) code);
          }
        }
        line = reader.readLine();
      }
      is.close();
    } catch (Exception except) {
      throw new RuntimeException(
          DOMMessageFormatter.formatMessage(
              DOMMessageFormatter.SERIALIZER_DOMAIN,
              "ResourceNotLoaded",
              new Object[] {ENTITIES_RESOURCE, except.toString()}));
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (Exception except) {
        }
      }
    }
  }
示例#4
0
  /** Set parameters and properties */
  public void setParameter(String name, Object value) throws DOMException {
    // set features

    if (value instanceof Boolean) {
      boolean state = ((Boolean) value).booleanValue();
      try {
        if (name.equalsIgnoreCase(Constants.DOM_COMMENTS)) {
          fConfiguration.setFeature(INCLUDE_COMMENTS_FEATURE, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_DATATYPE_NORMALIZATION)) {
          fConfiguration.setFeature(NORMALIZE_DATA, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_ENTITIES)) {
          fConfiguration.setFeature(CREATE_ENTITY_REF_NODES, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_DISALLOW_DOCTYPE)) {
          fConfiguration.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_SUPPORTED_MEDIATYPES_ONLY)
            || name.equalsIgnoreCase(Constants.DOM_NORMALIZE_CHARACTERS)
            || name.equalsIgnoreCase(Constants.DOM_CHECK_CHAR_NORMALIZATION)
            || name.equalsIgnoreCase(Constants.DOM_CANONICAL_FORM)) {
          if (state) { // true is not supported
            String msg =
                DOMMessageFormatter.formatMessage(
                    DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_SUPPORTED", new Object[] {name});
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
          }
          // setting those features to false is no-op
        } else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACES)) {
          fConfiguration.setFeature(NAMESPACES, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_INFOSET)) {
          // Setting false has no effect.
          if (state) {
            // true: namespaces, namespace-declarations,
            // comments, element-content-whitespace
            fConfiguration.setFeature(NAMESPACES, true);
            fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, true);
            fConfiguration.setFeature(INCLUDE_COMMENTS_FEATURE, true);
            fConfiguration.setFeature(INCLUDE_IGNORABLE_WHITESPACE, true);

            // false: validate-if-schema, entities,
            // datatype-normalization, cdata-sections
            fConfiguration.setFeature(DYNAMIC_VALIDATION, false);
            fConfiguration.setFeature(CREATE_ENTITY_REF_NODES, false);
            fConfiguration.setFeature(NORMALIZE_DATA, false);
            fConfiguration.setFeature(CREATE_CDATA_NODES_FEATURE, false);
          }
        } else if (name.equalsIgnoreCase(Constants.DOM_CDATA_SECTIONS)) {
          fConfiguration.setFeature(CREATE_CDATA_NODES_FEATURE, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_NAMESPACE_DECLARATIONS)) {
          fConfiguration.setFeature(Constants.DOM_NAMESPACE_DECLARATIONS, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_WELLFORMED)
            || name.equalsIgnoreCase(Constants.DOM_IGNORE_UNKNOWN_CHARACTER_DENORMALIZATIONS)) {
          if (!state) { // false is not supported
            String msg =
                DOMMessageFormatter.formatMessage(
                    DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_SUPPORTED", new Object[] {name});
            throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
          }
          // setting these features to true is no-op
          // REVISIT: implement "namespace-declaration" feature
        } else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE)) {
          fConfiguration.setFeature(VALIDATION_FEATURE, state);
          if (fSchemaType != Constants.NS_DTD) {
            fConfiguration.setFeature(XMLSCHEMA, state);
            fConfiguration.setFeature(XMLSCHEMA_FULL_CHECKING, state);
          }
          if (state) {
            fConfiguration.setFeature(DYNAMIC_VALIDATION, false);
          }
        } else if (name.equalsIgnoreCase(Constants.DOM_VALIDATE_IF_SCHEMA)) {
          fConfiguration.setFeature(DYNAMIC_VALIDATION, state);
          // Note: validation and dynamic validation are mutually exclusive
          if (state) {
            fConfiguration.setFeature(VALIDATION_FEATURE, false);
          }
        } else if (name.equalsIgnoreCase(Constants.DOM_ELEMENT_CONTENT_WHITESPACE)) {
          fConfiguration.setFeature(INCLUDE_IGNORABLE_WHITESPACE, state);
        } else if (name.equalsIgnoreCase(Constants.DOM_PSVI)) {
          // XSModel - turn on PSVI augmentation
          fConfiguration.setFeature(PSVI_AUGMENT, true);
          fConfiguration.setProperty(
              DOCUMENT_CLASS_NAME, "com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl");
        } else {
          // Constants.DOM_CHARSET_OVERRIDES_XML_ENCODING feature,
          // Constants.DOM_SPLIT_CDATA feature,
          // or any Xerces feature
          String normalizedName;
          if (name.equals(NAMESPACE_GROWTH)) {
            normalizedName = NAMESPACE_GROWTH;
          } else if (name.equals(TOLERATE_DUPLICATES)) {
            normalizedName = TOLERATE_DUPLICATES;
          } else {
            normalizedName = name.toLowerCase(Locale.ENGLISH);
          }
          fConfiguration.setFeature(normalizedName, state);
        }

      } catch (XMLConfigurationException e) {
        String msg =
            DOMMessageFormatter.formatMessage(
                DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_FOUND", new Object[] {name});
        throw new DOMException(DOMException.NOT_FOUND_ERR, msg);
      }
    } else { // set properties
      if (name.equalsIgnoreCase(Constants.DOM_ERROR_HANDLER)) {
        if (value instanceof DOMErrorHandler || value == null) {
          try {
            fErrorHandler = new DOMErrorHandlerWrapper((DOMErrorHandler) value);
            fConfiguration.setProperty(ERROR_HANDLER, fErrorHandler);
          } catch (XMLConfigurationException e) {
          }
        } else {
          // REVISIT: type mismatch
          String msg =
              DOMMessageFormatter.formatMessage(
                  DOMMessageFormatter.DOM_DOMAIN, "TYPE_MISMATCH_ERR", new Object[] {name});
          throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg);
        }

      } else if (name.equalsIgnoreCase(Constants.DOM_RESOURCE_RESOLVER)) {
        if (value instanceof LSResourceResolver || value == null) {
          try {
            fConfiguration.setProperty(
                ENTITY_RESOLVER, new DOMEntityResolverWrapper((LSResourceResolver) value));
          } catch (XMLConfigurationException e) {
          }
        } else {
          // REVISIT: type mismatch
          String msg =
              DOMMessageFormatter.formatMessage(
                  DOMMessageFormatter.DOM_DOMAIN, "TYPE_MISMATCH_ERR", new Object[] {name});
          throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg);
        }

      } else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_LOCATION)) {
        if (value instanceof String || value == null) {
          try {
            if (value == null) {
              fSchemaLocation = null;
              fConfiguration.setProperty(
                  Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, null);
            } else {
              fSchemaLocation = (String) value;
              // map DOM schema-location to JAXP schemaSource property
              // tokenize location string
              StringTokenizer t = new StringTokenizer(fSchemaLocation, " \n\t\r");
              if (t.hasMoreTokens()) {
                ArrayList locations = new ArrayList();
                locations.add(t.nextToken());
                while (t.hasMoreTokens()) {
                  locations.add(t.nextToken());
                }
                fConfiguration.setProperty(
                    Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, locations.toArray());
              } else {
                fConfiguration.setProperty(
                    Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_SOURCE, value);
              }
            }
          } catch (XMLConfigurationException e) {
          }
        } else {
          // REVISIT: type mismatch
          String msg =
              DOMMessageFormatter.formatMessage(
                  DOMMessageFormatter.DOM_DOMAIN, "TYPE_MISMATCH_ERR", new Object[] {name});
          throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg);
        }

      } else if (name.equalsIgnoreCase(Constants.DOM_SCHEMA_TYPE)) {
        if (value instanceof String || value == null) {
          try {
            if (value == null) {
              // turn off schema features
              fConfiguration.setFeature(XMLSCHEMA, false);
              fConfiguration.setFeature(XMLSCHEMA_FULL_CHECKING, false);
              // map to JAXP schemaLanguage
              fConfiguration.setProperty(
                  Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, null);
              fSchemaType = null;
            } else if (value.equals(Constants.NS_XMLSCHEMA)) {
              // turn on schema features
              fConfiguration.setFeature(XMLSCHEMA, true);
              fConfiguration.setFeature(XMLSCHEMA_FULL_CHECKING, true);
              // map to JAXP schemaLanguage
              fConfiguration.setProperty(
                  Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE,
                  Constants.NS_XMLSCHEMA);
              fSchemaType = Constants.NS_XMLSCHEMA;
            } else if (value.equals(Constants.NS_DTD)) {
              // turn off schema features
              fConfiguration.setFeature(XMLSCHEMA, false);
              fConfiguration.setFeature(XMLSCHEMA_FULL_CHECKING, false);
              // map to JAXP schemaLanguage
              fConfiguration.setProperty(
                  Constants.JAXP_PROPERTY_PREFIX + Constants.SCHEMA_LANGUAGE, Constants.NS_DTD);
              fSchemaType = Constants.NS_DTD;
            }
          } catch (XMLConfigurationException e) {
          }
        } else {
          String msg =
              DOMMessageFormatter.formatMessage(
                  DOMMessageFormatter.DOM_DOMAIN, "TYPE_MISMATCH_ERR", new Object[] {name});
          throw new DOMException(DOMException.TYPE_MISMATCH_ERR, msg);
        }

      } else if (name.equalsIgnoreCase(DOCUMENT_CLASS_NAME)) {
        fConfiguration.setProperty(DOCUMENT_CLASS_NAME, value);
      } else {
        // Try to set the property.
        String normalizedName = name.toLowerCase(Locale.ENGLISH);
        try {
          fConfiguration.setProperty(normalizedName, value);
          return;
        } catch (XMLConfigurationException e) {
        }

        // If this is a boolean parameter a type mismatch should be thrown.
        try {
          if (name.equals(NAMESPACE_GROWTH)) {
            normalizedName = NAMESPACE_GROWTH;
          } else if (name.equals(TOLERATE_DUPLICATES)) {
            normalizedName = TOLERATE_DUPLICATES;
          }
          fConfiguration.getFeature(normalizedName);
          throw newTypeMismatchError(name);

        } catch (XMLConfigurationException e) {
        }

        // Parameter is not recognized
        throw newFeatureNotFoundError(name);
      }
    }
  }
示例#5
0
 private static DOMException newTypeMismatchError(String name) {
   String msg =
       DOMMessageFormatter.formatMessage(
           DOMMessageFormatter.DOM_DOMAIN, "TYPE_MISMATCH_ERR", new Object[] {name});
   return new DOMException(DOMException.TYPE_MISMATCH_ERR, msg);
 }
示例#6
0
 private static DOMException newFeatureNotFoundError(String name) {
   String msg =
       DOMMessageFormatter.formatMessage(
           DOMMessageFormatter.DOM_DOMAIN, "FEATURE_NOT_FOUND", new Object[] {name});
   return new DOMException(DOMException.NOT_FOUND_ERR, msg);
 }