Validation

001 package org.gxml.book.validation; 
002  
003 import java.io.InputStream; 
004 import java.net.URI; 
005 import java.util.LinkedList; 
006 import java.util.List; 
007  
008 import org.gxml.book.common.SampleApp; 
009 import org.gxml.sa.GxFragmentBuilder; 
010 import org.gxml.sa.GxModel; 
011 import org.gxml.sa.GxProcessingContext; 
012 import org.gxml.xdm.Resolved; 
013 import org.gxml.xdm.Resolver; 
014 import org.gxml.xs.SmException; 
015 import org.gxml.xs.SmExceptionCatcher; 
016 import org.gxml.xs.SmExceptionHandler; 
017 import org.gxml.xs.SmMetaLoadArgs; 
018  
019 import com.tibco.gxml.sa.common.helpers.DocumentBuilderFactory; 
020 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilder; 
021 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilderFactory; 
022 import com.tibco.gxml.sa.common.helpers.SmAtomBridgeOnGxAtomBridgeAdapter; 
023 import com.tibco.gxml.sa.processor.validation.GxContentValidator; 
024 import com.tibco.gxml.sa.processor.validation.GxValidatorCache; 
025 import com.tibco.gxml.sa.processor.validation.GxValidatorCacheFactory; 
026 import com.tibco.gxml.sa.processor.validation.ValidatorCacheFactory; 
027 import com.tibco.gxml.xs.W3cXmlSchemaParser; 
028  
029 public abstract class ValidationSample<I, U, N extends I, A extends I, S, T, X> extends SampleApp<I, U, N, A, S, T, X> 
030 { 
031     public void testByteStreamValidation() throws Exception 
032     { 
033         // Load a top-level schema into the processing context. 
034         final List<Resolved<InputStream>> resources = new LinkedList<Resolved<InputStream>>(); 
035         resources.add(getResolver().resolveInputStream(new URI("PurchaseOrder.xsd"))); 
036  
037         final SmExceptionCatcher errors = new SmExceptionCatcher(); 
038         final SmMetaLoadArgs args = new SmMetaLoadArgs(); 
039  
040         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
041  
042         final W3cXmlSchemaParser<A, S> parser = new W3cXmlSchemaParser<A, S>(new SmAtomBridgeOnGxAtomBridgeAdapter<A, S>(pcx.getAtomBridge())); 
043  
044         for (final Resolved<InputStream> resource : resources) 
045         { 
046             pcx.register(parser.parse(resource.getLocation(), resource.getResource(), resource.getSystemId(), errors, args, pcx)); 
047         } 
048  
049         pcx.lock(); 
050  
051         // Create a validator... 
052         final GxValidatorCacheFactory<A, S, T> vcf = new ValidatorCacheFactory<I, U, N, A, S, T, X>(pcx); 
053         final GxValidatorCache<A, S, T> vc = vcf.newValidatorCache(); 
054         final GxContentValidator<A, S, T> validator = vc.newContentValidator(); 
055  
056         // Set the downstream event handler which contains annotations and typed content. 
057         // validator.setGxContentHandler(/* ...*/null); 
058         validator.setExceptionHandler(errors); 
059  
060         // The document node that we wish to validate. 
061         final Resolved<InputStream> xmlInput = getResolver().resolveInputStream(new URI("PurchaseOrder.xml")); 
062  
063         final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
064  
065         final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 
066  
067         final N document = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
068  
069         // Stream the document into the validator. 
070         final GxModel<N, A, S, T> model = pcx.getModel(); 
071  
072         model.stream(document, true, true, validator); 
073  
074         if (errors.size() > 0) 
075         { 
076             // You've got errors.' 
077         } 
078     } 
079  
080     public void testTreeValidation() throws Exception 
081     { 
082         final Resolver resolver = getResolver(); 
083  
084         // Load a top-level schema into the processing context. 
085         final List<Resolved<InputStream>> resources = new LinkedList<Resolved<InputStream>>(); 
086         resources.add(getResolver().resolveInputStream(new URI("PurchaseOrder.xsd"))); 
087  
088         final SmExceptionCatcher errors = new SmExceptionCatcher(); 
089         final SmMetaLoadArgs args = new SmMetaLoadArgs(); 
090  
091         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
092         final W3cXmlSchemaParser<A, S> parser = new W3cXmlSchemaParser<A, S>(new SmAtomBridgeOnGxAtomBridgeAdapter<A, S>(pcx.getAtomBridge())); 
093         for (final Resolved<InputStream> resource : resources) 
094         { 
095             pcx.register(parser.parse(resource.getLocation(), resource.getResource(), resource.getSystemId(), errors, args, pcx)); 
096         } 
097         pcx.lock(); 
098         // The document node that we wish to validate. 
099         @SuppressWarnings("unused") 
100         final URI xmlLocation = new URI("PurchaseOrder.xml"); 
101         final URI xmlSystemId = new URI("PurchaseOrder.xml"); 
102         final Resolved<InputStream> xmlInput = resolver.resolveInputStream(xmlSystemId); 
103  
104         final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
105  
106         final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 
107  
108         final N documentIn = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 
109  
110         @SuppressWarnings("unused") 
111         final N documentOut = validate(documentIn, errors, pcx); 
112  
113         if (errors.size() > 0) 
114         { 
115             // You've got errors.' 
116             for (@SuppressWarnings("unused") 
117             final SmException error : errors) 
118             { 
119                 // System.out.println(error.getLocalizedMessage()); 
120             } 
121         } 
122     } 
123  
124     /** 
125      * This static function illustrates a helper function for validating a document tree. <br/> 
126      * Note that we assume that the processing context is already loaded with meta-data. 
127      * 
128      * @param node 
129      *            The input document. 
130      * @param errors 
131      *            The error handler. 
132      * @param pcx 
133      *            The processing context. 
134      */ 
135     public static <I, U, N extends I, A extends I, S, T, X> N validate(final N node, final SmExceptionHandler errors, final GxProcessingContext<I, U, N, A, S, T, X> pcx) 
136     { 
137         final GxValidatorCacheFactory<A, S, T> vcf = new ValidatorCacheFactory<I, U, N, A, S, T, X>(pcx); 
138  
139         // We already have a tree as input so we'll use the content validator' 
140         // and stream the document in as a bunch of events (a bit like SAX, but not lexical). 
141         final GxValidatorCache<A, S, T> vc = vcf.newValidatorCache(); 
142  
143         final GxContentValidator<A, S, T> validator = vc.newContentValidator(); 
144  
145         validator.setExceptionHandler(errors); 
146  
147         final GxModel<N, A, S, T> model = pcx.getModel(); 
148  
149         // We want to produce a node so we'll need a fragment builder at the output.' 
150         final GxFragmentBuilder<N, A, S, T> builder = pcx.newFragmentBuilder(); 
151  
152         // Connect the pieces together so that the validation output builds a tree. 
153         validator.setGxContentHandler(builder); 
154  
155         // Make it so! 
156         model.stream(node, true, true, validator); 
157  
158         // Practice safe coding: We don't know what might happen if there are errors.' 
159         final List<? extends N> nodes = builder.getNodes(); 
160         if (nodes.size() > 0) 
161         { 
162             return nodes.get(0); 
163         } 
164         else 
165         { 
166             return null; 
167         } 
168     } 
169 }