001 import javax.xml.transform.TransformerException; 002 003 import org.apache.xpath.XPathAPI; 004 005 import org.w3c.dom.DOMConfiguration; 006 import org.w3c.dom.DOMImplementation; 007 import org.w3c.dom.Document; 008 import org.w3c.dom.DocumentFragment; 009 import org.w3c.dom.NodeList; 010 import org.w3c.dom.bootstrap.DOMImplementationRegistry; 011 import org.w3c.dom.ls.DOMImplementationLS; 012 import org.w3c.dom.ls.LSParser; 013 import org.w3c.dom.ls.LSSerializer; 014 import org.w3c.dom.ranges.DocumentRange; 015 import org.w3c.dom.ranges.Range; 016 017 /** 018 * This class provides demonstrates the DOM Range interface and 019 * the Load and Save Serializer Interface 020 * 021 * The XPath API was taken from Xalan since it didn't work yet. 022 * 023 * @author Udo Altmann 024 * @version 1.0 (2005) 025 * 026 */ 027 public class RangeDemo { 028 029 /** 030 * @param args xmlfile xpath1 xpath2 031 * @throws ClassCastException 032 * @throws ClassNotFoundException 033 * @throws InstantiationException 034 * @throws IllegalAccessException 035 * @throws TransformerException 036 * 037 */ 038 public static void main(String[] args) throws ClassCastException, ClassNotFoundException, InstantiationException, IllegalAccessException, TransformerException { 039 040 if(args.length< 3){ 041 System.out.println("RangeDemo xmlfile xpath1 xpath2"); 042 return; 043 } 044 System.setProperty(DOMImplementationRegistry.PROPERTY, 045 "org.apache.xerces.dom.DOMImplementationSourceImpl"); 046 047 // Class from org.w3c.dom.bootstrap 048 DOMImplementationRegistry registry; 049 registry = DOMImplementationRegistry.newInstance(); 050 051 // Require Load/Save and Range 052 DOMImplementationLS impl = (DOMImplementationLS) registry 053 .getDOMImplementation("LS Range"); 054 055 LSParser parser = impl.createLSParser( 056 DOMImplementationLS.MODE_SYNCHRONOUS, null); 057 058 DOMConfiguration conf = parser.getDomConfig(); 059 060 // Set Parameters 061 conf.setParameter("validate-if-schema", new Boolean(true)); 062 conf.setParameter("cdata-sections", new Boolean(false)); 063 conf.setParameter("entities", new Boolean(false)); 064 Document doc = parser.parseURI(args[0]); 065 066 if(((DOMImplementation)impl).hasFeature("Range", "2.0")){ 067 // Interface is fetched from the Document object 068 DocumentRange dr = (DocumentRange)doc; 069 Range r = dr.createRange(); 070 071 NodeList nl; 072 073 // We don't care about the length of the NodeList, demo only use 074 // Set start and end 075 // The XPathAPI is a Non-DOM Xalan class 076 nl = XPathAPI.selectNodeList(doc, args[1]); 077 r.setStart(nl.item(0), 0); 078 079 nl = XPathAPI.selectNodeList(doc, args[2]); 080 r.setEnd(nl.item(0), 0); 081 082 // Create a DocumentFragment 083 DocumentFragment df = r.cloneContents(); 084 085 // Create a Serializer - see createLSParser 086 LSSerializer writer = impl.createLSSerializer(); 087 System.out.println(writer.writeToString(df)); 088 } else { 089 System.out.println("Range not supported"); 090 } 091 092 } 093 094 }