org.apache.axiom.om.OMElement类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(16.9k)|赞(0)|评价(0)|浏览(517)

本文整理了Java中org.apache.axiom.om.OMElement类的一些代码示例,展示了OMElement类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OMElement类的具体详情如下:
包路径:org.apache.axiom.om.OMElement
类名称:OMElement

OMElement介绍

[英]A particular kind of node that represents an element infoset information item.

An element has a collection of children, attributes, and namespace declarations. In contrast with DOM, this interface exposes namespace declarations separately from the attributes.

Namespace declarations are either added explicitly using #declareNamespace(String,String), #declareDefaultNamespace(String) or #declareNamespace(OMNamespace), or are created implicitly as side effect of other method calls:

  • If the element is created with a namespace and no matching namespace declaration is in scope in the location in the tree where the element is created, then an appropriate namespace declaration will be automatically added to the newly created element. The exact rules depend on the method chosen to create the element; see for example OMFactory#createOMElement(QName).
  • If an attribute with a namespace is added, but no matching namespace declaration is in scope in the element, one is automatically added. See #addAttribute(OMAttribute) for more details.
    Thus, creating a new element or adding an attribute preserves the consistency of the object model with respect to namespaces. However, Axiom does not enforce namespace well-formedness for all possible operations on the object model. E.g. moving an element from one location in the tree to another one may cause the object model to loose its namespace well-formedness. In that case it is possible that the object model contains elements or attributes with namespaces for which no corresponding namespace declarations are in scope.

Fortunately, loosing namespace well-formedness has only very limited impact:

  • If namespace well-formedness is lost, the string to QName resolution for attribute values and element content may be inconsistent, i.e. #resolveQName(String), #getTextAsQName() and OMText#getTextAsQName() may return incorrect results. However, it should be noted that these methods are most relevant for object model instances that have been loaded from existing documents or messages. These object models are guaranteed to be well-formed with respect to namespaces (unless they have been modified after loading).
  • During serialization, Axiom will automatically repair any namespace inconsistencies. It will add necessary namespace declarations to the output document where they are missing in the object model and generate modified namespace declarations where the original ones in the object model are inconsistent. It will also omit redundant namespace declarations. Axiom guarantees that in the output document, every element and attribute (and OMText instance with a QName value) will have the same namespace URI as in the object model, thus preserving the intended semantics of the document. On the other hand, the namespace prefixes used in the output document may differ from the ones in the object model.
  • More precisely, Axiom will always make sure that any OMElement or OMAttributenode will keep the namespace URI that has been assigned to the node at creation time, unless the namespace is explicitly changed using #setNamespace(OMNamespace) or OMNamedInformationItem#setNamespace(OMNamespace,boolean).
    [中]表示元素信息集信息项的特定类型的节点。
    元素具有子元素、属性和命名空间声明的集合。与DOM不同,该接口将命名空间声明与属性分开公开。
    命名空间声明可以使用#declareNamespace(String,String)、#declareDefaultNamespace(String)或#declareNamespace(OMNamespace)显式添加,也可以作为其他方法调用的副作用隐式创建:
    *如果元素是用名称空间创建的,并且在创建元素的树中的位置的作用域中没有匹配的名称空间声明,那么将自动向新创建的元素添加适当的名称空间声明。确切的规则取决于创建元素所选择的方法;例如,请参见OMFactory#CreateOmeElement(QName)。
    *如果添加了具有名称空间的属性,但元素的作用域中没有匹配的名称空间声明,则会自动添加一个名称空间声明。有关更多详细信息,请参见#添加属性(OMAttribute)。
    因此,创建新元素或添加属性可以保持对象模型与名称空间的一致性。然而,Axiom并没有为对象模型上所有可能的操作强制执行名称空间良好格式。例如,将元素从树中的一个位置移动到另一个位置可能会导致对象模型失去其名称空间的良好形式。在这种情况下,对象模型可能包含的元素或属性的名称空间中没有相应的名称空间声明。
    幸运的是,失去名称空间良好形式的影响非常有限:
    *如果命名空间格式良好丢失,属性值和元素内容的字符串到QName的解析可能不一致,即#resolveQName(string)、#getTextAsQName()和OMText#getTextAsQName()可能返回不正确的结果。但是,应该注意,这些方法与从现有文档或消息加载的对象模型实例最相关。这些对象模型保证在名称空间方面格式良好(除非加载后对其进行了修改)。
    *在序列化过程中,Axiom将自动修复任何名称空间不一致的情况。它将向输出文档中添加对象模型中缺少的必要名称空间声明,并在对象模型中原始名称空间声明不一致的地方生成修改后的名称空间声明。它还将省略冗余的名称空间声明。Axiom保证在输出文档中,每个元素和属性(以及带有QName值的OMText实例)将具有与对象模型中相同的名称空间URI,从而保留文档的预期语义。另一方面,输出文档中使用的名称空间前缀可能与对象模型中使用的名称空间前缀不同。
    *更准确地说,Axiom将始终确保任何OmeElement或OMAttributenode将保留在创建时分配给节点的名称空间URI,除非使用#setNamespace(OMNamespace)或OMNamedInformationItem#setNamespace(OMNamespace,布尔值)显式更改名称空间。

