public void appendNode(Node parent, Node newChild, Node previousSibling) {
   if (previousSibling != null) {
     Node nextSibling = previousSibling.getNextSibling();
     if (nextSibling != null) {
       parent.insertBefore(newChild, nextSibling);
     } else {
       parent.appendChild(newChild);
     }
   } else {
     if (parent.hasChildNodes()) {
       parent.insertBefore(newChild, parent.getFirstChild());
     } else {
       parent.appendChild(newChild);
     }
   }
 }
  /**
   * Pretty prints a node.
   *
   * @param doc The document the node comes from.
   * @param node The node that should be pretty printed.
   * @param prefix The prefix the node should get.
   */
  private static void prettyPrint(Document doc, Node node, String prefix) {
    String childPrefix = prefix + "  ";

    // Add the indenting to the children
    NodeList childList = node.getChildNodes();
    boolean hasChildren = false;
    for (int i = childList.getLength() - 1; i >= 0; i--) {
      Node child = childList.item(i);
      boolean isNormalNode = (!child.getNodeName().startsWith("#"));
      if (isNormalNode) {
        // Add the indenting to this node
        Node textNode = doc.createTextNode(childPrefix);
        node.insertBefore(textNode, child);

        // pretty print the child's children
        prettyPrint(doc, child, childPrefix);

        hasChildren = true;
      }
    }

    // Add the indenting to the end tag
    if (hasChildren) {
      Node textNode = doc.createTextNode(prefix);
      node.appendChild(textNode);
    }
  }
Example #3
0
 void doApply(
     Stylesheet stylesheet,
     QName mode,
     Node context,
     int pos,
     int len,
     Node parent,
     Node nextSibling)
     throws TransformerException {
   Document doc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument();
   DocumentFragment fragment = doc.createDocumentFragment();
   format.apply(stylesheet, mode, context, pos, len, fragment, null);
   String f = Expr._string(context, Collections.singleton(fragment));
   String value = format(f, compute(stylesheet, context, pos, len));
   Text text = doc.createTextNode(value);
   if (nextSibling != null) {
     parent.insertBefore(text, nextSibling);
   } else {
     parent.appendChild(text);
   }
   // xsl:number doesn't process children
   if (next != null) {
     next.apply(stylesheet, mode, context, pos, len, parent, nextSibling);
   }
 }
Example #4
0
 private Element getElement(String name, Node parent) {
   NodeList list = doc.getElementsByTagNameNS(XMLSelectorInfo.SELECTOR_NS, name);
   if (list.getLength() == 0) {
     Element temp = doc.createElementNS(XMLSelectorInfo.SELECTOR_NS, name);
     parent.insertBefore(temp, parent.getFirstChild());
     return temp;
   }
   return (Element) list.item(0);
 }
Example #5
0
  private void insertComments(Node followingNode, List<String> comments) {
    if (comments == null) return;

    Node parent = followingNode.getParentNode();
    for (String comment : comments) {
      Comment commentNode = document.createComment(comment);
      parent.insertBefore(commentNode, followingNode);
    }
  }
Example #6
0
 @Expando(name = "+=")
 public XmlNode appendChild(ELContext elctx, Object value) {
   Node newnode = coerceToNode(elctx, value);
   if (newnode != null) {
     Node parent = this.parent.realize(true).toDOM();
     Node refnode = this.toDOM();
     if (refnode != null) refnode = refnode.getNextSibling();
     parent.insertBefore(newnode, refnode);
   }
   return this;
 }
Example #7
0
 /**
  * Copy old node's children to newNode.If the newNode is the father of the old node,then the old
  * node's children will be inserted before the old node,otherwise,the old node's children just
  * append to the newNode.
  *
  * @param old
  * @param newNode
  */
 public static void copyChildren(Node old, Node newNode) {
   Node child = old.getFirstChild();
   while (child != null) {
     Node next = child.getNextSibling();
     child = old.removeChild(child);
     if (old.getParentNode() == newNode) {
       newNode.insertBefore(child, old);
     } else {
       newNode.appendChild(child);
     }
     child = next;
   }
 }
Example #8
0
  /** @see railo.runtime.type.Array#sort(java.lang.String, java.lang.String) */
  public void sort(String sortType, String sortOrder) throws PageException {
    if (size() <= 1) return;

    struct.getInnerArray().sort(sortType, sortOrder);

    Object[] nodes = struct.getInnerArray().toArray();
    Node last = (Node) nodes[nodes.length - 1], current;
    Node parent = last.getParentNode();
    for (int i = nodes.length - 2; i >= 0; i--) {
      current = (Node) nodes[i];
      parent.insertBefore(current, last);
      last = current;
    } // MUST testen
  }
