This section illustrates one way of using gXML. All gXML processors, including custom processing, run within a GxProcessingContext instance that provides necessary meta data. A GxProcessingContext instance in turn is created through a GxApplication instance. It is your responsibility to 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. This approach will allow you to write your application generically and then inject the choice of parameterization as late as possible for maximum code reuse and flexibility.This, of course, is not the only way to use gXML. An existing architecture may force the choice of parameterization and create silos of XML processing. The degree of integration in this case may be less that is possible with a homogeneous solution.Whatever the approach, the best way to use gXML is to write generic, parameterized, and XML processing code whenever possible.017 import com.tibco.gxml.sa.processor.serialization.api.GxSerializerFactory;018 import com.tibco.gxml.sa.processor.serialization.impl.SerializerFactory;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>026 return new SampleResolver(new URI("../../plugins/org.gxml.book/resources/foo.xml"));034 protected String serialize(final N node, final GxProcessingContext<I, U, N, A, S, T, X> pcx)036 final GxSerializerFactory<I, U, N, A, S, T, X> sf = new SerializerFactory<I, U, N, A, S, T, X>(pcx);043 final GxSequenceHandler<A, S, T> handler = sf.newSerializer(w);061 * Some bridge implementations may use {@link String} directly for symbols. They must make them behave according to064 public void assertNodeSymbolSemantics(final N node, final GxProcessingContext<I, U, N, A, S, T, X> pcx)074 assertSymbolSemantics(model.getLocalName(node), nameBridge);089 public void assertSymbolSemantics(final S symbol, final GxNameBridge<S> nameBridge)092 PreCondition.assertArgumentNotNull(nameBridge, "nameBridge");094 assertSame(symbol, nameBridge.symbolize(copy(symbol.toString())));098 * Do anything to manufacture a String that is equal, but not identical (the same), as the original.100 * This method has the post-condition that the strings are equal but not the same.109 final String copy = original.concat("junk").substring(0, original.length());110 // Post-conditions verify that this actually works and isn't "optimized" out.'A catalog provides the means to isolate your application from the physical location of file resources. Writing a catalog simply means implementing the GxCatalog interface so that it maps form the logical locations specified in code or XML resources to the corresponding physical location.028 * This default implementation requires that neither parameter be null, and performs the expected action to retrieve032 * the base URI against which the target is to be resolved; must not be null037 public Resolved<InputStream> resolveInputStream(final URI location) throws IOException055 private Resolved<InputStream> retrieve(final URI location, final URI uri) throws IOException063 final File canonFile = new File(uri.toString()).getCanonicalFile();083 return new Resolved<InputStream>(location, stream, toRetrieve.toURI());The final task in providing a concrete GxApplication class is to implement the newProcessingContext method on a derived class. This is where you get to choose the tree, atomic values, meta data and symbols that your application will use. In many cases you will use an off-the-shelf processing context class, but it is also possible to assemble your own variety or build one entirely from scratch.If you are going to use gXML with org.w3c.dom.Node, you still have choices for the atomic values that your system will use as well as the meta data implementation. This example uses atomic values that are mostly Java Wrapper types and the reference sequence type implementation, SmSequenceType.013 import com.tibco.gxml.sa.common.helpers.GxMetaBridgeOnSmMetaBridgeAdapter;014 import com.tibco.gxml.sa.common.helpers.SmAtomBridgeOnGxAtomBridgeAdapter;019 * Demonstration of constructing a concrete GxApplication(Mutable) implementation using the DOM processing context.021 public final class DomValidatingParsingSample extends BookValidatingParsingSample<Object, Object, Node, Object, String, SmSequenceType<Object, String>, Object> implements GxApplicationMutable<Object, Object, Node, Object, String, SmSequenceType<Object, String>, Object>023 public final GxProcessingContextMutable<Object, Object, Node, Object, String, SmSequenceType<Object, String>, Object> newProcessingContext()025 // The name bridge is created along with the processing context for maximum concurrency.026 final GxNameBridge<String> nameBridge = new StringNameBridge();027 final AtomBridge<String> atomBridge = new AtomBridge<String>(nameBridge);028 final SmMetaBridge<Object, String> cache = new SmMetaBridgeFactory<Object, String>(new SmAtomBridgeOnGxAtomBridgeAdapter<Object, String>(atomBridge)).newMetaBridge();029 final GxMetaBridge<Object, String, SmSequenceType<Object, String>> metaBridge = new GxMetaBridgeOnSmMetaBridgeAdapter<Object, String>(cache, atomBridge);031 final DomProcessingContext<Object, SmSequenceType<Object, String>> pcx = new DomProcessingContext<Object, SmSequenceType<Object, String>>(this, metaBridge, cache);
Copyright © TIBCO Software Inc. All Rights Reserved.