Navigation

001 package org.gxml.book.parsing; 
002  
003 import java.io.InputStream; 
004 import java.net.URI; 
005  
006 import org.gxml.book.common.SampleApp; 
007 import org.gxml.sa.GxModel; 
008 import org.gxml.sa.GxNameBridge; 
009 import org.gxml.sa.GxProcessingContext; 
010 import org.gxml.xdm.Resolved; 
011 import org.gxml.xdm.Resolver; 
012  
013 import com.tibco.gxml.sa.common.helpers.DocumentBuilderFactory; 
014 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilder; 
015 import com.tibco.gxml.sa.common.helpers.GxDocumentBuilderFactory; 
016  
017 public abstract class BookNavigationParsingSample<I, U, N extends I, A extends I, S, T, X> extends SampleApp<I, U, N, A, S, T, X> 
018 { 
019     public void testBooksByNealStephenson() throws Exception 
020     { 
021         final Resolver resolver = getResolver(); 
022  
023         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
024  
025         final URI systemId = new URI("books.xml"); 
026         final Resolved<InputStream> source = resolver.resolveInputStream(systemId); 
027  
028         final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
029  
030         final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 
031  
032         final N doc = builder.parse(source.getResource(), source.getSystemId()); 
033  
034         final GxModel<N, A, S, T> model = pcx.getModel(); 
035  
036         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
037  
038         final S namespaceURI = nameBridge.symbolize("http://www.example.com/books"); 
039  
040         final N inventory = model.getFirstChildElementByName(doc, namespaceURI, nameBridge.symbolize("inventory")); 
041  
042         for (final N book : model.getChildElementsByName(inventory, namespaceURI, nameBridge.symbolize("book"))) 
043         { 
044             boolean found = false; 
045  
046             for (final N author : model.getChildElementsByName(book, namespaceURI, nameBridge.symbolize("author"))) 
047             { 
048                 if (model.getStringValue(author).equals("Neal Stephenson")) 
049                 { 
050                     found = true; 
051                     break; 
052                 } 
053             } 
054  
055             if (found) 
056             { 
057                 final N title = model.getFirstChildElementByName(book, namespaceURI, nameBridge.symbolize("title")); 
058  
059                 System.out.println(model.getStringValue(title)); 
060             } 
061         } 
062     } 
063  
064     public void testPurchaseOrder() throws Exception 
065     { 
066         final Resolver resolver = getResolver(); 
067  
068         final GxProcessingContext<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
069         final GxModel<N, A, S, T> model = pcx.getModel(); 
070         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
071  
072         final URI systemId = new URI("PurchaseOrder.xml"); 
073         final Resolved<InputStream> source = resolver.resolveInputStream(systemId); 
074  
075         final GxDocumentBuilderFactory<N, S> factory = new DocumentBuilderFactory<I, U, N, A, S, T, X>(pcx); 
076  
077         final GxDocumentBuilder<N> builder = factory.newDocumentBuilder(); 
078  
079         final N po = builder.parse(source.getResource(), source.getSystemId()); 
080  
081         final N root = model.getFirstChildElement(po); 
082  
083         final N items = model.getFirstChildElementByName(root, null, nameBridge.symbolize("items")); 
084  
085         double total = 0; 
086         for (final N item : model.getChildElementsByName(items, null, nameBridge.symbolize("item"))) 
087         { 
088             System.out.println("partNum:" + model.getAttributeStringValue(item, nameBridge.empty(), nameBridge.symbolize("partNum"))); 
089  
090             final N price = model.getFirstChildElementByName(item, null, nameBridge.symbolize("USPrice")); 
091             total += Double.valueOf(model.getStringValue(price)).doubleValue(); 
092         } 
093         System.out.println("Grand total = " + total); 
094     } 
095 }