Package org.wso2.carbon.dataservices.core

Source Code of org.wso2.carbon.dataservices.core.DataServiceDocLitWrappedSchemaGenerator

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. 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.wso2.carbon.dataservices.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.namespace.QName;

import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
import org.apache.axis2.description.java2wsdl.TypeTable;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaAttribute;
import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaForm;
import org.apache.ws.commons.schema.XmlSchemaImport;
import org.apache.ws.commons.schema.XmlSchemaObject;
import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
import org.apache.ws.commons.schema.XmlSchemaParticle;
import org.apache.ws.commons.schema.XmlSchemaSequence;
import org.apache.ws.commons.schema.XmlSchemaType;
import org.apache.ws.commons.schema.XmlSchemaUse;
import org.apache.ws.commons.schema.constants.Constants;
import org.apache.ws.commons.schema.utils.NamespaceMap;
import org.wso2.carbon.dataservices.common.DBConstants;
import org.wso2.carbon.dataservices.common.DBConstants.ResultTypes;
import org.wso2.carbon.dataservices.core.description.operation.Operation;
import org.wso2.carbon.dataservices.core.description.query.Query;
import org.wso2.carbon.dataservices.core.description.resource.Resource;
import org.wso2.carbon.dataservices.core.description.resource.Resource.ResourceID;
import org.wso2.carbon.dataservices.core.engine.CallQuery;
import org.wso2.carbon.dataservices.core.engine.CallQueryGroup;
import org.wso2.carbon.dataservices.core.engine.CallableRequest;
import org.wso2.carbon.dataservices.core.engine.DataService;
import org.wso2.carbon.dataservices.core.engine.OutputElement;
import org.wso2.carbon.dataservices.core.engine.OutputElementGroup;
import org.wso2.carbon.dataservices.core.engine.QueryParam;
import org.wso2.carbon.dataservices.core.engine.Result;
import org.wso2.carbon.dataservices.core.engine.StaticOutputElement;
import org.wso2.carbon.dataservices.core.engine.CallQuery.WithParam;

/**
* Used to generate XML Schema for a Data Service.
*/
public class DataServiceDocLitWrappedSchemaGenerator {
 
  private static final String DUMMY_NAME = "__dummy__";

  /**
   * Populates the given axis service schema with the data service information it encapsulates.
   * @param axisService The target service
   * @throws DataServiceFault
   */
  public static void populateServiceSchema(AxisService axisService) throws DataServiceFault {
    DataService dataservice = (DataService) axisService.getParameter(
        DBConstants.DATA_SERVICE_OBJECT).getValue();
    CommonParams cparams = new CommonParams(axisService, dataservice);
   
    List<List<Operation>> allOps = extractOperations(dataservice);
    List<Operation> normalOperations = allOps.get(0);
    List<Operation> batchOperations = allOps.get(1);
    List<Resource> resources = extractResources(dataservice);
   
    /* create the fault element */
    createAndStoreFaultElement(cparams);
   
    /* create the request status element */
    createAndStoreRequestStatusElement(cparams);
   
    /* process normal operations */
    for (Operation normalOp : normalOperations) {
      processRequest(cparams, normalOp);
    }
    /* process batch operations */
    for (Operation batchOp : batchOperations) {
      processRequest(cparams, batchOp);
    }
    /* process resources */
    for (Resource resource : resources) {
      processRequest(cparams, resource);
    }
       
    /* set the schema */
    axisService.addSchema(cparams.getSchemaMap().values());
  }
 
  /**
   * Process a single request, i.e. operation/resource.
   * @param cparams The common parameters used in the schema generator
   * @param request The request to be processed
   */
  private static void processRequest(CommonParams cparams, CallableRequest request)
      throws DataServiceFault {
    /* process input parameters */
    processRequestInput(cparams, request);
    /* process output types */
    processRequestOutput(cparams, request);
  }
 
