Implementing GxApplication

You must write a class that provides an instance of GxApplication. The best way to do this is to write an abstract class that implements all but the newProcessingContext method of GxApplication.

001 package org.gxml.book.common; 
002  
003 import java.io.StringWriter; 
004 import java.net.URI; 
005 import java.net.URISyntaxException; 
006  
007 import junit.framework.TestCase; 
008  
009 import org.gxml.sa.GxApplication; 
010 import org.gxml.sa.GxModel; 
011 import org.gxml.sa.GxNameBridge; 
012 import org.gxml.sa.GxProcessingContext; 
013 import org.gxml.sa.GxSequenceHandler; 
014 import org.gxml.xdm.Resolver; 
015  
016 import com.tibco.gxml.sa.api.common.util.PreCondition; 
017 import com.tibco.gxml.sa.processor.serialization.api.GxSerializerFactory; 
018 import com.tibco.gxml.sa.processor.serialization.impl.SerializerFactory; 
019  
020 public abstract class SampleApp<I, U, N extends I, A extends I, S, T, X> extends TestCase implements GxApplication<I, U, N, A, S, T, X> 
021 { 
022     public Resolver getResolver() 
023     { 
024         try 
025         { 
026             return new SampleResolver(new URI("../../plugins/org.gxml.book/resources/foo.xml")); 
027         } 
028         catch (final URISyntaxException e) 
029         { 
030             throw new AssertionError(e); 
031         } 
032     } 
033  
034     protected String serialize(final N node, final GxProcessingContext<I, U, N, A, S, T, X> pcx) 
035     { 
036         final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx); 
037  
038         // Configure for "pretty" printing. 
039         sf.setIndent(Boolean.TRUE); 
040  
041         final StringWriter w = new StringWriter(); 
042  
043         final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w); 
044  
045         final GxModel<N, A, S, T> model = pcx.getModel(); 
046  
047         handler.startDocument(null); 
048         try 
049         { 
050             model.stream(node, true, true, handler); 
051         } 
052         finally 
053         { 
054             handler.endDocument(); 
055         } 
056  
057         return w.toString(); 
058     } 
059  
060     /** 
061      * Some bridge implementations may use {@link String} directly for symbols. They must make them behave according to 
062      * symbol semantics (==,toString). 
063      */ 
064     public void assertNodeSymbolSemantics(final N node, final GxProcessingContext<I, U, N, A, S, T, X> pcx) 
065     { 
066         final GxModel<N, A, S, T> model = pcx.getModel(); 
067         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
068  
069         switch (model.getNodeKind(node)) 
070         { 
071             case ELEMENT: 
072             { 
073                 assertSymbolSemantics(model.getNamespaceURI(node), nameBridge); 
074                 assertSymbolSemantics(model.getLocalName(node), nameBridge); 
075             } 
076             case TEXT: 
077             case DOCUMENT: 
078             { 
079  
080             } 
081             break; 
082             default: 
083             { 
084                 throw new AssertionError(model.getNodeKind(node)); 
085             } 
086         } 
087     } 
088  
089     public void assertSymbolSemantics(final S symbol, final GxNameBridge<S> nameBridge) 
090     { 
091         PreCondition.assertArgumentNotNull(symbol, "symbol"); 
092         PreCondition.assertArgumentNotNull(nameBridge, "nameBridge"); 
093         assertSame(symbol, nameBridge.symbolize(symbol.toString())); 
094         assertSame(symbol, nameBridge.symbolize(copy(symbol.toString()))); 
095     } 
096  
097     /** 
098      * Do anything to manufacture a String that is equal, but not identical (the same), as the original. 
099      * <p> 
100      * This method has the post-condition that the strings are equal but not the same. 
101      * </p> 
102      * 
103      * @param original 
104      *            The original. 
105      * @return A copy of the original string. 
106      */ 
107     private String copy(final String original) 
108     { 
109         final String copy = original.concat("junk").substring(0, original.length()); 
110         // Post-conditions verify that this actually works and isn't "optimized" out.' 
111         assertEquals(original, copy); 
112         assertNotSame(original, copy); 
113         // Be Paranoid 
114         assertTrue(original.equals(copy)); 
115         assertFalse(original == copy); 
116         // OK. That'll do.' 
117         return copy; 
118     } 
119 }