throw new XPathException(ue.getMessage());
}
}
public TailCall processLeavingTail(XPathContext context) throws XPathException {
final Controller controller = context.getController();
final APIcommand command = controller.getApiCommand();
XPathContext c2 = context.newMinorContext();
int action = APPEND_CONTENT;
if (methodExpression != null) {
String method = methodExpression.evaluateAsString(context).toString();
StructuredQName methodQ;
if (method.indexOf(':') >= 0) {
methodQ = StructuredQName.fromLexicalQName(method, false, nsResolver);
} else {
methodQ = new StructuredQName("", "", method);
}
if ("replace-content".equals(methodQ.getLocalName())) {
// TODO: check the namespace URI is NamespaceConstant.IXSL
action = REPLACE_CONTENT;
}
}
String hrefValue = null;
if (href != null) {
hrefValue = href.evaluateAsString(context).toString();
} else if (command == APIcommand.UPDATE_HTML) {
throw new XPathException("html update - no href value for result-document instruction");
} else {
hrefValue = "result" + (controller.getResultDocumentCount() + 1);
}
NodeInfo target = null;
Node targetNode = null;
String contextNodeName = "";
String absURI = "";
if (command == APIcommand.TRANSFORM_TO_DOCUMENT) {
absURI = getValidAbsoluteURI(controller, hrefValue);
targetNode = XMLDOM.createDocument(absURI);
} else if (command == APIcommand.TRANSFORM_TO_FRAGMENT || command == APIcommand.TRANSFORM_TO_HTML_FRAGMENT){
absURI = getValidAbsoluteURI(controller, hrefValue);
targetNode = HTMLDocumentWrapper.createDocumentFragment((Document)controller.getTargetNode());
} else if (hrefValue.startsWith("#")) {
hrefValue = hrefValue.substring(1);
targetNode = ((Document)controller.getTargetNode()).getElementById(hrefValue); // com.google.gwt.dom.client.Document.get().getElementById(hrefValue);
} else if (hrefValue.startsWith("?select=")) {
String select = hrefValue.substring(8);
AbstractStaticContext env = new AbstractStaticContext() {
public String getURIForPrefix(String prefix) throws XPathException {
return null;
}
public Expression bindVariable(StructuredQName qName) throws XPathException {
return null;
}
public NamespaceResolver getNamespaceResolver() {
return null;
}
//override getFunctionLibrary to return that loaded for the prepared stylesheet
public FunctionLibrary getFunctionLibrary() {
return controller.getPreparedStylesheet().getFunctionLibrary();
}
};
ExpressionVisitor visitor = new ExpressionVisitor();
visitor.setConfiguration(context.getConfiguration());
visitor.setExecutable(new Executable(context.getConfiguration()));
visitor.setStaticContext(env);
env.setConfiguration(context.getConfiguration());
Container container = (StyleElement)getSourceLocator();
Expression expr = null;
try {
expr = ExpressionTool.make(select, env, container, 0, Token.EOF, getSourceLocator());
} catch (Exception e) {
// occurs if expression contains references to variables etc. within the dynamic context
throw new XPathException("Error on evaluating (in static context) result-document href: " + hrefValue);
}
expr = visitor.typeCheck(expr, NodeKindTest.DOCUMENT);
XPathContext c3 = context.newCleanContext();
//context for ?select expression is the html page if an external node is the context
Document page = (Document)controller.getTargetNode(); //com.google.gwt.dom.client.Document.get();
Item cItem = context.getContextItem();
NodeInfo currentContextItem;
if (cItem instanceof JSObjectValue){
currentContextItem = null;
} else {
currentContextItem = (NodeInfo)cItem;
}
boolean useCurrentContext;
if (currentContextItem == null) {
useCurrentContext = false;
} else {
useCurrentContext = (currentContextItem.getBaseURI().equals(page.getURL()));
}
NodeInfo contextItem;
if (useCurrentContext) {
contextItem = currentContextItem;
if (LogConfiguration.loggingIsEnabled() && contextItem.getNodeKind() == Type.ELEMENT) {
contextNodeName = controller.getNamePool().getLocalName(contextItem.getNameCode());
}
} else {
contextItem = new HTMLDocumentWrapper(page, page.getURL(), context.getConfiguration(), DocType.UNKNOWN);
}
if (LogConfiguration.loggingIsEnabled()) {
contextNodeName = (contextNodeName.equals("")? "" : " context node: " + contextNodeName);
}
AxisIterator iterator = SingleNodeIterator.makeIterator(contextItem);
iterator.next(); // position on the single item
c3.setCurrentIterator(iterator);
SequenceIterator iter = expr.iterate(c3);
Item resultItem = iter.next();
if (resultItem == null) {} // do nothing
else if (!(resultItem instanceof NodeInfo)) {
throw new XPathException("non-node returned by result-document href: " + hrefValue);
} else {
target = (NodeInfo)resultItem;
targetNode = (com.google.gwt.dom.client.Node)((HTMLNodeWrapper)target).getUnderlyingNode();
}
}
else if (command == APIcommand.UPDATE_HTML) {
throw new XPathException("expected '?select=' or '#' at start of result-document href, found: " + hrefValue);
}
if (targetNode == null) {
logger.warning("result-document target not found for href: " + hrefValue + contextNodeName);
return null;
} else {
logger.fine("processing result-document for href: " + hrefValue + contextNodeName);
}
//checkAcceptableUri(context, absoluteResultURI.toString());
//IFrameElement container = Document.get().createIFrameElement();
Node container = null;
if (command == APIcommand.UPDATE_HTML) {
container = HTMLDocumentWrapper.createDocumentFragment((Document)controller.getTargetNode());
} else {
addResultDocument(context, new DocumentURI(absURI), (Document)targetNode);
container = targetNode;
}
PipelineConfiguration pipe = controller.makePipelineConfiguration();
Receiver out = controller.openResult(pipe, c2, container, action);
try {
content.process(c2);
out.endDocument();
} catch (XPathException err) {
err.setXPathContext(context);
err.maybeSetLocation(getSourceLocator());
throw err;
}
controller.closeResult(out, c2);
if (command == APIcommand.UPDATE_HTML){
PendingUpdateList list = controller.getPendingUpdateList();
if (action == REPLACE_CONTENT && command == APIcommand.UPDATE_HTML) {
int existingChildren = targetNode.getChildCount();
for (int i=0; i<existingChildren; i++) {
Node child = targetNode.getChild(i);
list.add(new DeleteAction(child));