/**
   * Constructs an instance of {@link StructuredDocument} matching the mime-type specified by the
   * <CODE>mimetype</CODE> parameter. The <CODE>doctype</CODE> parameter identifies the base type of
   * the {@link StructuredDocument}.
   *
   * @param mimetype Specifies the mime media type to be associated with the {@link
   *     StructuredDocument} to be created.
   * @param reader A Reader from which the document will be constructed.
   * @return StructuredDocument The instance of {@link StructuredDocument} or null if it could not
   *     be created.
   * @throws IOException If there is a problem reading from the stream.
   * @throws NoSuchElementException if the mime-type has not been registerd.
   * @throws UnsupportedOperationException if the mime-type provided is not a text oriented
   *     mimetype.
   */
  public static StructuredDocument newStructuredDocument(MimeMediaType mimetype, Reader reader)
      throws IOException {
    if (!factory.loadedProperty) {
      factory.loadedProperty = factory.doLoadProperty();
    }

    Instantiator instantiator = (Instantiator) factory.getInstantiator(mimetype.getMimeMediaType());

    if (!(instantiator instanceof TextInstantiator)) {
      // XXX 20020502 [email protected] we could probably do something
      // really inefficient that would allow it to work, but better not to.
      // if ReaderInputStream existed, it would be easy to do.
      if (LOG.isEnabledFor(Level.WARN))
        LOG.warn(
            "Document Class '"
                + instantiator.getClass().getName()
                + "' associated with '"
                + mimetype
                + "' is not a text oriented document");
      throw new UnsupportedOperationException(
          "Document Class '"
              + instantiator.getClass().getName()
              + "' associated with '"
              + mimetype
              + "' is not a text oriented document");
    }

    return ((TextInstantiator) instantiator).newInstance(mimetype, reader);
  }
  /**
   * Constructs an instance of {@link StructuredDocument} matching the mime-type specified by the
   * <CODE>mimetype</CODE> parameter. The <CODE>doctype</CODE> parameter identifies the base type of
   * the {@link StructuredDocument}.
   *
   * @param mimetype Specifies the mime media type to be associated with the {@link
   *     StructuredDocument} to be created.
   * @param doctype Specifies the root type of the {@link StructuredDocument} to be created.
   * @return StructuredDocument The instance of {@link StructuredDocument} or null if it could not
   *     be created.
   * @throws NoSuchElementException invalid mime-media-type
   */
  public static StructuredDocument newStructuredDocument(MimeMediaType mimetype, String doctype) {
    if (!factory.loadedProperty) {
      factory.loadedProperty = factory.doLoadProperty();
    }

    Instantiator instantiator = (Instantiator) factory.getInstantiator(mimetype.getMimeMediaType());

    return instantiator.newInstance(mimetype, doctype);
  }
  /**
   * Constructs an instance of {@link StructuredDocument} matching the mime-type specified by the
   * <CODE>mimetype</CODE> parameter. The <CODE>doctype</CODE> parameter identifies the base type of
   * the {@link StructuredDocument}.
   *
   * @param mimetype Specifies the mime media type to be associated with the {@link
   *     StructuredDocument} to be created.
   * @param stream Contains an InputStream from which the document will be constructed.
   * @return StructuredDocument The instance of {@link StructuredDocument} or null if it could not
   *     be created.
   * @throws IOException If there is a problem reading from the stream.
   * @throws NoSuchElementException if the mime-type has not been registerd.
   */
  public static StructuredDocument newStructuredDocument(MimeMediaType mimetype, InputStream stream)
      throws IOException {
    if (!factory.loadedProperty) {
      factory.loadedProperty = factory.doLoadProperty();
    }

    Instantiator instantiator = (Instantiator) factory.getInstantiator(mimetype.getMimeMediaType());

    return instantiator.newInstance(mimetype, stream);
  }