Example #9
0
 void insertChildAt(int index, XmlNode node) {
   Node parent = this.dom;
   Node child = parent.getOwnerDocument().importNode(node.dom, true);
   if (parent.getChildNodes().getLength() < index) {
     //    TODO    Check ECMA for what happens here
     throw new IllegalArgumentException(
         "index=" + index + " length=" + parent.getChildNodes().getLength());
   }
   if (parent.getChildNodes().getLength() == index) {
     parent.appendChild(child);
   } else {
     parent.insertBefore(child, parent.getChildNodes().item(index));
   }
 }
 // this creates the new Annotation element as the first child
 // of the Node
 private synchronized void writeToDOM(Node target, short type) {
   Document futureOwner =
       (type == XSAnnotation.W3C_DOM_ELEMENT) ? target.getOwnerDocument() : (Document) target;
   DOMParser parser = fGrammar.getDOMParser();
   StringReader aReader = new StringReader(fData);
   InputSource aSource = new InputSource(aReader);
   try {
     parser.parse(aSource);
   } catch (SAXException e) {
     // this should never happen!
     // REVISIT:  what to do with this?; should really not
     // eat it...
   } catch (IOException i) {
     // ditto with above
   }
   Document aDocument = parser.getDocument();
   Element annotation = aDocument.getDocumentElement();
   Node newElem = futureOwner.importNode(annotation, true);
   target.insertBefore(newElem, target.getFirstChild());
 }
 /*    */ public org.w3c.dom.Text splitText(int offset) throws DOMException {
   /* 44 */ int length = this.textRep.getLength();
   /*    */
   /* 48 */ String tailData = this.textRep.substringData(offset, length);
   /* 49 */ this.textRep.deleteData(offset, length);
   /*    */
   /* 52 */ javax.xml.soap.Text tailText = new CommentImpl(tailData);
   /* 53 */ Node myParent = getParentNode();
   /* 54 */ if (myParent != null) {
     /* 55 */ NodeList brothers = myParent.getChildNodes();
     /*    */
     /* 57 */ for (int i = 0; i < brothers.getLength(); i++) {
       /* 58 */ if (brothers.item(i).equals(this)) {
         /* 59 */ myParent.insertBefore(tailText, this);
         /* 60 */ return tailText;
         /*    */ }
       /*    */ }
     /*    */ }
   /* 64 */ return tailText;
   /*    */ }
Example #12
0
  private Element getChildElementWithNodeName(
      Node currentNode, String childName, boolean ifNullBuildNewElement) {

    NodeList childrenList = currentNode.getChildNodes();
    for (int i = 0; i < childrenList.getLength(); i++) {
      Node child = childrenList.item(i);
      if (child instanceof Element && child.getNodeName().equals(childName)) {
        return (Element) child;
      }
    }

    // if no such child element can be found
    if (ifNullBuildNewElement) {
      Element newNode = currentNode.getOwnerDocument().createElement(childName);
      Node firstChild = currentNode.getFirstChild();
      currentNode.insertBefore(newNode, firstChild);
      return newNode;
    } else {
      return null;
    }
  }
  public void setCellIndex(int cellIndex) {
    Node parent;
    Node child;
    int index;

    parent = getParentNode();
    if (parent instanceof HTMLTableRowElement) {
      child = parent.getFirstChild();
      while (child != null) {
        if (child instanceof HTMLTableCellElement) {
          if (cellIndex == 0) {
            if (this != child) parent.insertBefore(this, child);
            return;
          }
          --cellIndex;
        }
        child = child.getNextSibling();
      }
    }
    parent.appendChild(this);
  }
  /**
   * Verifies an enveloped signed message and returns the signed data.
   *
   * @param parent The enveloped signed XML object.
   * @param element The actual element (null => root).
   * @param signature The enveloped signature.
   * @param id The mandatory ID element.
   * @return XML document "as-is").
   */
  public XMLObjectWrapper validateEnvelopedSignature(
      XMLObjectWrapper parent, Element element, XMLSignatureWrapper signature, String id)
      throws IOException {
    if (!signature.reference_object_1.enveloped) {
      throw new IOException("Expected enveloped signature");
    }
    if (!signature.reference_object_1.id.equals(id)) {
      throw new IOException("Id mismatch (" + signature.reference_object_1.id + ", " + id + ").");
    }

    try {
      signature.reference_object_1.element = element == null ? parent.getRootElement() : element;
      checkKeyInfoReference(signature);
      Node signsin = signature.getRootElement().getNextSibling();
      Node signpar = signature.getRootElement().getParentNode();
      signpar.removeChild(signature.getRootElement());
      checkMainReference(signature);
      signpar.insertBefore(signature.getRootElement(), signsin);
      verify(signature);
    } catch (GeneralSecurityException gse) {
      throw new IOException(gse.getMessage());
    }
    return parent;
  }
