📄 messageelement.java
字号:
/** * get an iterator over the children * This iterator <i>may</i> get confused if changes are made to the * children while the iteration is in progress. * @return an iterator over child elements. * @see javax.xml.soap.SOAPElement#getChildElements() */ public Iterator getChildElements() { initializeChildren(); return children.iterator(); } /** * Convenience method to get the first matching child for a given QName. * * @param qname * @return child element or null * @see javax.xml.soap.SOAPElement#getChildElements() */ public MessageElement getChildElement(QName qname) { if (children != null) { for (Iterator i = children.iterator(); i.hasNext();) { MessageElement child = (MessageElement) i.next(); if (child.getQName().equals(qname)) return child; } } return null; } /** * get an iterator over child elements * @param qname namespace/element name of parts to find. * This iterator is not (currently) susceptible to change in the element * list during its lifetime, though changes in the contents of the elements * are picked up. * @return an iterator. */ public Iterator getChildElements(QName qname) { initializeChildren(); int num = children.size(); Vector c = new Vector(num); for (int i = 0; i < num; i++) { MessageElement child = (MessageElement)children.get(i); Name cname = child.getElementName(); if (cname.getURI().equals(qname.getNamespaceURI()) && cname.getLocalName().equals(qname.getLocalPart())) { c.add(child); } } return c.iterator(); } /** * get an iterator over child elements * @param childName namespace/element name of parts to find. * This iterator is not (currently) susceptible to change in the element * list during its lifetime, though changes in the contents of the elements * are picked up. * @return an iterator. * @see javax.xml.soap.SOAPElement#getChildElements(javax.xml.soap.Name) */ public Iterator getChildElements(Name childName) { return getChildElements(new QName(childName.getURI(), childName.getLocalName())); } //DOM methods /** * @see org.w3c.dom.Element#getTagName() * @return the name of the element */ public String getTagName() { return prefix == null ? name : prefix + ":" + name; } /** * remove a named attribute. * @see org.w3c.dom.Element#removeAttribute(String) * @param attrName name of the attributes * @throws DOMException */ public void removeAttribute(String attrName) throws DOMException { AttributesImpl impl = (AttributesImpl)attributes; int index = impl.getIndex(attrName); if(index >= 0){ AttributesImpl newAttrs = new AttributesImpl(); // copy except the removed attribute for(int i = 0; i < impl.getLength(); i++){ // shift after removal if(i != index){ String uri = impl.getURI(i); String local = impl.getLocalName(i); String qname = impl.getQName(i); String type = impl.getType(i); String value = impl.getValue(i); newAttrs.addAttribute(uri,local,qname,type,value); } } // replace it attributes = newAttrs; } } /** * test for an attribute existing * @param attrName name of attribute (or null) * @return true if it exists * Note that the behaviour for a null parameter (returns false) is not guaranteed in future * @see org.w3c.dom.Element#hasAttribute(String) */ public boolean hasAttribute(String attrName) { if(attrName == null) // Do I have to send an exception? attrName = ""; for(int i = 0; i < attributes.getLength(); i++){ if(attrName.equals(attributes.getQName(i))) return true; } return false; } /** * get an attribute by name * @param attrName of attribute * @return the attribute value or null * @see org.w3c.dom.Element#getAttribute(String) */ public String getAttribute(String attrName) { return attributes.getValue(attrName); } /** * Remove an attribute. If the removed * attribute has a default value it is immediately replaced. The * replacing attribute has the same namespace URI and local name, as * well as the original prefix. * If there is no matching attribute, the operation is a no-op. * @see org.w3c.dom.Element#removeAttributeNS(String, String) * @param namespace namespace of attr * @param localName local name * @throws DOMException */ public void removeAttributeNS(String namespace, String localName) throws DOMException { makeAttributesEditable(); Name name = new PrefixedQName(namespace, localName, null); removeAttribute(name); } /** * set or update an attribute. * @see org.w3c.dom.Element#setAttribute(String, String) * @param name attribute name * @param value attribute value * @throws DOMException */ public void setAttribute(String name, String value) throws DOMException { AttributesImpl impl = makeAttributesEditable(); int index = impl.getIndex(name); if (index < 0) { // not found String uri = ""; String localname = name; String qname = name; String type = "CDDATA"; impl.addAttribute(uri, localname, qname, type, value); } else { // found impl.setLocalName(index, value); } } /** * Test for an attribute * @see org.w3c.dom.Element#hasAttributeNS(String, String) * @param namespace * @param localName * @return */ public boolean hasAttributeNS(String namespace, String localName) { if (namespace == null) { namespace = ""; } if (localName == null) // Do I have to send an exception? or just return false { localName = ""; } for(int i = 0; i < attributes.getLength(); i++){ if( namespace.equals(attributes.getURI(i)) && localName.equals(attributes.getLocalName(i))) return true; } return false; } /** * This unimplemented operation is meand to return an attribute as a node * @see org.w3c.dom.Element#getAttributeNode(String) * @param attrName * @return null, always. * @todo Fix this for SAAJ 1.2 Implementation. marked as deprecated to warn people * it is broken * @deprecated this is not implemented */ public Attr getAttributeNode(String attrName) { return null; } /** * remove a an attribue * @param oldAttr * @return oldAttr * @throws DOMException */ public Attr removeAttributeNode(Attr oldAttr) throws DOMException { makeAttributesEditable(); Name name = new PrefixedQName(oldAttr.getNamespaceURI(), oldAttr.getLocalName(), oldAttr.getPrefix()); removeAttribute(name); return oldAttr; } /** * set the attribute node. * @see org.w3c.dom.Element#setAttributeNode(org.w3c.dom.Attr) * @param newAttr * @return newAttr * @throws DOMException * @deprecated this is not implemented * @todo implement */ public Attr setAttributeNode(Attr newAttr) throws DOMException { return newAttr; } /** * set an attribute as a node * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr) * @todo implement properly. * @param newAttr * @return null * @throws DOMException */ public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { //attributes. AttributesImpl attributes = makeAttributesEditable(); // how to convert to DOM ATTR attributes.addAttribute(newAttr.getNamespaceURI(), newAttr.getLocalName(), newAttr.getLocalName(), "CDATA", newAttr.getValue()); return null; } /** * @see org.w3c.dom.Element#getElementsByTagName(String) * @param tagName tag to look for. * @return a list of elements */ public NodeList getElementsByTagName(String tagName) { NodeListImpl nodelist = new NodeListImpl(); for (int i = 0; children != null && i < children.size(); i++) { if (children.get(i) instanceof Node) { Node el = (Node)children.get(i); if (el.getLocalName() != null && el.getLocalName() .equals(tagName)) nodelist.addNode(el); if (el instanceof Element) { NodeList grandchildren = ((Element)el).getElementsByTagName(tagName); for (int j = 0; j < grandchildren.getLength(); j++) { nodelist.addNode(grandchildren.item(j)); } } } } return nodelist; } /** * get the attribute with namespace/local name match. * @see org.w3c.dom.Element#getAttributeNS(String, String) * @param namespaceURI namespace * @param localName name * @return string value or null if not found * @todo: this could be refactored to use getAttributeValue() */ public String getAttributeNS(String namespaceURI, String localName) { if(namespaceURI == null) { namespaceURI = ""; } for (int i = 0; i < attributes.getLength(); i++) { if (attributes.getURI(i).equals(namespaceURI) && attributes.getLocalName(i).equals(localName)) { return attributes.getValue(i); } } return null; } /** * set an attribute or alter an existing one * @see org.w3c.dom.Element#setAttributeNS(String, String, String) * @param namespaceURI namepsace * @param qualifiedName qualified name of the attribue * @param value value * @throws DOMException */ public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException { AttributesImpl attributes = makeAttributesEditable(); String localName = qualifiedName.substring(qualifiedName.indexOf(":")+1, qualifiedName.length()); if (namespaceURI == null) { namespaceURI = "intentionalNullURI"; } attributes.addAttribute(namespaceURI, localName, qualifiedName, "CDATA", value); } /** * @see org.w3c.dom.Element#getAttributeNS(String, String) * @deprecated not implemented! * @param namespace namespace * @param localName local name * @return null */ public Attr getAttributeNodeNS(String namespace, String localName) { return null; //TODO: Fix this for SAAJ 1.2 Implementation } /** * @see org.w3c.dom.Element#getElementsByTagNameNS(String, String) * @param namespace namespace * @param localName local name of element * @return (potentially empty) list of elements that match the (namespace,localname) tuple */ public NodeList getElementsByTagNameNS(String namespace, String localName) { return getElementsNS(this,namespace,localName); } /** * helper method for recusively getting the element that has namespace URI and localname * @param parentElement parent element * @param namespace namespace * @param localName local name of element * @return (potentially empty) list of elements that match the (namespace,localname) tuple */ protected NodeList getElementsNS(org.w3c.dom.Element parentElement, String namespace, String localName) { NodeList children = parentElement.getChildNodes(); NodeListImpl matches = new NodeListImpl(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i) instanceof Text) { continue; } Element child = (Element) children.item(i); if (namespace.equals(child.getNamespaceURI()) && localName.equals(child.getLocalName())) { matches.addNode(child); } // search the grand-children. matches.addNodeList(child.getElementsByTagNameNS(namespace, localName)); } return matches; } /** * get a child node * @param index index value * @return child or null for out of range value * @see org.w3c.dom.NodeList#item(int) */ public Node item(int index) { if (children != null && children.size() > index) { return (Node) children.get(index);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -