Debian Codegraph @

net/sf/saxon/event/PIGrabber.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * The <tt>PIGrabber</tt> class is a {@link ProxyReceiver} that looks for {@code xml-stylesheet} processing
28 * instructions and tests whether they match specified criteria; for those that do, it creates
29 * a {@link Source} object referring to the relevant stylesheet
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 * Define the matching criteria
52 * @param media the required media. Note that Saxon does not implement the complex CSS3-based syntax for
53 * media queries. By default, the media value is simply ignored. An algorithm for
54 * comparing the requested media with the declared media can be defined using
55 * the method {@link Configuration#setMediaQueryEvaluator(Comparator)}.
56 * @param title the required title
57 */
58
59 public void setCriteria(String media, String title) {
60 this.reqMedia = media;
61 this.reqTitle = title;
62 }
63
64 /**
65 * Set the base URI
66 *
67 * @param uri the base URI
68 */
69
70 public void setBaseURI(String uri) {
71 baseURI = uri;
72 }
73
74 /**
75 * Set the URI resolver to be used for the href attribute
76 *
77 * @param resolver the URI resolver
78 */
79
80 public void setURIResolver(URIResolver resolver) {
81 uriResolver = resolver;
82 }
83
84 /**
85 * Abort the parse when the first start element tag is found
86 */
87
88 public void startElement(NodeName namecode, SchemaType typecode, Location location, int properties)
89 throws XPathException {
90 terminated = true;
91 // abort the parse when the first start element tag is found
92 throw new XPathException("#start#");
93 }
94
95 /**
96 * Determine whether the parse terminated because the first start element tag was found
97 *
98 * @return true if the parse was terminated when the document element was encountered (as distinct
99 * from being terminated because of some exception condition, for example a parse error)
100 */
101
102 public boolean isTerminated() {
103 return terminated;
104 }
105
106 /**
107 * Handle xml-stylesheet PI
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 // System.err.println("Found xml-stylesheet media=" + piMedia + " title=" + piTitle);
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) && // see bug 1729
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 // System.err.println("Adding " + href);
141 if (piTitle == null && (piAlternate == null || piAlternate.equals("no"))) {
142 stylesheets.add(0, href);
143 } else {
144 stylesheets.add(href);
145 }
146 } else {
147 //System.err.println("No match on required media=" + reqMedia + " title=" + reqTitle );
148 }
149 }
150 }
151
152 /**
153 * Return list of stylesheets that matched, as an array of Source objects
154 *
155 * @return null if there were no matching stylesheets.
156 * @throws net.sf.saxon.trans.XPathException
157 * if a URI cannot be resolved
158 */
159
160 /*@Nullable*/
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 }//