[prev in list] [next in list] [prev in thread] [next in thread]
List: xalan-dev
Subject: Extension element return value as variable
From: Adam Jenkins <mail () adamjenkins ! net>
Date: 2021-02-06 10:49:59
Message-ID: 3A2CA162-2B78-4B3E-9F12-7ED4AB903FCA () adamjenkins ! net
[Download RAW message or body]
Hi Everyone,
I have a custom extension element that returns a value (a spring context that stores \
a large cache of constants). I enclosed it in a variable thinking the returned \
object would be set into the variable for use later however it's storing an \
XRTreeFrag (the children themselves) into the VariableStack.
You'll see what I'm trying to do below. Does anyone have any idea of a better way to \
accomplish this?
Cheers
Adam
<xsl:template match="/">
<xsl:variable name="test1">
<spring:context base="CLASSPATH">
<spring:resource \
location="/net/adamjenkins/sxe/spring/spring-config.xml"/> </spring:context>
</xsl:variable>
<mockelement:run select="$test1"/>
public class Spring{
public AbstractApplicationContext context(XSLProcessorContext context, \
ElemExtensionCall extensionElement){ //returns the spring context
}
}
public class MockElement {
public void run(XSLProcessorContext context, ElemExtensionCall extensionElement) \
throws TransformerException { System.out.println("running");
XPathContext xCtx = context.getTransformer().getXPathContext();
String selectExpressionString = null;
boolean namespacePushed = false;
boolean expressionPushed = false;
try{
selectExpressionString = extensionElement.getAttribute("select"); \
XPath xpath = new XPath(selectExpressionString, xCtx.getSAXLocator(), \
extensionElement, XPath.SELECT); xCtx.pushNamespaceContext(extensionElement); \
namespacePushed = true;
int current = xCtx.getCurrentNode();
xCtx.pushCurrentNodeAndExpression(current, current);
expressionPushed=true;
Expression expr = xpath.getExpression();
XObject result = expr.execute(xCtx);
//If I set the variable using select=" " I get back an XObject with the correct \
variable for "result"
//if I set the variable using an extension element as above I get back an \
XRTreeFrag object }catch(Throwable t){
t.printStackTrace();
}
finally
{
if(namespacePushed) xCtx.popNamespaceContext();
if(expressionPushed) xCtx.popCurrentNodeAndExpression();
}
}
}
[Attachment #3 (unknown)]
<html><head><meta http-equiv="Content-Type" content="text/html; \
charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \
line-break: after-white-space;" class="">Hi Everyone,<div class=""><br \
class=""></div><div class="">I have a custom extension element that returns a value \
(a spring context that stores a large cache of constants). I enclosed it in a \
variable thinking the returned object would be set into the variable for use later \
however it's storing an XRTreeFrag (the children themselves) into the \
VariableStack.</div><div class=""><br class=""></div><div class="">You'll see what \
I'm trying to do below. Does anyone have any idea of a better way to accomplish \
this?</div><div class=""><br class=""></div><div class="">Cheers</div><div \
class="">Adam</div><div class=""><br class=""></div><div class=""><br \
class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; \
line-height: normal; font-family: Menlo; color: rgb(63, 127, 127);" class=""><span \
style="color: #000000" class=""> </span><span style="color: \
#008080" class=""><b class=""><</b></span><b class="">xsl:template </b><span \
style="color: #7f007f" class=""><b class="">match</b></span><span style="color: \
#000000" class="">=</span><span style="color: #2a00ff" class=""><i \
class="">"/"</i></span><span style="color: #008080" class=""><b \
class="">></b></span></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; font-family: Menlo; color: rgb(63, 127, 127);" class=""><span \
style="color: #000000" class=""> </span><span \
style="color: #008080" class=""><b class=""><</b></span><b class="">xsl:variable \
</b><span style="color: #7f007f" class=""><b class="">name</b></span><span \
style="color: #000000" class="">=</span><span style="color: #2a00ff" class=""><i \
class="">"test1"</i></span><span style="color: #008080" class=""><b \
class="">></b></span></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" \
style="white-space:pre"> </span> <span style="color: #008080" \
class=""><</span><span style="color: #3f7f7f" class="">spring:context</span> <span \
style="color: #7f007f" class="">base</span>="CLASSPATH"<span style="color: #008080" \
class="">></span></div><div style="margin: 0px; font-stretch: normal; line-height: \
normal; font-family: Menlo;" class=""> \
<span style="color: #008080" class=""><</span><span style="color: \
#3f7f7f" class="">spring:resource</span> <span style="color: #7f007f" \
class="">location</span>="/net/adamjenkins/sxe/spring/spring-config.xml"<span \
style="color: #008080" class="">/></span></div><div style="margin: 0px; \
font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(63, 127, \
127);" class=""><span style="color: #000000" class=""> \
</span><span style="color: #008080" \
class=""></</span>spring:context<span style="color: #008080" \
class="">></span></div><div style="margin: 0px; font-stretch: normal; line-height: \
normal; font-family: Menlo;" class=""> <span style="color: \
#008080" class=""><b class=""></</b></span><span style="color: #3f7f7f" \
class=""><b class="">xsl:variable</b></span><span style="color: #008080" class=""><b \
class="">></b></span> </div><div style="margin: 0px; \
font-stretch: normal; line-height: normal; font-family: Menlo;" class=""> \
<span style="color: #008080" class=""><</span><span \
style="color: #3f7f7f" class="">mockelement:run</span> <span style="color: #7f007f" \
class="">select</span>="$test1"<span style="color: #008080" \
class="">/></span></div></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; font-family: Menlo;" class=""><br class=""></div><div \
style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo;" \
class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; \
font-stretch: normal; line-height: normal; color: rgb(127, 0, 85);" class=""><b \
class="">public</b><span style="color: #000000" class=""> </span><b \
class="">class</b><span style="color: #000000" class=""> Spring{</span></div><div \
style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(127, 0, \
85);" class=""><span style="color: #000000" class=""><br \
class=""></span></div></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""><span style="color: #7f0055" \
class=""><b class=""> public</b></span> AbstractApplicationContext \
context(XSLProcessorContext <span style="color: #6a3e3e" class="">context</span>, \
ElemExtensionCall <span style="color: #6a3e3e" \
class="">extensionElement</span>){</div><div style="margin: 0px; font-stretch: \
normal; line-height: normal;" class=""> //returns the \
spring context</div><div style="margin: 0px; font-stretch: normal; line-height: \
normal;" class=""> }</div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""><br class=""></div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class="">}</div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""><br class=""></div><div \
style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div \
style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span \
style="color: #7f0055" class=""><b class="">public</b></span> <span style="color: \
#7f0055" class=""><b class="">class</b></span> MockElement {</div><p style="margin: \
0px; font-stretch: normal; line-height: normal; min-height: 14px;" class=""><span \
class="Apple-tab-span" style="white-space:pre"> </span><br \
class="webkit-block-placeholder"></p><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""><span class="Apple-tab-span" \
style="white-space:pre"> </span><span style="color: #7f0055" class=""><b \
class="">public</b></span> <span style="color: #7f0055" class=""><b \
class="">void</b></span> run(XSLProcessorContext <span style="color: #6a3e3e" \
class="">context</span>, ElemExtensionCall <span style="color: #6a3e3e" \
class="">extensionElement</span>) <span style="color: #7f0055" class=""><b \
class="">throws</b></span> TransformerException {</div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""><span class="Apple-tab-span" \
style="white-space:pre"> </span>System.<span style="color: #0000c0" class=""><b \
class=""><i class="">out</i></b></span>.println(<span style="color: #2a00ff" \
class="">"running"</span>);</div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""> XPathContext <span \
style="color: #6a3e3e" class="">xCtx</span> = <span style="color: #6a3e3e" \
class="">context</span>.getTransformer().getXPathContext(); </div><div \
style="margin: 0px; font-stretch: normal; line-height: normal; color: rgb(106, 62, \
62);" class=""><span style="color: #000000" class=""> \
String </span>selectExpressionString<span style="color: #000000" class=""> = \
</span><span style="color: #7f0055" class=""><b class="">null</b></span><span \
style="color: #000000" class="">;</span></div><div style="margin: 0px; font-stretch: \
normal; line-height: normal; color: rgb(106, 62, 62);" class=""><span style="color: \
#000000" class=""> </span><span style="color: #7f0055" \
class=""><b class="">boolean</b></span><span style="color: #000000" class=""> \
</span>namespacePushed<span style="color: #000000" class=""> = </span><span \
style="color: #7f0055" class=""><b class="">false</b></span><span style="color: \
#000000" class="">;</span></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; color: rgb(106, 62, 62);" class=""><span style="color: #000000" \
class=""> </span><span style="color: #7f0055" class=""><b \
class="">boolean</b></span><span style="color: #000000" class=""> \
</span>expressionPushed<span style="color: #000000" class=""> = </span><span \
style="color: #7f0055" class=""><b class="">false</b></span><span style="color: \
#000000" class="">;</span></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""> <span style="color: \
#7f0055" class=""><b class="">try</b></span>{ </div><div style="margin: \
0px; font-stretch: normal; line-height: normal;" class=""> \
<span style="color: #6a3e3e" class="">selectExpressionString</span> = \
<span style="color: #6a3e3e" class="">extensionElement</span>.getAttribute(<span \
style="color: #2a00ff" class="">"select"</span>); \
</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" \
class=""> XPath <span style="color: #6a3e3e" \
class="">xpath</span> = <span style="color: #7f0055" class=""><b \
class="">new</b></span> XPath(<span style="color: #6a3e3e" \
class="">selectExpressionString</span>, <span style="color: #6a3e3e" \
class="">xCtx</span>.getSAXLocator(), <span style="color: #6a3e3e" \
class="">extensionElement</span>, XPath.<span style="color: #0000c0" class=""><b \
class=""><i class="">SELECT</i></b></span>);</div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""> \
<span style="color: #6a3e3e" \
class="">xCtx</span>.pushNamespaceContext(<span style="color: #6a3e3e" \
class="">extensionElement</span>); \
</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" \
class=""> <span style="color: #6a3e3e" \
class="">namespacePushed</span> = <span style="color: #7f0055" class=""><b \
class="">true</b></span>;</div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""> <span \
style="color: #7f0055" class=""><b class="">int</b></span> <span style="color: \
#6a3e3e" class="">current</span> = <span style="color: #6a3e3e" \
class="">xCtx</span>.getCurrentNode();</div><div style="margin: 0px; font-stretch: \
normal; line-height: normal;" class=""> \
<span style="color: #6a3e3e" class="">xCtx</span>.pushCurrentNodeAndExpression(<span \
style="color: #6a3e3e" class="">current</span>, <span style="color: #6a3e3e" \
class="">current</span>); </div><div style="margin: 0px; \
font-stretch: normal; line-height: normal; color: rgb(106, 62, 62);" class=""><span \
style="color: #000000" class=""> \
</span>expressionPushed<span style="color: #000000" class="">=</span><span \
style="color: #7f0055" class=""><b class="">true</b></span><span style="color: \
#000000" class="">;</span></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""> Expression \
<span style="color: #6a3e3e" class="">expr</span> = <span style="color: #6a3e3e" \
class="">xpath</span>.getExpression();</div><div style="margin: 0px; font-stretch: \
normal; line-height: normal;" class=""> \
XObject <span style="text-decoration: underline ; color: #6a3e3e" \
class="">result</span> = <span style="color: #6a3e3e" \
class="">expr</span>.execute(<span style="color: #6a3e3e" \
class="">xCtx</span>);</div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""><br class=""></div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""><span class="Apple-tab-span" \
style="white-space:pre"> </span> //If I set the variable using select=" \
" I get back an XObject with the correct variable for "result"</div><div \
style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> \
//if I set the variable using an extension element \
as above I get back an XRTreeFrag object</div><div style="margin: 0px; font-stretch: \
normal; line-height: normal;" class=""> }<span \
style="color: #7f0055" class=""><b class="">catch</b></span>(Throwable <span \
style="color: #6a3e3e" class="">t</span>){</div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""> \
<span style="color: #6a3e3e" \
class="">t</span>.printStackTrace();</div><div style="margin: 0px; font-stretch: \
normal; line-height: normal;" class=""> }</div><div \
style="margin: 0px; font-stretch: normal; line-height: normal;" class=""> \
<span style="color: #7f0055" class=""><b \
class="">finally</b></span></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""> { \
</div><div style="margin: 0px; font-stretch: normal; line-height: \
normal;" class=""> <span style="color: \
#7f0055" class=""><b class="">if</b></span>(<span style="color: #6a3e3e" \
class="">namespacePushed</span>) <span style="color: #6a3e3e" \
class="">xCtx</span>.popNamespaceContext();</div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""> \
<span style="color: #7f0055" class=""><b class="">if</b></span>(<span \
style="color: #6a3e3e" class="">expressionPushed</span>) <span style="color: #6a3e3e" \
class="">xCtx</span>.popCurrentNodeAndExpression(); \
</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" \
class=""> } </div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""><span class="Apple-tab-span" \
style="white-space:pre"> </span>}</div><div style="margin: 0px; font-stretch: normal; \
line-height: normal; min-height: 14px;" class=""><br class=""></div><div \
style="margin: 0px; font-stretch: normal; line-height: normal;" \
class="">}</div></div><div style="margin: 0px; font-stretch: normal; line-height: \
normal;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; \
line-height: normal;" class=""><br class=""></div><div style="margin: 0px; \
font-stretch: normal; line-height: normal;" class=""><br \
class=""></div></div></body></html>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic