throws Exception
{
if (src == null || src.equals(""))
throw new Exception("SourceTypeAction: src attribute should be defined and non-empty.");
Source source = sourceResolver.resolveURI(src);
XMLPullParser parser = new Xerces2();
if (source.exists()) {
InputStream is = source.getInputStream();
parser.setInputSource(new XMLInputSource(null, src, null, is, null));
} else {
getLogger().warn("Source '"+source+"' not found");
return null;
}
// load nothing external
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
// note: namespace-aware parsing is by default true
SourceInfo sourceInfo = new SourceInfo();
// pull-parse the document until we reach the document element and put the collected information
// into the sourceInfo object
try
{
XMLEvent event;
while ((event = parser.nextEvent()) != null)
{
if (event.type == XMLEvent.DOCTYPE_DECL)
{
DoctypeDeclEvent doctypeDeclEvent = (DoctypeDeclEvent)event;
sourceInfo.setPublicId(doctypeDeclEvent.pubid);
}
else if (event.type == XMLEvent.PROCESSING_INSTRUCTION)
{
ProcessingInstructionEvent piEvent = (ProcessingInstructionEvent)event;
sourceInfo.addProcessingInstruction(piEvent.target, piEvent.data != null ? piEvent.data.toString() : null);
}
else if (event.type == XMLEvent.ELEMENT)
{
ElementEvent elementEvent = (ElementEvent)event;
if (elementEvent.element.uri == RDF_NAMESPACE)
{
while ((event = parser.nextEvent()) != null &&
elementEvent.element.uri == RDF_NAMESPACE)
event = parser.nextEvent();
}
sourceInfo.setDocumentElementLocalName(elementEvent.element.localpart);
sourceInfo.setDocumentElementNamespace(elementEvent.element.uri);
sourceInfo.setXsiSchemaLocation(elementEvent.attributes.getValue(XSI_NAMESPACE, "schemaLocation"));
sourceInfo.setXsiNoNamespaceSchemaLocation(elementEvent.attributes.getValue(XSI_NAMESPACE, "noNamespaceSchemaLocation"));
// stop parsing after the root element
break;
}
}
}
finally
{
// this will also close the inputstream
parser.cleanup();
}
// Run over the SourceTypes until one is found that matches the information collected in sourceInfo
Iterator sourceTypeIt = sourceTypes.iterator();
while (sourceTypeIt.hasNext())