Mutation

001 package org.gxml.book.mutable; 
002  
003 import java.math.BigDecimal; 
004  
005 import javax.xml.XMLConstants; 
006  
007 import org.gxml.book.common.MutableApp; 
008 import org.gxml.sa.GxAtomBridge; 
009 import org.gxml.sa.GxNameBridge; 
010 import org.gxml.sa.mutable.GxModelMutable; 
011 import org.gxml.sa.mutable.GxProcessingContextMutable; 
012 import org.gxml.xdm.NodeKind; 
013  
014 /** 
015  * This sample illustrates the use of the optional mutability API. 
016  * 
017  * @author dholmes 
018  * 
019  * @param <I> 
020  * @param <U> 
021  * @param <N> 
022  * @param <A> 
023  * @param <S> 
024  * @param <T> 
025  * @param <X> 
026  */ 
027 public abstract class MutableSample<I, U, N extends I, A extends I, S, T, X> extends 
MutableApp<I, U, N, A, S, T, X> 
028 { 
029     /** 
030      * This is a test of basic mutability through the optional mutability API. 
031      * Line 2 
032      * Line 3 
033      * Line 4 // OK 
034      */ 
035     public void testIntroduction() throws Exception 
036     { 
037         final GxProcessingContextMutable<I, U, N, A, S, T, X> pcx = newProcessingContext(); 
038         final GxAtomBridge<A, S> atomBridge = pcx.getAtomBridge(); 
039         final GxNameBridge<S> nameBridge = pcx.getNameBridge(); 
040  
041         /* // Create a new document. */ 
042         final N documentNode = pcx.newDocument(); 
043  
044         final GxModelMutable<N, A, S, T> model = pcx.getModel(); 
045  
046         assertEquals(NodeKind.DOCUMENT, model.getNodeKind(documentNode)); 
047  
048         // Every node in the tree has an owner which is a document node. /* OK */ 
049         final N owner = model.getOwner(documentNode); 
050  
051         assertTrue(model.isSameNode(documentNode, owner)); 
052  
053         final S namespaceURI = nameBridge.symbolize("http://www.example.com"); 
054         final S localName = nameBridge.symbolize("foo"); 
055         final String prefix = "x"; 
056         final N documentElement = model.createElement(owner, namespaceURI, localName, prefix); 
057  
058         // Append the document element to the documentNode. 
059         model.appendChild(documentNode, documentElement); 
060  
061         model.setNamespace(documentElement, prefix, namespaceURI); 
062  
063         model.setAttribute(documentElement, nameBridge.empty(), nameBridge.symbolize("version"), 
XMLConstants.DEFAULT_NS_PREFIX, atomBridge.wrapAtom(atomBridge.createDecimal
(BigDecimal.valueOf(2.7)))); 
064  
065         // Append four text nodes to the document element. 
066         model.appendChild(documentElement, model.createText(owner, "Hello")); 
067         model.appendChild(documentElement, model.createText(owner, " ")); 
068         model.appendChild(documentElement, model.createText(owner, "World")); 
069         model.appendChild(documentElement, model.createText(owner, "!")); 
070  
071         // Compress the four contiguous text nodes into a single text node. 
072         model.normalize(documentNode); 
073  
074         @SuppressWarnings("unused") 
075         final String strval = serialize(documentNode, pcx); 
076         //System.out.println(strval); 
077     } 
078 }