Package org.apache.yoko.tools.processors.idl

Source Code of org.apache.yoko.tools.processors.idl.SequenceVisitor

/**
* 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.yoko.tools.processors.idl;

import javax.xml.namespace.QName;

import antlr.collections.AST;

import org.apache.schemas.yoko.bindings.corba.Anonsequence;
import org.apache.schemas.yoko.bindings.corba.Sequence;
import org.apache.schemas.yoko.bindings.corba.TypeMappingType;

import org.apache.ws.commons.schema.XmlSchema;
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.XmlSchemaSequence;
import org.apache.ws.commons.schema.XmlSchemaType;

import org.apache.ws.commons.schema.constants.Constants;

import org.apache.yoko.wsdl.CorbaTypeImpl;

public class SequenceVisitor extends VisitorBase {

    private AST identifierNode;
   
    public SequenceVisitor(Scope scope,
                           XmlSchemaCollection xmlSchemas,
                           XmlSchema xmlSchema,
                           TypeMappingType typeMapRef,
                           AST identifierNodeRef) {
        super(scope, xmlSchemas, xmlSchema, typeMapRef);
        identifierNode = identifierNodeRef;
    }
   
    public static boolean accept(AST node) {
        if (node.getType() == IDLTokenTypes.LITERAL_sequence) {
            return true;
        }
        return false;
    }
   
    public void visit(AST seq) {
        // <sequence_type> ::= "sequence" "<" <simple_type_spec> "," <positive_int_const> ">"
        //                   | "sequence" "<" <simple_type_spec> ">"
       
       
        AST simpleTypeSpecNode = seq.getFirstChild();
        // REVISIT: TypesUtils.getPrimitiveCorbaTypeNameNode should be renamed
        // to something more suitable and should be made more general.
        AST boundNode = TypesUtils.getCorbaTypeNameNode(simpleTypeSpecNode);

       
        SimpleTypeSpecVisitor visitor = new SimpleTypeSpecVisitor(new Scope(getScope(), identifierNode),
                                                                  schemas,
                                                                  schema,
                                                                  typeMap,
                                                                  null);
        visitor.visit(simpleTypeSpecNode);
       
        XmlSchemaType stype = visitor.getSchemaType();
        CorbaTypeImpl ctype = visitor.getCorbaType();
       

        long bound = -1;
        if (boundNode != null) {
            bound = Long.parseLong(boundNode.toString());
        }

        Scope scopedName = null;
        if (identifierNode == null) {
            // anonymous type
            scopedName = TypesUtils.generateAnonymousScopedName(getScope(), schema);
        } else {
            scopedName = new Scope(getScope(), identifierNode);
        }

        XmlSchemaType schemaType = null;
        if (stype != schemas.getTypeByQName(Constants.XSD_UNSIGNEDBYTE)) {
            schemaType = generateSchemaType(stype, scopedName, bound);
        } else {
            // According to CORBA Binding for WSDL specification,
            // idl:sequence<octet> maps to xs:base64Binary by default.
            schemaType = schemas.getTypeByQName(Constants.XSD_BASE64);
        }
       
        CorbaTypeImpl corbaType = null;
        if (identifierNode == null) {
            corbaType = generateCorbaAnonsequence(ctype,
                                                  schemaType,
                                                  scopedName,
                                                  bound);
        } else {
            corbaType = generateCorbaSequence(ctype,
                                              schemaType,
                                              scopedName,
                                              bound);
        }


        setSchemaType(schemaType);
        setCorbaType(corbaType);
    }

    private XmlSchemaType generateSchemaType(XmlSchemaType stype, Scope scopedName, long bound) {
        XmlSchemaComplexType ct = new XmlSchemaComplexType(schema);
        ct.setName(scopedName.toString());
        XmlSchemaSequence sequence = new XmlSchemaSequence();
        XmlSchemaElement el = new XmlSchemaElement();
        el.setName("item");
        el.setMinOccurs(0);
        if (bound != -1) {
            el.setMaxOccurs(bound);
        } else {
            el.setMaxOccurs(Long.MAX_VALUE);
        }
        el.setSchemaTypeName(stype.getQName());
        sequence.getItems().add(el);
        ct.setParticle(sequence);
        return ct;
    }
   
    private CorbaTypeImpl generateCorbaSequence(CorbaTypeImpl ctype,
                                                XmlSchemaType schemaType,
                                                Scope scopedName,
                                                long bound) {
        //create the corba sequence
        Sequence corbaSeq = new Sequence();
        if (bound == -1) {
            bound = 0;
        }               
        corbaSeq.setBound(bound);
        corbaSeq.setQName(new QName(typeMap.getTargetNamespace(), scopedName.toString()));
        corbaSeq.setType(schemaType.getQName());
        corbaSeq.setElemtype(ctype.getQName());
        corbaSeq.setRepositoryID(scopedName.toIDLRepositoryID());

        return corbaSeq;
    }

    private Anonsequence generateCorbaAnonsequence(CorbaTypeImpl ctype,
                                                   XmlSchemaType schemaType,
                                                   Scope scopedName,
                                                   long bound) {
        // create corba anonsequence
        Anonsequence result = new Anonsequence();
        if (bound == -1) {
            bound = 0;
        }               
        result.setBound(bound);
        result.setQName(new QName(typeMap.getTargetNamespace(), scopedName.toString()));
        result.setType(schemaType.getQName());
        result.setElemtype(ctype.getQName());

        // add schemaType
        if (schemas.getTypeByQName(schemaType.getQName()) == null) {
            schema.getItems().add(schemaType);
            schema.addType(schemaType);
        }
       
        // add corbaType
        typeMap.getStructOrExceptionOrUnion().add(result);
       
        return result;
    }
   
}
TOP

Related Classes of org.apache.yoko.tools.processors.idl.SequenceVisitor

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.