代码示例

代码示例来源:origin: org.wso2.carbon.registry/org.wso2.carbon.registry.extensions

public void setStates(OMElement locationConfiguration) throws RegistryException {
  Iterator confElements = locationConfiguration.getChildElements();
  while (confElements.hasNext()) {
    OMElement confElement = (OMElement)confElements.next();
    if (confElement.getQName().equals(new QName("state"))) {
      states.put(confElement.getAttributeValue(new QName("key")), confElement.getText());
    }
  }
}

代码示例来源:origin: stackoverflow.com

OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);

OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);

OMElement omuserName = omFactory.createOMElement(new QName("", "Username", "wsse"), null);
omuserName.setText("myusername");

OMElement omPassword = omFactory.createOMElement(new QName("", "Password", "wsse"), null);
omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
omPassword.setText("mypassword");

omusertoken.addChild(omuserName);
omusertoken.addChild(omPassword);
omSecurityElement.addChild(omusertoken);
stub._getServiceClient().addHeader(omSecurityElement);

代码示例来源:origin: org.wso2.bpel/ode-bpel-epr

@SuppressWarnings("unchecked")
private static OMElement stripNamespace(OMElement element) {
  OMElement parent = OM.createOMElement(new QName("", element.getLocalName()));
  Iterator<OMElement> iter = (Iterator<OMElement>) element.getChildElements();
  while (iter.hasNext()) {
    OMElement child = iter.next();
    child = child.cloneOMElement();
    parent.addChild(child);
  }
  return parent;
}

代码示例来源:origin: org.apache.axis2/axis2-kernel

public String getURL() {
  String urlValue = null;
  OMElement url = data.getFirstChildWithName(new QName(
      DRConstants.SERVICE_DATA.URL));
  if (url != null) {
    urlValue = url.getText();
  }
  return urlValue;
}

代码示例来源:origin: wmixvideo/nfe

