XSLT

001 package org.gxml.book.xslt; 
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.GxMetaBridge; 
016 import org.gxml.sa.GxModel; 
017 import org.gxml.sa.GxNameBridge; 
018 import org.gxml.sa.GxProcessingContext; 
019 import org.gxml.sa.GxSequenceHandler; 
020 import org.gxml.sa.GxVariantBridge; 
021 import org.gxml.xdm.NodeKind; 
022 import org.gxml.xdm.Resolved; 
023 import org.gxml.xdm.Resolver; 
024 import org.gxml.xs.SmName; 
025 import org.gxml.xs.SmNativeType; 
026  
027 import com.tibco.gxml.sa.api.common.lang.ExprException; 
028 import com.tibco.gxml.sa.common.helpers.DocumentBuilderFactory; 
029 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilder; 
030 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilderFactory; 
031 import com.tibco.gxml.sa.processor.serialization.api.GxSerializerFactory; 
032 import com.tibco.gxml.sa.processor.serialization.impl.SerializerFactory; 
033 import com.tibco.gxml.sa.processor.xslt.GxTransform; 
034 import com.tibco.gxml.sa.processor.xslt.GxTransformBuilder; 
035 import com.tibco.gxml.sa.processor.xslt.GxTransformer; 
036 import com.tibco.gxml.sa.processor.xslt.XSLTransformBuilder; 
037 import com.tibco.gxmlsa.processor.org.exslt.strings.ExsltStringsFunctionGroup; 
038  
039 public abstract class XSLTSample<I, U, N extends I, A extends I, S, T, X> extends SampleApp<I, U, N, A, S, T, X> 
040 { 
041     public void testExample() throws ParserConfigurationException, IOException, GxException, ExprException, URISyntaxException 
042     { 
043         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
044         final GxMetaBridge<A, S, T> metaBridge = pcx.getMetaBridge(); 
045         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
046  
047         final Resolver resolver = getResolver(); 
048  
049         final URI xmlSystemId = new URI("hotel.xml"); 
050         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(xmlSystemId); 
051  
052         final GxDocumentBuilderFactory<N, S> f = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
053         f.setIgnoreComments(false); 
054  
055         final GxDocumentBuilder<N> builder = f.newDocumentBuilder(); 
056  
057         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
058  
059         final URI xslSystemId = new URI("hotel.xsl"); 
060         final Resolved<InputStream> xslInput = resolver.resolveInputStream(xslSystemId); 
061  
062         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>(pcx); 
063  
064         compiler.setCompatibleMode(true); 
065         // compiler.setRestrictedMode(true); // XSLT 2.0 subset for mapper. 
066  
067         // Specify the static type for the context item: 
068         // document-node(element(*,xs:untyped)) 
069         final T documentType = metaBridge.documentType(metaBridge.elementType(new SmName<S>(null, null, nameBridge), metaBridge.getType(SmNativeType.UNTYPED), false)); 
070         compiler.setFocus(documentType); 
071  
072         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), xslInput.getSystemId()); 
073  
074         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
075  
076         // TODO: Extract output configuration. 
077         // compiled.configure(sf); 
078  
079         sf.setIndent(true); 
080  
081         final StringWriter w = new StringWriter(); 
082  
083         final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w); 
084  
085         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
086  
087         transformer.transform(document, pcx, handler); 
088  
089         @SuppressWarnings("unused") 
090         final String s = w.toString(); 
091         // System.out.println(s); 
092     } 
093  
094     @SuppressWarnings("unused") 
095     private void bar(final GxProcessingContext<I, U, N, A, S, T, X> pcx) 
096     { 
097         try 
098         { 
099             final GxTransformBuilder<I, U, N, A, S, T, X> builder = new XSLTransformBuilder<I, U, N, A, S, T, X>(pcx); 
100  
101             final GxTransform<I, U, N, A, S, T, X> transform = builder.prepareTransform(new StringReader("<x xsl:version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'></x>"), new URI("")); 
102  
103             final GxTransformer<I, U, N, A, S, T, X> transformer = transform.newTransformer(); 
104  
105             final N document = transformer.transform(null, pcx); 
106  
107             final GxModel<N, A, S, T> model = pcx.getModel(); 
108  
109             final N element = model.getFirstChild(document); 
110  
111             final String name = model.getLocalNameAsString(element); 
112  
113             // System.out.println("XSLT: " + name); 
114         } 
115         catch (final Throwable e) 
116         { 
117             e.printStackTrace(); 
118         } 
119     } 
120  
121     public void skipVariableBinding() throws ParserConfigurationException, IOException, GxException, ExprException, URISyntaxException 
122     { 
123         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
124  
125         final Resolver resolver = getResolver(); 
126  
127         final URI xslSystemId = new URI("email.xsl"); 
128         final Resolved<InputStream> xslInput = resolver.resolveInputStream(xslSystemId); 
129  
130         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>(pcx); 
131  
132         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), xslInput.getSystemId()); 
133  
134         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
135  
136         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
137         final SmName<S> varName = nameBridge.name(new QName("to")); 
138         final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 
139         final X value = valueBridge.stringValue("David"); 
140  
141         transformer.bindVariableValue(varName, value); 
142         transformer.bindVariableValue(nameBridge.name(new QName("http://www.example.com", "from")), valueBridge.stringValue("Julie")); 
143  
144         final N documentNode = transformer.transform(null, pcx); 
145  
146         final GxModel<N, A, S, T> model = pcx.getModel(); 
147  
148         assertEquals(NodeKind.DOCUMENT, model.getNodeKind(documentNode)); 
149         final N email = model.getFirstChildElement(documentNode); 
150         final N to = model.getFirstChildElementByName(email, nameBridge.symbolize("http://www.example.com"), nameBridge.symbolize("to")); 
151         assertEquals("David", model.getStringValue(to)); 
152         final N from = model.getFirstChildElementByName(email, null, nameBridge.symbolize("from")); 
153         assertEquals("Julie", model.getStringValue(from)); 
154         final N again = model.getFirstChildElementByName(email, nameBridge.symbolize("http://www.example.com"), null); 
155         assertEquals("David", model.getStringValue(again)); 
156     } 
157  
158     public void skipExternalFunctions() throws ParserConfigurationException, IOException, GxException, ExprException, URISyntaxException 
159     { 
160         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
161  
162         final Resolver resolver = getResolver(); 
163  
164         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(new URI("exslt.xml")); 
165  
166         final GxDocumentBuilderFactory<N, S> f = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
167  
168         final GxDocumentBuilder<N> builder = f.newDocumentBuilder(); 
169  
170         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
171  
172         final Resolved<InputStream> xslInput = resolver.resolveInputStream(new URI("exslt.xsl")); 
173  
174         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>(pcx); 
175  
176         final String namespaceURI = "http://exslt.org/strings"; 
177         final ExsltStringsFunctionGroup<I, U, N, A, S, T, X> functions = new ExsltStringsFunctionGroup<I, U, N, A, S, T, X>(namespaceURI, pcx); 
178         compiler.setFunctionSigns(namespaceURI, functions); 
179         compiler.setFunctionImpls(namespaceURI, functions); 
180  
181         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), xslInput.getSystemId()); 
182  
183         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
184  
185         // TODO: Extract configuration. 
186         // compiled.configure(sf); 
187  
188         sf.setIndent(true); 
189  
190         final StringWriter w = new StringWriter(); 
191  
192         final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w); 
193  
194         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
195  
196         transformer.transform(document, pcx, handler); 
197  
198         // System.out.println(w.toString()); 
199     } 
200  
201     public void skipHotel() throws ParserConfigurationException, IOException, GxException, ExprException, URISyntaxException 
202     { 
203         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
204  
205         final Resolver resolver = getResolver(); 
206  
207         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(new URI("hotel.xml")); 
208  
209         final GxDocumentBuilderFactory<N, S> f = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
210  
211         final GxDocumentBuilder<N> builder = f.newDocumentBuilder(); 
212  
213         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
214  
215         final Resolved<InputStream> xslInput = resolver.resolveInputStream(new URI("hotel.xsl")); 
216  
217         final GxTransformBuilder<I, U, N, A, S, T, X> compiler = new XSLTransformBuilder<I, U, N, A, S, T, X>(pcx); 
218  
219         final GxTransform<I, U, N, A, S, T, X> compiled = compiler.prepareTransform(xslInput.getResource(), xslInput.getSystemId()); 
220  
221         final GxTransformer<I, U, N, A, S, T, X> transformer = compiled.newTransformer(); 
222         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
223         final SmName<S> varName = nameBridge.name(new QName("MessageData")); 
224         final GxVariantBridge<I, N, A, X> valueBridge = pcx.getVariantBridge(); 
225         final X value = valueBridge.node(document); 
226  
227         transformer.bindVariableValue(varName, value); 
228  
229         final N documentNode = transformer.transform(null, pcx); 
230  
231         final GxModel<N, A, S, T> model = pcx.getModel(); 
232  
233         assertEquals(NodeKind.DOCUMENT, model.getNodeKind(documentNode)); 
234         final N searchHotelRequest = model.getFirstChildElement(documentNode); 
235         final N parameters = model.getFirstChildElementByName(searchHotelRequest, nameBridge.symbolize("http://xmlns.example.com/1189038295781"), nameBridge.symbolize("parameters")); 
236         final N searchHotel = model.getFirstChildElementByName(parameters, nameBridge.symbolize("http://www.xyzcorp/procureservice/QueryGDS_Europe/"), nameBridge.symbolize("searchHotel")); 
237         final N country = model.getFirstChildElementByName(searchHotel, nameBridge.symbolize("http://www.xyzcorp/procureservice/QueryGDS_Europe/"), nameBridge.symbolize("country")); 
238         assertEquals("USA", model.getStringValue(country)); 
239     } 
240 }