  /**
   * Process the given request's input parameters.
   * @param cparams The common parameters used in the schema generator
   * @param request The request used to process the input
   */
  private static void processRequestInput(CommonParams cparams, CallableRequest request)
      throws DataServiceFault {
    String requestName = request.getRequestName();
    AxisOperation axisOp = cparams.getAxisService().getOperation(new QName(requestName));
    CallQueryGroup cqGroup = request.getCallQueryGroup();
    CallQuery defCallQuery = cqGroup.getDefaultCallQuery();
    Query query = defCallQuery.getQuery();
    AxisMessage inMessage = axisOp.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    /* only create input parameters, if there are any available */
    if (inMessage != null &&
        (cparams.getDataService().isEmptyParamFix() ||
        defCallQuery.getWithParams().size() > 0)) {
      inMessage.setName(requestName + Java2WSDLConstants.MESSAGE_SUFFIX)
      /* create input message element */
      XmlSchemaElement inputElement = createElement(cparams, query.getInputNamespace(),
          requestName, true);
      /* complex type for input message element */
      XmlSchemaComplexType inputComplexType = createComplexType(cparams,
          query.getInputNamespace(), requestName, false);
      /* set element type */
      inputElement.setType(inputComplexType);
      /* batch requests */
      if (request instanceof Operation && ((Operation) request).isBatchRequest()) {
        XmlSchemaElement nestedEl = new XmlSchemaElement();
        Operation parentOp = ((Operation) request).getParentOperation()
            if (parentOp != null) {
              nestedEl.setRefName(cparams.getRequestInputElementMap().get(
                  parentOp.getRequestName()));
              nestedEl.setMaxOccurs(Long.MAX_VALUE);
              addElementToComplexTypeSequence(cparams, inputComplexType, query.getInputNamespace(),
                  nestedEl, false);
            } else {
          throw new DataServiceFault("No parent operation for batch request: "
              + request.getRequestName());
            }
      } else {
        /* normal requests */
        XmlSchemaElement tmpEl;
        Map<String, WithParam> withParams = defCallQuery.getWithParams();
        WithParam tmpWithParam;
        /* create elements for individual parameters */
        for (QueryParam queryParam : query.getQueryParams()) {
          if (DBConstants.QueryTypes.IN.equals(queryParam.getType())
              || DBConstants.QueryTypes.INOUT.equals(queryParam.getType())) {
            tmpWithParam = withParams.get(queryParam.getName());
            if (tmpWithParam == null) {
              /* this query param's value must be coming from an export, not from the
               * operation's parameter */
              continue;
            }
            tmpEl = createInputEntryElement(cparams, query, queryParam, tmpWithParam);
            /* add to input element complex type */
            addElementToComplexTypeSequence(cparams, inputComplexType, query.getInputNamespace(),
                tmpEl, false);
          }
        }       
      }
      /* set the input element qname in message */
      inMessage.setElementQName(inputElement.getQName());
      /* store request name and element qname mapping */
      cparams.getRequestInputElementMap().put(request.getRequestName(),
          inMessage.getElementQName());
    }
  }
 
  /**
   * Create an element for an input parameter.
   * @param cparams The common parameters used in the schema generator
   * @param query The query which the parameter belongs to
   * @param queryParam The query parameter
   * @param withParam The operation parameter mapping
   * @return Newly created element
   */
  private static XmlSchemaElement createInputEntryElement(CommonParams cparams, Query query,
      QueryParam queryParam, WithParam withParam) {
    XmlSchemaElement element = createElement(cparams, query.getInputNamespace(),
        withParam.getOriginalName(), false);
    element.setSchemaTypeName(DBUtils.getSimpleSchemaTypeName(cparams.getTypeTable(),
        DBUtils.getJavaTypeFromSQLType(queryParam.getSqlType())));
    /* default is minOccurs=1, maxOccurs=1 */
    if (DBConstants.QueryParamTypes.ARRAY.equals(queryParam.getParamType())) {
      element.setMaxOccurs(Long.MAX_VALUE);
      element.setMinOccurs(0);
    } else if (queryParam.getDefaultValue() != null) {
      element.setMinOccurs(0);
    }
    element.setNillable(true);
    return element;
  }

