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 saxon9 Show documentation
Show all versions of saxon9 Show documentation
Provides a basic XSLT 2.0 and XQuery 1.0 processor (W3C Recommendations,
January 2007). Command line interfaces and implementations of several
Java APIs (DOM, XPath, s9api) are also included.
The newest version!
package net.sf.saxon.event;
import net.sf.saxon.Configuration;
import net.sf.saxon.StandardURIResolver;
import net.sf.saxon.om.ProcInstParser;
import net.sf.saxon.trans.XPathException;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import java.util.ArrayList;
/**
* The PIGrabber class is a Receiver that looks for xml-stylesheet processing
* instructions and tests whether they match specified criteria; for those that do, it creates
* an InputSource object referring to the relevant stylesheet
* @author Michael H. Kay
*/
public class PIGrabber extends ProxyReceiver {
private Configuration config = null;
private String reqMedia = null;
private String reqTitle = null;
private String baseURI = null;
private URIResolver uriResolver = null;
private ArrayList stylesheets = new ArrayList();
private boolean terminated = false;
public void setFactory(Configuration config) {
this.config = config;
}
public void setCriteria(String media, String title, String charset) {
this.reqMedia = media;
this.reqTitle = title;
}
/**
* Set the base URI
*/
public void setBaseURI(String uri) {
baseURI = uri;
}
/**
* Set the URI resolver to be used for the href attribute
*/
public void setURIResolver(URIResolver resolver) {
uriResolver = resolver;
}
public void open() {
nextReceiver = new Sink();
}
/**
* Abort the parse when the first start element tag is found
*/
public void startElement (int namecode, int typecode, int locationId, 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
*/
public boolean isTerminated() {
return terminated;
}
/**
* Handle xml-stylesheet PI
*/
public void processingInstruction(String target, CharSequence data, int 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 || reqMedia.equals(piMedia)) &&
( ( 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
*/
public Source[] getAssociatedStylesheets() throws TransformerException {
if (stylesheets.size()==0) {
return null;
}
if (uriResolver==null) {
uriResolver = new StandardURIResolver(config);
}
Source[] result = new Source[stylesheets.size()];
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy