Validating
001 package org.gxml.book.parsing; 002 003 import java.io.InputStream; 004 import java.net.URI; 005 006 import javax.xml.namespace.QName; 007 008 import org.gxml.book.common.SampleApp; 009 import org.gxml.sa.GxApplication; 010 import org.gxml.sa.GxAtomBridge; 011 import org.gxml.sa.GxModel; 012 import org.gxml.sa.GxNameBridge; 013 import org.gxml.sa.GxProcessingContext; 014 import org.gxml.xdm.Resolved; 015 import org.gxml.xdm.Resolver; 016 import org.gxml.xs.SmComponentBag; 017 import org.gxml.xs.SmExceptionCatcher; 018 import org.gxml.xs.SmMetaLoadArgs; 019 import org.gxml.xs.SmName; 020 021 import com.tibco.gxml.sa.common.helpers.DocumentBuilderFactory; 022 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilder; 023 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilderFactory; 024 import com.tibco.gxml.sa.common.helpers.SmAtomBridgeOnGxAtomBridgeAdapter; 025 import com.tibco.gxml.xs.W3cXmlSchemaParser; 026 027 public abstract class BookValidatingParsingSample<I, U, N extends I, A extends I, S, T, X> extends SampleApp<I, U, N, A, S, T, X> 028 { 029 public void testValidatingParse() throws Exception 030 { 031 final GxApplication<I, U, N, A, S, T, X> app = this; 032 033 final Resolver resolver = app.getResolver(); 034 035 final SmMetaLoadArgs args = new SmMetaLoadArgs(); 036 037 final SmExceptionCatcher errors = new SmExceptionCatcher(); 038 039 final GxProcessingContext<I, U, N, A, S, T, X> pcx = app.newProcessingContext(); 040 041 final Resolved<InputStream> resource = getResolver().resolveInputStream(new URI ("email.xsd")); 042 043 final W3cXmlSchemaParser<A, S> parser = new W3cXmlSchemaParser<A, S> (new SmAtomBridgeOnGxAtomBridgeAdapter<A, S>(pcx.getAtomBridge())); 044 045 final SmComponentBag<A, S> components = parser.parse(resource.getLocation(), resource.getResource(), resource.getSystemId(), errors, args, pcx); 046 047 pcx.register(components); 048 049 pcx.lock(); 050 051 final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 052 053 assertEquals(0, errors.size()); 054 055 final URI xmlURI = new URI("email.xml"); 056 final Resolved<InputStream> xmlInput = resolver.resolveInputStream(xmlURI); 057 058 final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 059 060 // Enable validation of the XML input. 061 factory.setValidating(true, nameBridge.name(new QName("http://www.example.com", "email"))); 062 063 final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 064 065 // TODO: Need to catch errors... 066 // builder.setExceptionHandler(errors); 067 068 final N doc = builder.parse(xmlInput.getResource(), xmlInput.getSystemId()); 069 070 assertEquals(0, errors.size()); 071 072 // System.out.println(serialize(doc, pcx)); 073 074 final GxModel<N, A, S, T> model = pcx.getModel(); 075 final GxAtomBridge<A, S> atomBridge = pcx.getAtomBridge(); 076 077 final N email = model.getFirstChildElement(doc); 078 final S namespaceURI = nameBridge.symbolize("http://www.example.com"); 079 final N sent = model.getFirstChildElementByName(email, namespaceURI, nameBridge.symbolize("sent")); 080 assertNotNull("model.getFirstChildElementByName", sent); 081 final SmName<S> typeName = model.getTypeName(sent); 082 assertNotNull("model.getTypeName", typeName); 083 assertEquals("dateTime", typeName.toQName().getLocalPart()); 084 final A dateTime = model.getTypedValue(sent).get(0); 085 086 // assertTrue(metaBridge.sameAs(metaBridge.handle(pcx.getTypeDefinition(type)), 087 // metaBridge.getType(SmNativeType.DATETIME))); 088 089 assertEquals("2008-03-23T14:49:30-05:00", atomBridge.getC14NForm(dateTime)); 090 } 091 }
Copyright © 2022. Cloud Software Group, Inc. All Rights Reserved.