Java org.apache.axiom.om.OMElement 代码实例

・18 分钟阅读

以下是展示如何使用org.apache.axiom.om.OMElement的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


public Mediator createSpecificMediator(OMElement elem, Properties properties) {
        SendMediator sm =  new SendMediator();
        // after successfully creating the mediator
        // set its common attributes such as tracing etc
        processAuditStatus(sm,elem);
        OMElement epElement = elem.getFirstChildWithName(ENDPOINT_Q);
        if (epElement != null) {
            // create the endpoint and set it in the send mediator
            Endpoint endpoint = EndpointFactory.getEndpointFromElement(epElement, true, properties);
            if (endpoint != null) {
                sm.setEndpoint(endpoint);
            }
        }
        String receivingSequence = elem.getAttributeValue(RECEIVING_SEQUENCE);
        if (receivingSequence != null) {
            ValueFactory valueFactory = new ValueFactory();
            Value value = valueFactory.createValue(XMLConfigConstants.RECEIVE, elem);
            sm.setReceivingSequence(value);
        }
        String buildMessage = elem.getAttributeValue(BUILD_MESSAGE);
        if ("true".equals(buildMessage)) {
            sm.setBuildMessage(true);
        }
        return sm;
    }
 

实例 2


private static void addProtectedTokens(OMElement configuration, OMNamespace secureVaultNamespace,
                                       SecretResolver secretResolver){
    Iterator iterator = configuration.
            getChildrenWithNamespaceURI(secureVaultNamespace.getNamespaceURI());
    while(iterator.hasNext()){
        OMElement omElement = (OMElement)iterator.next();
        String attributeValue = omElement.
                getAttributeValue(new QName(secureVaultNamespace.getNamespaceURI(),
                                            SecurityConstants.SECURE_VAULT_ALIAS,
                                            secureVaultNamespace.getPrefix()));
        if(attributeValue != null && !attributeValue.equals("")){
            secretResolver.addProtectedToken(attributeValue);
        }
        addProtectedTokens(omElement, secureVaultNamespace, secretResolver);
    }
}
 

实例 3


/**
 * Creates an <code>SecretResolver</code> instance from an XML
 *
 * @param configuration <code>SecretResolver</code> configuration as XML object, OMElement
 * @param isCapLetter   whether the XML element begins with a cap letter
 * @return an <code>SecretResolver</code> instance
 */
public static SecretResolver create(OMElement configuration, boolean isCapLetter) {
    SecretResolver secretResolver = new SecretResolver();
    String secureVaultElementName;
    String passwordProvider = null;
    OMNamespace secureVaultNamespace = null;
    if(isCapLetter){
        secureVaultElementName = SecurityConstants.SECURE_VAULT_CAP;
    } else {
        secureVaultElementName = SecurityConstants.SECURE_VAULT_SIMPLE;
    }
    Iterator iterator = configuration.getAllDeclaredNamespaces();
    while(iterator.hasNext()){
        OMNamespace omNamespace = (OMNamespace) iterator.next();
        if(omNamespace.getNamespaceURI().startsWith(SecurityConstants.SECURE_VAULT_NS)){
            secureVaultNamespace = omNamespace;
            break;
        }
    }
    if(secureVaultNamespace != null){
        Iterator itr = configuration.
                getChildrenWithName(new QName(secureVaultNamespace.getNamespaceURI(),                                                  secureVaultElementName, secureVaultNamespace.getPrefix()));
        if(itr.hasNext()){
            passwordProvider = ((OMElement)itr.next()).
                    getAttributeValue(new QName((SecurityConstants.SECURE_VAULT_ATTRIBUTE)));
        }
        initPasswordManager(secretResolver, passwordProvider);
        if(secretResolver.isInitialized()){
            addProtectedTokens(configuration, secureVaultNamespace, secretResolver);
        }
    }
    return secretResolver;
}
 

实例 4


public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault {
        OMElement omElement = messageContext.getEnvelope().getBody().getFirstElement();
        if (omElement != null) {
            Iterator it = omElement.getChildElements();
            String paraString = "";
            String encoding= format.getCharSetEncoding();
            if(encoding==null)    {
                encoding= "UTF-8";
            }
            while (it.hasNext()) {
                OMElement ele1 = (OMElement) it.next();
                String parameter;
                try {
                    parameter = ele1.getLocalName() + "=" + URLEncoder.encode(ele1.getText(),encoding).replace("+", "%20");
                } catch (UnsupportedEncodingException e) {
                    throw new IllegalArgumentException("UnsupportedEncoding for " + ele1.getText());
                }
                //    "2a" "_JsonReader_PD_2a" replace with ""
                //    "$a" "_JsonReader_PS_a" replace with $
                parameter = parameter.replace("_JsonReader_PD_","").replace("_JsonReader_PS_","$");
                paraString = "".equals(paraString) ? parameter : (paraString + "&" + parameter);
            }
            return paraString.getBytes();
        }
        return new byte[0];
    }
 

