| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.parser.Location; |
| 12 |
import net.sf.saxon.lib.StandardURIResolver; |
| 13 |
import net.sf.saxon.om.NodeName; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.util.ProcInstParser; |
| 16 |
import net.sf.saxon.type.SchemaType; |
| 17 |
|
| 18 |
import javax.xml.transform.Source; |
| 19 |
import javax.xml.transform.TransformerException; |
| 20 |
import javax.xml.transform.URIResolver; |
| 21 |
import javax.xml.transform.sax.SAXSource; |
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.Comparator; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class PIGrabber extends ProxyReceiver { |
| 33 |
|
| 34 |
private Configuration config = null; |
| 35 |
private String reqMedia = null; |
| 36 |
private String reqTitle = null; |
| 37 |
private String baseURI = null; |
| 38 |
private URIResolver uriResolver = null; |
| 39 |
private List<String> stylesheets = new ArrayList<>(); |
| 40 |
private boolean terminated = false; |
| 41 |
|
| 42 |
public PIGrabber(Receiver next) { |
| 43 |
super(next); |
| 44 |
} |
| 45 |
|
| 46 |
public void setFactory(Configuration config) { |
| 47 |
this.config = config; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public void setCriteria(String media, String title) { |
| 60 |
this.reqMedia = media; |
| 61 |
this.reqTitle = title; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public void setBaseURI(String uri) { |
| 71 |
baseURI = uri; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void setURIResolver(URIResolver resolver) { |
| 81 |
uriResolver = resolver; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public void startElement(NodeName namecode, SchemaType typecode, Location location, int properties) |
| 89 |
throws XPathException { |
| 90 |
terminated = true; |
| 91 |
|
| 92 |
throw new XPathException("#start#"); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public boolean isTerminated() { |
| 103 |
return terminated; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) |
| 111 |
throws XPathException { |
| 112 |
if (target.equals("xml-stylesheet")) { |
| 113 |
|
| 114 |
String value = data.toString(); |
| 115 |
String piMedia = ProcInstParser.getPseudoAttribute(value, "media"); |
| 116 |
String piTitle = ProcInstParser.getPseudoAttribute(value, "title"); |
| 117 |
String piType = ProcInstParser.getPseudoAttribute(value, "type"); |
| 118 |
String piAlternate = ProcInstParser.getPseudoAttribute(value, "alternate"); |
| 119 |
|
| 120 |
if (piType == null) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if ((piType.equals("text/xml") || piType.equals("application/xml") || |
| 127 |
piType.equals("text/xsl") || piType.equals("applicaton/xsl") || piType.equals("application/xml+xslt")) && |
| 128 |
|
| 129 |
(reqMedia == null || piMedia == null || |
| 130 |
getConfiguration().getMediaQueryEvaluator().compare(piMedia, reqMedia) == 0) && |
| 131 |
|
| 132 |
((piTitle == null && (piAlternate == null || piAlternate.equals("no"))) || |
| 133 |
(reqTitle == null) || |
| 134 |
(piTitle != null && piTitle.equals(reqTitle)))) { |
| 135 |
String href = ProcInstParser.getPseudoAttribute(value, "href"); |
| 136 |
if (href == null) { |
| 137 |
throw new XPathException("xml-stylesheet PI has no href attribute"); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (piTitle == null && (piAlternate == null || piAlternate.equals("no"))) { |
| 142 |
stylesheets.add(0, href); |
| 143 |
} else { |
| 144 |
stylesheets.add(href); |
| 145 |
} |
| 146 |
} else { |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public Source[] getAssociatedStylesheets() throws TransformerException { |
| 162 |
if (stylesheets.isEmpty()) { |
| 163 |
return null; |
| 164 |
} |
| 165 |
if (uriResolver == null) { |
| 166 |
uriResolver = new StandardURIResolver(config); |
| 167 |
} |
| 168 |
Source[] result = new Source[stylesheets.size()]; |
| 169 |
for (int i = 0; i < stylesheets.size(); i++) { |
| 170 |
String href = stylesheets.get(i); |
| 171 |
Source s = uriResolver.resolve(href, baseURI); |
| 172 |
if (s instanceof SAXSource) { |
| 173 |
((SAXSource) s).setXMLReader(config.getStyleParser()); |
| 174 |
} |
| 175 |
if (s == null) { |
| 176 |
s = config.getSystemURIResolver().resolve(href, baseURI); |
| 177 |
} |
| 178 |
result[i] = s; |
| 179 |
} |
| 180 |
return result; |
| 181 |
} |
| 182 |
|
| 183 |
} |