protected Import parseImport(Element importEl,
Definition def,
Map importedDefs)
throws WSDLException
{
Import importDef = def.createImport();
try
{
String namespaceURI = DOMUtils.getAttribute(importEl,
Constants.ATTR_NAMESPACE);
String locationURI = DOMUtils.getAttribute(importEl,
Constants.ATTR_LOCATION);
String contextURI = null;
if (namespaceURI != null)
{
importDef.setNamespaceURI(namespaceURI);
}
if (locationURI != null)
{
importDef.setLocationURI(locationURI);
if (importDocuments)
{
try
{
contextURI = def.getDocumentBaseURI();
Definition importedDef = null;
InputStream inputStream = null;
InputSource inputSource = null;
URL url = null;
if (loc != null)
{
inputSource = loc.getImportInputSource(contextURI, locationURI);
/*
We now have available the latest import URI. This might
differ from the locationURI so check the importedDefs for it
since it is this that we pass as the documentBaseURI later.
*/
String liu = loc.getLatestImportURI();
importedDef = (Definition)importedDefs.get(liu);
inputSource.setSystemId(liu);
}
else
{
URL contextURL = (contextURI != null)
? StringUtils.getURL(null, contextURI)
: null;
url = StringUtils.getURL(contextURL, locationURI);
importedDef = (Definition)importedDefs.get(url.toString());
if (importedDef == null)
{
inputStream = StringUtils.getContentAsInputStream(url);
if (inputStream != null)
{
inputSource = new InputSource(inputStream);
inputSource.setSystemId(url.toString());
}
}
}
if (importedDef == null)
{
if (inputSource == null)
{
throw new WSDLException(WSDLException.OTHER_ERROR,
"Unable to locate imported document " +
"at '" + locationURI + "'" +
(contextURI == null
? "."
: ", relative to '" + contextURI +
"'."));
}
Document doc = getDocument(inputSource, inputSource.getSystemId());
if (inputStream != null)
{
inputStream.close();
}
Element documentElement = doc.getDocumentElement();
/*
Check if it's a wsdl document.
If it's not, don't retrieve and process it.
This should later be extended to allow other types of
documents to be retrieved and processed, such as schema
documents (".xsd"), etc...
*/
if (QNameUtils.matches(Constants.Q_ELEM_DEFINITIONS,
documentElement))
{
if (verbose)
{
System.out.println("Retrieving document at '" + locationURI +
"'" +
(contextURI == null
? "."
: ", relative to '" + contextURI + "'."));
}
String urlString =
(loc != null)
? loc.getLatestImportURI()
: (url != null)
? url.toString()
: locationURI;
importedDef = readWSDL(urlString,
documentElement,
importedDefs);
}
else
{
QName docElementQName = QNameUtils.newQName(documentElement);
if (SchemaConstants.XSD_QNAME_LIST.contains(docElementQName))
{
if (verbose)
{
System.out.println("Retrieving schema wsdl:imported from '" + locationURI +
"'" +
(contextURI == null
? "."
: ", relative to '" + contextURI + "'."));
}
WSDLFactory factory = getWSDLFactory();
importedDef = factory.newDefinition();
if (extReg != null)
{
importedDef.setExtensionRegistry(extReg);
}
String urlString =
(loc != null)
? loc.getLatestImportURI()
: (url != null)
? url.toString()
: locationURI;
importedDef.setDocumentBaseURI(urlString);
Types types = importedDef.createTypes();
types.addExtensibilityElement(
parseSchema(Types.class, documentElement, importedDef));
importedDef.setTypes(types);
}
}
}
if (importedDef != null)
{
importDef.setDefinition(importedDef);
}
}
catch (WSDLException e)
{
throw e;
}
catch (RuntimeException e)
{
throw e;
}
catch (Exception e)
{
throw new WSDLException(WSDLException.OTHER_ERROR,
"Unable to resolve imported document at '" +
locationURI +
(contextURI == null
? "'." : "', relative to '" + contextURI + "'")
, e);
}
} //end importDocs
} //end locationURI
}
catch (WSDLException e)
{
if (e.getLocation() == null)
{
e.setLocation(XPathUtils.getXPathExprFromNode(importEl));
}
else
{
//If definitions are being parsed recursively for nested imports
//the exception location must be built up recursively too so
//prepend this element's xpath to exception location.
String loc = XPathUtils.getXPathExprFromNode(importEl) + e.getLocation();
e.setLocation(loc);
}
throw e;
}
//register any NS decls with the Definition
NamedNodeMap attrs = importEl.getAttributes();
registerNSDeclarations(attrs, def);
Element tempEl = DOMUtils.getFirstChildElement(importEl);
while (tempEl != null)
{
if (QNameUtils.matches(Constants.Q_ELEM_DOCUMENTATION, tempEl))
{
importDef.setDocumentationElement(tempEl);
}
else
{
importDef.addExtensibilityElement(
parseExtensibilityElement(Import.class, tempEl, def));
}
tempEl = DOMUtils.getNextSiblingElement(tempEl);
}