KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > PIGrabber


1 package net.sf.saxon.event;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.StandardURIResolver;
4 import net.sf.saxon.om.ProcInstParser;
5 import net.sf.saxon.trans.DynamicError;
6 import net.sf.saxon.trans.XPathException;
7
8 import javax.xml.transform.Source JavaDoc;
9 import javax.xml.transform.TransformerException JavaDoc;
10 import javax.xml.transform.URIResolver JavaDoc;
11 import javax.xml.transform.sax.SAXSource JavaDoc;
12 import java.util.ArrayList JavaDoc;
13
14 /**
15   * The PIGrabber class is a Receiver that looks for xml-stylesheet processing
16   * instructions and tests whether they match specified criteria; for those that do, it creates
17   * an InputSource object referring to the relevant stylesheet
18   * @author Michael H. Kay
19   */

20
21 public class PIGrabber extends ProxyReceiver {
22
23     private Configuration config = null;
24     private String JavaDoc reqMedia = null;
25     private String JavaDoc reqTitle = null;
26     private String JavaDoc baseURI = null;
27     private URIResolver uriResolver = null;
28     private ArrayList JavaDoc stylesheets = new ArrayList JavaDoc();
29     private boolean terminated = false;
30
31     public void setFactory(Configuration config) {
32         this.config = config;
33     }
34
35     public void setCriteria(String JavaDoc media, String JavaDoc title, String JavaDoc charset) {
36         this.reqMedia = media;
37         this.reqTitle = title;
38     }
39
40     /**
41     * Set the base URI
42     */

43
44     public void setBaseURI(String JavaDoc uri) {
45         baseURI = uri;
46     }
47
48     /**
49     * Set the URI resolver to be used for the href attribute
50     */

51
52     public void setURIResolver(URIResolver resolver) {
53         uriResolver = resolver;
54     }
55
56     public void open() {
57         nextReceiver = new Sink();
58     }
59
60     /**
61     * Abort the parse when the first start element tag is found
62     */

63
64     public void startElement (int namecode, int typecode, int locationId, int properties)
65     throws XPathException {
66         terminated = true;
67         // abort the parse when the first start element tag is found
68
throw new DynamicError("#start#");
69     }
70
71     /**
72     * Determine whether the parse terminated because the first start element tag was found
73     */

74
75     public boolean isTerminated() {
76         return terminated;
77     }
78
79     /**
80     * Handle xml-stylesheet PI
81     */

82
83     public void processingInstruction(String JavaDoc target, CharSequence JavaDoc data, int locationId, int properties)
84     throws XPathException {
85         if (target.equals("xml-stylesheet")) {
86
87             String JavaDoc value = data.toString();
88             String JavaDoc piMedia = ProcInstParser.getPseudoAttribute(value, "media");
89             String JavaDoc piTitle = ProcInstParser.getPseudoAttribute(value, "title");
90             String JavaDoc piType = ProcInstParser.getPseudoAttribute(value, "type");
91             String JavaDoc piAlternate = ProcInstParser.getPseudoAttribute(value, "alternate");
92
93             if (piType==null) return;
94
95             // System.err.println("Found xml-stylesheet media=" + piMedia + " title=" + piTitle);
96

97             if ( (piType.equals("text/xml") || piType.equals("application/xml") ||
98                     piType.equals("text/xsl") || piType.equals("applicaton/xsl") || piType.equals("application.xml+xslt")) &&
99
100                     (reqMedia==null || piMedia==null || reqMedia.equals(piMedia)) &&
101
102                     ( ( piTitle==null && (piAlternate==null || piAlternate.equals("no"))) ||
103                       ( reqTitle==null ) ||
104                       ( piTitle!=null && piTitle.equals(reqTitle) ) ) )
105             {
106                 String JavaDoc href = ProcInstParser.getPseudoAttribute(value, "href");
107                 if (href==null) {
108                     throw new DynamicError("xml-stylesheet PI has no href attribute");
109                 }
110
111                 // System.err.println("Adding " + href);
112
if (piTitle==null && (piAlternate==null || piAlternate.equals("no"))) {
113                     stylesheets.add(0, href);
114                 } else {
115                     stylesheets.add(href);
116                 }
117             } else {
118                 //System.err.println("No match on required media=" + reqMedia + " title=" + reqTitle );
119
}
120         }
121     }
122
123     /**
124     * Return list of stylesheets that matched, as an array of Source objects
125     * @return null if there were no matching stylesheets.
126     * @throws net.sf.saxon.trans.XPathException if a URI cannot be resolved
127     */

128
129     public Source JavaDoc[] getAssociatedStylesheets() throws TransformerException JavaDoc {
130         if (stylesheets.size()==0) {
131             return null;
132         }
133         if (uriResolver==null) {
134             uriResolver = new StandardURIResolver(config);
135         }
136         Source JavaDoc[] result = new Source JavaDoc[stylesheets.size()];
137         for (int i=0; i<stylesheets.size(); i++) {
138             String JavaDoc href = (String JavaDoc)stylesheets.get(i);
139             Source JavaDoc s = uriResolver.resolve(href, baseURI);
140             if (s instanceof SAXSource JavaDoc) {
141                 ((SAXSource JavaDoc)s).setXMLReader(config.getStyleParser());
142             }
143             if (s == null) {
144                 s = config.getSystemURIResolver().resolve(href, baseURI);
145             }
146             result[i] = s;
147         }
148         return result;
149     }
150
151 }
152 //
153
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
154
// you may not use this file except in compliance with the License. You may obtain a copy of the
155
// License at http://www.mozilla.org/MPL/
156
//
157
// Software distributed under the License is distributed on an "AS IS" basis,
158
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
159
// See the License for the specific language governing rights and limitations under the License.
160
//
161
// The Original Code is: all this file.
162
//
163
// The Initial Developer of the Original Code is Michael H. Kay.
164
//
165
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
166
//
167
// Contributor(s): none.
168
//
169
Popular Tags