KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > PIGrabber


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 JavaDoc;
6 import java.util.Vector JavaDoc;
7 import javax.xml.transform.sax.SAXSource JavaDoc;
8 import javax.xml.transform.*;
9
10 /**
11   * The PIGrabber class is a SAX ContentHandler that looks for xml-stylesheet processing
12   * instructions and tests whether they match specified criteria; for those that do, it creates
13   * an InputSource object referring to the relevant stylesheet
14   * @author Michael H. Kay (mhkay@iclway.co.uk)
15   */

16   
17 public class PIGrabber extends DefaultHandler {
18
19     private String JavaDoc reqMedia = null;
20     private String JavaDoc reqTitle = null;
21     private String JavaDoc baseURI = null;
22     private URIResolver uriResolver = null;
23     private Vector JavaDoc stylesheets = new Vector JavaDoc();
24
25     public void setCriteria(String JavaDoc media, String JavaDoc title, String JavaDoc charset) {
26         this.reqMedia = media;
27         this.reqTitle = title;
28     }
29
30     /**
31     * Set the base URI
32     */

33
34     public void setBaseURI(String JavaDoc uri) {
35         baseURI = uri;
36     }
37
38     /**
39     * Set the URI resolver to be used for the href attribute
40     */

41
42     public void setURIResolver(URIResolver resolver) {
43         uriResolver = resolver;
44     }
45
46     /**
47     * Abort the parse when the first start element tag is found
48     */

49
50     public void startElement (String JavaDoc uri, String JavaDoc localName,
51                   String JavaDoc qName, Attributes attributes) throws SAXException {
52
53         // abort the parse when the first start element tag is found
54
throw new SAXException("#start#");
55     }
56
57     /**
58     * Handle xml-stylesheet PI
59     */

60
61     public void processingInstruction (String JavaDoc target, String JavaDoc data)
62     throws SAXException
63     {
64         if (target.equals("xml-stylesheet")) {
65
66             String JavaDoc piMedia = ProcInstParser.getPseudoAttribute(data, "media");
67             String JavaDoc piTitle = ProcInstParser.getPseudoAttribute(data, "title");
68             String JavaDoc piType = ProcInstParser.getPseudoAttribute(data, "type");
69             String JavaDoc piAlternate = ProcInstParser.getPseudoAttribute(data, "alternate");
70
71             if (piType==null) return;
72
73             // System.err.println("Found xml-stylesheet media=" + piMedia + " title=" + piTitle);
74

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 JavaDoc href = ProcInstParser.getPseudoAttribute(data, "href");
85                 if (href==null) {
86                     throw new SAXException("xml-stylesheet PI has no href attribute");
87                 }
88
89                 // System.err.println("Adding " + href);
90
if (piTitle==null && (piAlternate==null || piAlternate.equals("no"))) {
91                     stylesheets.insertElementAt(href, 0);
92                 } else {
93                     stylesheets.addElement(href);
94                 }
95             } else {
96                 //System.err.println("No match on required media=" + reqMedia + " title=" + reqTitle );
97
}
98         }
99     }
100
101     /**
102     * Return list of stylesheets that matched, as an array of Source objects
103     * @return null if there were no matching stylesheets.
104     * @throws TransformerException if a URI cannot be resolved
105     */

106
107     public SAXSource JavaDoc[] getAssociatedStylesheets() throws TransformerException {
108         if (stylesheets.size()==0) {
109             return null;
110         }
111         if (uriResolver==null) {
112             uriResolver = new StandardURIResolver();
113         }
114         SAXSource JavaDoc[] result = new SAXSource JavaDoc[stylesheets.size()];
115         for (int i=0; i<stylesheets.size(); i++) {
116             String JavaDoc href = (String JavaDoc)stylesheets.elementAt(i);
117             Source JavaDoc s = uriResolver.resolve(href, baseURI);
118             if (!(s instanceof SAXSource JavaDoc)) {
119                 throw new TransformerException("Associated stylesheet URI must yield a SAX source");
120             }
121             result[i] = (SAXSource JavaDoc)s;
122         }
123         return result;
124     }
125
126     /**
127     * Get the stylesheet URIs as an array of Strings
128     */

129
130     public String JavaDoc[] getStylesheetURIs() throws SAXException {
131         if (stylesheets.size()==0) {
132             return null;
133         }
134         String JavaDoc[] result = new String JavaDoc[stylesheets.size()];
135         for (int i=0; i<stylesheets.size(); i++) {
136             result[i] = (String JavaDoc)stylesheets.elementAt(i);
137         }
138         return result;
139     }
140
141 }
142 //
143
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
144
// you may not use this file except in compliance with the License. You may obtain a copy of the
145
// License at http://www.mozilla.org/MPL/
146
//
147
// Software distributed under the License is distributed on an "AS IS" basis,
148
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
149
// See the License for the specific language governing rights and limitations under the License.
150
//
151
// The Original Code is: all this file.
152
//
153
// The Initial Developer of the Original Code is
154
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
155
//
156
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
157
//
158
// Contributor(s): none.
159
//
160
Popular Tags