实例 5


public Condition createCondition(OMElement ruleElement) throws EvaluatorException {
    Condition r = new Condition();
    OMAttribute priorityAtt = ruleElement.getAttribute(new QName(EvaluatorConstants.PRIORITY));
    if (priorityAtt != null) {
        int p = Integer.parseInt(priorityAtt.getAttributeValue());
        r.setPriority(p);
    }
    OMElement ce = ruleElement.getFirstElement();
    EvaluatorFactory ef = EvaluatorFactoryFinder.getInstance().
            findEvaluatorFactory(ce.getLocalName());
    if (ef == null) {
        handleException("Invalid configuration element: " + ce.getLocalName());
        return null;
    }
    Evaluator evaluator = ef.create(ce);
    r.setEvaluator(evaluator);
    return r;
}
 

实例 6


protected void serializeSourceTextRetriever(SourceTextRetriever textRetriever,
                                            OMElement element) throws EvaluatorException {
    if (textRetriever instanceof HeaderTextRetriever) {
        element.addAttribute(fac.createOMAttribute(EvaluatorConstants.TYPE, nullNS,
                EvaluatorConstants.HEADER));
        addSourceAttribute(textRetriever.getSource(), element);
    } else if (textRetriever instanceof ParameterTextRetriever) {
        element.addAttribute(fac.createOMAttribute(EvaluatorConstants.TYPE, nullNS,
                EvaluatorConstants.PARAM));
        addSourceAttribute(textRetriever.getSource(), element);
    } else if (textRetriever instanceof PropertyTextRetriever) {
        element.addAttribute(fac.createOMAttribute(EvaluatorConstants.TYPE, nullNS,
                EvaluatorConstants.PROPERTY));
        addSourceAttribute(textRetriever.getSource(), element);
    } else if (textRetriever instanceof SOAPEnvelopeTextRetriever) {
        element.addAttribute(fac.createOMAttribute(EvaluatorConstants.TYPE, nullNS,
                EvaluatorConstants.SOAP));
        addSourceAttribute(textRetriever.getSource(), element);
    } else {
        element.addAttribute(fac.createOMAttribute(EvaluatorConstants.TYPE, nullNS,
                EvaluatorConstants.URL));
        if (textRetriever.getSource() != null) {
            element.addAttribute(fac.createOMAttribute(EvaluatorConstants.SOURCE,
                    nullNS, textRetriever.getSource()));
        }
    }
}
 

实例 7


private static void removeNamespaces(OMElement element, boolean processAttrbs) {
    OMNamespace ns = element.getNamespace();
    Iterator i = element.getAllDeclaredNamespaces();
    while (i.hasNext()) {
        i.next();
        i.remove();
    }
    String prefix;
    if (ns != null) {
        prefix = "";//element.getNamespace().getPrefix();
        element.setNamespace(element.getOMFactory().createOMNamespace("", prefix));
    }
    Iterator children = element.getChildElements();
    while (children.hasNext()) {
        removeNamespaces((OMElement) children.next(), processAttrbs);
    }
    if (!processAttrbs) {
        return;
    }
    Iterator attrbs = element.getAllAttributes();
    while (attrbs.hasNext()) {
        OMAttribute attrb = (OMAttribute) attrbs.next();
        prefix = "";//attrb.getQName().getPrefix();
        attrb.setOMNamespace(attrb.getOMFactory().createOMNamespace("", prefix));
        //element.removeAttribute(attrb);
    }
}
 

实例 8


public ReplaceMediator createSpecificMediator(OMElement elem, Properties properties) {
    ReplaceMediator mediator = new ReplaceMediator();
    OMAttribute attTarget = elem.getAttribute(ATT_TARGET);
    OMAttribute attProperty = elem.getAttribute(ATT_PROPERTY);
    if (attTarget != null) {
        try {
            mediator.setTarget(new SynapseXPath(attTarget));
        } catch (JaxenException e) {
            handleException("Invalid XPath specified for the target attribute : " +
                attTarget.getAttributeValue());
        }
    }
    if (attProperty != null) {
        mediator.setProperty(attProperty.getAttributeValue());
    } else {
        handleException("The 'property' attribute is required for the replace mediator");
    }
    return mediator;
}
 

实例 9


/**
 * Builds the Artifact object when an artifact element is given
 *
 * @param artifactEle - artifact OMElement
 * @return created Artifact object
 */