_messageContext = new org.apache.axis2.context.MessageContext();
  env = this.toEnvelope(Stub.getFactory(_operationClient.getOptions().getSoapVersionURI()), nfeDistDFeInteresse, this.optimizeContent(new javax.xml.namespace.QName("http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe", "nfeDistDFeInteresse")));
  _messageContext.setEnvelope(env);
  final org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();
  final java.lang.Object object = this.fromOM(_returnEnv.getBody().getFirstElement(), NFeDistDFeInteresseResponse.class, this.getEnvelopeNamespaces(_returnEnv));
  return (NFeDistDFeInteresseResponse) object;
} catch (final org.apache.axis2.AxisFault f) {
  final org.apache.axiom.om.OMElement faultElt = f.getDetail();
  if (faultElt != null) {
    if (this.faultExceptionNameMap.containsKey(faultElt.getQName())) {
        final java.lang.String exceptionClassName = (java.lang.String) this.faultExceptionClassNameMap.get(faultElt.getQName());
        final java.lang.Class exceptionClass = java.lang.Class.forName(exceptionClassName);
        final java.lang.Exception ex = (java.lang.Exception) exceptionClass.newInstance();
        final java.lang.String messageClassName = (java.lang.String) this.faultMessageMap.get(faultElt.getQName());
        final java.lang.Class messageClass = java.lang.Class.forName(messageClassName);
        final java.lang.Object messageObject = this.fromOM(faultElt, messageClass, null);
        final java.lang.reflect.Method m = exceptionClass.getMethod("setFaultMessage", messageClass);
        m.invoke(ex, messageObject);
        throw new java.rmi.RemoteException(ex.getMessage(), ex);
      } catch (final ClassCastException | InstantiationException | IllegalAccessException | java.lang.reflect.InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {

代码示例来源:origin: org.apache.axis2/axis2-transport-http

private static String getProxyHost(OMElement proxyConfiguration) throws AxisFault {
  OMElement proxyHostElement = proxyConfiguration.getFirstChildWithName(new QName(PROXY_HOST_ELEMENT));
  if (proxyHostElement == null) {
    log.error(PROXY_HOST_ELEMENT_NOT_FOUND);
    throw new AxisFault(PROXY_HOST_ELEMENT_NOT_FOUND);
  }
  String proxyHost = proxyHostElement.getText();
  if (proxyHost == null) {
    log.error(PROXY_HOST_ELEMENT_WITH_EMPTY_VALUE);
    throw new AxisFault(PROXY_HOST_ELEMENT_WITH_EMPTY_VALUE);
  }
  return proxyHost;
}

代码示例来源:origin: org.apache.synapse/synapse-samples

public static MessageContext sendUsingSwA(String fileName, String targetEPR) throws IOException {
  OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);
  MessageContext mc = new MessageContext();
  String attachmentID = mc.addAttachment(dataHandler);
  OMElement request = factory.createOMElement("request", ns);
  OMElement imageId = factory.createOMElement("imageId", ns);
  imageId.setText(attachmentID);
  request.addChild(imageId);
  payload.addChild(request);
  env.getBody().addChild(payload);
  mc.setEnvelope(env);
  MessageContext response = mepClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
  SOAPBody body = response.getEnvelope().getBody();
  String imageContentId = body.
      getFirstChildWithName(new QName("http://services.samples", "uploadFileUsingSwAResponse")).
      getFirstChildWithName(new QName("http://services.samples", "response")).
      getFirstChildWithName(new QName("http://services.samples", "imageId")).
      getText();
  Attachments attachment = response.getAttachmentMap();

代码示例来源:origin: org.wso2.carbon.data/org.wso2.carbon.dataservices.core

private static String getTextValueFromOMElement(OMElement omEl) {
  String nillValue = omEl.getAttributeValue(
      new QName(DBConstants.XSI_NAMESPACE, DBConstants.NIL));
  if (nillValue != null && (nillValue.equals("1") || nillValue.equals("true"))) {
    return null;
  } else {
    return omEl.getText();
  }
}

代码示例来源:origin: wso2/wso2-synapse

public OMElement processDocument(InputStream inputStream,
    String contentType, MessageContext messageContext) throws AxisFault {
  try {
    //Fix for https://wso2.org/jira/browse/CARBON-7256
    messageContext.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
    //We will create a SOAP message, which holds the input message as a blob
    SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
    SOAPEnvelope env = factory.getDefaultEnvelope();
    if (inputStream != null) {
      OMNamespace ns = factory.createOMNamespace(
          RelayConstants.BINARY_CONTENT_QNAME.getNamespaceURI(), "ns");
      OMElement omEle = factory.createOMElement(
          RelayConstants.BINARY_CONTENT_QNAME.getLocalPart(), ns);
      StreamingOnRequestDataSource ds = new StreamingOnRequestDataSource(inputStream);
      DataHandler dataHandler = new DataHandler(ds);
      //create an OMText node with the above DataHandler and set optimized to true
      OMText textData = factory.createOMText(dataHandler, true);
      omEle.addChild(textData);
      env.getBody().addChild(omEle);
    }
    return env;
  } catch (SOAPProcessingException e) {
    throw AxisFault.makeFault(e);
  } catch (OMException e) {
    throw AxisFault.makeFault(e);
  }
}

代码示例来源:origin: org.wso2.carbon.commons/org.wso2.carbon.reporting.template.core

private void createChartReportElement() {
  OMFactory fac = OMAbstractFactory.getOMFactory();
  chartReportElement = fac.createOMElement(new QName(CHART_REPORT));
  chartReportElement.addAttribute("name", chartReport.getReportName(), null);
  reportElement.addChild(chartReportElement);
}

代码示例来源:origin: org.wso2.wsas/wso2wsas-admin

public String getProofKeyType() throws AxisFault {
  try {
    MessageContext msgCtx = MessageContext.getCurrentMessageContext();
    AxisConfiguration config = msgCtx.getConfigurationContext().getAxisConfiguration();
    AxisService service = config.getService(ServerConstants.STS_NAME);
    Parameter origParam = service
        .getParameter(SAMLTokenIssuerConfig.SAML_ISSUER_CONFIG
            .getLocalPart());
    if(origParam != null) {
      OMElement samlConfigElem =
          origParam.getParameterElement().
              getFirstChildWithName(SAMLTokenIssuerConfig.SAML_ISSUER_CONFIG);
      SAMLTokenIssuerConfig samlConfig = new SAMLTokenIssuerConfig(samlConfigElem);
      return samlConfig.getProofKeyType();
    } else {
      throw new AxisFault("missing parameter : "
                + SAMLTokenIssuerConfig.SAML_ISSUER_CONFIG.getLocalPart());
    }
  } catch (Exception e) {
    throw new AxisFault(e.getMessage(), e);
  }
}

代码示例来源:origin: org.apache.sandesha2/sandesha2-samples

public void onMessage(MessageContext arg0) {
    SOAPBody body = arg0.getEnvelope().getBody();
    
    OMElement echoStringResponseElem = body.getFirstChildWithName(new QName (applicationNamespaceName,echoStringResponse));
    if (echoStringResponseElem==null) { 
      System.out.println("Error: SOAPBody does not have a 'echoStringResponse' child");
      return;
    }
    
    OMElement echoStringReturnElem = echoStringResponseElem.getFirstChildWithName(new QName (applicationNamespaceName,EchoStringReturn));
    if (echoStringReturnElem==null) { 
      System.out.println("Error: 'echoStringResponse' element does not have a 'EchoStringReturn' child");
      return;
    }
    
    String resultStr = echoStringReturnElem.getText();
    System.out.println("Callback '" + name +  "' got result:" + resultStr);
  }
}

代码示例来源:origin: org.apache.axis2/axis2-transport-jms

/**
 * Guess the message type to use for JMS looking at the message contexts' envelope
 * @param msgContext the message context
 * @return JMSConstants.JMS_BYTE_MESSAGE or JMSConstants.JMS_TEXT_MESSAGE or null
 */
private String guessMessageType(MessageContext msgContext) {
  OMElement firstChild = msgContext.getEnvelope().getBody().getFirstElement();
  if (firstChild != null) {
    if (BaseConstants.DEFAULT_BINARY_WRAPPER.equals(firstChild.getQName())) {
      return JMSConstants.JMS_BYTE_MESSAGE;
    } else if (BaseConstants.DEFAULT_TEXT_WRAPPER.equals(firstChild.getQName())) {
      return JMSConstants.JMS_TEXT_MESSAGE;
    }
  }
  return null;
}

代码示例来源:origin: org.wso2.carbon.governance/org.wso2.carbon.governance.list

@Override
public void setPayload(OMElement bodyContent) throws XMLStreamException {
  OMFactory factory = bodyContent.getOMFactory();
  OMElement returnElement = factory.createOMElement(new QName(bodyContent.getNamespace().getPrefix() + ":return"));
  returnElement.setText(String.valueOf(succeed));
  bodyContent.addChild(returnElement);
}

代码示例来源:origin: stackoverflow.com

SOAPEnvelope mes = messageContext.getEnvelope();
SOAPHeader mesh = mes.getHeader();
SOAPBody mesb = mes.getBody();
OMElement messageId = mesh.getFirstChildWithName(new QName("http://www.w3.org/2005/08/addressing","MessageID"));
String messageIDStr = messageId.getText();
OMElement bodyChild = mesb.getFirstElement();
OMElement remoteAddress = bodyChild.getFirstChildWithName(new QName(
                  "http://YourNameSpaceURI",
                  "remoteAddress"));
String remoteAddressStr = remoteAddress.getText();

代码示例来源:origin: org.apache.synapse/synapse-core

private void populatePropertyName(BeanMediator mediator, OMElement elem) {
  String attributeValue;
  attributeValue = elem.getAttributeValue(new QName(BeanConstants.PROPERTY));
  if (attributeValue != null) {
    mediator.setPropertyName(attributeValue);
  } else {
    handleException("'property' attribute of Bean mediator is required when " +
        "SET/GET_PROPERTY action is set.");
  }
}

代码示例来源:origin: org.wso2.carbon.data/org.wso2.carbon.dataservices.core

@SuppressWarnings("unchecked")
private void secureVaultResolve(OMElement dbsElement) {
  String secretAliasAttr = dbsElement.getAttributeValue(
      new QName(DataSourceConstants.SECURE_VAULT_NS, DataSourceConstants.SECRET_ALIAS_ATTR_NAME));
  if (secretAliasAttr != null) {
    dbsElement.setText(DBUtils.loadFromSecureVault(secretAliasAttr));
  }
  Iterator<OMElement> childEls = (Iterator<OMElement>) dbsElement.getChildElements();
  while (childEls.hasNext()) {
    this.secureVaultResolve(childEls.next());
  }
}

代码示例来源:origin: org.apache.synapse/synapse-core

private static OMElement getSerializedDescription(MessageProcessor processor) {
  OMElement descriptionElem = fac.createOMElement(
      new QName(SynapseConstants.SYNAPSE_NAMESPACE, "description"));
  if (processor.getDescription() != null) {
    descriptionElem.setText(processor.getDescription());
    return descriptionElem;
  } else {
    return null;
  }
}

代码示例来源:origin: org.apache.synapse/synapse-core

/**
 * This method will check the given OMElement represent either a static property or not
 * 
 * @param property - OMElement to be checked for the static property
 * @return boolean true if the elemet represents a static property element false otherwise
 */
public static boolean isStaticProperty(OMElement property) {
  return "property".equals(property.getLocalName().toLowerCase())
    && (property.getAttributeValue(new QName("expression")) == null);
}

代码示例来源:origin: wso2/wso2-synapse

/**
 * Reads the SOAP body of a message and attempts to retreive the session identifier string
 *
 * @param msgCtx Axis2 MessageContext
 * @return a String uniquely identifying a session or null
 */
public static String getSourceSession(MessageContext msgCtx) {
  String srcSession;
  SOAPBody body = msgCtx.getEnvelope().getBody();
  OMNamespace ns = getNamespaceOfFIXPayload(body);
  if (ns == null) {
    OMElement messageNode = body.getFirstChildWithName(new QName(FIXConstants.FIX_MESSAGE));
    srcSession = messageNode.getAttributeValue(new QName(
        FIXConstants.FIX_MESSAGE_INCOMING_SESSION));
  } else {
    srcSession = getSourceSession(body, ns);
  }
  return srcSession;
}

相关文章

微信公众号

最新文章

更多