Example #1
0
  public Document newDocument() throws SAXException {
    Document docResult = null;

    try {
      DocumentBuilderFactory factory = XmlParser.createDocumentBuilderFactory();
      DocumentBuilder builder = factory.newDocumentBuilder();
      builder.setErrorHandler(new ErrorHandler());

      docResult = builder.newDocument();
    } catch (ParserConfigurationException e) {
      throw new SAXException(e);
    }

    return docResult;
  }
Example #2
0
  public Document readDocument(Object source, Node after) throws IOException, SAXException {
    Document docResult = null;

    try {
      DocumentBuilderFactory factory = XmlParser.createDocumentBuilderFactory();
      DocumentBuilder builder = factory.newDocumentBuilder();
      builder.setErrorHandler(new ErrorHandler());

      File f = null;
      if (source instanceof String) {
        f = new File((String) source);
        docResult = builder.parse(f);
      } else if (source instanceof InputStream) {
        docResult = builder.parse((InputStream) source);
      }
      Node nodeRoot = docResult.getFirstChild();

      // //////////////////////////////////////////////////////
      // Make sure we have a DBSetup XML file.

      // This is dumb, because there could be an XML comment before
      // the root element. The root node is not the same thing as the
      // root element.
      if (nodeRoot.getNodeName().equalsIgnoreCase("Covalent.DBSetup") == false) {
        if (source instanceof String) {
          throw new IOException(source.toString() + " is not a DBSetup XML file.");
        } else {
          throw new IOException("Source is not a DBSetup XML file.");
        }
      }

      // ///////////////////////////////////////////////////////
      // Look for include tags

      NodeList listNodes = nodeRoot.getChildNodes();

      for (int iNode = 0; iNode < listNodes.getLength(); iNode++) {
        Node node = listNodes.item(iNode);

        if (node.getNodeName().equalsIgnoreCase("include") == true) {
          NamedNodeMap map = node.getAttributes();

          for (int iAttr = 0; iAttr < map.getLength(); iAttr++) {
            Node nodeMap = map.item(iAttr);

            if (nodeMap.getNodeName().equalsIgnoreCase("file") == true) {
              File fileInclude = new File(nodeMap.getNodeValue());

              if (fileInclude.isAbsolute() == false) {
                if (!(source instanceof String)) {
                  throw new IOException("Cannot resolve paths" + " relative to a " + "stream.");
                }
                String strPath = f.getAbsolutePath();
                int iIndex = strPath.lastIndexOf(File.separatorChar);
                strPath = strPath.substring(0, iIndex);
                fileInclude = new File(strPath + File.separatorChar + fileInclude.getPath());
              }

              this.readDocument(fileInclude.getAbsolutePath(), node);
              nodeRoot.removeChild(node);
            }
          }
        } else if (after != null) DBSetup.importNodeAfter(after, node, true);
      }
    } catch (ParserConfigurationException e) {
      throw new SAXException(e);
    }

    return docResult;
  }