private static SynapseLibrary createSynapseLibraryWithDeps(OMElement artifactEle) {
    if (artifactEle == null) {
        return null;
    }
    SynapseLibrary synLib = new SynapseLibrary(readAttribute(artifactEle, LibDeployerConstants.NAME),
                                               readAttribute(artifactEle, LibDeployerConstants.PACKAGE_ATTR));
    synLib.setDescription(readChildText(artifactEle,LibDeployerConstants.DESCRIPTION_ELEMENT));
    // read the dependencies
    Iterator itr = artifactEle.getChildrenWithLocalName(LibDeployerConstants.DEPENDENCY);
    while (itr.hasNext()) {
        OMElement depElement = (OMElement) itr.next();
        // create a synLib for each dependency and add to the root synLib
        LibraryArtifact.Dependency dep = new LibraryArtifact.Dependency(readAttribute(depElement,
                                                                 LibDeployerConstants.ARTIFACT));
        synLib.addDependency(dep);
    }
    return synLib;
}
 

实例 10


@Override
public String process(OMElement node) {
    childQName = new QName(prefixNameSpaceMap.get(nameSpacePrefix), localName);
    try{
        Iterator children = node.getChildElements();
        OMElement child = (OMElement) children.next();
        OMElement grandChild = child.getFirstChildWithName(childQName);
        if (grandChild != null) {
            if (nextParserComponent == null) {
                return child.toString();
            } else {
                return nextParserComponent.process(child);
            }
        } else {
            return "";
        }
    }catch (NoSuchElementException e){
        return "";
    }
}
 

实例 11


protected void removeSessionID(OMElement header, QName keyQName) {
        OMElement sgcIDElm = getHeaderBlock(header, keyQName);
        if (sgcIDElm != null) {
            sgcIDElm.detach();
        }
    }
 

实例 12


public OMElement setClientName(OMElement cName) {
        cName.build();
        cName.detach();
        cName.setText("Sessions are not supported in this service.");
        return cName;
    }
 

实例 13


private static void removeIndentations(OMElement elem) {
    Iterator children = elem.getChildren();
    while (children.hasNext()) {
        OMNode child = (OMNode) children.next();
        if (child instanceof OMText) {
            if ("".equals(((OMText) child).getText().trim())) {
                children.remove();
            }
        } else if (child instanceof OMElement) {
            removeIndentations((OMElement) child);
        }
    }
}
 

实例 14


public OMElement uploadFileUsingMTOM(OMElement request) throws Exception {
        OMText binaryNode = (OMText) request.
            getFirstChildWithName(new QName("http://services.samples", "request")).
            getFirstChildWithName(new QName("http://services.samples", "image")).
            getFirstOMChild();
        DataHandler dataHandler = (DataHandler) binaryNode.getDataHandler();
        InputStream is = dataHandler.getInputStream();
        File tempFile = File.createTempFile("mtom-", ".gif");
        FileOutputStream fos = new FileOutputStream(tempFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
        byte data[] = new byte[BUFFER];
        int count;
        while ((count = is.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        System.out.println("Wrote MTOM content to temp file : " + tempFile.getAbsolutePath());
        OMFactory factory = request.getOMFactory();
        OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
        OMElement payload  = factory.createOMElement("uploadFileUsingMTOMResponse", ns);
        OMElement response = factory.createOMElement("response", ns);
        OMElement image    = factory.createOMElement("image", ns);
        FileDataSource fileDataSource = new FileDataSource(tempFile);
        dataHandler = new DataHandler(fileDataSource);
        OMText textData = factory.createOMText(dataHandler, true);
        image.addChild(textData);
        response.addChild(image);
        payload.addChild(response);
        MessageContext outMsgCtx = MessageContext.getCurrentMessageContext()
            .getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
        outMsgCtx.setProperty(
            org.apache.axis2.Constants.Configuration.ENABLE_MTOM,
            org.apache.axis2.Constants.VALUE_TRUE);
        return payload;
    }
 

实例 15


/**
 * Order of entries is irrelevant, however its nice to have some order.
 * @param synCfg
 * @param outputStream
 * @throws XMLStreamException
 */
public static void serializeConfiguration(SynapseConfiguration synCfg,
    OutputStream outputStream) throws XMLStreamException {
    if (log.isDebugEnabled()) {
        log.debug("Serializing the XML Configuration to the output stream");
    }
    OMElement definitions
            = ConfigurationFactoryAndSerializerFinder.serializeConfiguration(synCfg);
    definitions.serialize(outputStream);
}
 
讨论
淘淘あ西西 profile image