/**
* 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.Anonarray;
import org.apache.schemas.yoko.bindings.corba.Array;
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.yoko.wsdl.CorbaTypeImpl;
public class ArrayVisitor extends VisitorBase {
private AST identifierNode;
private XmlSchemaType schemaType;
private CorbaTypeImpl corbaType;
public ArrayVisitor(Scope scope,
XmlSchemaCollection xmlSchemas,
XmlSchema xmlSchema,
TypeMappingType typeMapRef,
XmlSchemaType schemaTypeRef,
CorbaTypeImpl corbaTypeRef,
AST identifierNodeRef) {
super(scope, xmlSchemas, xmlSchema, typeMapRef);
identifierNode = identifierNodeRef;
schemaType = schemaTypeRef;
corbaType = corbaTypeRef;
}
public static boolean accept(AST node) {
boolean result = false;
AST sizeNode = node.getFirstChild();
if (sizeNode != null) {
// check that node has a fixed_array_size child node
result = true;
while (sizeNode != null
&& result) {
// check that all fixed_array_size nodes encode
// positive integers
String s = sizeNode.toString();
for (int j = 0; j < s.length(); j++) {
if (!Character.isDigit(s.charAt(j))) {
result = false;
}
}
sizeNode = sizeNode.getNextSibling();
}
}
return result;
}
public void visit(AST node) {
// <array_declarator> ::= <identifier> <fixed_array_size>+
// <fixed_array_size> ::= "[" <positive_int_const> "]"
AST firstSizeNode = node.getFirstChild();
AST nextSizeNode = firstSizeNode.getNextSibling();
Types result = null;
// process all anonarrays, skip first array as it might not be anonymous
if (nextSizeNode != null) {
result = doAnonarray(nextSizeNode, schemaType.getQName(), corbaType.getQName());
} else {
result = new Types();
result.setSchemaType(schemaType);
result.setCorbaType(corbaType);
}
// process first array
Long size = new Long(firstSizeNode.toString());
XmlSchemaType stype = null;
CorbaTypeImpl ctype = null;
if (identifierNode != null) {
Scope scopedName = getScope();
stype = generateSchemaArray(scopedName.toString(), size, result.getSchemaType().getQName());
ctype = generateCorbaArray(scopedName, size, result.getCorbaType().getQName());
} else {
// anonymous array
Scope scopedName = TypesUtils.generateAnonymousScopedName(getScope(), schema);
stype = generateSchemaArray(scopedName.toString(),
size,
result.getSchemaType().getQName());
ctype = generateCorbaAnonarray(scopedName.toString(),
size,
result.getCorbaType().getQName());
}
// add schemaType
schema.getItems().add(stype);
schema.addType(stype);
// add corbaType
typeMap.getStructOrExceptionOrUnion().add(ctype);
setSchemaType(stype);
setCorbaType(ctype);
}
private Types doAnonarray(AST node, QName stype, QName ctype) {
Types result = new Types();
if (node != null) {
AST next = node.getNextSibling();
result = doAnonarray(next, stype, ctype);
Scope scopedName = TypesUtils.generateAnonymousScopedName(getScope(), schema);
Long size = new Long(node.toString());
if (result.getSchemaType() == null) {
result.setSchemaType(generateSchemaArray(scopedName.toString(),
size,
stype));
} else {
result.setSchemaType(generateSchemaArray(scopedName.toString(),
size,
result.getSchemaType().getQName()));
}
if (result.getCorbaType() == null) {
result.setCorbaType(generateCorbaAnonarray(scopedName.toString(),
size,
ctype));
} else {
result.setCorbaType(generateCorbaAnonarray(scopedName.toString(),
size,
result.getCorbaType().getQName()));
}
// add schemaType
schema.getItems().add(result.getSchemaType());
schema.addType(result.getSchemaType());
// add corbaType
typeMap.getStructOrExceptionOrUnion().add(result.getCorbaType());
}
return result;
}
private XmlSchemaComplexType generateSchemaArray(String name, Long size, QName type) {
XmlSchemaComplexType complexType = new XmlSchemaComplexType(schema);
complexType.setName(name);
XmlSchemaSequence sequence = new XmlSchemaSequence();
XmlSchemaElement element = new XmlSchemaElement();
element.setMinOccurs(size);
element.setMaxOccurs(size);
element.setName("item");
element.setSchemaTypeName(type);
sequence.getItems().add(element);
complexType.setParticle(sequence);
return complexType;
}
private Array generateCorbaArray(Scope scopedName, Long size, QName type) {
Array array = new Array();
array.setQName(new QName(typeMap.getTargetNamespace(), scopedName.toString()));
array.setBound(size);
array.setRepositoryID(scopedName.toIDLRepositoryID());
array.setType(type);
return array;
}
private Anonarray generateCorbaAnonarray(String name, Long size, QName type) {
Anonarray anonarray = new Anonarray();
anonarray.setQName(new QName(typeMap.getTargetNamespace(), name));
anonarray.setBound(size);
anonarray.setType(type);
return anonarray;
}
class Types {
private XmlSchemaType schemaType;
private CorbaTypeImpl corbaType;
public Types() {
schemaType = null;
corbaType = null;
}
public Types(XmlSchemaType stype, CorbaTypeImpl ctype) {
schemaType = stype;
corbaType = ctype;
}
public void setSchemaType(XmlSchemaType stype) {
schemaType = stype;
}
public void setCorbaType(CorbaTypeImpl ctype) {
corbaType = ctype;
}
public XmlSchemaType getSchemaType() {
return schemaType;
}
public CorbaTypeImpl getCorbaType() {
return corbaType;
}
}
}