net.sf.saxon.event.PIGrabber Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Saxon-HE Show documentation
Show all versions of Saxon-HE Show documentation
The XSLT and XQuery Processor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2023 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.event;
import net.sf.saxon.Configuration;
import net.sf.saxon.functions.ResolveURI;
import net.sf.saxon.lib.DirectResourceResolver;
import net.sf.saxon.lib.ResourceRequest;
import net.sf.saxon.lib.ResourceResolver;
import net.sf.saxon.om.AttributeMap;
import net.sf.saxon.om.NamespaceMap;
import net.sf.saxon.om.NodeName;
import net.sf.saxon.s9api.Location;
import net.sf.saxon.str.UnicodeString;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.transpile.CSharpReplaceBody;
import net.sf.saxon.tree.util.ProcInstParser;
import net.sf.saxon.type.SchemaType;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* The PIGrabber
class is a {@link ProxyReceiver} that looks for {@code xml-stylesheet} processing
* instructions and tests whether they match specified criteria; for those that do, it creates
* a {@link Source} object referring to the relevant stylesheet
*/
public class PIGrabber extends ProxyReceiver {
private Configuration config = null;
private String reqMedia = null;
private String reqTitle = null;
private String baseURI = null;
private ResourceResolver resourceResolver = null;
private final List stylesheets = new ArrayList<>();
private boolean terminated = false;
public PIGrabber(Receiver next) {
super(next);
}
public void setFactory(Configuration config) {
this.config = config;
}
/**
* Define the matching criteria
* @param media the required media. Note that Saxon does not implement the complex CSS3-based syntax for
* media queries. By default, the media value is simply ignored. An algorithm for
* comparing the requested media with the declared media can be defined using
* the method {@link Configuration#setMediaQueryEvaluator(Comparator)}.
* @param title the required title
*/
public void setCriteria(String media, String title) {
this.reqMedia = media;
this.reqTitle = title;
}
/**
* Set the base URI
*
* @param uri the base URI
*/
public void setBaseURI(String uri) {
baseURI = uri;
}
/**
* Set the URI resolver to be used for the href attribute
*
* @param resolver the URI resolver
*/
public void setResourceResolver(ResourceResolver resolver) {
resourceResolver = resolver;
}
/**
* Abort the parse when the first start element tag is found
*/
@Override
public void startElement(NodeName elemName, SchemaType type,
AttributeMap attributes, NamespaceMap namespaces,
Location location, int properties)
throws XPathException {
terminated = true;
// abort the parse when the first start element tag is found
throw new XPathException("#start#");
}
/**
* Determine whether the parse terminated because the first start element tag was found
*
* @return true if the parse was terminated when the document element was encountered (as distinct
* from being terminated because of some exception condition, for example a parse error)
*/
public boolean isTerminated() {
return terminated;
}
/**
* Handle xml-stylesheet PI
*/
@Override
public void processingInstruction(String target, UnicodeString data, Location locationId, int properties)
throws XPathException {
if (target.equals("xml-stylesheet")) {
String value = data.toString();
String piMedia = ProcInstParser.getPseudoAttribute(value, "media");
String piTitle = ProcInstParser.getPseudoAttribute(value, "title");
String piType = ProcInstParser.getPseudoAttribute(value, "type");
String piAlternate = ProcInstParser.getPseudoAttribute(value, "alternate");
if (piType == null) {
return;
}
// System.err.println("Found xml-stylesheet media=" + piMedia + " title=" + piTitle);
if ((piType.equals("text/xml") || piType.equals("application/xml") ||
piType.equals("text/xsl") || piType.equals("applicaton/xsl") || piType.equals("application/xml+xslt")) &&
(reqMedia == null || piMedia == null ||
getConfiguration().getMediaQueryEvaluator().compare(piMedia, reqMedia) == 0) && // see bug 1729
((piTitle == null && (piAlternate == null || piAlternate.equals("no"))) ||
(reqTitle == null) ||
(piTitle != null && piTitle.equals(reqTitle)))) {
String href = ProcInstParser.getPseudoAttribute(value, "href");
if (href == null) {
throw new XPathException("xml-stylesheet PI has no href attribute");
}
// System.err.println("Adding " + href);
if (piTitle == null && (piAlternate == null || piAlternate.equals("no"))) {
stylesheets.add(0, href);
} else {
stylesheets.add(href);
}
} else {
//System.err.println("No match on required media=" + reqMedia + " title=" + reqTitle );
}
}
}
/**
* Return list of stylesheets that matched, as an array of Source objects
*
* @return null if there were no matching stylesheets.
* @throws net.sf.saxon.trans.XPathException
* if a URI cannot be resolved
*/
/*@Nullable*/
@CSharpReplaceBody(code="return new javax.xml.transform.Source[0];")
public Source[] getAssociatedStylesheets() throws TransformerException {
if (stylesheets.isEmpty()) {
return null;
}
Source[] result = new Source[stylesheets.size()];
ResourceRequest request = new ResourceRequest();
request.baseUri = baseURI;
request.nature = ResourceRequest.XSLT_NATURE;
request.purpose = ResourceRequest.ANY_PURPOSE;
for (int i = 0; i < stylesheets.size(); i++) {
String href = stylesheets.get(i);
request.relativeUri = href;
try {
request.uri = ResolveURI.makeAbsolute(href, baseURI).toString();
} catch (URISyntaxException e) {
throw XPathException.makeXPathException(e);
}
Source s = request.resolve(resourceResolver,
config.getResourceResolver(),
new DirectResourceResolver(config));
if (s instanceof SAXSource) {
((SAXSource) s).setXMLReader(config.getStyleParser());
}
result[i] = s;
}
return result;
}
}