示例#1
0
  /** Prints the specified node, recursively. */
  public String print(Node node) {

    // is there anything to do?
    if (node == null) {
      return sb.toString();
    }

    int type = node.getNodeType();
    switch (type) {
        // print document
      case Node.DOCUMENT_NODE:
        {
          return print(((Document) node).getDocumentElement());
          // out.flush();
          // break;
        }

        // print element with attributes
      case Node.ELEMENT_NODE:
        {
          sb.append('<');
          sb.append(node.getNodeName());
          Attr attrs[] = sortAttributes(node.getAttributes());
          for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            sb.append(' ');
            sb.append(attr.getNodeName());
            sb.append("=\"");
            sb.append(normalize(attr.getNodeValue()));
            sb.append('"');
          }
          sb.append('>');
          NodeList children = node.getChildNodes();
          if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
              print(children.item(i));
            }
          }
          break;
        }

        // handle entity reference nodes
      case Node.ENTITY_REFERENCE_NODE:
        {
          if (canonical) {
            NodeList children = node.getChildNodes();
            if (children != null) {
              int len = children.getLength();
              for (int i = 0; i < len; i++) {
                print(children.item(i));
              }
            }
          } else {
            sb.append('&');
            sb.append(node.getNodeName());
            sb.append(';');
          }
          break;
        }

        // print cdata sections
      case Node.CDATA_SECTION_NODE:
        {
          if (canonical) {
            sb.append(normalize(node.getNodeValue()));
          } else {
            sb.append("<![CDATA[");
            sb.append(node.getNodeValue());
            sb.append("]]>");
          }
          break;
        }

        // print text
      case Node.TEXT_NODE:
        {
          sb.append(normalize(node.getNodeValue()));
          break;
        }

        // print processing instruction
      case Node.PROCESSING_INSTRUCTION_NODE:
        {
          sb.append("<?");
          sb.append(node.getNodeName());
          String data = node.getNodeValue();
          if (data != null && data.length() > 0) {
            sb.append(' ');
            sb.append(data);
          }
          sb.append("?>");
          break;
        }
        // handle entity reference nodes
      case Node.DOCUMENT_FRAGMENT_NODE:
        {
          NodeList children = node.getChildNodes();
          if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
              print(children.item(i));
            }
          }
          break;
        }
    }

    if (type == Node.ELEMENT_NODE) {
      sb.append("</");
      sb.append(node.getNodeName());
      sb.append('>');
    }

    return sb.toString();
  } // print(Node)