Example #15
0
 void doApply(
     Stylesheet stylesheet,
     QName mode,
     Node context,
     int pos,
     int len,
     Node parent,
     Node nextSibling)
     throws TransformerException {
   Node result = null;
   Document doc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument();
   short nodeType = source.getNodeType();
   if (nodeType == Node.ATTRIBUTE_NODE && parent.getFirstChild() != null) {
     // Ignore attributes added after child elements
   } else {
     // Namespace aliasing
     if (nodeType == Node.ELEMENT_NODE) {
       String prefix = source.getPrefix();
       if (prefix == null) prefix = "#default";
       String resultPrefix = (String) stylesheet.namespaceAliases.get(prefix);
       if (resultPrefix != null) {
         if ("#default".equals(resultPrefix)) resultPrefix = null;
         String uri = source.lookupNamespaceURI(resultPrefix);
         String name = source.getNodeName();
         // Create a new element node in the result document
         result = doc.createElementNS(uri, name);
         // copy attributes
         NamedNodeMap srcAttrs = source.getAttributes();
         NamedNodeMap dstAttrs = result.getAttributes();
         int l = srcAttrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = srcAttrs.item(i);
           if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attr = attr.cloneNode(true);
             attr = doc.adoptNode(attr);
             dstAttrs.setNamedItemNS(attr);
           }
         }
       }
     }
     if (result == null) {
       // Create result node
       result = source.cloneNode(false);
       // Remove any XSL attributes
       NamedNodeMap attrs = result.getAttributes();
       if (attrs != null) {
         int l = attrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = attrs.item(i);
           if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attrs.removeNamedItem(attr.getNodeName());
             i--;
             l--;
           }
         }
       }
       Node result2 = doc.adoptNode(result);
       if (result2 == null) {
         String msg =
             "Error adopting node to result tree: "
                 + result
                 + " ("
                 + result.getClass().getName()
                 + ")";
         DOMSourceLocator l = new DOMSourceLocator(context);
         throw new TransformerException(msg, l);
       }
       result = result2;
     }
     if (nextSibling != null) parent.insertBefore(result, nextSibling);
     else parent.appendChild(result);
     if (nodeType == Node.ELEMENT_NODE)
       stylesheet.addNamespaceNodes(source, result, doc, elementExcludeResultPrefixes);
     // children
     if (children != null) children.apply(stylesheet, mode, context, pos, len, result, null);
   }
   // next sibling
   if (next != null) next.apply(stylesheet, mode, context, pos, len, parent, nextSibling);
 }
Example #16
0
 public static void setFirst(Node parent, Node node) {
   Node first = parent.getFirstChild();
   if (first != null) parent.insertBefore(node, first);
   else parent.appendChild(node);
 }
  public boolean runTest(Test test) throws Exception {
    String filename = test.file.toString();
    if (this.verbose) {
      System.out.println(
          "Running "
              + filename
              + " against "
              + this.host
              + ":"
              + this.port
              + " with "
              + this.browser);
    }
    this.document = parseDocument(filename);

    if (this.baseUrl == null) {
      NodeList links = this.document.getElementsByTagName("link");
      if (links.getLength() != 0) {
        Element link = (Element) links.item(0);
        setBaseUrl(link.getAttribute("href"));
      }
    }
    if (this.verbose) {
      System.out.println("Base URL=" + this.baseUrl);
    }

    Node body = this.document.getElementsByTagName("body").item(0);
    Element resultContainer = document.createElement("div");
    resultContainer.setTextContent("Result: ");
    Element resultElt = document.createElement("span");
    resultElt.setAttribute("id", "result");
    resultElt.setIdAttribute("id", true);
    resultContainer.appendChild(resultElt);
    body.insertBefore(resultContainer, body.getFirstChild());

    Element executionLogContainer = document.createElement("div");
    executionLogContainer.setTextContent("Execution Log:");
    Element executionLog = document.createElement("div");
    executionLog.setAttribute("id", "log");
    executionLog.setIdAttribute("id", true);
    executionLog.setAttribute("style", "white-space: pre;");
    executionLogContainer.appendChild(executionLog);
    body.appendChild(executionLogContainer);

    NodeList tableRows = document.getElementsByTagName("tr");
    Element theadRow = (Element) tableRows.item(0);
    test.name = theadRow.getTextContent();
    appendCellToRow(theadRow, "Result");

    this.commandProcessor =
        new HtmlCommandProcessor(this.host, this.port, this.browser, this.baseUrl);
    String resultState;
    String resultLog;
    test.result = true;
    try {
      this.commandProcessor.start();
      test.commands = new Command[tableRows.getLength() - 1];
      for (int i = 1; i < tableRows.getLength(); i++) {
        Element stepRow = (Element) tableRows.item(i);
        Command command = executeStep(stepRow);
        appendCellToRow(stepRow, command.result);
        test.commands[i - 1] = command;
        if (command.error) {
          test.result = false;
        }
        if (command.failure) {
          test.result = false;
          // break;
        }
      }
      resultState = test.result ? "PASSED" : "FAILED";
      resultLog = (test.result ? "Test Complete" : "Error");
      this.commandProcessor.stop();
    } catch (Exception e) {
      test.result = false;
      resultState = "ERROR";
      resultLog = "Failed to initialize session\n" + e;
      e.printStackTrace();
    }
    document.getElementById("result").setTextContent(resultState);
    Element log = document.getElementById("log");
    log.setTextContent(log.getTextContent() + resultLog + "\n");
    return test.result;
  }