/** * 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); } }
/** * returns a property from a XMl Node * * @param node * @param key * @param caseSensitive * @return Object matching key * @throws SAXException */ public static Object getProperty(Node node, Collection.Key k, boolean caseSensitive) throws SAXException { // String lcKey=StringUtil.toLowerCase(key); if (k.getLowerString().startsWith("xml")) { // Comment if (k.equals(XMLCOMMENT)) { StringBuffer sb = new StringBuffer(); NodeList list = node.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n instanceof Comment) { sb.append(((Comment) n).getData()); } } return sb.toString(); } // NS URI if (k.equals(XMLNSURI)) { undefinedInRoot(k, node); return param(node.getNamespaceURI(), ""); } // Prefix if (k.equals(XMLNSPREFIX)) { undefinedInRoot(k, node); return param(node.getPrefix(), ""); } // Root else if (k.equals(XMLROOT)) { Element re = getRootElement(node, caseSensitive); if (re == null) throw new SAXException( "Attribute [" + k.getString() + "] not found in XML, XML is empty"); return param(re, ""); } // Parent else if (k.equals(XMLPARENT)) { Node parent = getParentNode(node, caseSensitive); if (parent == null) { if (node.getNodeType() == Node.DOCUMENT_NODE) throw new SAXException( "Attribute [" + k.getString() + "] not found in XML, there is no parent element, you are already at the root element"); throw new SAXException( "Attribute [" + k.getString() + "] not found in XML, there is no parent element"); } return parent; } // Name else if (k.equals(XMLNAME)) { return node.getNodeName(); } // Value else if (k.equals(XMLVALUE)) { return StringUtil.toStringEmptyIfNull(node.getNodeValue()); } // type else if (k.equals(XMLTYPE)) { return getTypeAsString(node, true); } // Attributes else if (k.equals(XMLATTRIBUTES)) { NamedNodeMap attr = node.getAttributes(); if (attr == null) throw undefined(k, node); return new XMLAttributes(node.getOwnerDocument(), attr, caseSensitive); } // Text else if (k.equals(XMLTEXT)) { undefinedInRoot(k, node); StringBuffer sb = new StringBuffer(); NodeList list = node.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n instanceof Text || n instanceof CDATASection) { sb.append(((CharacterData) n).getData()); } } return sb.toString(); } else if (k.equals(XMLCDATA)) { undefinedInRoot(k, node); StringBuffer sb = new StringBuffer(); NodeList list = node.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n instanceof Text || n instanceof CDATASection) { sb.append(((CharacterData) n).getData()); } } return sb.toString(); } // children else if (k.equals(XMLCHILDREN)) { return new XMLNodeList(node, caseSensitive); } } if (node instanceof Document) { node = ((Document) node).getDocumentElement(); if (node == null) throw new SAXException("Attribute [" + k.getString() + "] not found in XML, XML is empty"); // if((!caseSensitive && node.getNodeName().equalsIgnoreCase(k.getString())) || (caseSensitive // && node.getNodeName().equals(k.getString()))) { if (nameEqual(node, k.getString(), caseSensitive)) { return XMLStructFactory.newInstance(node, caseSensitive); } } else if (node.getNodeType() == Node.ELEMENT_NODE && Decision.isInteger(k)) { int index = Caster.toIntValue(k, 0); int count = 0; Node parent = node.getParentNode(); String nodeName = node.getNodeName(); Element[] children = XMLUtil.getChildElementsAsArray(parent); for (int i = 0; i < children.length; i++) { if (XMLUtil.nameEqual(children[i], nodeName, caseSensitive)) count++; if (count == index) return XMLCaster.toXMLStruct(children[i], caseSensitive); } String detail; if (count == 0) detail = "there are no Elements with this name"; else if (count == 1) detail = "there is only 1 Element with this name"; else detail = "there are only " + count + " Elements with this name"; throw new SAXException( "invalid index [" + k.getString() + "] for Element with name [" + node.getNodeName() + "], " + detail); } else { List<Node> children = XMLUtil.getChildNodesAsList(node, Node.ELEMENT_NODE, caseSensitive, null); int len = children.size(); Array array = null; // new ArrayImpl(); Element el; XMLStruct sct = null, first = null; for (int i = 0; i < len; i++) { el = (Element) children.get(i); // XMLCaster.toXMLStruct(getChildNode(index),caseSensitive); if (XMLUtil.nameEqual(el, k.getString(), caseSensitive)) { sct = XMLCaster.toXMLStruct(el, caseSensitive); if (array != null) { array.appendEL(sct); } else if (first != null) { array = new ArrayImpl(); array.appendEL(first); array.appendEL(sct); } else { first = sct; } } } if (array != null) { try { return new XMLMultiElementStruct(array, false); } catch (PageException e) { } } if (first != null) return first; } throw new SAXException("Attribute [" + k.getString() + "] not found"); }
/** * removes child from a node * * @param node * @param key * @param caseSensitive * @return removed property */ public static Object removeProperty(Node node, Collection.Key k, boolean caseSensitive) { // String lcKeyx=k.getLowerString(); if (k.getLowerString().startsWith("xml")) { // Comment if (k.equals(XMLCOMMENT)) { StringBuffer sb = new StringBuffer(); NodeList list = node.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n instanceof Comment) { sb.append(((Comment) n).getData()); node.removeChild(XMLCaster.toRawNode(n)); } } return sb.toString(); } // Text else if (k.equals(XMLTEXT)) { StringBuffer sb = new StringBuffer(); NodeList list = node.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n instanceof Text || n instanceof CDATASection) { sb.append(((CharacterData) n).getData()); node.removeChild(XMLCaster.toRawNode(n)); } } return sb.toString(); } // children else if (k.equals(XMLCHILDREN)) { NodeList list = node.getChildNodes(); for (int i = list.getLength() - 1; i >= 0; i--) { node.removeChild(XMLCaster.toRawNode(list.item(i))); } return list; } } NodeList nodes = node.getChildNodes(); Array array = new ArrayImpl(); for (int i = nodes.getLength() - 1; i >= 0; i--) { Object o = nodes.item(i); if (o instanceof Element) { Element el = (Element) o; if (nameEqual(el, k.getString(), caseSensitive)) { array.appendEL(XMLCaster.toXMLStruct(el, caseSensitive)); node.removeChild(XMLCaster.toRawNode(el)); } } } if (array.size() > 0) { try { return new XMLMultiElementStruct(array, false); } catch (PageException e) { } } return null; }
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; }