示例#1
0
  /**
   * Determines if there is a comment Node that contains the given string.
   *
   * @param node the Node to look in
   * @param str the string value to match in the comment nodes
   *     <p>CTL declaration, if we ever want to use it
   *     <!--Sample Usage:
   *            ctl:checkCommentNodes($xml.resp, 'complexContent')-->
   *     <ctl:function name="ctl:checkCommentNodes"> <ctl:param name="node"/> <ctl:param
   *     name="string"/> <ctl:description>Checks a Node for comments that contain the given
   *     string.</ctl:description> <ctl:java class="com.occamlab.te.util.DomUtils"
   *     method="checkCommentNodes"/> </ctl:function>
   * @return the original Document with the update attribute nodes
   */
  public static boolean checkCommentNodes(Node node, String str) {

    // Get nodes of node and go through them
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      Node child = children.item(i);
      NodeList childChildren = child.getChildNodes();
      if (childChildren.getLength() > 0) {
        // Recurse for all children
        boolean okDownThere = checkCommentNodes(child, str);
        if (okDownThere == true) {
          return true;
        }
      }
      // Investigate comments
      if (child.getNodeType() == Node.COMMENT_NODE) {
        // If we got a comment that contains the string we are happy
        Comment comment = (Comment) child;
        if (comment.getNodeValue().contains(str)) {
          return true;
        }
      }
    }
    return false;
  }
  /**
   * Method outputCommentToWriter
   *
   * @param currentComment
   * @param writer TODO
   * @throws IOException
   */
  static final void outputCommentToWriter(Comment currentComment, OutputStream writer)
      throws IOException {
    final int position = getPositionRelativeToDocumentElement(currentComment);
    if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
      writer.write('\n');
    }
    writer.write(_BEGIN_COMM);

    final String data = currentComment.getData();
    final int length = data.length();

    for (int i = 0; i < length; i++) {
      char c = data.charAt(i);
      if (c == 0x0D) {
        writer.write(__XD_);
      } else {
        writeCharToUtf8(c, writer);
      }
    }

    writer.write(_END_COMM);
    if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
      writer.write('\n');
    }
  }
示例#3
0
  private static int printChildElems(
      Element elem, String endient, boolean toplevel, PrintStream pstrm) {
    endient += ELEM_ENDIENT;

    int count = 0;
    boolean after_comment = false;

    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
      Node node = nodes.item(i);

      if (node instanceof Element) {
        if (after_comment == false && (toplevel || count == 0)) {
          pstrm.println("");
        }
        after_comment = false;
        printElem((Element) node, endient, false, pstrm);
        count++;
      } else if (node instanceof Comment) {
        if (toplevel && count != 0) {
          after_comment = true;
          pstrm.println("");
        }
        Comment comm = (Comment) node;

        String data = comm.getData();
        String commEnd;

        if (data.indexOf('\n') == -1 && data.indexOf('\r') == -1) {
          commEnd = "-->";
        } else {
          commEnd = "\n" + endient + "-->";
        }

        String head = "\n";
        if (toplevel) {
          head = "";
        }

        pstrm.println(head + endient + "<!--" + data + commEnd);
      } else if (node instanceof Text) { // && elem.getTagName().equals("header") ) {
        pstrm.print(node.getNodeValue());
      }
    }

    return count;
  }
示例#4
0
  public static void parseElement(Node element) {
    String tagName = element.getNodeName();

    NodeList children = element.getChildNodes();

    System.out.print("<" + tagName);

    NamedNodeMap map = element.getAttributes();

    if (null != map) {
      for (int i = 0; i < map.getLength(); i++) {
        Attr attr = (Attr) map.item(i);

        String attrName = attr.getName();
        String attrValue = attr.getValue();

        System.out.print(" " + attrName + "=\"" + attrValue + "\"");
      }
    }

    System.out.print(">");

    for (int i = 0; i < children.getLength(); i++) {
      Node node = children.item(i);

      short nodeType = node.getNodeType();

      if (nodeType == Node.ELEMENT_NODE) {
        parseElement((Element) node);
      } else if (nodeType == Node.TEXT_NODE) {
        System.out.print(node.getNodeValue());
      } else if (nodeType == Node.COMMENT_NODE) {
        System.out.print("<!--");

        Comment comment = (Comment) node;

        String data = comment.getData();

        System.out.print(data);

        System.out.print("-->");
      }
    }

    System.out.print("</" + tagName + ">");
  }
示例#5
0
 private static StringBuffer nodeAsText(Node elm, StringBuffer sb, boolean ignoreComments) {
   if (elm.getNodeType() == Node.CDATA_SECTION_NODE) {
     CDATASection cdata = (CDATASection) elm;
     sb.append("<![CDATA[");
     sb.append(cdata.getData());
     sb.append("]]>");
     return sb;
   }
   if (elm.getNodeType() == Node.COMMENT_NODE) {
     if (ignoreComments) {
       return sb;
     }
     Comment c = (Comment) elm;
     sb.append("<!--");
     sb.append(c.getData());
     sb.append("-->");
     return sb;
   }
   if (elm.getNodeType() == Node.TEXT_NODE) {
     Text t = (Text) elm;
     sb.append(StringHelper.escapeXml(t.getData(), "<&"));
     return sb;
   }
   NodeList childs = elm.getChildNodes();
   sb.append("<" + elm.getNodeName());
   attributes2String(elm, sb);
   if (childs.getLength() > 0) {
     sb.append(">");
     for (int i = 0; i < childs.getLength(); i++) {
       Node child = childs.item(i);
       nodeAsText(child, sb, ignoreComments);
     }
     sb.append("</" + elm.getNodeName() + ">");
   } else {
     sb.append("/>");
   }
   return sb;
 }
  /**
   * generateComment method
   *
   * @return java.lang.String
   * @param comment org.w3c.dom.Comment
   */
  public String generateComment(Comment comment) {
    if (comment == null) return null;

    String data = comment.getData();
    int length = (data != null ? data.length() : 0);
    StringBuffer buffer = new StringBuffer(length + 8);
    CommentImpl impl = (CommentImpl) comment;
    if (!impl.isJSPTag()) buffer.append(COMMENT_OPEN);
    else buffer.append(JSPTag.COMMENT_OPEN);
    if (data != null) buffer.append(data);
    if (!impl.isJSPTag()) buffer.append(COMMENT_CLOSE);
    else buffer.append(JSPTag.COMMENT_CLOSE);
    return buffer.toString();
  }