  /**
   * Process the given request's output types.
   * @param cparams The common parameters used in the schema generator
   * @param request The request used to process the output
   */
  private static void processRequestOutput(CommonParams cparams, CallableRequest request)
      throws DataServiceFault {
    CallQuery defCQ = request.getCallQueryGroup().getDefaultCallQuery();
    if (!(defCQ.getQuery().hasResult() || request.isReturnRequestStatus())) {
      return;
    }
   
    AxisOperation axisOp = cparams.getAxisService().getOperation(
        new QName(request.getRequestName()));
    AxisMessage outMessage = axisOp.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
    outMessage.setName(request.getRequestName() + Java2WSDLConstants.RESPONSE);
   
    if (request.isReturnRequestStatus()) {
      outMessage.setElementQName(new QName(DBConstants.WSO2_DS_NAMESPACE,
          DBConstants.REQUEST_STATUS_WRAPPER_ELEMENT));
      return;
    }
   
    Result result = defCQ.getQuery().getResult();
    if (result.isXsAny() || result.getResultType() == ResultTypes.RDF) {
      outMessage.setElementQName(Constants.XSD_ANYTYPE);
      return;
    }
   
    /* create dummy element to contain the result element */
    XmlSchemaElement dummyParentElement = new XmlSchemaElement();
    dummyParentElement.setQName(new QName(result.getNamespace(), DUMMY_NAME));
    XmlSchema dummySchema = retrieveSchema(cparams, result.getNamespace());
    XmlSchemaComplexType dummyType = new XmlSchemaComplexType(dummySchema);
    dummyType.setName(DUMMY_NAME);
    dummyParentElement.setType(dummyType);
    /* lets do it */
    processCallQuery(cparams, dummyParentElement, defCQ);
    /* extract the element and set it to the message */
    XmlSchemaElement resultEl = (XmlSchemaElement) ((XmlSchemaSequence) dummyType.getParticle())
        .getItems().getItem(0);
    outMessage.setElementQName(resultEl.getRefName());
  }
 
  /**
   * Process the given call query to create the element/types.
   * @param cparams The common parameters used in the schema generator
   * @param parentElement The parent element where the created element will be added to
   * @param callQuery The call query used to create the element/types
   */
  private static void processCallQuery(CommonParams cparams, XmlSchemaElement parentElement,
      CallQuery callQuery) throws DataServiceFault {
    if (!callQuery.getQuery().hasResult()) {
      return;
    }
    Result result = callQuery.getQuery().getResult();
    XmlSchemaElement activeElement = parentElement;
    XmlSchemaElement tmpElement;
    /* process result wrapper */
    if (!DBUtils.isEmptyString(result.getElementName())) {     
      tmpElement = getElement(cparams, result.getNamespace(), result.getElementName());
      if (tmpElement != null) {
        /* the element already exists .. */
        addToElement(cparams, activeElement, tmpElement, true);
        /* no need to continue */
        return;
      }
      activeElement = createAndAddToElement(cparams, activeElement,
          result.getElementName(), result.getNamespace(), true);
    }
    /* process result row */
    if (!DBUtils.isEmptyString(result.getRowName())) {
      tmpElement = getElement(cparams, result.getNamespace(), result.getRowName());
      if (tmpElement != null) {
        addToElement(cparams, activeElement, tmpElement, true);
        return;
      }
      activeElement = createAndAddToElement(cparams, activeElement,
          result.getRowName(), result.getNamespace(), false);
      /* rows can be from zero to infinity */
      activeElement.setMinOccurs(0);
      activeElement.setMaxOccurs(Long.MAX_VALUE);
    }
    /* process the result content */
    processElementGroup(cparams, activeElement, result.getDefaultElementGroup());
  }
 
