schemaunwrapperextension.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 411 行 · 第 1/2 页

JAVA
411
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.wsdl.codegen.extension;

import org.apache.axis2.AxisFault;
import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.axis2.wsdl.WSDLUtil;
import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
import org.apache.axis2.wsdl.codegen.CodeGenerationException;
import org.apache.axis2.wsdl.i18n.CodegenMessages;
import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
import org.apache.axis2.wsdl.util.Constants;
import org.apache.axis2.wsdl.util.MessagePartInformationHolder;
import org.apache.ws.commons.schema.*;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * This extension invokes the schema unwrapper depending on the users setting. it is desirable to
 * put this extension before other extensions since extnsions such as the databinding extension may
 * well depend on the schema being unwrapped previously. For a complete unwrap the following format
 * of the schema is expected < element > < complexType > < sequence > < element
 * /> < /sequence > < /complexType > < /element >
 * <p/>
 * When an unwrapped WSDL is encountered Axis2 generates a wrapper schema and that wrapper schema
 * has the above mentioned format. This unwrapping algorithm will work on a pure doc/lit WSDL if it
 * has the above mentioned format only
 */
public class SchemaUnwrapperExtension extends AbstractCodeGenerationExtension {

    private CodeGenConfiguration codeGenConfiguration;

    /**
     * @param configuration
     * @throws CodeGenerationException
     */
    public void engage(CodeGenConfiguration configuration) throws CodeGenerationException {
        this.codeGenConfiguration = configuration;

        if (!configuration.isParametersWrapped()) {

            // A check to avoid nasty surprises - Since unwrapping is not
            // supported by all frameworks, we check the framework name to be
            // compatible
            if (!ConfigPropertyFileLoader.getUnwrapSupportedFrameworkNames().
                    contains(configuration.getDatabindingType())) {
                throw new CodeGenerationException(
                        CodegenMessages.getMessage("extension.unsupportedforunwrapping"));
            } else if (!ConfigPropertyFileLoader.getUnwrapDirectFrameworkNames().
                    contains(configuration.getDatabindingType())) {

                //walk the schema and find the top level elements
                List services = configuration.getAxisServices();
                AxisService axisService;

                for (Iterator servicesIter = services.iterator(); servicesIter.hasNext();) {
                    axisService = (AxisService) servicesIter.next();
                    for (Iterator operations = axisService.getOperations();
                         operations.hasNext();) {
                        AxisOperation op = (AxisOperation) operations.next();

                        if (WSDLUtil.isInputPresentForMEP(op.getMessageExchangePattern())) {
                            walkSchema(op.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE),
                                    WSDLConstants.INPUT_PART_QNAME_SUFFIX);
                        }
                        // get the out put parameter details as well to unwrap the responses
                        //TODO: support xmlbeans
                        if (configuration.getDatabindingType().equals("adb")) {
                            if (WSDLUtil.isOutputPresentForMEP(op.getMessageExchangePattern())) {
                                walkSchema(op.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE),
                                        WSDLConstants.OUTPUT_PART_QNAME_SUFFIX);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * walk the given schema element For a successful unwrapping the element should have the
     * following structure &lt; element &gt; &lt; complexType &gt; &lt; sequence &gt; &lt; element
     * /&gt; &lt; /sequence &gt; &lt; /complexType &gt; &lt; /element &gt;
     */

    public void walkSchema(AxisMessage message, String qnameSuffix)
            throws CodeGenerationException {
        //nothing to unwrap
        if (message.getSchemaElement() == null) {
            return;
        }


        List partNameList = new LinkedList();

        XmlSchemaElement schemaElement = message.getSchemaElement();
        XmlSchemaType schemaType = schemaElement.getSchemaType();
        QName schemaTypeQname = schemaElement.getSchemaTypeName();
        if (schemaType == null) {
            if (schemaTypeQname != null) {
                // find the schema type from all the schemas
                // now we need to get the schema of the extension type from the parent schema. For that let's first retrieve
                // the parent schema
                AxisService axisService = message.getAxisOperation().getAxisService();
                ArrayList schemasList = axisService.getSchema();

                XmlSchema schema = null;
                for (Iterator iter = schemasList.iterator(); iter.hasNext();) {
                    schema = (XmlSchema) iter.next();
                    schemaType = getSchemaType(schema, schemaTypeQname);
                    if (schemaType != null) {
                        break;
                    }
                }
            }
        }

        if (schemaType instanceof XmlSchemaComplexType) {
            handleAllCasesOfComplexTypes(schemaType,
                    message,
                    partNameList,
                    qnameSuffix);
        } else if ((schemaType instanceof XmlSchemaSimpleType) ||
               ((schemaTypeQname != null) && (schemaTypeQname.equals(new QName("http://www.w3.org/2001/XMLSchema", "anyType")))) ) {
            QName opName = message.getAxisOperation().getName();
            partNameList.add(WSDLUtil.getPartQName(opName.getLocalPart(),
                    qnameSuffix,
                    schemaElement.getQName().getLocalPart()));
        } else if (schemaType == null) {
            throw new CodeGenerationException("Can not determine the schema type for the "
                    + schemaElement.getName());
        } else {
            //we've no idea how to unwrap a non complexType!!!!!!
            throw new CodeGenerationException(
                    CodegenMessages.getMessage("extension.unsupportedSchemaFormat",
                            schemaType.getName(), "complexType"));
        }


        try {
            //set in the axis message that the unwrapping was success
            message.addParameter(getParameter(
                    Constants.UNWRAPPED_KEY,
                    Boolean.TRUE));

            // attach the opName and the parts name list into the
            // axis message by using the holder
            MessagePartInformationHolder infoHolder = new MessagePartInformationHolder();
            infoHolder.setOperationName(message.getAxisOperation().getName());
            infoHolder.setPartsList(partNameList);

            //attach it to the parameters
            message.addParameter(
                    getParameter(Constants.UNWRAPPED_DETAILS,
                            infoHolder));

        } catch (AxisFault axisFault) {
            throw new CodeGenerationException(axisFault);
        }

    }

    private void handleAllCasesOfComplexTypes(XmlSchemaType schemaType,
                                              AxisMessage message,
                                              List partNameList,
                                              String qnameSuffix) throws CodeGenerationException {

        // if a complex type name exits for a element then
        // we keep that complex type to support unwrapping
        if (schemaType instanceof XmlSchemaComplexType) {
            XmlSchemaComplexType cmplxType = (XmlSchemaComplexType) schemaType;
            if (cmplxType.getContentModel() == null) {
                if (cmplxType.getParticle() != null) {
                    processXMLSchemaSequence(cmplxType.getParticle(), message, partNameList,
                            qnameSuffix);
                }
            } else {
                // now lets handle case with extensions
                processComplexContentModel(cmplxType, message, partNameList, qnameSuffix);
            }
            // handle attributes here
            processAttributes(cmplxType, message, partNameList, qnameSuffix);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?