1 package com.icl.saxon; 2 import com.icl.saxon.om.ProcInstParser; 3 import org.xml.sax.*; 4 import org.xml.sax.helpers.*; 5 import java.net.URL ; 6 import java.util.Vector ; 7 import javax.xml.transform.sax.SAXSource ; 8 import javax.xml.transform.*; 9 10 16 17 public class PIGrabber extends DefaultHandler { 18 19 private String reqMedia = null; 20 private String reqTitle = null; 21 private String baseURI = null; 22 private URIResolver uriResolver = null; 23 private Vector stylesheets = new Vector (); 24 25 public void setCriteria(String media, String title, String charset) { 26 this.reqMedia = media; 27 this.reqTitle = title; 28 } 29 30 33 34 public void setBaseURI(String uri) { 35 baseURI = uri; 36 } 37 38 41 42 public void setURIResolver(URIResolver resolver) { 43 uriResolver = resolver; 44 } 45 46 49 50 public void startElement (String uri, String localName, 51 String qName, Attributes attributes) throws SAXException { 52 53 throw new SAXException("#start#"); 55 } 56 57 60 61 public void processingInstruction (String target, String data) 62 throws SAXException 63 { 64 if (target.equals("xml-stylesheet")) { 65 66 String piMedia = ProcInstParser.getPseudoAttribute(data, "media"); 67 String piTitle = ProcInstParser.getPseudoAttribute(data, "title"); 68 String piType = ProcInstParser.getPseudoAttribute(data, "type"); 69 String piAlternate = ProcInstParser.getPseudoAttribute(data, "alternate"); 70 71 if (piType==null) return; 72 73 75 if ( (piType.equals("text/xml") || piType.equals("application/xml") || 76 piType.equals("text/xsl") || piType.equals("applicaton/xsl")) && 77 78 (reqMedia==null || piMedia==null || reqMedia.equals(piMedia)) && 79 80 ( ( piTitle==null && (piAlternate==null || piAlternate.equals("no"))) || 81 ( reqTitle==null ) || 82 ( piTitle!=null && piTitle.equals(reqTitle) ) ) ) 83 { 84 String href = ProcInstParser.getPseudoAttribute(data, "href"); 85 if (href==null) { 86 throw new SAXException("xml-stylesheet PI has no href attribute"); 87 } 88 89 if (piTitle==null && (piAlternate==null || piAlternate.equals("no"))) { 91 stylesheets.insertElementAt(href, 0); 92 } else { 93 stylesheets.addElement(href); 94 } 95 } else { 96 } 98 } 99 } 100 101 106 107 public SAXSource [] getAssociatedStylesheets() throws TransformerException { 108 if (stylesheets.size()==0) { 109 return null; 110 } 111 if (uriResolver==null) { 112 uriResolver = new StandardURIResolver(); 113 } 114 SAXSource [] result = new SAXSource [stylesheets.size()]; 115 for (int i=0; i<stylesheets.size(); i++) { 116 String href = (String )stylesheets.elementAt(i); 117 Source s = uriResolver.resolve(href, baseURI); 118 if (!(s instanceof SAXSource )) { 119 throw new TransformerException("Associated stylesheet URI must yield a SAX source"); 120 } 121 result[i] = (SAXSource )s; 122 } 123 return result; 124 } 125 126 129 130 public String [] getStylesheetURIs() throws SAXException { 131 if (stylesheets.size()==0) { 132 return null; 133 } 134 String [] result = new String [stylesheets.size()]; 135 for (int i=0; i<stylesheets.size(); i++) { 136 result[i] = (String )stylesheets.elementAt(i); 137 } 138 return result; 139 } 140 141 } 142 | Popular Tags |