1 package net.sf.saxon.event; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.StandardURIResolver; 4 import net.sf.saxon.om.ProcInstParser; 5 import net.sf.saxon.trans.DynamicError; 6 import net.sf.saxon.trans.XPathException; 7 8 import javax.xml.transform.Source ; 9 import javax.xml.transform.TransformerException ; 10 import javax.xml.transform.URIResolver ; 11 import javax.xml.transform.sax.SAXSource ; 12 import java.util.ArrayList ; 13 14 20 21 public class PIGrabber extends ProxyReceiver { 22 23 private Configuration config = null; 24 private String reqMedia = null; 25 private String reqTitle = null; 26 private String baseURI = null; 27 private URIResolver uriResolver = null; 28 private ArrayList stylesheets = new ArrayList (); 29 private boolean terminated = false; 30 31 public void setFactory(Configuration config) { 32 this.config = config; 33 } 34 35 public void setCriteria(String media, String title, String charset) { 36 this.reqMedia = media; 37 this.reqTitle = title; 38 } 39 40 43 44 public void setBaseURI(String uri) { 45 baseURI = uri; 46 } 47 48 51 52 public void setURIResolver(URIResolver resolver) { 53 uriResolver = resolver; 54 } 55 56 public void open() { 57 nextReceiver = new Sink(); 58 } 59 60 63 64 public void startElement (int namecode, int typecode, int locationId, int properties) 65 throws XPathException { 66 terminated = true; 67 throw new DynamicError("#start#"); 69 } 70 71 74 75 public boolean isTerminated() { 76 return terminated; 77 } 78 79 82 83 public void processingInstruction(String target, CharSequence data, int locationId, int properties) 84 throws XPathException { 85 if (target.equals("xml-stylesheet")) { 86 87 String value = data.toString(); 88 String piMedia = ProcInstParser.getPseudoAttribute(value, "media"); 89 String piTitle = ProcInstParser.getPseudoAttribute(value, "title"); 90 String piType = ProcInstParser.getPseudoAttribute(value, "type"); 91 String piAlternate = ProcInstParser.getPseudoAttribute(value, "alternate"); 92 93 if (piType==null) return; 94 95 97 if ( (piType.equals("text/xml") || piType.equals("application/xml") || 98 piType.equals("text/xsl") || piType.equals("applicaton/xsl") || piType.equals("application.xml+xslt")) && 99 100 (reqMedia==null || piMedia==null || reqMedia.equals(piMedia)) && 101 102 ( ( piTitle==null && (piAlternate==null || piAlternate.equals("no"))) || 103 ( reqTitle==null ) || 104 ( piTitle!=null && piTitle.equals(reqTitle) ) ) ) 105 { 106 String href = ProcInstParser.getPseudoAttribute(value, "href"); 107 if (href==null) { 108 throw new DynamicError("xml-stylesheet PI has no href attribute"); 109 } 110 111 if (piTitle==null && (piAlternate==null || piAlternate.equals("no"))) { 113 stylesheets.add(0, href); 114 } else { 115 stylesheets.add(href); 116 } 117 } else { 118 } 120 } 121 } 122 123 128 129 public Source [] getAssociatedStylesheets() throws TransformerException { 130 if (stylesheets.size()==0) { 131 return null; 132 } 133 if (uriResolver==null) { 134 uriResolver = new StandardURIResolver(config); 135 } 136 Source [] result = new Source [stylesheets.size()]; 137 for (int i=0; i<stylesheets.size(); i++) { 138 String href = (String )stylesheets.get(i); 139 Source s = uriResolver.resolve(href, baseURI); 140 if (s instanceof SAXSource ) { 141 ((SAXSource )s).setXMLReader(config.getStyleParser()); 142 } 143 if (s == null) { 144 s = config.getSystemURIResolver().resolve(href, baseURI); 145 } 146 result[i] = s; 147 } 148 return result; 149 } 150 151 } 152 | Popular Tags |