boolean hasDocName = docName != null && docName.length() > 0;
boolean hasXpath = xpath != null && xpath.length() > 0;
if ( hasDocName && hasXpath )
throw new CompilationException("'document' and 'xpath' attributes are mutually exclusive");
else if ( hasDocName ) {
Document doc = null;
try {
doc = domAdapter.getDocument(tx, docName);
}
catch ( DBException e ) {
// Null is ok
}
if ( doc != null ) {
try {
String path = context.getCanonicalDocumentName(new Key(docName));
String systemID = XSLTQueryResolver.URL_PREFIX + path;
src = new DOMSource(doc, systemID);
}
catch ( Exception e ) {
// This shouldn't happen
}
}
else
throw new CompilationException("Document '" + docName + "' not found");
}
else if ( hasXpath ) {
ResultSet rs;
if ( keys != null )
rs = context.queryDocument(tx, XPathQueryResolver.STYLE_XPATH, xpath, nsMap, keys);
else
rs = context.queryCollection(tx, XPathQueryResolver.STYLE_XPATH, xpath, nsMap);
Document doc = ResultSetWrapper.toDocument(rs);
String path = context.getCanonicalName();
String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
src = new DOMSource(doc, systemID);
}
else {
NodeList nl = sourceElem.getChildNodes();
Element e = null;
for ( int i = 0; i < nl.getLength(); i++ ) {
Node n = nl.item(i);
if ( n.getNodeType() == Node.ELEMENT_NODE ) {
if ( e == null )
e = (Element)n;
else
throw new CompilationException("Only a single Element is allowed for inline source");
}
}
String path = context.getCanonicalName();
String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
src = new DOMSource(e, systemID);
}
}
catch ( DBException e ) {
try {
tx.cancel();
}
catch ( DBException ex ) {
ex.printStackTrace(System.err);
}
if ( e instanceof QueryException )
throw (QueryException)e;
else
throw new CompilationException(e);
}
finally {
if ( tx.getStatus() == Transaction.ACTIVE ) {
try {
tx.commit();