/**
* 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 java.util.Iterator;
import java.util.Map;
import javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.PortType;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.ExtensionRegistry;
import javax.xml.namespace.QName;
import antlr.collections.AST;
import org.apache.schemas.yoko.bindings.corba.BindingType;
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.yoko.wsdl.CorbaConstants;
public class PortTypeVisitor extends VisitorBase {
Definition definition;
ExtensionRegistry extReg;
PortType portType;
WSDLASTVisitor wsdlASTVisitor;
String module;
public PortTypeVisitor(Scope scope,
XmlSchemaCollection xmlSchemas,
XmlSchema xmlSchema,
TypeMappingType corbaTypeMap,
Definition wsdlDefinition) {
super(scope, xmlSchemas, xmlSchema, corbaTypeMap);
definition = wsdlDefinition;
extReg = definition.getExtensionRegistry();
}
public static boolean accept(AST node) {
if (node.getType() == IDLTokenTypes.LITERAL_interface) {
return true;
}
return false;
}
public void visit(AST node) {
// <interface> ::= <interface_dcl>
// | <forward_dcl>
// <interface_dcl> ::= <interface_header> "{" <interface_body> "}"
// <forward_dcl> ::= ["abstract" | "local"] "interface" <identifier>
// <interface_header> ::= ["abstract" | "local"] "interface" <identifier>
// [<interface_inheritance_spec>]
// <interface_body> ::= <export>*
// <export> ::= <type_dcl> ";"
// | <const_dcl> ";"
// | <except_dcl> ";"
// | <attr_dcl> ";"
// | <op_dcl> ";"
// <interface_inheritance_spec> ::= ":" <interface_name> { "," <interface_name> }*
// <interface_name> ::= <scoped_name>
AST identifierNode = node.getFirstChild();
String interfaceName = identifierNode.toString();
Scope interfaceScope = new Scope(getScope(), interfaceName);
//Scope interfaceScope = getScope();
portType = definition.createPortType();
portType.setQName(new QName(definition.getTargetNamespace(), interfaceScope.toString()));
portType.setUndefined(false);
definition.addPortType(portType);
Binding binding = createBinding();
AST exportNode = identifierNode.getNextSibling();
while (exportNode != null) {
if (TypeDclVisitor.accept(exportNode)) {
TypeDclVisitor visitor = new TypeDclVisitor(interfaceScope,
schemas,
schema,
typeMap);
visitor.visit(exportNode);
} else if (ConstVisitor.accept(exportNode)) {
ConstVisitor visitor = new ConstVisitor(interfaceScope,
schemas,
schema,
typeMap);
visitor.visit(exportNode);
} else if (ExceptionVisitor.accept(exportNode)) {
ExceptionVisitor visitor = new ExceptionVisitor(interfaceScope,
schemas,
schema,
typeMap);
visitor.visit(exportNode);
} else if (AttributeVisitor.accept(exportNode)) {
AttributeVisitor attributeVisitor = new AttributeVisitor(interfaceScope,
schemas,
schema,
typeMap,
definition,
portType,
binding);
attributeVisitor.visit(exportNode);
} else if (OperationVisitor.accept(interfaceScope, schemas, schema, typeMap, exportNode)) {
OperationVisitor visitor = new OperationVisitor(interfaceScope,
schemas,
schema,
typeMap,
definition,
portType,
binding);
visitor.visit(exportNode);
} else {
throw new RuntimeException("[InterfaceVisitor] Invalid IDL: unknown element "
+ exportNode.toString());
}
exportNode = exportNode.getNextSibling();
}
}
public Binding createBinding() {
String bname = portType.getQName().getLocalPart() + "CORBABinding";
QName bqname = new QName(definition.getTargetNamespace(),
bname);
int count = 0;
while (queryBinding(bqname)) {
bname = bname + count;
bqname = new QName(definition.getTargetNamespace(), bname);
}
Binding binding = definition.createBinding();
binding.setPortType(portType);
binding.setQName(bqname);
try {
BindingType bindingType = (BindingType)
extReg.createExtension(Binding.class, CorbaConstants.NE_CORBA_BINDING);
bindingType.setRepositoryID(CorbaConstants.REPO_STRING
+ portType.getQName().getLocalPart().replace('.', '/')
+ CorbaConstants.IDL_VERSION);
binding.addExtensibilityElement(bindingType);
} catch (WSDLException ex) {
throw new RuntimeException(ex);
}
binding.setUndefined(false);
definition.addBinding(binding);
return binding;
}
private boolean queryBinding(QName bqname) {
Map bindings = definition.getBindings();
Iterator i = bindings.values().iterator();
while (i.hasNext()) {
Binding binding = (Binding)i.next();
if (binding.getQName().getLocalPart().equals(bqname.getLocalPart())) {
return true;
}
}
return false;
}
}