📄 messageelement.java
字号:
* the recorded SAX stream for this element if it is available. If * not, this method calls outputImpl() to allow subclasses and * programmatically created messages to serialize themselves. * * @param outputContext the SerializationContext we will write to. */ public final void output(SerializationContext outputContext) throws Exception { if ((recorder != null) && (!_isDirty)) { recorder.replay(startEventIndex, endEventIndex, new SAXOutputter(outputContext)); return; } // Turn QName attributes into strings if (qNameAttrs != null) { for (int i = 0; i < qNameAttrs.size(); i++) { QNameAttr attr = (QNameAttr)qNameAttrs.get(i); QName attrName = attr.name; setAttribute(attrName.getNamespaceURI(), attrName.getLocalPart(), outputContext.qName2String(attr.value)); } } /** * Write the encoding style attribute IF it's different from * whatever encoding style is in scope.... */ if (encodingStyle != null) { MessageContext mc = outputContext.getMessageContext(); SOAPConstants soapConstants = (mc != null) ? mc.getSOAPConstants() : SOAPConstants.SOAP11_CONSTANTS; if (parent == null) { // don't emit an encoding style if its "" (literal) if (!"".equals(encodingStyle)) { setAttribute(soapConstants.getEnvelopeURI(), Constants.ATTR_ENCODING_STYLE, encodingStyle); } } else if (!encodingStyle.equals(((MessageElement)parent).getEncodingStyle())) { setAttribute(soapConstants.getEnvelopeURI(), Constants.ATTR_ENCODING_STYLE, encodingStyle); } } outputImpl(outputContext); } /** * override point -output to a serialization context. * @param outputContext destination. * @throws Exception if something went wrong. */ protected void outputImpl(SerializationContext outputContext) throws Exception { if (textRep != null) { boolean oldPretty = outputContext.getPretty(); outputContext.setPretty(false); if (textRep instanceof CDATASection) { outputContext.writeString("<![CDATA["); outputContext.writeString(textRep.getData()); outputContext.writeString("]]>"); } else if (textRep instanceof Comment) { outputContext.writeString("<!--"); outputContext.writeString(textRep.getData()); outputContext.writeString("-->"); } else if (textRep instanceof Text) { outputContext.writeSafeString(textRep.getData()); } outputContext.setPretty(oldPretty); return; } if (prefix != null) outputContext.registerPrefixForURI(prefix, namespaceURI); if (namespaces != null) { for (Iterator i = namespaces.iterator(); i.hasNext();) { Mapping mapping = (Mapping) i.next(); outputContext.registerPrefixForURI(mapping.getPrefix(), mapping.getNamespaceURI()); } } if (objectValue != null) { outputContext.serialize(new QName(namespaceURI, name), attributes, objectValue); return; } outputContext.startElement(new QName(namespaceURI, name), attributes); if (children != null) { for (Iterator it = children.iterator(); it.hasNext();) { ((NodeImpl)it.next()).output(outputContext); } } outputContext.endElement(); } /** * Generate a string representation by serializing our contents * This is not a lightweight operation, and is repeated whenever * you call this method. * If the serialization fails, an error is logged and the classic * {@link Object#toString()} operation invoked instead. * @return a string representing the class */ public String toString() { try { return getAsString(); } catch( Exception exp ) { //couldn't turn to a string. //log it log.error(Messages.getMessage("exception00"), exp); //then hand off to our superclass, which is probably object return super.toString(); } } /** * add a new namespace/prefix mapping * @param map new mapping to add * @todo: this code does not verify that the mapping does not exist already; it * is possible to create duplicate mappings. */ public void addMapping(Mapping map) { if (namespaces == null) { namespaces = new ArrayList(); } namespaces.add(map); } // JAXM SOAPElement methods... /** * add the child element * @param childName uri, prefix and local name of the element to add * @return the child element * @throws SOAPException * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.Name) */ public SOAPElement addChildElement(Name childName) throws SOAPException { MessageElement child = new MessageElement(childName.getLocalName(), childName.getPrefix(), childName.getURI()); addChild(child); return child; } /** * add a child element in the message element's own namespace * @param localName * @return the child element * @throws SOAPException * @see javax.xml.soap.SOAPElement#addChildElement(String) */ public SOAPElement addChildElement(String localName) throws SOAPException { // Inherit parent's namespace MessageElement child = new MessageElement(getNamespaceURI(), localName); addChild(child); return child; } /** * add a child element * @param localName * @param prefixName * @return the child element * @throws SOAPException * @see javax.xml.soap.SOAPElement#addChildElement(String, String) */ public SOAPElement addChildElement(String localName, String prefixName) throws SOAPException { MessageElement child = new MessageElement(getNamespaceURI(prefixName), localName); child.setPrefix(prefixName); addChild(child); return child; } /** * add a child element * @param localName * @param childPrefix * @param uri * @return the child element * @throws SOAPException * @see javax.xml.soap.SOAPElement#addChildElement(String, String, String) */ public SOAPElement addChildElement(String localName, String childPrefix, String uri) throws SOAPException { MessageElement child = new MessageElement(uri, localName); child.setPrefix(childPrefix); child.addNamespaceDeclaration(childPrefix, uri); addChild(child); return child; } /** * The added child must be an instance of MessageElement rather than * an abitrary SOAPElement otherwise a (wrapped) ClassCastException * will be thrown. * @see javax.xml.soap.SOAPElement#addChildElement(javax.xml.soap.SOAPElement) */ public SOAPElement addChildElement(SOAPElement element) throws SOAPException { try { addChild((MessageElement)element); setDirty(); return element; } catch (ClassCastException e) { throw new SOAPException(e); } } /** * add a text node to the document. * @return ourselves * @see javax.xml.soap.SOAPElement#addTextNode(String) */ public SOAPElement addTextNode(String s) throws SOAPException { try { Text text = getOwnerDocument().createTextNode(s); ((org.apache.axis.message.Text)text).setParentElement(this); return this; } catch (java.lang.IncompatibleClassChangeError e) { Text text = new org.apache.axis.message.Text(s); this.appendChild(text); return this; } catch (ClassCastException e) { throw new SOAPException(e); } } /** * add a new attribute * @param attrName name of the attribute * @param value a string value * @return ourselves * @throws SOAPException * @see javax.xml.soap.SOAPElement#addAttribute(javax.xml.soap.Name, String) */ public SOAPElement addAttribute(Name attrName, String value) throws SOAPException { try { addAttribute(attrName.getPrefix(), attrName.getURI(), attrName.getLocalName(), value); } catch (RuntimeException t) { throw new SOAPException(t); } return this; } /** * create a {@link Mapping} mapping and add to our namespace list. * @param prefix * @param uri * @return * @throws SOAPException for any {@link RuntimeException} caught * @todo for some reason this logic catches all rutime exceptions and * rethrows them as SOAPExceptions. This is unusual behavio, and should * be looked at closely. * @see javax.xml.soap.SOAPElement#addNamespaceDeclaration(String, String) */ public SOAPElement addNamespaceDeclaration(String prefix, String uri) throws SOAPException { try { Mapping map = new Mapping(uri, prefix); addMapping(map); } catch (RuntimeException t) { //TODO: why is this here? Nowhere else do we turn runtimes into SOAPExceptions. throw new SOAPException(t); } return this; } /** * Get the value of an attribute whose namespace and local name are described. * @param attrName qualified name of the attribute * @return the attribute or null if there was no match * @see SOAPElement#getAttributeValue(javax.xml.soap.Name) */ public String getAttributeValue(Name attrName) { return attributes.getValue(attrName.getURI(), attrName.getLocalName()); } /** * Get an interator to all the attributes of the node. * The iterator is over a static snapshot of the node names; if attributes * are added or deleted during the iteration, this iterator will be not * be updated to follow the changes. * @return an iterator of the attributes. * @see javax.xml.soap.SOAPElement#getAllAttributes() */ public Iterator getAllAttributes() { int num = attributes.getLength(); Vector attrs = new Vector(num); for (int i = 0; i < num; i++) { String q = attributes.getQName(i); String prefix = ""; if (q != null) { int idx = q.indexOf(":"); if (idx > 0) { prefix = q.substring(0, idx); } else { prefix= ""; } } attrs.add(new PrefixedQName(attributes.getURI(i), attributes.getLocalName(i), prefix)); } return attrs.iterator(); } // getNamespaceURI implemented above /** * get an iterator of the prefixes. The iterator * does not get updated in response to changes in the namespace list. * @return an iterator over a vector of prefixes * @see javax.xml.soap.SOAPElement#getNamespacePrefixes() */ public Iterator getNamespacePrefixes() { Vector prefixes = new Vector(); for (int i = 0; namespaces != null && i < namespaces.size(); i++) { prefixes.add(((Mapping)namespaces.get(i)).getPrefix()); } return prefixes.iterator(); } /** * get the full name of the element * @return * @see javax.xml.soap.SOAPElement#getElementName() */ public Name getElementName() { return new PrefixedQName(getNamespaceURI(), getName(), getPrefix()); } /** * remove an element * @param attrName name of the element * @return true if the attribute was found and removed. * @see javax.xml.soap.SOAPElement#removeAttribute(javax.xml.soap.Name) */ public boolean removeAttribute(Name attrName) { AttributesImpl attributes = makeAttributesEditable(); boolean removed = false; for (int i = 0; i < attributes.getLength() && !removed; i++) { if (attributes.getURI(i).equals(attrName.getURI()) && attributes.getLocalName(i).equals(attrName.getLocalName())) { attributes.removeAttribute(i); removed = true; } } return removed; } /** * remove a namespace declaration. * @param namespacePrefix * @return true if the prefix was found and removed. * @see javax.xml.soap.SOAPElement#removeNamespaceDeclaration(String) */ public boolean removeNamespaceDeclaration(String namespacePrefix) { makeAttributesEditable(); boolean removed = false; for (int i = 0; namespaces != null && i < namespaces.size() && !removed; i++) { if (((Mapping)namespaces.get(i)).getPrefix().equals(namespacePrefix)) { namespaces.remove(i); removed = true; } } return removed; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -