1 package net.sf.saxon; 2 import net.sf.saxon.event.CommentStripper; 3 import net.sf.saxon.event.ReceivingContentHandler; 4 import net.sf.saxon.event.StartTagBuffer; 5 import net.sf.saxon.style.StyleNodeFactory; 6 import net.sf.saxon.style.StylesheetStripper; 7 import net.sf.saxon.style.UseWhenFilter; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.tree.DocumentImpl; 10 import net.sf.saxon.tree.TreeBuilder; 11 import org.xml.sax.Locator ; 12 13 import javax.xml.transform.Templates ; 14 import javax.xml.transform.sax.TemplatesHandler ; 15 16 17 24 25 public class TemplatesHandlerImpl extends ReceivingContentHandler implements TemplatesHandler { 26 27 private TreeBuilder builder; 28 private StyleNodeFactory nodeFactory; 29 private Templates templates; 30 private String systemId; 31 32 37 38 protected TemplatesHandlerImpl(Configuration config) { 39 40 setPipelineConfiguration(config.makePipelineConfiguration()); 41 42 nodeFactory = new StyleNodeFactory(config); 43 44 builder = new TreeBuilder(); 45 builder.setPipelineConfiguration(getPipelineConfiguration()); 46 builder.setNodeFactory(nodeFactory); 47 builder.setLineNumbering(true); 48 49 StartTagBuffer startTagBuffer = new StartTagBuffer(); 50 51 UseWhenFilter useWhenFilter = new UseWhenFilter(startTagBuffer); 52 useWhenFilter.setUnderlyingReceiver(builder); 53 useWhenFilter.setPipelineConfiguration(getPipelineConfiguration()); 54 55 startTagBuffer.setUnderlyingReceiver(useWhenFilter); 56 startTagBuffer.setPipelineConfiguration(getPipelineConfiguration()); 57 58 StylesheetStripper styleStripper = new StylesheetStripper(); 59 styleStripper.setStylesheetRules(config.getNamePool()); 60 styleStripper.setUnderlyingReceiver(startTagBuffer); 61 styleStripper.setPipelineConfiguration(getPipelineConfiguration()); 62 63 CommentStripper commentStripper = new CommentStripper(); 64 commentStripper.setUnderlyingReceiver(styleStripper); 65 commentStripper.setPipelineConfiguration(getPipelineConfiguration()); 66 67 this.setReceiver(commentStripper); 68 69 } 70 71 74 75 public Templates getTemplates() { 76 if (templates==null) { 77 DocumentImpl doc = (DocumentImpl)builder.getCurrentRoot(); 78 if (doc==null) { 79 return null; 80 } 81 PreparedStylesheet sheet = new PreparedStylesheet(getConfiguration()); 82 try { 83 sheet.setStylesheetDocument(doc, nodeFactory); 84 templates = sheet; 85 } catch (XPathException tce) { 86 throw new UnsupportedOperationException (tce.getMessage()); 88 } 89 } 90 91 return templates; 92 } 93 94 97 98 public void setSystemId(String url) { 99 systemId = url; 100 builder.setSystemId(url); 101 } 102 103 106 107 public void setDocumentLocator (Locator locator) { 108 super.setDocumentLocator(locator); 109 setSystemId(locator.getSystemId()); 110 } 111 112 115 116 public String getSystemId() { 117 return systemId; 118 } 119 120 } 121 122 | Popular Tags |