XQuery
001 package org.gxml.book.xquery; 002 003 import java.io.StringWriter; 004 import java.math.BigInteger; 005 import java.net.URI; 006 007 import javax.xml.namespace.QName; 008 009 import org.gxml.book.common.SampleApp; 010 import org.gxml.sa.GxAtomBridge; 011 import org.gxml.sa.GxNameBridge; 012 import org.gxml.sa.GxProcessingContext; 013 import org.gxml.sa.GxSequenceHandler; 014 import org.gxml.sa.GxVariantBridge; 015 import org.gxml.xs.SmName; 016 017 import com.tibco.gxml.sa.api.common.lang.GxXQConnection; 018 import com.tibco.gxml.sa.api.common.lang.GxXQDataSource; 019 import com.tibco.gxml.sa.api.common.lang.GxXQExpression; 020 import com.tibco.gxml.sa.api.common.lang.GxXQPreparedExpression; 021 import com.tibco.gxml.sa.processor.serialization.api.GxSerializerFactory; 022 import com.tibco.gxml.sa.processor.serialization.impl.SerializerFactory; 023 import com.tibco.gxml.sa.processor.xquery.XQEngine; 024 import com.tibco.gxml.sa.processor.xquery.XQErrorCatcher; 025 026 /** 027 * Introduction to XQuery. 028 */ 029 public abstract class XQuerySample<I, U, N extends I, A extends I, S, T, X> extends SampleApp<I, U, N, A, S, T, X> 030 { 031 public void testExample() throws Exception 032 { 033 // Obtain a new processing context from the application. 034 final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 035 036 final GxXQDataSource<I, U, N, A, S, T, X> ds = new XQEngine<I, U, N, A, S, T, X>(pcx); 037 038 final GxXQConnection<I, U, N, A, S, T, X> conn = ds.getConnection(); 039 040 final String expression = "<x>{text{for $i in (1,2,3,4) return $i * 2}}</x>"; 041 042 final GxXQPreparedExpression<I, U, N, A, S, T, X> expr = conn.prepareExpression(expression); 043 044 final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 045 sf.setMethod(new QName("xml")); 046 sf.setOmitXmlDeclaration(true); 047 final StringWriter sw = new StringWriter(); 048 final GxSequenceHandler<A, S, T> handler = sf.newSerializer(sw); 049 050 expr.executeQuery(handler); 051 052 final String actual = sw.toString(); 053 assertEquals(expression, "<x>2 4 6 8</x>", actual); 054 } 055 056 public void testGettingStarted() throws Exception 057 { 058 // Obtain a new processing context from the application. 059 final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 060 061 final GxXQDataSource<I, U, N, A, S, T, X> ds = new XQEngine<I, U, N, A, S, T, X>(pcx); 062 063 final GxXQConnection<I, U, N, A, S, T, X> conn = ds.getConnection(); 064 065 final GxXQExpression<I, U, N, A, S, T, X> expr = conn.createExpression(); 066 067 final String es = "for $n in fn:doc('catalog.xml')//item return fn:data($n/name)"; 068 069 final URI systemId = new URI("catalog.xml"); 070 071 expr.setBaseURI(systemId); 072 073 @SuppressWarnings("unused") 074 final X value = expr.executeQuery(es); 075 } 076 077 public void testHelloWorld() throws Exception 078 { 079 final GxProcessingContext<I, U, N, A, S, T, X> pcx = this.newProcessingContext(); 080 081 final GxXQDataSource<I, U, N, A, S, T, X> ds = new XQEngine<I, U, N, A, S, T, X>(pcx); 082 083 final GxXQConnection<I, U, N, A, S, T, X> conn = ds.getConnection(); 084 085 conn.setScriptingMode(true); 086 087 final String expression = "declare variable $x external; concat('Hello, ',$x, '!')"; 088 089 final GxXQPreparedExpression<I, U, N, A, S, T, X> expr = conn.prepareExpression(expression); 090 091 final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 092 sf.setOmitXmlDeclaration(true); 093 sf.setIndent(false); 094 sf.setMethod(new QName("xml")); 095 final StringWriter sw = new StringWriter(); 096 final GxSequenceHandler<A, S, T> handler = sf.newSerializer(sw); 097 098 final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 099 final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 100 101 final SmName<S> varName = new SmName<S>(nameBridge.symbolize("x"), nameBridge); 102 final X value = valueBridge.stringValue("World"); 103 104 expr.bindVariableValue(varName, value); 105 106 expr.executeQuery(handler); 107 108 String actual = sw.toString(); 109 assertEquals(expression, "Hello, World!", actual); 110 } 111 112 public void testMergeTextNodes() throws Exception 113 { 114 // Obtain a new processing context from the application. 115 final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 116 117 final GxXQDataSource<I, U, N, A, S, T, X> ds = new XQEngine<I, U, N, A, S, T, X>(pcx); 118 119 final GxXQConnection<I, U, N, A, S, T, X> conn = ds.getConnection(); 120 121 // final String expression = ""; 122 final String expression = "count((element elem {1, 'string', 1,2e3})/text())"; 123 124 final GxXQPreparedExpression<I, U, N, A, S, T, X> expr = conn.prepareExpression(expression); 125 126 final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 127 sf.setMethod(new QName("xml")); 128 sf.setOmitXmlDeclaration(true); 129 final StringWriter sw = new StringWriter(); 130 final GxSequenceHandler<A, S, T> handler = sf.newSerializer(sw); 131 132 expr.executeQuery(handler); 133 134 final String actual = sw.toString(); 135 assertEquals(expression, "1", actual); 136 } 137 138 public void testProblem() throws Exception 139 { 140 final GxProcessingContext<I, U, N, A, S, T, X> pcx = this.newProcessingContext(); 141 142 final GxXQDataSource<I, U, N, A, S, T, X> ds = new XQEngine<I, U, N, A, S, T, X>(pcx); 143 144 final GxXQConnection<I, U, N, A, S, T, X> conn = ds.getConnection(); 145 146 final XQErrorCatcher messages = new XQErrorCatcher(); 147 148 conn.setErrorHandler(messages); 149 conn.setCompatibleMode(false); 150 conn.setScriptingMode(true); 151 152 final String expression = "(xs:untypedAtomic('1'),xs:untypedAtomic('2')) = (xs:untypedAtomic('2.0'),2.0)"; 153 154 final GxXQPreparedExpression<I, U, N, A, S, T, X> expr = conn.prepareExpression(expression); 155 156 final X value = expr.executeQuery(); 157 158 final GxVariantBridge<I, N, A, X> variantBridge = pcx.getVariantBridge(); 159 switch (variantBridge.getNature(value)) 160 { 161 case ITEMS: 162 { 163 @SuppressWarnings("unused") 164 final Iterable<I> items = variantBridge.getItemSet(value); 165 // System.out.println(items); 166 } 167 break; 168 case ATOM: 169 { 170 @SuppressWarnings("unused") 171 final A atom = variantBridge.getAtom(value); 172 @SuppressWarnings("unused") 173 final GxAtomBridge<A, S> atomBridge = pcx.getAtomBridge(); 174 // System.out.println(atomBridge.getC14NForm(atom)); 175 } 176 break; 177 case STRING: 178 { 179 @SuppressWarnings("unused") 180 final String strval = variantBridge.getString(value); 181 // System.out.println(strval); 182 } 183 break; 184 case INTEGER: 185 { 186 @SuppressWarnings("unused") 187 final BigInteger integer = variantBridge.getInteger(value); 188 // System.out.println(integer); 189 } 190 break; 191 default: 192 { 193 throw new AssertionError(variantBridge.getNature(value)); 194 } 195 } 196 } 197 198 public void testTyping() throws Exception 199 { 200 final GxProcessingContext<I, U, N, A, S, T, X> pcx = this.newProcessingContext(); 201 202 final GxXQDataSource<I, U, N, A, S, T, X> ds = new XQEngine<I, U, N, A, S, T, X>(pcx); 203 204 final GxXQConnection<I, U, N, A, S, T, X> conn = ds.getConnection(); 205 206 conn.setScriptingMode(true); 207 208 final XQErrorCatcher messages = new XQErrorCatcher(); 209 210 conn.setErrorHandler(messages); 211 212 final String expression = "declare variable $x external; contains(string(number($x)),'NaN')"; 213 214 final GxXQPreparedExpression<I, U, N, A, S, T, X> expr = conn.prepareExpression(expression); 215 216 final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 217 sf.setOmitXmlDeclaration(true); 218 sf.setIndent(false); 219 sf.setMethod(new QName("xml")); 220 final StringWriter sw = new StringWriter(); 221 final GxSequenceHandler<A, S, T> handler = sf.newSerializer(sw); 222 223 final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 224 final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 225 226 final SmName<S> varName = new SmName<S>(nameBridge.symbolize("x"), nameBridge); 227 final X value = valueBridge.doubleValue(5.0); 228 229 expr.bindVariableValue(varName, value); 230 231 expr.executeQuery(handler); 232 233 String actual = sw.toString(); 234 assertEquals(expression, "false", actual); 235 } 236 }
Copyright © Cloud Software Group, Inc. All rights reserved.