示例#7
0
  private boolean shallowEquals(NodeInfo n1, Node n2) {
    if (n1 == n2) return true;
    if (n1 == null || n2 == null) return false;

    int type1 = n1.getNodeKind();
    if (type1 == Node.CDATA_SECTION_NODE) type1 = Node.TEXT_NODE;
    else if (type1 == NodeType.NAMESPACE) type1 = Node.ATTRIBUTE_NODE;

    int type2 = n2.getNodeType();
    if (type2 == Node.CDATA_SECTION_NODE) type2 = Node.TEXT_NODE;

    if (type1 != type2) return false;

    switch (type1) {
      case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction pi2 = (ProcessingInstruction) n2;
        String target1 = n1.getDisplayName();
        String target2 = pi2.getTarget();
        if (!target1.equals(target2)) return false;
        String data1 = n1.getStringValue();
        String data2 = pi2.getData();
        if (!data1.equals(data2)) return false;
        break;
      case Node.COMMENT_NODE:
        Comment comment2 = (Comment) n2;
        data1 = n1.getStringValue();
        data2 = comment2.getData();
        if (!data1.equals(data2)) return false;
        break;
      case Node.ELEMENT_NODE:
        Element element2 = (Element) n2;
        String namespaceURI1 = n1.getURI();
        if (namespaceURI1 == null) namespaceURI1 = "";
        String namespaceURI2 = element2.getNamespaceURI();
        if (namespaceURI2 == null) namespaceURI2 = "";
        if (!namespaceURI1.equals(namespaceURI2)) return false;
        String localName1 = n1.getLocalPart();
        String localName2 = element2.getLocalName();
        if (!localName1.equals(localName2)) return false;

        NodeInfoSequence attrs1 = new NodeInfoSequence(n1, Axis.ATTRIBUTE);
        NamedNodeMap attrs2 = element2.getAttributes();
        BitSet bitSet = new BitSet();
        NodeInfo attr1;
        while ((attr1 = attrs1.findNext()) != null) {
          if (isNamespaceDeclaration(attr1)) continue;
          namespaceURI1 = attr1.getURI();
          if (namespaceURI1 == null) namespaceURI1 = "";
          localName1 = attr1.getLocalPart();
          String value1 = attr1.getStringValue();

          int found = -1;
          for (int i = 0; i < attrs2.getLength(); i++) {
            Attr attr2 = (Attr) attrs2.item(i);
            namespaceURI2 = attr2.getNamespaceURI();
            if (namespaceURI2 == null) namespaceURI2 = "";
            localName2 = attr2.getLocalName();
            if (namespaceURI1.equals(namespaceURI2) && localName1.equals(localName2)) {
              String value2 = attr2.getNodeValue();
              if (!value1.equals(value2)) return false;
              found = i;
              break;
            }
          }
          if (found == -1) return false;
          else bitSet.set(found);
        }
        for (int i = 0; i < attrs2.getLength(); i++) {
          if (!bitSet.get(i)) {
            Attr attr2 = (Attr) attrs2.item(i);
            if (!DOMUtil.isNamespaceDeclaration(attr2)) return false;
          }
        }

        break;
      case Node.ATTRIBUTE_NODE:
        Attr attr2 = (Attr) n2;
        namespaceURI1 = isNamespaceDeclaration(n1) ? Namespaces.URI_XMLNS : n1.getURI();
        if (namespaceURI1 == null) namespaceURI1 = "";
        namespaceURI2 = attr2.getNamespaceURI();
        if (namespaceURI2 == null) namespaceURI2 = "";
        if (!namespaceURI1.equals(namespaceURI2)) return false;
        localName1 = n1.getLocalPart();
        localName2 = attr2.getLocalName();
        if (!localName1.equals(localName2)) return false;
        String value1 = n1.getStringValue();
        String value2 = attr2.getNodeValue();
        if (!value1.equals(value2)) return false;
        break;
      case Node.TEXT_NODE:
        value1 = n1.getStringValue();
        value2 = n2.getNodeValue();
        if (!value1.equals(value2)) return false;
    }

    return true;
  }
 private void checkComment(Comments expected, Node commentNode) {
   assertTrue(commentNode.getNodeType() == Node.COMMENT_NODE);
   Comment comment = (Comment) commentNode;
   assertEquals(expected.toString(), comment.getTextContent());
 }
示例#9
0
 public boolean enter(Comment comment) {
   buffer.append("<!--");
   buffer.append(comment.getData());
   buffer.append("-->");
   return true;
 }