  /**
   * Process the given element group.
   * @param cparams The common parameters used in the schema generator
   * @param parentElement The parent element where the element group will be added to
   * @param elementGroup The element group to be added to the parent element
   * @throws DataServiceFault
   */
  private static void processElementGroup(CommonParams cparams, XmlSchemaElement parentElement,
      OutputElementGroup elementGroup) throws DataServiceFault {
    XmlSchemaElement activeElement = parentElement;
    if (!DBUtils.isEmptyString(elementGroup.getName())) {
      XmlSchemaElement tmpElement = getElement(cparams,
          elementGroup.getNamespace(), elementGroup.getName());
      if (tmpElement != null) {
        addToElement(cparams, activeElement, tmpElement, true);
        /* element group already exists, nothing else to do here .. */
        return;
      }
      activeElement = createAndAddToElement(cparams, activeElement,
          elementGroup.getName(), elementGroup.getNamespace(), true);
    }
    /* process attributes */
    for (StaticOutputElement attribute : elementGroup.getAttributeEntries()) {
      addAttributeToComplexType(extractElementSchemaType(cparams, activeElement),
          attribute.getName(), attribute.getXsdType());
    }
    /* process elements / elementGroups / call-queries */
    StaticOutputElement tmpStaticEl;
    for (OutputElement outEl : elementGroup.getAllElements()) {
      if (outEl instanceof StaticOutputElement) {
        tmpStaticEl = (StaticOutputElement) outEl;
        processStaticOutputElement(cparams, activeElement, tmpStaticEl);
      } else if (outEl instanceof CallQueryGroup) {
        processCallQuery(cparams, activeElement,
            ((CallQueryGroup) outEl).getDefaultCallQuery());
      } else if (outEl instanceof OutputElementGroup) {
        processElementGroup(cparams, activeElement, (OutputElementGroup) outEl);
      }
    }
  }
 
  /**
   * Processes a static output element, and adds its element to the schema.
   * @param cparams The common parameters used in the schema generator
   * @param parentElement Parent element the new element will be added to
   * @param staticEl The output element, the schema element to be based on
   */
  private static void processStaticOutputElement(CommonParams cparams,
      XmlSchemaElement parentElement,  StaticOutputElement staticEl) {
    XmlSchemaComplexType parentType = extractElementSchemaType(cparams, parentElement);
    boolean global = !parentElement.getQName().getNamespaceURI().equals(staticEl.getNamespace());
    XmlSchemaElement tmpSchemaEl = createElement(cparams, staticEl.getNamespace(),
        staticEl.getName(), global);
    tmpSchemaEl.setMinOccurs(staticEl.isOptional() ? 0 : 1);
    tmpSchemaEl.setMaxOccurs(1);
    tmpSchemaEl.setNillable(true);
    tmpSchemaEl.setSchemaTypeName(staticEl.getXsdType());
    addElementToComplexTypeSequence(cparams, parentType, parentElement.getQName().getNamespaceURI(),
        tmpSchemaEl, global);
  }
 
  /**
   * Extracts the schema type from the given element.
   * @param cparams The common parameters used in the schema generator
   * @param element The element the type will be extract from
   * @return The element type
   */
  private static XmlSchemaComplexType extractElementSchemaType(CommonParams cparams,
      XmlSchemaElement element) {
    XmlSchemaComplexType type = (XmlSchemaComplexType) element.getSchemaType();
    /* if null, this must be globally defined */
    if (type == null) {
      type = cparams.getTypeMap().get(element.getSchemaTypeName());
    }
    return type;
  }

  /**
   * Creates a new element and complex schema type with the given name and the namespace.
   * @param cparams The common parameters used in the schema generator
   * @param parentElement The parent element to where the new element will be added
   * @param name The name of the element
   * @param namespace The namespace of the element
   * @param global Signal if the newly created element is global or not
   * @return The newly added XML schema element
   */
  private static XmlSchemaElement createAndAddToElement(CommonParams cparams,
      XmlSchemaElement parentElement, String name, String namespace, boolean global) {   
    XmlSchemaElement tmpElement = createElement(cparams, namespace, name, global);
    XmlSchemaComplexType type = createComplexType(cparams, namespace, name, true);
    tmpElement.setSchemaTypeName(type.getQName());
    addToElement(cparams, parentElement, tmpElement, global);
    return tmpElement;
  }
 
