📄 messageelement.java
字号:
/** * get the prefix for a given namespace URI * @param searchNamespaceURI namespace * @return null for null or emtpy uri, null for no match, and the prefix iff there is a match */ public String getPrefix(String searchNamespaceURI) { if ((searchNamespaceURI == null) || ("".equals(searchNamespaceURI))) return null; if (href != null && getRealElement() != null) { return getRealElement().getPrefix(searchNamespaceURI); } for (int i = 0; namespaces != null && i < namespaces.size(); i++) { Mapping map = (Mapping) namespaces.get(i); if (map.getNamespaceURI().equals(searchNamespaceURI)) { return map.getPrefix(); } } if (parent != null) { return ((MessageElement) parent).getPrefix(searchNamespaceURI); } return null; } /** * map from a prefix to a namespace. * Will recurse <i>upward the element tree</i> until we get a match * @param searchPrefix * @return the prefix, or null for no match */ public String getNamespaceURI(String searchPrefix) { if (searchPrefix == null) { searchPrefix = ""; } if (href != null && getRealElement() != null) { return getRealElement().getNamespaceURI(searchPrefix); } for (int i = 0; namespaces != null && i < namespaces.size(); i++) { Mapping map = (Mapping) namespaces.get(i); if (map.getPrefix().equals(searchPrefix)) { return map.getNamespaceURI(); } } if (parent != null) { return ((MessageElement) parent).getNamespaceURI(searchPrefix); } if (log.isDebugEnabled()) { log.debug(Messages.getMessage("noPrefix00", "" + this, searchPrefix)); } return null; } /** * Returns value of the node as an object of registered type. * @return Object of proper type, or null if no mapping could be found. */ public Object getObjectValue() { Object obj = null; try { obj = getObjectValue(null); } catch (Exception e) { log.debug("getValue()", e); } return obj; } /** * Returns value of the node as an object of registered type. * @param cls Class that contains top level deserializer metadata * @return Object of proper type, or null if no mapping could be found. */ public Object getObjectValue(Class cls) throws Exception { if (objectValue == null) { objectValue = getValueAsType(getType(), cls); } return objectValue; } /** * Sets value of this node to an Object. * A serializer needs to be registered for this object class for proper * operation. * <p> * Note that this method will log an error and no-op if there are * any children in the MessageElement or if the MessageElement was * constructed from XML. * @param newValue node's value or null. */ public void setObjectValue(Object newValue) throws SOAPException { if (children != null && !children.isEmpty()) { SOAPException exc = new SOAPException(Messages.getMessage("childPresent")); log.error(Messages.getMessage("childPresent"), exc); throw exc; } if (textRep != null) { SOAPException exc = new SOAPException(Messages.getMessage("xmlPresent")); log.error(Messages.getMessage("xmlPresent"), exc); throw exc; } this.objectValue = newValue; } public Object getValueAsType(QName type) throws Exception { return getValueAsType(type, null); } /** * This is deserialization logic mixed in to our element class. * It is only valid we have a deserializer, which means that we were created * using {@link MessageElement#MessageElement(String, String, String, org.xml.sax.Attributes, org.apache.axis.encoding.DeserializationContext)} * @param type type to look up a deserializer for. * @param cls class to use for looking up the deserializer. This takes precedence over the type field. * @return the value of the deserializer * @throws Exception */ public Object getValueAsType(QName type, Class cls) throws Exception { if (context == null) { throw new Exception(Messages.getMessage("noContext00")); } Deserializer dser = null; if (cls == null) { dser = context.getDeserializerForType(type); } else { dser = context.getDeserializerForClass(cls); } if (dser == null) { throw new Exception(Messages.getMessage("noDeser00", "" + type)); } boolean oldVal = context.isDoneParsing(); context.deserializing(true); context.pushElementHandler(new EnvelopeHandler((SOAPHandler)dser)); publishToHandler((org.xml.sax.ContentHandler) context); context.deserializing(oldVal); return dser.getValue(); } /** * class that represents a qname in a the qNameAttrs vector. */ protected static class QNameAttr { public QName name; public QName value; } /** * add an attribute to the qname vector. This is a separate vector from the * main attribute list. * @param namespace * @param localName * @param value */ public void addAttribute(String namespace, String localName, QName value) { if (qNameAttrs == null) { qNameAttrs = new Vector(); } QNameAttr attr = new QNameAttr(); attr.name = new QName(namespace, localName); attr.value = value; qNameAttrs.addElement(attr); // !!! Add attribute to attributes! } /** * add a normal CDATA/text attribute. * There is no check whether or not the attribute already exists. * @param namespace namespace URI * @param localName local anme * @param value value */ public void addAttribute(String namespace, String localName, String value) { AttributesImpl attributes = makeAttributesEditable(); attributes.addAttribute(namespace, localName, "", "CDATA", value); } /** * add an attribute. * Note that the prefix is not added to our mapping list. * Also, there is no check whether or not the attribute already exists. * @param attrPrefix prefix. * @param namespace namespace URI * @param localName * @param value */ public void addAttribute(String attrPrefix, String namespace, String localName, String value) { AttributesImpl attributes = makeAttributesEditable(); String attrName = localName; if (attrPrefix != null && attrPrefix.length() > 0) { attrName = attrPrefix + ":" + localName; } attributes.addAttribute(namespace, localName, attrName, "CDATA", value); } /** * Set an attribute, adding the attribute if it isn't already present * in this element, and changing the value if it is. Passing null as the * value will cause any pre-existing attribute by this name to go away. */ public void setAttribute(String namespace, String localName, String value) { AttributesImpl attributes = makeAttributesEditable(); int idx = attributes.getIndex(namespace, localName); if (idx > -1) { // Got it, so replace it's value. if (value != null) { attributes.setValue(idx, value); } else { attributes.removeAttribute(idx); } return; } addAttribute(namespace, localName, value); } /** * get the value of an attribute * @param localName * @return the value or null */ public String getAttributeValue(String localName) { if (attributes == null) { return null; } return attributes.getValue(localName); } /** * bind a a new soap envelope. sets the dirty bit. * @param env */ public void setEnvelope(SOAPEnvelope env) { env.setDirty(); message = env; } /** * get our current envelope * @return envelope or null. */ public SOAPEnvelope getEnvelope() { return message; } /** * get the 'real' element -will follow hrefs. * @return the message element or null if there is a href to something * that is not a MessageElemeent. */ public MessageElement getRealElement() { if (href == null) { return this; } Object obj = context.getObjectByRef(href); if (obj == null) { return null; } if (!(obj instanceof MessageElement)) { return null; } return (MessageElement) obj; } /** * get the message element as a document. * This serializes the element to a string and then parses it. * @see #getAsString() * @return * @throws Exception */ public Document getAsDocument() throws Exception { String elementString = getAsString(); Reader reader = new StringReader(elementString); Document doc = XMLUtils.newDocument(new InputSource(reader)); if (doc == null) { throw new Exception( Messages.getMessage("noDoc00", elementString)); } return doc; } /** * get the message element as a string. * This is not a cheap operation, as we have to serialise the * entire message element to the current context, then * convert it to a string. * Nor is it cached; repeated calls repeat the operation. * @return an XML fragment in a string. * @throws Exception if anything went wrong */ public String getAsString() throws Exception { SerializationContext serializeContext = null; StringWriter writer = new StringWriter(); MessageContext msgContext; if (context != null) { msgContext = context.getMessageContext(); } else { msgContext = MessageContext.getCurrentContext(); } serializeContext = new SerializationContext(writer, msgContext); serializeContext.setSendDecl(false); setDirty(false); output(serializeContext); writer.close(); return writer.getBuffer().toString(); } /** * create a DOM from the message element, by * serializing and deserializing the element * @see #getAsString() * @see #getAsDocument() * @return the root document element of the element * @throws Exception */ public Element getAsDOM() throws Exception { return getAsDocument().getDocumentElement(); } /** * replay the sax events to a handler * @param handler * @throws SAXException */ public void publishToHandler(ContentHandler handler) throws SAXException { if (recorder == null) { throw new SAXException(Messages.getMessage("noRecorder00")); } recorder.replay(startEventIndex, endEventIndex, handler); } /** * replay the sax events to a SAX content handles * @param handler * @throws SAXException */ public void publishContents(ContentHandler handler) throws SAXException { if (recorder == null) { throw new SAXException(Messages.getMessage("noRecorder00")); } recorder.replay(startContentsIndex, endEventIndex-1, handler); } /** This is the public output() method, which will always simply use
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -