/*
*
* Copyright 2001-2004 The Apache Software Foundation.
*
*
* Licensed 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.beehive.wsm.wsdl;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.namespace.QName;
import org.xmlsoap.schemas.wsdl.TTypes;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
import org.apache.xmlbeans.impl.xb.xsdschema.TopLevelComplexType;
import org.apache.xmlbeans.impl.xb.xsdschema.TopLevelElement;
import org.apache.xmlbeans.impl.xb.xsdschema.TopLevelSimpleType;
public class Schema {
SchemaDocument.Schema[] schemas;
/**
* @param tt
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public Schema(TTypes tt) throws IllegalAccessException, NoSuchFieldException {
schemas = Utilities.selectChildren(tt, SchemaDocument.Schema.class);
}
/**
* @param stream
* @throws IOException
* @throws XmlException
*/
public Schema(InputStream stream) throws XmlException, IOException {
SchemaDocument schemaDoc = SchemaDocument.Factory.parse(stream);
schemas = new SchemaDocument.Schema[1];
schemas[0] = schemaDoc.getSchema();
}
public XmlObject getTypeNode(QName q) {
// first find the schema with matching namespace
SchemaDocument.Schema schema = null;
for (SchemaDocument.Schema nxtSchema : schemas) {
if (nxtSchema.getTargetNamespace() != null
&& nxtSchema.getTargetNamespace().equals(
q.getNamespaceURI())) {
schema = nxtSchema;
break;
}
}
if (null == schema)
return null; // namespace is not found in this schema.
// look in complex types
TopLevelComplexType[] tlComplexTypes = schema.getComplexTypeArray();
for( TopLevelComplexType nxtComplexType : tlComplexTypes) {
if( nxtComplexType.getName().equals(q.getLocalPart())) {
return nxtComplexType;
}
}
// look in simple types
TopLevelSimpleType[] tlSimpleTypes = schema.getSimpleTypeArray();
for( TopLevelSimpleType nxtSimpleType : tlSimpleTypes) {
if( nxtSimpleType.getName().equals(q.getLocalPart())) {
return nxtSimpleType;
}
}
// look in element types
TopLevelElement[] tlElementTypes = schema.getElementArray();
for( TopLevelElement nxtElement : tlElementTypes) {
if( nxtElement.getName().equals(q.getLocalPart())) {
return nxtElement;
}
}
return null; // it is not in comlex or simple types!
}
}