  private static void addToElement(CommonParams cparams,
      XmlSchemaElement parentElement, XmlSchemaElement element, boolean elementRef) {
    /* check if new element's namespace and parent's namespace is the same,
     * if it's different, this has to be forced to be element references */
    if (!parentElement.getQName().getNamespaceURI().equals(
        element.getQName().getNamespaceURI())) {
      elementRef = true;
    }   
    XmlSchemaType type = extractElementSchemaType(cparams, parentElement);
    addElementToComplexTypeSequence(cparams, (XmlSchemaComplexType) type,
        parentElement.getQName().getNamespaceURI(), element, elementRef);
  }
 
  /**
   * Creates the default data services fault element, and stores it in the schema.
   * @param cparams The common parameters used in the schema generator
   */
  private static void createAndStoreFaultElement(CommonParams cparams) {
    XmlSchemaElement element = createElement(cparams, DBConstants.WSO2_DS_NAMESPACE,
        DBConstants.DS_FAULT_ELEMENT, true);
    element.setSchemaTypeName(Constants.XSD_STRING);
  }
 
  /**
   * Creates the default data services request status element, and stores it in the schema.
   * @param cparams The common parameters used in the schema generator
   */
  private static void createAndStoreRequestStatusElement(CommonParams cparams) {
    XmlSchemaElement element = createElement(cparams, DBConstants.WSO2_DS_NAMESPACE,
        DBConstants.REQUEST_STATUS_WRAPPER_ELEMENT, true);
    element.setSchemaTypeName(Constants.XSD_STRING);
  }
   
  /**
   * Creates an XML schema element. If an element with the given QName already exists,
   * the returned elements's name maybe different in the case of creating a new element
   * and the given name is already taken and re-using is disabled.
   * @param cparams The common parameters used in the schema generator
   * @param namespace The namespace of the element
   * @param name The name of the element
   * @param global If this is a global element
   * @return The created or existing XML schema element
   */
  private static XmlSchemaElement createElement(CommonParams cparams, String namespace,
      String name, boolean global) {
    Map<QName, XmlSchemaElement> elementMap = cparams.getElementMap();
    if (name == null || namespace == null) {
      return new XmlSchemaElement();
    }
    QName qname = new QName(namespace, name);
    if (global && elementMap.containsKey(qname)) {
      int suffix = 1; // start with 2, if x is there, the next one would be x2
      while (elementMap.containsKey((new QName(namespace, name + (++suffix)))));
      name = name + suffix;
      qname = new QName(namespace, name);       
    }
    XmlSchemaElement element = new XmlSchemaElement();
    element.setQName(qname);
    element.setName(name);
    if (global) {
      elementMap.put(qname, element);
      XmlSchema schema = retrieveSchema(cparams, namespace);
      schema.getItems().add(element);
      schema.getElements().add(qname, element);
    }
    return element;
  }
   
  /**
   * Returns the XML schema element, if it's already created.
   * @param cparams The common parameters used in the schema generator
   * @param namespace The namespace of the element
   * @param name The local name of the element
   * @return Existing element or, null if the element cannot be found
   */
  private static XmlSchemaElement getElement(CommonParams cparams, String namespace,
      String name) {
    // cparams.getElementMap().get(new QName(namespace, name)); wont work !!
    for (Entry<QName, XmlSchemaElement> entry : cparams.getElementMap().entrySet()) {
      if (entry.getKey().getLocalPart().equals(name)
          && entry.getKey().getNamespaceURI().equals(namespace)) {
        return entry.getValue();
      }
    }
    return null;
  }
 
