Example #1
0
  /**
   * removes all comments from a node
   *
   * @param node node to remove elements from
   * @param type Type Definition to remove (Constant value from class Node)
   * @param deep remove also in sub nodes
   */
  private static synchronized void removeChilds(Node node, short type, boolean deep) {
    NodeList list = node.getChildNodes();

    for (int i = list.getLength(); i >= 0; i--) {
      Node n = list.item(i);
      if (n == null) continue;
      else if (n.getNodeType() == type) node.removeChild(XMLCaster.toRawNode(n));
      else if (deep) removeChilds(n, type, deep);
    }
  }
Example #2
0
  public static Object setProperty(Node node, Collection.Key k, Object value, boolean caseSensitive)
      throws PageException {
    Document doc = (node instanceof Document) ? (Document) node : node.getOwnerDocument();

    // Comment
    if (k.equals(XMLCOMMENT)) {
      removeChilds(XMLCaster.toRawNode(node), Node.COMMENT_NODE, false);
      node.appendChild(XMLCaster.toRawNode(XMLCaster.toComment(doc, value)));
    }
    // NS URI
    else if (k.equals(XMLNSURI)) {
      // TODO impl
      throw new ExpressionException("XML NS URI can't be set", "not implemented");
    }
    // Prefix
    else if (k.equals(XMLNSPREFIX)) {
      // TODO impl
      throw new ExpressionException("XML NS Prefix can't be set", "not implemented");
      // node.setPrefix(Caster.toString(value));
    }
    // Root
    else if (k.equals(XMLROOT)) {
      doc.appendChild(XMLCaster.toNode(doc, value));
    }
    // Parent
    else if (k.equals(XMLPARENT)) {
      Node parent = getParentNode(node, caseSensitive);
      Key name = KeyImpl.init(parent.getNodeName());
      parent = getParentNode(parent, caseSensitive);

      if (parent == null)
        throw new ExpressionException(
            "there is no parent element, you are already on the root element");

      return setProperty(parent, name, value, caseSensitive);
    }
    // Name
    else if (k.equals(XMLNAME)) {
      throw new XMLException("You can't assign a new value for the property [xmlname]");
    }
    // Type
    else if (k.equals(XMLTYPE)) {
      throw new XMLException("You can't change type of a xml node [xmltype]");
    }
    // value
    else if (k.equals(XMLVALUE)) {
      node.setNodeValue(Caster.toString(value));
    }
    // Attributes
    else if (k.equals(XMLATTRIBUTES)) {
      Element parent = XMLCaster.toElement(doc, node);
      Attr[] attres = XMLCaster.toAttrArray(doc, value);
      // print.ln("=>"+value);
      for (int i = 0; i < attres.length; i++) {
        if (attres[i] != null) {
          parent.setAttributeNode(attres[i]);
          // print.ln(attres[i].getName()+"=="+attres[i].getValue());
        }
      }
    }
    // Text
    else if (k.equals(XMLTEXT)) {
      removeChilds(XMLCaster.toRawNode(node), Node.TEXT_NODE, false);
      node.appendChild(XMLCaster.toRawNode(XMLCaster.toText(doc, value)));
    }
    // CData
    else if (k.equals(XMLCDATA)) {
      removeChilds(XMLCaster.toRawNode(node), Node.CDATA_SECTION_NODE, false);
      node.appendChild(XMLCaster.toRawNode(XMLCaster.toCDATASection(doc, value)));
    }
    // Children
    else if (k.equals(XMLCHILDREN)) {
      Node[] nodes = XMLCaster.toNodeArray(doc, value);
      removeChilds(XMLCaster.toRawNode(node), Node.ELEMENT_NODE, false);
      for (int i = 0; i < nodes.length; i++) {
        if (nodes[i] == node) throw new XMLException("can't assign a XML Node to himself");
        if (nodes[i] != null) node.appendChild(XMLCaster.toRawNode(nodes[i]));
      }
    } else {
      Node child = XMLCaster.toNode(doc, value);
      if (!k.getString().equalsIgnoreCase(child.getNodeName())) {
        throw new XMLException(
            "if you assign a XML Element to a XMLStruct , assignment property must have same name like XML Node Name",
            "Property Name is "
                + k.getString()
                + " and XML Element Name is "
                + child.getNodeName());
      }
      NodeList list = XMLUtil.getChildNodes(node, Node.ELEMENT_NODE);
      int len = list.getLength();
      Node n;
      for (int i = 0; i < len; i++) {
        n = list.item(i);
        if (nameEqual(n, k.getString(), caseSensitive)) {
          node.replaceChild(XMLCaster.toRawNode(child), XMLCaster.toRawNode(n));
          return value;
        }
      }
      node.appendChild(XMLCaster.toRawNode(child));
    }

    return value;
  }