Parsing a Character Stream and a Byte Stream
001 package org.gxml.book.parsing; 002 003 import java.io.InputStream; 004 import java.io.Reader; 005 import java.io.StringReader; 006 import java.net.URI; 007 008 import org.gxml.book.common.SampleApp; 009 import org.gxml.sa.GxModel; 010 import org.gxml.sa.GxNameBridge; 011 import org.gxml.sa.GxProcessingContext; 012 import org.gxml.xdm.NodeKind; 013 import org.gxml.xdm.Resolved; 014 import org.gxml.xdm.Resolver; 015 016 import com.tibco.gxml.sa.common.helpers.DocumentBuilderFactory; 017 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilder; 018 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilderFactory; 019 020 public abstract class BookIntroParsingSample<I, U, N extends I, A extends I, S, T, X> extends SampleApp<I, U, N, A, S, T, X> 021 { 022 public void testCharacterStreamParse() throws Exception 023 { 024 final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 025 026 final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 027 028 final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 029 030 final String xmlString = "<e>123</e>"; 031 final URI systemId = new URI("e.xml"); 032 final Reader characterStream = new StringReader(xmlString); 033 final N doc = builder.parse(characterStream, systemId); 034 035 final GxModel<N, A, S, T> model = pcx.getModel(); 036 037 assertEquals(NodeKind.DOCUMENT, model.getNodeKind(doc)); 038 039 final N e = model.getFirstChildElement(doc); 040 assertEquals(NodeKind.ELEMENT, model.getNodeKind(e)); 041 assertEquals("e", model.getLocalNameAsString(e)); 042 assertEquals("123", model.getStringValue(e)); 043 } 044 045 public void testByteStreamParse() throws Exception 046 { 047 final Resolver resolver = getResolver(); 048 049 final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 050 051 final URI systemId = new URI("email.xml"); 052 final Resolved<InputStream> source = resolver.resolveInputStream(systemId); 053 054 final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 055 056 final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 057 058 final N document = builder.parse(source.getResource(), source.getSystemId()); 059 060 final GxModel<N, A, S, T> model = pcx.getModel(); 061 062 assertEquals(NodeKind.DOCUMENT, model.getNodeKind(document)); 063 064 final N email = model.getFirstChildElement(document); 065 assertEquals(NodeKind.ELEMENT, model.getNodeKind(email)); 066 assertEquals("email", model.getLocalNameAsString(email)); 067 final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 068 final S namespaceURI = nameBridge.symbolize("http://www.example.com"); 069 final S localName = nameBridge.symbolize("from"); 070 final N from = model.getFirstChildElementByName(email, namespaceURI, localName); 071 assertEquals("Julie", model.getStringValue(from)); 072 073 for (final N node : model.getDescendantOrSelfAxis(document)) 074 { 075 assertNodeSymbolSemantics(node, pcx); 076 } 077 } 078 }
Copyright © Cloud Software Group, Inc. All rights reserved.