void getAttributes(Object node, String localName, String namespaceUri, List result) {
   if (node instanceof Element) {
     Element e = (Element) node;
     if (localName == null) {
       result.addAll(e.getAttributes());
     } else {
       Attribute attr = e.getAttribute(localName, Namespace.getNamespace("", namespaceUri));
       if (attr != null) {
         result.add(attr);
       }
     }
   } else if (node instanceof ProcessingInstruction) {
     ProcessingInstruction pi = (ProcessingInstruction) node;
     if ("target".equals(localName)) {
       result.add(new Attribute("target", pi.getTarget()));
     } else if ("data".equals(localName)) {
       result.add(new Attribute("data", pi.getData()));
     } else {
       result.add(new Attribute(localName, pi.getValue(localName)));
     }
   } else if (node instanceof DocType) {
     DocType doctype = (DocType) node;
     if ("publicId".equals(localName)) {
       result.add(new Attribute("publicId", doctype.getPublicID()));
     } else if ("systemId".equals(localName)) {
       result.add(new Attribute("systemId", doctype.getSystemID()));
     } else if ("elementName".equals(localName)) {
       result.add(new Attribute("elementName", doctype.getElementName()));
     }
   }
 }
Esempio n. 2
0
  /**
   * This method tells you the line of the XML file being parsed. For an in-memory document, it's
   * meaningless. The location is only valid for the current parsing lifecycle, but the document has
   * already been parsed. Therefore, it returns -1 for both line and column numbers.
   *
   * @param document JDOM <code>Document</code>.
   */
  private void documentLocator(Document document) {
    locator = new JDOMLocator();
    String publicID = null;
    String systemID = null;

    if (document != null) {
      DocType docType = document.getDocType();
      if (docType != null) {
        publicID = docType.getPublicID();
        systemID = docType.getSystemID();
      }
    }
    locator.setPublicId(publicID);
    locator.setSystemId(systemID);
    locator.setLineNumber(-1);
    locator.setColumnNumber(-1);

    contentHandler.setDocumentLocator(locator);
  }
Esempio n. 3
0
  // Example catalog.xml file contents
  //
  // <?xml version="1.0"?>
  //  <!-- commented out to prevent network access
  //     !DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN"
  //    "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd"
  //   -->
  // <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
  //   <group prefer="public" xml:base="">
  //     <public
  //	 publicId="-//AIAA//DTD for Flight Dynamic Models - Functions 2.0//EN"
  //	 uri="schemas/DAVEfunc.dtd"/>
  //
  //     <public
  //	 publicId="-//W3C//DTD MathML 2.0//EN"
  //	 uri="schemas/mathml2.dtd"/>
  //
  //    </group>
  // </catalog>
  public Document load() throws IOException {
    Document doc = null;
    XMLCatalogResolver cr = null;
    String directory_uri = this.base_uri.substring(0, this.base_uri.lastIndexOf('/'));
    String errorLine = "No error.";
    boolean tryValidationFlag = true;
    int numberOfFailures = 0;
    boolean success = false;
    while (numberOfFailures < 2 && !success) {
      success = true;
      errorLine = "Error when attempting validation...";

      // see if we can open the file first, and deal with any exceptions
      try {
        // open the XML file
        _inputStream = new FileInputStream(this.inputFileName);
        assert (_inputStream != null);
      } catch (IOException e) {
        System.err.println("Error opening file '" + this.inputFileName + "': " + e.getMessage());
        System.exit(-1);
      }

      // now see if we can parse it; try with and without validation
      try {
        // Load XML into JDOM Document
        SAXBuilder builder = new SAXBuilder(tryValidationFlag);
        // must turn off Xerces desire to load external DTD regardless of validation
        builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        if (tryValidationFlag) {
          String[] catalogs;
          // environment variable XML_CATALOG_FILES trumps properties file
          String xml_catalog_files = System.getenv("XML_CATALOG_FILES");
          if (xml_catalog_files == null) {
            xml_catalog_files = System.getProperty("xml.catalog.files");
          }
          if (xml_catalog_files != null) {
            catalogs =
                new String[] {xml_catalog_files, "catalog.xml", "catalog", "file:/etc/xml/catalog"};
          } else {
            catalogs = new String[] {"catalog.xml", "catalog", "file:/etc/xml/catalog"};
          }
          cr = new XMLCatalogResolver(catalogs);

          //                    // here to test stuff
          //
          //                    cr.setUseLiteralSystemId(false);
          //
          //                    boolean preferPublic = cr.getPreferPublic();
          //                    if (preferPublic)
          //                        System.out.println("Prefer public");
          //                    else
          //                        System.out.println("Prefer system");
          //
          //                    System.out.println("call to resolvePublic for DAVE-ML models
          // gives:");
          //                    System.out.println(cr.resolvePublic("-//AIAA//DTD for Flight Dynamic
          // Models - Functions 2.0//EN",
          //                            "http://www.daveml.org/DTDs/2p0/DAVEfunc.dtd"));
          //
          //                    System.out.println();
          //
          //                    System.out.println("call to resolvePublic for MathML2 namespace
          // gives:");
          //                    System.out.println(cr.resolvePublic("-//W3C//DTD MathML 2.0//EN",
          //                            "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"));
          //
        } else {
          cr = null;
        }
        builder.setEntityResolver(cr);
        doc = builder.build(_inputStream, directory_uri);
      } catch (java.io.FileNotFoundException e) {
        errorLine = errorLine + e.getMessage();
        numberOfFailures++;
        success = false;
      } catch (java.net.UnknownHostException e) {
        errorLine = errorLine + " (network unavailable)";
        numberOfFailures++;
        success = false;
      } catch (java.net.ConnectException e) {
        errorLine = errorLine + " (connection timed out)";
        numberOfFailures++;
        success = false;
      } catch (java.io.IOException e) {
        errorLine = errorLine + " (general I/O problem)";
        numberOfFailures++;
        success = false;
      } catch (JDOMException e) {
        errorLine = errorLine + "\n" + e.getMessage();
        numberOfFailures++;
        success = false;
      }
      if (numberOfFailures == 1 && !success) {
        System.err.println(errorLine);
        System.err.println("Proceeding without validation.");
        tryValidationFlag = false;
        _inputStream.close();
      }
    }
    if (!success && numberOfFailures >= 2) {
      _inputStream.close();
      System.err.println(errorLine);
      System.err.println(
          "Unable to load file successfully (tried with and without validation), aborting.");
      System.exit(-1);
    }
    //        if (success && this.isVerbose()) {
    if (success) {
      System.out.println("Loaded '" + this.inputFileName + "' successfully, ");
      org.jdom.DocType dt = doc.getDocType();
      switch (numberOfFailures) {
        case 0: // validated against some DTD
          System.out.print("Validating against '");
          String catalogURI = cr.resolvePublic(dt.getPublicID(), dt.getSystemID());
          if (catalogURI == null) {
            System.out.print(dt.getSystemID());
          } else {
            System.out.print(catalogURI);
          }
          System.out.println(".'");
          break;
        case 1: // no validation
          System.out.println("WITHOUT validation.");
      }
    }

    return doc;
  }