Esempio n. 1
0
  /**
   * Parse a file, which should have a root which is the associated tag.
   *
   * @param in File to parse
   * @param tag Root tag which the parsed file should contain
   */
  public static void parse(File in, XmlTagHandler tag) throws XmlParseException {
    SAXBuilder builder;
    Document doc;

    builder = new SAXBuilder();
    InputStream is = null;

    // open the file ourselves.  the builder(File)
    // method escapes " " -> "%20" and bombs
    try {
      is = new FileInputStream(in);
      doc = builder.build(is);
    } catch (IOException exc) {
      throw new XmlParseException(exc.getMessage());
    } catch (JDOMException exc) {
      throw new XmlParseException(exc.getMessage());
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
        }
      }
    }
    generalParse(tag, doc);
  }
Esempio n. 2
0
  public static void parse(InputStream is, XmlTagHandler tag, EntityResolver resolver)
      throws XmlParseException {
    SAXBuilder builder;
    Document doc;

    builder = new SAXBuilder();

    if (resolver != null) {
      builder.setEntityResolver(resolver);
    }

    try {
      if (resolver != null) {
        // WTF?  seems relative entity URIs are allowed
        // by certain xerces impls.  but fully qualified
        // file://... URLs trigger a NullPointerException
        // in others.  setting base here worksaround
        doc = builder.build(is, "");
      } else {
        doc = builder.build(is);
      }
    } catch (JDOMException exc) {
      XmlParseException toThrow = new XmlParseException(exc.getMessage());
      toThrow.initCause(exc);
      throw toThrow;
    } catch (IOException exc) {
      XmlParseException toThrow = new XmlParseException(exc.getMessage());
      toThrow.initCause(exc);
      throw toThrow;
    }

    generalParse(tag, doc);
  }