  /**
   * Creates XML schema complex type with the given namespace and the name.
   * @param cparams The common parameters used in the schema generator
   * @param namespace The namespace of the complex type
   * @param name The name of the complex type
   * @param global Signal if the newly created type is global or not
   * @return The newly create XML schema complex type
   */
  private static XmlSchemaComplexType createComplexType(CommonParams cparams, String namespace,
      String name, boolean global) {   
    XmlSchema schema = retrieveSchema(cparams, namespace);
    XmlSchemaComplexType type = new XmlSchemaComplexType(schema);
    Map<QName, XmlSchemaComplexType> typeMap = cparams.getTypeMap();
    if (global) {
      QName qname = new QName(namespace, name);
      if (typeMap.containsKey(qname)) {
        int suffix = 1; // start with 2, if T is there, the next one would be T2
        while (typeMap.containsKey(new QName(namespace, name + (++suffix))));
        name = name + suffix;
        qname = new QName(namespace, name);
      }   
      type.setName(name);
      cparams.getTypeMap().put(qname, type);
      schema.getItems().add(type);
    }
    return type;
  }
 
  /**
   * Adds the given parameter to the complex type.
   * @param complexType The complex type which the attribute will be added to
   * @param name The name of the attribute
   * @param xsdType The type of the attribute
   */
  @SuppressWarnings("unchecked")
  private static void addAttributeToComplexType(XmlSchemaComplexType complexType, String name,
      QName xsdType) {
    XmlSchemaAttribute attr = new XmlSchemaAttribute();
    attr.setName(name);
    attr.setSchemaTypeName(xsdType);
    attr.setUse(new XmlSchemaUse("optional"));
    XmlSchemaAttribute tmpAttr;
    for (Iterator<XmlSchemaAttribute> itr = complexType.getAttributes().getIterator();
             itr.hasNext();) {
      tmpAttr = itr.next();
      if (tmpAttr.getName().equals(attr.getName())) {
        /* current attribute is already set, nothing more to do */
        return;
      }
    }
    complexType.getAttributes().add(attr);
  }
 
  /**
   * Adds the given element to the complex type sequence.
   * @param cparams The common parameters used in the schema generator
   * @param complexType The complex type to where the element to be added
   * @param complexTypeNS The complex type namespace
   * @param element The element to be added
   * @param elementRef Tells if the element passed in should be added as an element reference
   */
  private static void addElementToComplexTypeSequence(CommonParams cparams,
      XmlSchemaComplexType complexType, String complexTypeNS,
      XmlSchemaElement element, boolean elementRef) {
    XmlSchemaParticle particle = complexType.getParticle();
    XmlSchemaSequence sequence;
    if (particle != null || particle instanceof XmlSchemaSequence) {
      sequence = (XmlSchemaSequence) particle;
    } else {
      sequence = new XmlSchemaSequence();
      complexType.setParticle(sequence);
    }
    XmlSchemaElement tmpElement;
    if (elementRef) {
      tmpElement = new XmlSchemaElement();
      tmpElement.setRefName(element.getQName());
      resolveSchemaImports(cparams.getSchemaMap().get(
          complexTypeNS),  tmpElement.getRefName().getNamespaceURI());
    } else {
      tmpElement = element;
    }
    sequence.getItems().add(tmpElement);
  }
   
  /**
   * Resolves schema imports by adding an schema import section to the host schema.
   * @param hostSchema The host schema
   * @param refNamespace The external schema reference
   */
  private static void resolveSchemaImports(XmlSchema hostSchema, String refNamespace) {
    if (!hostSchema.getTargetNamespace().equals(refNamespace)) {
      XmlSchemaImport schemaImport = new XmlSchemaImport();
      schemaImport.setNamespace(refNamespace);
      if (!containsSchemaImport(hostSchema, schemaImport)) {
        hostSchema.getItems().add(schemaImport);
      }
    }
  }
 
  /**
   * Checks if the specified schema import is existent.
   * @param schema The target schema to be checked
   * @param schemaImport The schema import
   * @return true if the schema import is there in the given schema
   */
  private static boolean containsSchemaImport(XmlSchema schema, XmlSchemaImport schemaImport) {
    XmlSchemaObjectCollection list = schema.getItems();
    int c = list.getCount();
    XmlSchemaObject obj;
    for (int i = 0; i < c; i++) {
      obj = list.getItem(i);
      if (obj instanceof XmlSchemaImport) {
        if (((XmlSchemaImport) obj).getNamespace().equals(schemaImport.getNamespace())) {
          return true;
        }
      }
    }
    return false;
  }
 
