Constructing a Data Model Tree Programmatically

This example demonstrates constructing a tree directly using the fragment builder.

001 package org.gxml.book.snoopy; 
002  
003 import java.io.IOException; 
004 import java.io.InputStream; 
005 import java.io.StringReader; 
006 import java.io.StringWriter; 
007 import java.net.URI; 
008 import java.net.URISyntaxException; 
009  
010 import javax.xml.namespace.QName; 
011 import javax.xml.parsers.ParserConfigurationException; 
012  
013 import org.gxml.book.common.SampleApp; 
014 import org.gxml.sa.GxException; 
015 import org.gxml.sa.GxFragmentBuilder; 
016 import org.gxml.sa.GxMetaBridge; 
017 import org.gxml.sa.GxModel; 
018 import org.gxml.sa.GxNameBridge; 
019 import org.gxml.sa.GxProcessingContext; 
020 import org.gxml.sa.GxSequenceHandler; 
021 import org.gxml.sa.GxVariantBridge; 
022 import org.gxml.xdm.NodeKind; 
023 import org.gxml.xdm.Resolved; 
024 import org.gxml.xdm.Resolver; 
025 import org.gxml.xs.SmName; 
026  
027 import com.tibco.gxml.sa.api.common.lang.ExprException; 
028 import com.tibco.gxml.sa.api.common.lang.ExprResult; 
029 import com.tibco.gxml.sa.api.common.lang.GxExpr; 
030 import com.tibco.gxml.sa.api.common.lang.GxExprContextDynamicArgs; 
031 import com.tibco.gxml.sa.api.common.lang.GxExprContextStaticArgs; 
032 import com.tibco.gxml.sa.api.common.lang.GxLanguageToolKit; 
033 import com.tibco.gxml.sa.common.helpers.DocumentBuilderFactory; 
034 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilder; 
035 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilderFactory; 
036 import com.tibco.gxml.sa.processor.serialization.api.GxSerializerFactory; 
037 import com.tibco.gxml.sa.processor.serialization.impl.SerializerFactory; 
038 import com.tibco.gxml.sa.processor.xquery.LanguageToolKit; 
039 import com.tibco.gxml.sa.processor.xslt.GxTransform; 
040 import com.tibco.gxml.sa.processor.xslt.GxTransformBuilder; 
041 import com.tibco.gxml.sa.processor.xslt.GxTransformer; 
042 import com.tibco.gxml.sa.processor.xslt.XSLTransformBuilder; 
043 import com.tibco.gxmlsa.processor.org.exslt.strings.ExsltStringsFunctionGroup; 
044  
045 public abstract class SnoopySample<I, U, N extends I, A extends I, S, T, X> extends 
SampleApp<I, U, N, A, S, T, X> 
046 { 
047     public void testDocumentFromString() 
048     { 
049         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
050  
051         final N document = documentFromString(pcx); 
052  
053         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
054  
055         sf.setIndent(true); 
056  
057         final StringWriter sw = new StringWriter(); 
058  
059         final GxSequenceHandler<A, S, T> serializer = sf.newSerializer(sw); 
060  
061         final GxModel<N, A, S, T> model = pcx.getModel(); 
062  
063         model.stream(document, true, true, serializer); 
064  
065         // System.out.println(sw.toString()); 
066     } 
067  
068     public void testFragmentBuilder() 
069     { 
070         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
071  
072         final N document = documentFromEvents(pcx); 
073  
074         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
075  
076         sf.setIndent(true); 
077  
078         final StringWriter sw = new StringWriter(); 
079  
080         final GxSequenceHandler<A, S, T> serializer = sf.newSerializer(sw); 
081  
082         final GxModel<N, A, S, T> model = pcx.getModel(); 
083  
084         model.stream(document, true, true, serializer); 
085  
086         // System.out.println(sw.toString()); 
087     } 
088  
089     private N documentFromString(final GxProcessingContext<I, U, N, A, S, T, X> pcx) 
090     { 
091         final String strval = "" + "<?xml version='1.0' encoding='UTF-8'?>" + "<book isbn='0836217462'>" + " 
<title>Being a Dog Is a Full-Time Job</title>" + " <author>Charles M. Schultz</author>" + " <character>" + " 
<name>Snoopy</name>" + " <since>1950-10-04</since>" + " </character>" + "</book>"; 
092  
093         final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
094  
095         final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 
096  
097         try 
098         { 
099             return builder.parse(new StringReader(strval), null); 
100         } 
101         catch (final IOException e) 
102         { 
103             throw new AssertionError(); 
104         } 
105     } 
106  
107     private N documentFromEvents(final GxProcessingContext<I, U, N, A, S, T, X> pcx) 
108     { 
109         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
110  
111         final S NULL_NS_URI = nameBridge.empty(); 
112         final S BOOK = nameBridge.symbolize("book"); 
113         final S ISBN = nameBridge.symbolize("isbn"); 
114         final S TITLE = nameBridge.symbolize("title"); 
115         final S AUTHOR = nameBridge.symbolize("author"); 
116         final S CHARACTER = nameBridge.symbolize("character"); 
117         final S NAME = nameBridge.symbolize("name"); 
118         final S SINCE = nameBridge.symbolize("since"); 
119  
120         final GxFragmentBuilder<N, A, S, T> builder = pcx.newFragmentBuilder(); 
121  
122         // Note: Using try...finally not only ensures that elements get closed when errors 
123         // occur, it also helps to remind you to end elements and makes the levels in 
124         // the XML more obvious. 
125         builder.startDocument(null); 
126         try 
127         { 
128             builder.startElement(NULL_NS_URI, BOOK, "", null); 
129             try 
130             { 
131                 builder.attribute(NULL_NS_URI, ISBN, "", "0836217462"); 
132                 builder.startElement(NULL_NS_URI, TITLE, "", null); 
133                 try 
134                 { 
135                     builder.text("Being a Dog Is a Full-Time Job"); 
136                 } 
137                 finally 
138                 { 
139                     builder.endElement(); 
140                 } 
141                 builder.startElement(NULL_NS_URI, AUTHOR, "", null); 
142                 try 
143                 { 
144                     builder.text("Charles M. Schultz"); 
145                 } 
146                 finally 
147                 { 
148                     builder.endElement(); 
149                 } 
150                 builder.startElement(NULL_NS_URI, CHARACTER, "", null); 
151                 try 
152                 { 
153                     builder.startElement(NULL_NS_URI, NAME, "", null); 
154                     try 
155                     { 
156                         builder.text("Snoopy"); 
157                     } 
158                     finally 
159                     { 
160                         builder.endElement(); 
161                     } 
162                     builder.startElement(NULL_NS_URI, SINCE, "", null); 
163                     try 
164                     { 
165                         builder.text("1950-10-04"); 
166                     } 
167                     finally 
168                     { 
169                         builder.endElement(); 
170                     } 
171                 } 
172                 finally 
173                 { 
174                     builder.endElement(); 
175                 } 
176             } 
177             finally 
178             { 
179                 builder.endElement(); 
180             } 
181         } 
182         finally 
183         { 
184             builder.endDocument(); 
185         } 
186  
187         return builder.getNodes().get(0); 
188     } 
189  
190     public void testExample() throws ParserConfigurationException, IOException, GxException, ExprException, 
URISyntaxException 
191     { 
192         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
193  
194         final Resolver resolver = getResolver(); 
195  
196         final URI xmlSystemId = new URI("hotel.xml"); 
197         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(xmlSystemId); 
198  
199         final GxDocumentBuilderFactory<N, S> f = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
200  
201         final GxDocumentBuilder<N> builder = f.newDocumentBuilder(); 
202  
203         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
204  
205         final URI xslSystemId = new URI("hotel.xsl"); 
206         final Resolved<InputStream> xslInput = resolver.resolveInputStream(xslSystemId); 
207  
208         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>
(pcx); 
209  
210         // poem.xsl uses version="2.0", but we want to use XPath 1.0 compatibility mode 
211         // so that arguments to functions are converted etc. 
212         compiler.setCompatibleMode(true); 
213  
214         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), 
xslInput.getSystemId()); 
215  
216         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
217  
218         // TODO: Extract the configuration? 
219         // compiled.configure(sf); 
220  
221         sf.setIndent(true); 
222  
223         final StringWriter w = new StringWriter(); 
224  
225         final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w); 
226  
227         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
228  
229         transformer.transform(document, pcx, handler); 
230     } 
231  
232     public void testVariableBinding() throws ParserConfigurationException, IOException, GxException, 
ExprException, URISyntaxException 
233     { 
234         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
235  
236         final Resolver resolver = getResolver(); 
237  
238         final URI xslSystemId = new URI("email.xsl"); 
239         final Resolved<InputStream> xslInput = resolver.resolveInputStream(xslSystemId); 
240  
241         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, 
X>(pcx); 
242  
243         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), 
xslInput.getSystemId()); 
244  
245         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
246  
247         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
248         final SmName<S> varName = nameBridge.name(new QName("to")); 
249         final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 
250         final X value = valueBridge.stringValue("David"); 
251  
252         transformer.bindVariableValue(varName, value); 
253         transformer.bindVariableValue(nameBridge.name(new QName("http://www.example.com", "from")), 
valueBridge.stringValue("Julie")); 
254  
255         final N documentNode = transformer.transform(null, pcx); 
256  
257         final GxModel<N, A, S, T> model = pcx.getModel(); 
258  
259         assertEquals(NodeKind.DOCUMENT, model.getNodeKind(documentNode)); 
260         final N email = model.getFirstChildElement(documentNode); 
261         final N to = model.getFirstChildElementByName(email, nameBridge.symbolize("http://www.example.com"), 
nameBridge.symbolize("to")); 
262         assertEquals("David", model.getStringValue(to)); 
263         final N from = model.getFirstChildElementByName(email, null, nameBridge.symbolize("from")); 
264         assertEquals("Julie", model.getStringValue(from)); 
265         final N again = model.getFirstChildElementByName(email, nameBridge.symbolize("http://www.example.com"), 
null); 
266         assertEquals("David", model.getStringValue(again)); 
267     } 
268  
269     public void testExternalFunctions() throws ParserConfigurationException, IOException, GxException, 
ExprException, URISyntaxException 
270     { 
271         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
272  
273         final Resolver resolver = getResolver(); 
274  
275         final URI xmlSystemId = new URI("exslt.xml"); 
276         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(xmlSystemId); 
277  
278         final GxDocumentBuilderFactory<N, S> f = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
279  
280         final GxDocumentBuilder<N> builder = f.newDocumentBuilder(); 
281  
282         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
283  
284         final URI xslSystemId = new URI("exslt.xsl"); 
285         final Resolved<InputStream> xslInput = resolver.resolveInputStream(xslSystemId); 
286  
287         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>
(pcx); 
288  
289         final String namespaceURI = "http://exslt.org/strings"; 
290         final ExsltStringsFunctionGroup<I, U, N, A, S, T, X> functions = 
new ExsltStringsFunctionGroup<I, U, N, A, S, T, X>(namespaceURI, pcx); 
291         compiler.setFunctionSigns(namespaceURI, functions); 
292         compiler.setFunctionImpls(namespaceURI, functions); 
293  
294         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), 
xslInput.getSystemId()); 
295  
296         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
297  
298         // TODO: Extract the configuration. 
299         // compiled.configure(sf); 
300  
301         sf.setIndent(true); 
302  
303         final StringWriter w = new StringWriter(); 
304  
305         final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w); 
306  
307         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
308  
309         transformer.transform(document, pcx, handler); 
310  
311         // System.out.println(w.toString()); 
312     } 
313  
314     public void testHotel() throws ParserConfigurationException, IOException, GxException, ExprException, 
URISyntaxException 
315     { 
316         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
317  
318         final Resolver resolver = getResolver(); 
319  
320         final URI xmlSystemId = new URI("hotel.xml"); 
321         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(xmlSystemId); 
322  
323         final GxDocumentBuilderFactory<N, S> f = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
324  
325         final GxDocumentBuilder<N> builder = f.newDocumentBuilder(); 
326  
327         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
328  
329         final URI xslSystemId = new URI("hotel.xsl"); 
330         final Resolved<InputStream> xslInput = resolver.resolveInputStream(xslSystemId); 
331  
332         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>
(pcx); 
333  
334         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), 
xslInput.getSystemId()); 
335  
336         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
337  
338         // TODO: Extract the configuration. 
339         // compiled.configure(sf); 
340  
341         sf.setIndent(true); 
342  
343         final StringWriter w = new StringWriter(); 
344  
345         final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w); 
346  
347         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
348         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
349         final SmName<S> varName = nameBridge.name(new QName("MessageData")); 
350         final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 
351         final X value = valueBridge.node(document); 
352  
353         transformer.bindVariableValue(varName, value); 
354  
355         transformer.transform(null, pcx, handler); 
356  
357         // System.out.println(w.toString()); 
358     } 
359  
360     public void testHelloWorld() throws Exception 
361     { 
362         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
363         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
364  
365         final GxLanguageToolKit<I, U, N, A, S, T, X> xtk = new LanguageToolKit<I, U, 
N, A, S, T, X>(pcx); 
366  
367         final GxExprContextStaticArgs<I, U, N, A, S, T, X> senv = xtk.newStaticContextArgs(); 
368         final String NAMESPACE = "http://www.peanuts.com"; 
369  
370         senv.getInScopeNamespaces().declarePrefix("nuts", nameBridge.symbolize(NAMESPACE)); 
371  
372         final SnoopyFunctionGroup<I, U, N, A, S, T, X> peanutsFunctionGroup = new 
SnoopyFunctionGroup<I, U, N, A, S, T, X>(NAMESPACE, pcx); 
373         senv.setFunctionSigns(NAMESPACE, peanutsFunctionGroup); 
374         senv.setFunctionImpls(NAMESPACE, peanutsFunctionGroup); 
375  
376         final GxMetaBridge<A, S, T> metaBridge = pcx.getMetaBridge(); 
377  
378         final ExprResult<I, U, N, A, S, T, X> prepared = xtk.prepare
("nuts:GetVariableProperty('foo','bar')", metaBridge.emptyType(), senv); 
379  
380         final GxExpr<I, U, N, A, S, T, X> expr = prepared.getExpr(); 
381  
382         final GxExprContextDynamicArgs<I, U, N, A, S, T, X> darg = 
xtk.newDynamicContextArgs(); 
383  
384         final String strval = expr.stringFunction(xtk.emptyFocus(), darg, pcx); 
385  
386         assertEquals("Bingo!", strval); 
387     } 
388 }