KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLScript


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.expr.*;
5 import com.icl.saxon.om.Namespace;
6 import javax.xml.transform.*;
7
8 import com.icl.saxon.om.NamespaceException;
9 import java.util.StringTokenizer JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12 import java.net.MalformedURLException JavaDoc;
13
14 /**
15 * An xsl:script element in the stylesheet.<BR>
16 */

17
18 public class XSLScript extends StyleElement {
19
20     private Class JavaDoc javaClass = null;
21     private String JavaDoc implementsURI = null;
22     private String JavaDoc language = null;
23
24     public void prepareAttributes() throws TransformerConfigurationException {
25
26         String JavaDoc languageAtt = null;
27         String JavaDoc implementsAtt = null;
28         String JavaDoc srcAtt = null;
29         String JavaDoc archiveAtt = null;
30
31         StandardNames sn = getStandardNames();
32         AttributeCollection atts = getAttributeList();
33         
34         for (int a=0; a<atts.getLength(); a++) {
35             int nc = atts.getNameCode(a);
36             int f = nc & 0xfffff;
37             if (f==sn.LANGUAGE) {
38                 languageAtt = atts.getValue(a);
39             } else if (f==sn.IMPLEMENTS_PREFIX) {
40                 implementsAtt = atts.getValue(a);
41             } else if (f==sn.SRC) {
42                 srcAtt = atts.getValue(a);
43             } else if (f==sn.ARCHIVE) {
44                 archiveAtt = atts.getValue(a);
45             } else {
46                 checkUnknownAttribute(nc);
47             }
48         }
49         if (implementsAtt==null) {
50             reportAbsence("implements-prefix");
51             return;
52         } else {
53             try {
54                 short uriCode = getURICodeForPrefix(implementsAtt);
55                 implementsURI = getNamePool().getURIFromURICode(uriCode);
56             } catch (NamespaceException err) {
57                 compileError(err.getMessage());
58             }
59         }
60         
61         if (languageAtt==null) {
62             reportAbsence("language");
63             return;
64         } else {
65             language = languageAtt;
66         }
67
68         // TODO: validate that language is valid
69

70         if (language.equals("java")) {
71             if (srcAtt==null) {
72                 compileError("For java, the src attribute is mandatory");
73                 return;
74             }
75             if (!srcAtt.startsWith("java:")) {
76                 compileError("The src attribute must be a URI of the form java:full.class.Name");
77                 return;
78             }
79             String JavaDoc className = srcAtt.substring(5);
80             
81             if (archiveAtt==null) {
82                 try {
83                     javaClass = Loader.getClass(className);
84                 } catch (TransformerException err) {
85                     compileError(err);
86                     return;
87                 }
88             } else {
89                 URL JavaDoc base;
90                 try {
91                     base = new URL JavaDoc(getBaseURI());
92                 } catch (MalformedURLException JavaDoc err) {
93                     compileError("Invalid base URI " + getBaseURI());
94                     return;
95                 }
96                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(archiveAtt);
97                 int count = 0;
98                 while (st.hasMoreTokens()) {
99                     count++;
100                     st.nextToken();
101                 }
102                 URL JavaDoc[] urls = new URL JavaDoc[count];
103                 count = 0;
104                 st = new StringTokenizer JavaDoc(archiveAtt);
105                 while (st.hasMoreTokens()) {
106                     String JavaDoc s = (String JavaDoc)st.nextToken();
107                     try {
108                         urls[count++] = new URL JavaDoc(base, s);
109                     } catch (MalformedURLException JavaDoc err) {
110                         compileError("Invalid URL " + s);
111                         return;
112                     }
113                 }
114                 try {
115                     javaClass = new URLClassLoader JavaDoc(urls).loadClass(className);
116                 } catch (java.lang.ClassNotFoundException JavaDoc err) {
117                     compileError("Cannot find class " + className + " in the specified archive"
118                                     + (count>1 ? "s" : ""));
119                 } catch (java.lang.NoClassDefFoundError JavaDoc err2) {
120                     compileError("Cannot use the archive attribute with this Java VM");
121                 }
122             }
123         }
124     }
125
126     public void validate() throws TransformerConfigurationException {
127         if (getURI().equals(Namespace.XSLT)) {
128             // saxon:script is OK, but xsl:script requires version="1.1"
129
if (!forwardsCompatibleModeIsEnabled()) {
130                 compileError("To use xsl:script, set xsl:stylesheet version='1.1'");
131             }
132         }
133         checkTopLevel();
134     }
135
136     public void preprocess() throws TransformerConfigurationException {}
137
138     public void process(Context c) {}
139
140     /**
141     * Get the Java class, if this XSLScript element matches the specified URI.
142     * Otherwise return null
143     */

144
145     public Class JavaDoc getJavaClass(String JavaDoc uri) throws TransformerException {
146         if (language==null) {
147             // allow for forwards references
148
prepareAttributes();
149         }
150         if (language.equals("java") && implementsURI.equals(uri)) {
151             return javaClass;
152         } else {
153             return null;
154         }
155     }
156
157 }
158
159 //
160
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
161
// you may not use this file except in compliance with the License. You may obtain a copy of the
162
// License at http://www.mozilla.org/MPL/
163
//
164
// Software distributed under the License is distributed on an "AS IS" basis,
165
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
166
// See the License for the specific language governing rights and limitations under the License.
167
//
168
// The Original Code is: all this file.
169
//
170
// The Initial Developer of the Original Code is
171
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
172
//
173
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
174
//
175
// Contributor(s): none.
176
//
177
Popular Tags