  /**
   * Extracts all the data service operations.
   * @param dataservice The data service which contains the operations
   * @return [0] - Normal operation list, [1] - Batch operation list
   */
  private static List<List<Operation>> extractOperations(DataService dataservice) {
    List<Operation> normalOperations = new ArrayList<Operation>();
    List<Operation> batchOperations = new ArrayList<Operation>();   
    Operation tmpOp;
    for (String opName : dataservice.getOperationNames()) {
      tmpOp = dataservice.getOperation(opName);
      if (tmpOp.isBatchRequest()) {
        batchOperations.add(tmpOp);
      } else {
        normalOperations.add(tmpOp);
      }
    }   
    List<List<Operation>> allOps = new ArrayList<List<Operation>>();
    allOps.add(normalOperations);
    allOps.add(batchOperations);
    return allOps;
  }
 
  /**
   * Extracts all the resources in the data service.
   * @param dataservice The data service which contains the resources
   * @return The list of resources
   */
  private static List<Resource> extractResources(DataService dataservice) {
    List<Resource> resources = new ArrayList<Resource>();
    for (ResourceID rid : dataservice.getResourceIds()) {
      resources.add(dataservice.getResource(rid));
    }
    return resources;
  }
 
  /**
   * Retrieve the XML schema with the given namespace.
   * @param cparams Common parameters used in the schema generator
   * @param namespace The target namespace of the XML schema
   * @return The XML schema object
   */
  private static XmlSchema retrieveSchema(CommonParams cparams, String namespace) {
    Map<String, XmlSchema> schemaMap = cparams.getSchemaMap();
    if (!schemaMap.containsKey(namespace)) {
      XmlSchema schema = new XmlSchema(namespace, cparams.getXmlSchemaCollection());
      schema.setNamespaceContext(new NamespaceMap());
      schemaMap.put(namespace, schema);
      schema.setElementFormDefault(new XmlSchemaForm(XmlSchemaForm.QUALIFIED));
      schema.setAttributeFormDefault(new XmlSchemaForm(XmlSchemaForm.UNQUALIFIED));
    }
    return schemaMap.get(namespace);
  }
 
  /**
   * Represents commonly used parameters in the schema generator,
   * an instance of this is passed around the methods.
   */
  private static class CommonParams {
   
    private AxisService axisService;
   
    private DataService dataservice;
   
    private Map<String, XmlSchema> schemaMap;
   
    private XmlSchemaCollection xmlSchemaCollection;
   
    private Map<QName, XmlSchemaElement> elementMap;
   
    private Map<QName, XmlSchemaComplexType> typeMap;
   
    private TypeTable typeTable = new TypeTable();
   
    private Map<String, QName> requestInputElementMap = new HashMap<String, QName>();
   
    public CommonParams(AxisService axisService, DataService dataservice) {
      this.schemaMap = new HashMap<String, XmlSchema>();
      this.xmlSchemaCollection = new XmlSchemaCollection();
      this.elementMap = new HashMap<QName, XmlSchemaElement>();
      this.typeMap = new HashMap<QName, XmlSchemaComplexType>();
      this.axisService = axisService;
      this.dataservice = dataservice;
    }

    public Map<String, QName> getRequestInputElementMap() {
      return requestInputElementMap;
    }
   
    public TypeTable getTypeTable() {
      return typeTable;
    }

    public AxisService getAxisService() {
      return axisService;
    }
   
    public DataService getDataService() {
      return dataservice;
    }
   
    public Map<String, XmlSchema> getSchemaMap() {
      return schemaMap;
    }
   
    public XmlSchemaCollection getXmlSchemaCollection() {
      return xmlSchemaCollection;
    }
   
    public Map<QName, XmlSchemaElement> getElementMap() {
      return elementMap;
    }
   
    public Map<QName, XmlSchemaComplexType> getTypeMap() {
      return typeMap;
    }
   
  }
 
}
TOP

Related Classes of org.wso2.carbon.dataservices.core.DataServiceDocLitWrappedSchemaGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.