XPath

001 package org.gxml.book.xpath; 
002  
003 import org.gxml.book.common.SampleApp; 
004 import org.gxml.sa.GxMetaBridge; 
005 import org.gxml.sa.GxNameBridge; 
006 import org.gxml.sa.GxProcessingContext; 
007 import org.gxml.sa.GxVariantBridge; 
008 import org.gxml.xdm.Emulation; 
009 import org.gxml.xs.SmName; 
010 import org.gxml.xs.SmNativeType; 
011  
012 import com.tibco.gxml.sa.api.common.lang.ExprResult; 
013 import com.tibco.gxml.sa.api.common.lang.GxExpr; 
014 import com.tibco.gxml.sa.api.common.lang.GxExprContextDynamicArgs; 
015 import com.tibco.gxml.sa.api.common.lang.GxExprContextStaticArgs; 
016 import com.tibco.gxml.sa.api.common.lang.GxFocus; 
017 import com.tibco.gxml.sa.api.common.lang.GxLanguageToolKit; 
018 import com.tibco.gxml.sa.processor.xquery.LanguageToolKit; 
019 import com.tibco.gxmlsa.processor.org.exslt.math.ExsltMathFunctionGroup; 
020  
021 public abstract class XPathSample<I, U, N extends I, A extends I, S, T, X> extends 
SampleApp<I, U, N, A, S, T, X> 
022 { 
023     public void testGettingStarted() throws Exception 
024     { 
025         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
026  
027         // For demonstration purposes, register the language toolkit with the processing context. 
028         pcx.register("xyz", new LanguageToolKit<I, U, N, A, S, T, X>(pcx)); 
029  
030         @SuppressWarnings("unchecked") 
031         // Immediately get back the registered processor. 
032         GxLanguageToolKit<I, U, N, A, S, T, X> xtk = pcx.getProcessor("xyz", 
GxLanguageToolKit.class); 
033  
034         final GxExprContextStaticArgs<I, U, N, A, S, T, X> sarg = xtk.newStaticContextArgs(); 
035  
036         final GxMetaBridge<A, S, T> metaBridge = pcx.getMetaBridge(); 
037  
038         final ExprResult<I, U, N, A, S, T, X> prepared = xtk.prepare("concat('Hello', ', ', 
'World', '!')", metaBridge.emptyType(), sarg); 
039         final GxExpr<I, U, N, A, S, T, X> expr = prepared.getExpr(); 
040  
041         final GxExprContextDynamicArgs<I, U, N, A, S, T, X> darg = xtk.newDynamicContextArgs(); 
042  
043         final String strval = expr.stringFunction(xtk.emptyFocus(), darg, pcx); 
044  
045         assertEquals("Hello, World!", strval); 
046     } 
047  
048     public void testBindingVariables() throws Exception 
049     { 
050         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
051  
052         final GxLanguageToolKit<I, U, N, A, S, T, X> xtk = 
new LanguageToolKit<I, U, N, A, S, T, X>(pcx); 
053  
054         final GxExprContextStaticArgs<I, U, N, A, S, T, X> statArgs = xtk.newStaticContextArgs(); 
055         statArgs.setEmulation(Emulation.MODERN); 
056  
057         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
058         final SmName<S> varName = new SmName<S>(nameBridge.symbolize("x"), nameBridge); 
059  
060         final GxMetaBridge<A, S, T> metaBridge = pcx.getMetaBridge(); 
061         statArgs.bindVariableType(varName, metaBridge.getType(SmNativeType.STRING)); 
062  
063         final String es = "concat('Hello', ', ', $x, '!')"; 
064         final T sfocus = metaBridge.emptyType(); 
065  
066         final ExprResult<I, U, N, A, S, T, X> prepared = xtk.prepare(es, sfocus, statArgs); 
067  
068         final GxExprContextDynamicArgs<I, U, N, A, S, T, X> dynArgs = 
xtk.newDynamicContextArgs(); 
069         dynArgs.setEmulation(Emulation.MODERN); 
070  
071         final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 
072         final X value = valueBridge.stringValue("World"); 
073         dynArgs.bindVariableValue(varName, value); 
074  
075         final GxExpr<I, U, N, A, S, T, X> expr = prepared.getExpr(); 
076         final GxFocus<I> dfocus = xtk.emptyFocus(); 
077         final String strval = expr.stringFunction(dfocus, dynArgs, pcx); 
078  
079         assertEquals("Hello, World!", strval); 
080     } 
081  
082     public void testEXSLT() throws Exception 
083     { 
084         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
085         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
086  
087         final GxLanguageToolKit<I, U, N, A, S, T, X> xtk = 
new LanguageToolKit<I, U, N, A, S, T, X>(pcx); 
088  
089         final GxExprContextStaticArgs<I, U, N, A, S, T, X> sarg = xtk.newStaticContextArgs(); 
090         sarg.getInScopeNamespaces().declarePrefix("math", nameBridge.symbolize
("http://exslt.org/math")); 
091         final ExsltMathFunctionGroup<I, U, N, A, S, T, X> exsltMathFunctionGroup = new 
ExsltMathFunctionGroup<I, U, N, A, S, T, X>("http://exslt.org/math", pcx); 
092         sarg.setFunctionSigns("http://exslt.org/math", exsltMathFunctionGroup); 
093         // The function implementations can be provided now or just prior to execution. 
094         sarg.setFunctionImpls("http://exslt.org/math", exsltMathFunctionGroup); 
095  
096         final GxMetaBridge<A, S, T> metaBridge = pcx.getMetaBridge(); 
097  
098         final ExprResult<I, U, N, A, S, T, X> prepared = xtk.prepare("math:exp(1)", 
metaBridge.emptyType(), sarg); 
099  
100         final GxExpr<I, U, N, A, S, T, X> expr = prepared.getExpr(); 
101  
102         final GxExprContextDynamicArgs<I, U, N, A, S, T, X> darg = xtk.newDynamicContextArgs(); 
103         // Here we also (redundantly) provide the function implementations just prior to execution. 
104         darg.setFunctionImpls("http://exslt.org/math", exsltMathFunctionGroup); 
105  
106         final String strval = expr.stringFunction(xtk.emptyFocus(), darg, pcx); 
107  
108         assertEquals("2.7182818284590455", strval); 
109     } 
110  
111     public void testExpressionType() throws Exception 
112     { 
113         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
114  
115         final GxLanguageToolKit<I, U, N, A, S, T, X> xtk = 
new LanguageToolKit<I, U, N, A, S, T, X>(pcx); 
116  
117         final GxExprContextStaticArgs<I, U, N, A, S, T, X> sarg = xtk.newStaticContextArgs(); 
118  
119         final GxMetaBridge<A, S, T> metaBridge = pcx.getMetaBridge(); 
120  
121         final ExprResult<I, U, N, A, S, T, X> prepared = xtk.prepare("'Hello'", 
metaBridge.emptyType(), sarg); 
122         /* final GxExpr<I, U, N, A, S, T, X> expr = */prepared.getExpr(); 
123         /* final GxExprInfo<T> info = */prepared.getInfo(); 
124     } 
125 }