示例#2
0
  public boolean performTest(String arg) {
    boolean passed = true;
    try {
      Writer writer = new Writer(false);
      DOMParser parser = new DOMParser();
      if (!arg.equals("delete2") && !arg.equals("insert2")) {
        if (fStdOut) System.out.println("\n*************** Test == " + arg + " ***************");
        for (int i = 0; i < tests.length; i++) {
          if (fStdOut) System.out.println("\n\nTest[" + i + "]");
          if (fStdOut) System.out.println("\nBefore " + arg + ": document=" + tests[i] + ":");
          parser.parse(new InputSource(new StringReader(tests[i])));
          DocumentImpl document = (DocumentImpl) parser.getDocument();
          Range range = document.createRange();
          Node root = document.getDocumentElement();
          boolean surround = false;
          Node surroundNode = document.createElement(SURROUND);
          if (arg.equals("surround")) {
            surround = true;
          }

          if (i == 0) {
            range.setStart(root.getFirstChild(), 1);
            range.setEndBefore(root.getLastChild());
            if (surround) range.setEnd(root.getLastChild(), 1);

          } else if (i == 1) {
            Node n1 = root.getFirstChild().getNextSibling().getFirstChild();
            range.setStart(n1, 1);
            range.setEnd(root.getLastChild(), 1);
            if (surround) range.setEnd(n1, 2);
          } else if (i == 2) {
            range.setStart(root.getFirstChild(), 1);
            Node n2 = root.getFirstChild().getNextSibling().getFirstChild();
            range.setEnd(n2, 1);
            if (surround) range.setEndBefore(root.getLastChild());
          } else if (i == 3) {
            Node n3 = root.getFirstChild().getFirstChild();
            range.setStart(n3, 1);
            range.setEnd(root.getLastChild().getFirstChild(), 1);
            if (surround) {
              range.selectNode(root.getFirstChild().getNextSibling());
            }
          } else if (i == 4) {
            Node n4 = root.getFirstChild().getFirstChild().getNextSibling().getFirstChild();
            range.setStartAfter(n4);
            range.setEndAfter(root.getLastChild().getFirstChild());
            if (surround) {
              range.selectNodeContents(root.getFirstChild().getNextSibling());
            }
          }

          if (fStdOut) System.out.println("range.toString=" + range.toString());
          DocumentFragment frag = null;

          if (arg.equals("surround")) {
            try {
              if (fStdOut) System.out.println("surroundNode=" + surroundNode);
              range.surroundContents(surroundNode);
            } catch (org.w3c.dom.ranges.RangeException e) {
              if (fStdOut) System.out.println(e);
            }
            String result = toString(document);
            if (fStdOut) System.out.println("After surround: document=" + result + ":");
            if (!result.equals(surroundResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + surroundResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Surround document Test[" + i + "] FAILED!");
            }
          }

          if (arg.equals("insert")) {
            range.insertNode(document.createTextNode(INSERT));
            String result = toString(document);
            if (fStdOut) System.out.println("After  insert: document=" + result + ":");
            if (!result.equals(insertResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + insertResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Insert document Test[" + i + "] FAILED!");
            }

          } else if (arg.equals("delete")) {
            range.deleteContents();
            String result = toString(document);
            if (fStdOut) System.out.println("After delete:" + result + ":");
            if (!result.equals(deleteResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + deleteResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Delete document Test[" + i + "] FAILED!");
            }
          } else if (arg.equals("extract")) {
            frag = range.extractContents();
            // range.insertNode(document.createTextNode("^"));
            String result = toString(document);
            if (fStdOut) System.out.println("After extract: document=" + result + ":");
            if (!result.equals(deleteResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + deleteResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("*** Extract document Test[" + i + "] FAILED!");
            }
            String fragResult = toString(frag);
            if (fStdOut) System.out.println("After extract: fragment=" + fragResult + ":");
            if (!fragResult.equals(extractResult[i])) {
              if (fStdOut) System.out.println("Should be: fragment=" + extractResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("*** Extract Fragment Test[" + i + "] FAILED!");
            }
          } else if (arg.equals("clone")) {
            frag = range.cloneContents();
            String fragResult = toString(frag);
            if (fStdOut) System.out.println("After clone: fragment=" + fragResult);
            if (!fragResult.equals(extractResult[i])) {
              if (fStdOut) System.out.println("Should be: fragment=" + extractResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("*** Clone Fragment Test[" + i + "] FAILED!");
            }
          }
        }

      } else if (arg.equals("insert2")) {
        if (fStdOut) System.out.println("\n*************** Test == " + arg + " ***************");
        for (int i = 0; i < 4; i++) {

          if (fStdOut) System.out.println("\n\nTest[" + i + "]");
          if (fStdOut) System.out.println("\nBefore " + arg + ": document=" + INSERT2 + ":");
          parser.parse(new InputSource(new StringReader(INSERT2)));
          DocumentImpl document = (DocumentImpl) parser.getDocument();
          Node root = document.getDocumentElement();
          Range range = document.createRange();
          range.setStart(root.getFirstChild(), 11);
          range.setEnd(root.getFirstChild(), 18);
          Range rangei = document.createRange();
          if (i == 0) {
            rangei.setStart(root.getFirstChild(), 10);
            rangei.setEnd(root.getFirstChild(), 10);
          }
          if (i == 1) {
            rangei.setStart(root.getFirstChild(), 11);
            rangei.setEnd(root.getFirstChild(), 11);
          }
          if (i == 2) {
            rangei.setStart(root.getFirstChild(), 12);
            rangei.setEnd(root.getFirstChild(), 12);
          }
          if (i == 3) {
            rangei.setStart(root.getFirstChild(), 17);
            rangei.setEnd(root.getFirstChild(), 17);
          }
          // if (fStdOut) System.out.println("range: start1=="+range.getStartContainer());
          // root.insertBefore(document.createTextNode("YES!"), root.getFirstChild());
          // if (fStdOut) System.out.println("range: start2=="+range.getStartContainer());

          if (DEBUG)
            if (fStdOut) System.out.println("before insert start=" + range.getStartOffset());
          if (DEBUG) if (fStdOut) System.out.println("before insert end=" + range.getEndOffset());
          rangei.insertNode(document.createTextNode(INSERTED_TEXT));
          if (DEBUG)
            if (fStdOut) System.out.println("after insert start=" + range.getStartOffset());
          if (DEBUG) if (fStdOut) System.out.println("after insert end=" + range.getEndOffset());

          String result = toString(document);
          if (fStdOut) System.out.println("After insert2: document=" + result + ":");
          if (!result.equals(rangeInsertResult[i])) {
            if (fStdOut) System.out.println("Should be: document=" + rangeInsertResult[i] + ":");
            passed = false;
            if (fStdOut) System.out.println("Test FAILED!");
            if (fStdOut) System.out.println("*** Delete Ranges document Test[" + i + "] FAILED!");
          }
        }
      } else if (arg.equals("delete2")) {
        //
        // Range Deletion, acting upon another range.
        //

        if (fStdOut) System.out.println("\n*************** Test == " + arg + " ***************");
        for (int i = 0; i < rangeDelete.length; i++) {
          if (fStdOut) System.out.println("\n\nTest[" + i + "]");
          if (fStdOut) System.out.println("\nBefore " + arg + ": document=" + rangeDelete[i] + ":");
          parser.parse(new InputSource(new StringReader(rangeDelete[i])));
          DocumentImpl document = (DocumentImpl) parser.getDocument();
          Range range = document.createRange();
          Range ranged = document.createRange();
          Node root = document.getDocumentElement();
          boolean surround = false;
          Node surroundNode = document.createElement(SURROUND);
          if (arg.equals("surround")) {
            surround = true;
          }

          if (i == 0) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild(), 14);

            range.setStart(root.getFirstChild(), 11);
            range.setEnd(root.getFirstChild(), 19);
          } else if (i == 1) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild(), 22);

            range.setStart(root.getFirstChild(), 11);
            range.setEnd(root.getFirstChild(), 21);
          } else if (i == 2) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild().getNextSibling().getFirstChild(), 1);

            range.setStart(root.getFirstChild(), 11);

            range.setEndAfter(root.getFirstChild().getNextSibling().getFirstChild());
          } else if (i == 3) {
            ranged.setStart(root.getFirstChild(), 5);
            ranged.setEnd(root.getFirstChild(), 11);

            range.setStart(root.getFirstChild(), 11);
            range.setEnd(root.getFirstChild(), 21);
          } else if (i == 4) {
            ranged.selectNode(root.getFirstChild().getNextSibling());

            range.setStart(root.getFirstChild().getNextSibling().getFirstChild(), 6);
            range.setEnd(root.getFirstChild().getNextSibling().getFirstChild(), 15);
          }

          DocumentFragment frag = null;

          if (arg.equals("delete2")) {
            if (DEBUG) {
              if (fStdOut) System.out.println("BEFORE deleteContents()");
              if (fStdOut) System.out.println("ranged: startc=" + ranged.getStartContainer());
              if (fStdOut) System.out.println("ranged: starto=" + ranged.getStartOffset());
              if (fStdOut) System.out.println("ranged:   endc=" + ranged.getEndContainer());
              if (fStdOut) System.out.println("ranged:   endo=" + ranged.getEndOffset());

              if (fStdOut) System.out.println("range: startc=" + range.getStartContainer());
              if (fStdOut) System.out.println("range: starto=" + range.getStartOffset());
              if (fStdOut) System.out.println("range:   endc=" + range.getEndContainer());
              if (fStdOut) System.out.println("range:   endo=" + range.getEndOffset());
            }
            ranged.deleteContents();
            String result = null;
            if (DEBUG) {
              if (fStdOut) System.out.println("AFTER deleteContents()");
              result = toString(document);
              if (fStdOut) System.out.println("ranged: startc=" + ranged.getStartContainer());
              if (fStdOut) System.out.println("ranged: starto=" + ranged.getStartOffset());
              if (fStdOut) System.out.println("ranged:   endc=" + ranged.getEndContainer());
              if (fStdOut) System.out.println("ranged:   endo=" + ranged.getEndOffset());

              if (fStdOut) System.out.println("range: startc=" + range.getStartContainer());
              if (fStdOut) System.out.println("range: starto=" + range.getStartOffset());
              if (fStdOut) System.out.println("range:   endc=" + range.getEndContainer());
              if (fStdOut) System.out.println("range:   endo=" + range.getEndOffset());
            }

            ranged.insertNode(document.createTextNode("^"));

            result = toString(document);
            if (fStdOut) System.out.println("After delete2: document=" + result + ":");
            if (!result.equals(rangeDeleteResult[i])) {
              if (fStdOut) System.out.println("Should be: document=" + rangeDeleteResult[i] + ":");
              passed = false;
              if (fStdOut) System.out.println("Test FAILED!");
              if (fStdOut) System.out.println("*** Delete Ranges document Test[" + i + "] FAILED!");
            }
          }
        }
      }

    } catch (org.xml.sax.SAXParseException spe) {
      passed = false;
    } catch (org.xml.sax.SAXException se) {
      if (se.getException() != null) se.getException().printStackTrace(System.err);
      else se.printStackTrace(System.err);
      passed = false;
    } catch (Exception e) {
      e.printStackTrace(System.err);
      passed = false;
    }
    if (!passed) if (fStdOut) System.out.println("*** The " + arg + " Test FAILED! ***");

    return passed;
  }