Author: ips
Date: Mon Mar 14 14:07:47 2005
New Revision: 157474
URL: http://svn.apache.org/viewcvs?view=rev&rev=157474
Log:
added support for recursing more than one-level deep into imports/includes;
added support for more than one xsd:schema element within the wsdl:types
element; improved error checking/reporting
Modified:
incubator/apollo/trunk/src/java/org/apache/ws/util/WsrfWsdlUtils.java
Modified: incubator/apollo/trunk/src/java/org/apache/ws/util/WsrfWsdlUtils.java
URL:
http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/util/WsrfWsdlUtils.java?view=diff&r1=157473&r2=157474
==============================================================================
--- incubator/apollo/trunk/src/java/org/apache/ws/util/WsrfWsdlUtils.java
(original)
+++ incubator/apollo/trunk/src/java/org/apache/ws/util/WsrfWsdlUtils.java Mon
Mar 14 14:07:47 2005
@@ -45,6 +45,7 @@
*/
public abstract class WsrfWsdlUtils
{
+
/**
* DOCUMENT_ME
*
@@ -93,56 +94,51 @@
}
/**
- * @param propsDocName
+ * @param rpDocElemName
* @param def
- *
* @param baseUrl
+ *
* @return the names of the resource properties associated with the
specified portType
*/
- public static QName[] getResourcePropertyNames(QName propsDocName,
- Definition def, URL
baseUrl) throws InvalidWsrfWsdlException
+ public static QName[] getResourcePropertyNames( QName rpDocElemName,
+ Definition def, URL
baseUrl ) throws InvalidWsrfWsdlException
{
- if ( propsDocName == null )
+ if ( rpDocElemName == null )
{
return null;
}
- Element schemaElem = getSchemaElement( def );
- Element rpDocElementElem = getElementByName( schemaElem, propsDocName
);
-
- if(rpDocElementElem == null)
+ Set propNames = new HashSet();
+ Element[] schemaElems = getSchemaElements( def );
+ XsdElement rpDocXsdElem = null;
+ for ( int i = 0; i < schemaElems.length; i++ )
{
- String[] schemaLocations = findImportsAndIncludes(schemaElem,
propsDocName.getNamespaceURI());
- for (int i = 0; i < schemaLocations.length; i++)
+ rpDocXsdElem = getXsdElementElementRecursively( schemaElems[i],
rpDocElemName, baseUrl );
+ if ( rpDocXsdElem != null )
{
- String location = schemaLocations[i];
- Document shcemaDoc = null;
- try
- {
- URL schemaUrl = new URL(baseUrl, location);
- shcemaDoc = JaxpUtils.loadDocument(schemaUrl.openStream());
- }
- catch (Exception e)
- {
- throw new InvalidWsrfWsdlException("Failed to load schema
at location " + location);
- }
-
- rpDocElementElem = getElementByName(
shcemaDoc.getDocumentElement(), propsDocName );
- if(rpDocElementElem != null) break;
- }
- if(rpDocElementElem == null)
- {
- throw new InvalidWsrfWsdlException("Unable to locate the
ResourceProperties Document Element with QName " + propsDocName);
+ break;
}
}
- String type = rpDocElementElem.getAttribute( "type" );
+ if ( rpDocXsdElem == null )
+ {
+ throw new InvalidWsrfWsdlException(
+ "Unable to locate the ResourceProperties Document Element
with QName " + rpDocElemName );
+ }
+ String type = rpDocXsdElem.getElement().getAttribute( "type" );
Element rpDocTypeElem;
if ( !"".equals( type ) )
{
- rpDocTypeElem = getComplexTypeByName( schemaElem, type );
+ rpDocTypeElem = getComplexTypeByName(
rpDocXsdElem.getSchemaElement(), type );
}
else
{
- rpDocTypeElem = (Element) rpDocElementElem.getElementsByTagNameNS(
XmlConstants.NSURI_SCHEMA_XSD, "complexType" ).item( 0 );
+ rpDocTypeElem =
+ (Element)
rpDocXsdElem.getElement().getElementsByTagNameNS( XmlConstants.NSURI_SCHEMA_XSD,
+ "complexType" )
+ .item( 0 );
+ }
+ if ( rpDocTypeElem == null )
+ {
+ throw new InvalidWsrfWsdlException( "Unable to determine type of
the ResourceProperties document element with QName " + rpDocElemName + " -
expected either a type atribute or a nested anonymous complexType element." );
}
Element sequenceElem = null;
NodeList sequenceNodes = rpDocTypeElem.getElementsByTagNameNS(
XmlConstants.NSURI_SCHEMA_XSD, "sequence" );
@@ -160,60 +156,107 @@
}
if ( sequenceElem == null )
{
- throw new RuntimeException( "Resource property element definitions
must be contained in an xsd:sequence or an xsd:all element." );
+ throw new RuntimeException(
+ "Resource property element definitions must be contained
in an xsd:sequence or an xsd:all element." );
}
NodeList propElems = sequenceElem.getElementsByTagNameNS(
XmlConstants.NSURI_SCHEMA_XSD, "element" );
- Set propNames = new HashSet();
- for ( int i = 0; i < propElems.getLength(); i++ )
+ for ( int j = 0; j < propElems.getLength(); j++ )
{
- Element propElem = (Element) propElems.item( i );
- String ref = propElem.getAttribute( "ref" );
- if ( "".equals( ref ) ) {
- throw new InvalidWsrfWsdlException( "All element defs within
the resource property document def must have a 'ref' attribute that points at a
global element def." );
- }
- StringTokenizer tokenizer = new StringTokenizer( ref, ":" );
- if ( tokenizer.countTokens() != 2 )
+ Element propElem = (Element) propElems.item( j );
+ QName propName = getElementRefName( propElem,
rpDocXsdElem.getSchemaElement() );
+ propNames.add( propName );
+ }
+ return (QName[]) propNames.toArray( new QName[0] );
+ }
+
+ private static XsdElement getXsdElementElementRecursively( Element
xsdSchemaElem, QName xsdElemName, URL baseUrl )
+ throws InvalidWsrfWsdlException
+ {
+ XsdElement xsdElementElem = getXsdElementElement( xsdSchemaElem,
xsdElemName );
+ if ( xsdElementElem == null )
+ {
+ String[] schemaLocations = findImportsAndIncludes( xsdSchemaElem,
xsdElemName.getNamespaceURI() );
+ for ( int j = 0; j < schemaLocations.length; j++ )
{
- throw new InvalidWsrfWsdlException( "Value for 'ref' attribute
must be qualified via a namespace prefix." );
+ String location = schemaLocations[j];
+ URL schemaUrl = null;
+ Document schemaDoc = null;
+ try
+ {
+ schemaUrl = new URL( baseUrl, location );
+ schemaDoc = JaxpUtils.loadDocument( schemaUrl.openStream()
);
+ }
+ catch ( Exception e )
+ {
+ throw new InvalidWsrfWsdlException( "Failed to load schema
at location " + location );
+ }
+ xsdElementElem =
+ getXsdElementElementRecursively(
schemaDoc.getDocumentElement(), xsdElemName, schemaUrl );
+ if ( xsdElementElem != null )
+ {
+ break;
+ }
}
- String propPrefix = tokenizer.nextToken();
- String propLocalName = tokenizer.nextToken();
- // TODO: write our own prefix resolver to eliminate dep on Xalan
- PrefixResolver prefixResolver = new PrefixResolverDefault(
schemaElem );
- String propNamespace = prefixResolver.getNamespaceForPrefix(
propPrefix );
- propNames.add( new QName( propNamespace, propLocalName, propPrefix
) );
}
+ return xsdElementElem;
+ }
- return (QName[]) propNames.toArray( new QName[0] );
+ private static QName getElementRefName( Element propElem, Element
schemaElem )
+ throws InvalidWsrfWsdlException
+ {
+ String ref = propElem.getAttribute( "ref" );
+ if ( "".equals( ref ) )
+ {
+ throw new InvalidWsrfWsdlException(
+ "All element defs within the resource property document
def must have a 'ref' attribute that points at a global element def." );
+ }
+ StringTokenizer tokenizer = new StringTokenizer( ref, ":" );
+ if ( tokenizer.countTokens() != 2 )
+ {
+ throw new InvalidWsrfWsdlException( "Value for xsd:element 'ref'
attribute must be qualified via a namespace prefix." );
+ }
+ String propPrefix = tokenizer.nextToken();
+ String propLocalName = tokenizer.nextToken();
+ // TODO: write our own prefix resolver to eliminate dep on Xalan
+ PrefixResolver prefixResolver = new PrefixResolverDefault( schemaElem
);
+ String propNamespace = prefixResolver.getNamespaceForPrefix(
propPrefix );
+ if ( propNamespace == null )
+ {
+ throw new InvalidWsrfWsdlException( "Unable to resolve prefix '" +
propPrefix + "' in xsd:element 'ref' attribute value." );
+ }
+ QName propName = new QName( propNamespace, propLocalName, propPrefix );
+ return propName;
}
- private static String[] findImportsAndIncludes(Element schemaElem, String
namespaceURI)
+ private static String[] findImportsAndIncludes( Element schemaElem, String
namespaceURI )
{
List elems = new ArrayList();
- NodeList imports =
schemaElem.getElementsByTagNameNS(XmlConstants.NSURI_SCHEMA_XSD,
XmlConstants.XSD_ATTRIB_IMPORT);
- for (int i = 0; i < imports.getLength(); i++)
- {
- Element importNode = (Element) imports.item(i);
- Attr attributeNode = importNode.getAttributeNode("namespace");
- if(attributeNode != null)
+ NodeList imports = schemaElem.getElementsByTagNameNS(
XmlConstants.NSURI_SCHEMA_XSD,
+ XmlConstants.XSD_ATTRIB_IMPORT );
+ for ( int i = 0; i < imports.getLength(); i++ )
+ {
+ Element importNode = (Element) imports.item( i );
+ Attr attributeNode = importNode.getAttributeNode( "namespace" );
+ if ( attributeNode != null )
{
- if (namespaceURI.equals(attributeNode.getValue()))
+ if ( namespaceURI.equals( attributeNode.getValue() ) )
{
- elems.add(importNode);
+ elems.add( importNode );
}
}
}
- NodeList includes =
schemaElem.getElementsByTagNameNS(XmlConstants.NSURI_SCHEMA_XSD,
XmlConstants.XSD_ATTRIB_INCLUDE);
- for (int i = 0; i < includes.getLength(); i++)
+ NodeList includes = schemaElem.getElementsByTagNameNS(
XmlConstants.NSURI_SCHEMA_XSD,
+ XmlConstants.XSD_ATTRIB_INCLUDE );
+ for ( int i = 0; i < includes.getLength(); i++ )
{
- elems.add(includes.item(i));
+ elems.add( includes.item( i ) );
}
String[] locations = new String[elems.size()];
- for (int i = 0; i < elems.size(); i++)
+ for ( int i = 0; i < elems.size(); i++ )
{
- Element element = (Element) elems.get(i);
- Attr attributeNode = element.getAttributeNode("schemaLocation");
- if(attributeNode != null)
+ Element element = (Element) elems.get( i );
+ Attr attributeNode = element.getAttributeNode( "schemaLocation" );
+ if ( attributeNode != null )
{
locations[i] = attributeNode.getValue();
}
@@ -221,8 +264,8 @@
return locations;
}
- private static Element getElementByName( Element schemaElem,
- QName name )
+ private static XsdElement getXsdElementElement( Element schemaElem,
+ QName name )
{
NodeList children = schemaElem.getChildNodes();
for ( int i = 0; i < children.getLength(); i++ )
@@ -233,7 +276,7 @@
Element elementElem = (Element) child;
if ( String.valueOf( elementElem.getAttribute( "name" )
).equals( name.getLocalPart() ) )
{
- return elementElem;
+ return new XsdElement( elementElem, schemaElem );
}
}
}
@@ -262,10 +305,13 @@
public static QName getResourcePropertiesDocumentName( PortType portType )
{
Map extAttribs = portType.getExtensionAttributes();
- QName rpDocDefQName = (QName) extAttribs.get(
org.apache.ws.resource.properties.v1_2_draft05.ResourcePropertiesConstants.RESOURCE_PROPERTIES_PORTTYPE_ATTRIB
);
+ QName rpDocDefQName = (QName) extAttribs.get(
+
org.apache.ws.resource.properties.v1_2_draft05.ResourcePropertiesConstants.RESOURCE_PROPERTIES_PORTTYPE_ATTRIB
);
if ( rpDocDefQName == null )
{
- rpDocDefQName = (QName) extAttribs.get(
org.apache.ws.resource.properties.v1_2_draft01.ResourcePropertiesConstants.RESOURCE_PROPERTIES_PORTTYPE_ATTRIB
);
+ rpDocDefQName =
+ (QName) extAttribs.get(
+
org.apache.ws.resource.properties.v1_2_draft01.ResourcePropertiesConstants.RESOURCE_PROPERTIES_PORTTYPE_ATTRIB
);
}
return rpDocDefQName;
}
@@ -273,15 +319,20 @@
public static QName getMetadataDescriptorName( PortType portType )
{
Map extAttribs = portType.getExtensionAttributes();
- return (QName) extAttribs.get( new QName(
"http://docs.oasis-open.org/wsrf/2004/10/wsrf-WSResourceMetadataDescriptor-1.0-draft-01.xsd",
"metadataDescriptor", "wsrmd" ) );
+ return (QName) extAttribs.get( new QName(
+
"http://docs.oasis-open.org/wsrf/2004/10/wsrf-WSResourceMetadataDescriptor-1.0-draft-01.xsd",
+ "metadataDescriptor", "wsrmd" ) );
}
- public static String getMetadataDescriptorLocation( PortType portType )
throws org.apache.ws.resource.InvalidWsrfWsdlException
+ public static String getMetadataDescriptorLocation( PortType portType )
+ throws org.apache.ws.resource.InvalidWsrfWsdlException
{
Map extAttribs = portType.getExtensionAttributes();
final QName METADATA_DESCRIPTOR_PORTTYPE_ATTRIB =
- new QName(
"http://docs.oasis-open.org/wsrf/2004/10/wsrf-WSResourceMetadataDescriptor-1.0-draft-01.xsd",
"metadataDescriptorLocation", "wsrmd" );
- QName qValue = ((QName)extAttribs.get(
METADATA_DESCRIPTOR_PORTTYPE_ATTRIB ));
+ new QName(
+
"http://docs.oasis-open.org/wsrf/2004/10/wsrf-WSResourceMetadataDescriptor-1.0-draft-01.xsd",
+ "metadataDescriptorLocation", "wsrmd" );
+ QName qValue = ( (QName) extAttribs.get(
METADATA_DESCRIPTOR_PORTTYPE_ATTRIB ) );
if ( qValue == null )
{
return null;
@@ -290,15 +341,16 @@
StringTokenizer tokenizer = new StringTokenizer( value );
if ( tokenizer.countTokens() != 2 )
{
- throw new org.apache.ws.resource.InvalidWsrfWsdlException(
METADATA_DESCRIPTOR_PORTTYPE_ATTRIB + " attribute on portType must be of the
format \"namespaceURI locationURL\"");
+ throw new org.apache.ws.resource.InvalidWsrfWsdlException(
METADATA_DESCRIPTOR_PORTTYPE_ATTRIB +
+ " attribute on portType must be of the format
\"namespaceURI locationURL\"" );
}
tokenizer.nextToken();
return tokenizer.nextToken();
}
- private static Element getSchemaElement( Definition def )
+ private static Element[] getSchemaElements( Definition def )
{
- Element schemaElem = null;
+ List schemaElems = new ArrayList();
List extElems = def.getTypes().getExtensibilityElements();
for ( int i = 0; i < extElems.size(); i++ )
{
@@ -310,12 +362,33 @@
if ( elem.getNamespaceURI().equals(
XmlConstants.NSURI_SCHEMA_XSD )
&& elem.getLocalName().equals(
XmlConstants.XSD_SCHEMA.getLocalPart() ) )
{
- schemaElem = elem;
- break;
+ schemaElems.add( elem );
}
}
}
- return schemaElem;
+ return (Element[]) schemaElems.toArray( new Element[0] );
+ }
+
+ private static class XsdElement
+ {
+ private Element m_elem;
+ private Element m_schemaElem;
+
+ public XsdElement( Element elem, Element schemaElem )
+ {
+ m_elem = elem;
+ m_schemaElem = schemaElem;
+ }
+
+ public Element getElement()
+ {
+ return m_elem;
+ }
+
+ public Element getSchemaElement()
+ {
+ return m_schemaElem;
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]