Document parseDocument(String filename) throws Exception {
   FileReader reader = new FileReader(filename);
   String firstLine = new BufferedReader(reader).readLine();
   reader.close();
   Document document = null;
   if (firstLine.startsWith("<?xml")) {
     System.err.println("XML detected; using default XML parser.");
   } else {
     try {
       Class nekoParserClass = Class.forName("org.cyberneko.html.parsers.DOMParser");
       Object parser = nekoParserClass.newInstance();
       Method parse = nekoParserClass.getMethod("parse", new Class[] {String.class});
       Method getDocument = nekoParserClass.getMethod("getDocument", new Class[0]);
       parse.invoke(parser, filename);
       document = (Document) getDocument.invoke(parser);
     } catch (Exception e) {
       System.err.println("NekoHTML HTML parser not found; HTML4 support disabled.");
     }
   }
   if (document == null) {
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     try { // http://www.w3.org/blog/systeam/2008/02/08/w3c_s_excessive_dtd_traffic
       factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     } catch (ParserConfigurationException e) {
       System.err.println("Warning: Could not disable external DTD loading");
     }
     DocumentBuilder builder = factory.newDocumentBuilder();
     document = builder.parse(filename);
   }
   return document;
 }
예제 #2
0
  @Override
  public Transformer newTransformer(Source source, ByteArrayOutputStream errors)
      throws TransformerConfigurationException {

    TransformerFactory transformerFactory =
        TransformerFactory.newInstance(TRANSFORMER_FACTORY_CLASS_NAME, saxon6ClassLoader);

    try {
      ErrorListener errorListener = transformerFactory.getErrorListener();
      Class StandardErrorListenerClass =
          saxon6ClassLoader.loadClass(STANDARD_ERROR_LISTENER_CLASS_NAME);
      StandardErrorListenerClass.getMethod(SET_ERROR_OUTPUT_METHOD, PrintStream.class)
          .invoke(errorListener, new PrintStream(errors));

      return transformerFactory.newTransformer(source);

    } catch (ClassNotFoundException e) {
      throw new TransformerConfigurationException(TRANSFORMER_FACTORY_CLASS_NAME + " not found");
    } catch (NoSuchMethodException e) {
      throw new TransformerConfigurationException(
          TRANSFORMER_FACTORY_CLASS_NAME + ": no such method for error listener");
    } catch (IllegalAccessException e) {
      throw new TransformerConfigurationException(
          TRANSFORMER_FACTORY_CLASS_NAME + " not accessible");
    } catch (InvocationTargetException e) {
      throw new TransformerConfigurationException(
          TRANSFORMER_FACTORY_CLASS_NAME + ": could invoke method for error listener");
    }
  }