KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > SaxonScript


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.instruct.Executable;
4 import net.sf.saxon.om.AttributeCollection;
5 import net.sf.saxon.trans.XPathException;
6
7 import javax.xml.transform.TransformerException JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.net.URLClassLoader JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 /**
14 * A saxon:script element in the stylesheet.
15 */

16
17 public class SaxonScript extends StyleElement {
18
19     private Class JavaDoc javaClass = null;
20     private String JavaDoc implementsURI = null;
21     private String JavaDoc language = null;
22
23     public void prepareAttributes() throws XPathException {
24
25         String JavaDoc languageAtt = null;
26         String JavaDoc implementsAtt = null;
27         String JavaDoc srcAtt = null;
28         String JavaDoc archiveAtt = null;
29
30         AttributeCollection atts = getAttributeList();
31
32         for (int a=0; a<atts.getLength(); a++) {
33             int nc = atts.getNameCode(a);
34             String JavaDoc f = getNamePool().getClarkName(nc);
35             if (f==StandardNames.LANGUAGE) {
36                 languageAtt = atts.getValue(a).trim();
37             } else if (f==StandardNames.IMPLEMENTS_PREFIX) {
38                 implementsAtt = atts.getValue(a).trim();
39             } else if (f==StandardNames.SRC) {
40                 srcAtt = atts.getValue(a).trim();
41             } else if (f==StandardNames.ARCHIVE) {
42                 archiveAtt = atts.getValue(a).trim();
43             } else {
44                 checkUnknownAttribute(nc);
45             }
46         }
47         if (implementsAtt==null) {
48             reportAbsence("implements-prefix");
49             return;
50         }
51         implementsURI = getURIForPrefix(implementsAtt, false);
52         if (implementsURI == null) {
53             undeclaredNamespaceError(implementsAtt, null);
54             return;
55         }
56
57         if (languageAtt==null) {
58             reportAbsence("language");
59             return;
60         }
61         language = languageAtt;
62
63         if (language.equals("java")) {
64             if (srcAtt==null) {
65                 compileError("For java, the src attribute is mandatory");
66                 return;
67             }
68             if (!srcAtt.startsWith("java:")) {
69                 compileError("The src attribute must be a URI of the form java:full.class.Name");
70                 return;
71             }
72             String JavaDoc className = srcAtt.substring(5);
73
74             if (archiveAtt==null) {
75                 try {
76                     javaClass = getConfiguration().getClass(className, false, null);
77                 } catch (TransformerException JavaDoc err) {
78                     compileError(err);
79                     return;
80                 }
81             } else {
82                 URL JavaDoc base;
83                 try {
84                     base = new URL JavaDoc(getBaseURI());
85                 } catch (MalformedURLException JavaDoc err) {
86                     compileError("Invalid base URI " + getBaseURI());
87                     return;
88                 }
89                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(archiveAtt);
90                 int count = 0;
91                 while (st.hasMoreTokens()) {
92                     count++;
93                     st.nextToken();
94                 }
95                 URL JavaDoc[] urls = new URL JavaDoc[count];
96                 count = 0;
97                 st = new StringTokenizer JavaDoc(archiveAtt);
98                 while (st.hasMoreTokens()) {
99                     String JavaDoc s = st.nextToken();
100                     try {
101                         urls[count++] = new URL JavaDoc(base, s);
102                     } catch (MalformedURLException JavaDoc err) {
103                         compileError("Invalid URL " + s);
104                         return;
105                     }
106                 }
107                 try {
108                     javaClass = new URLClassLoader JavaDoc(urls).loadClass(className);
109                 } catch (java.lang.ClassNotFoundException JavaDoc err) {
110                     compileError("Cannot find class " + className + " in the specified archive"
111                                     + (count>1 ? "s" : ""));
112                 } catch (java.lang.NoClassDefFoundError JavaDoc err2) {
113                     compileError("Cannot use the archive attribute with this Java VM");
114                 }
115             }
116         } else {
117             // language != java
118
compileError("The only language supported for Saxon extension functions is 'java'");
119         }
120         getPrincipalStylesheet().declareJavaClass(implementsURI, javaClass);
121     }
122
123     public void validate() throws XPathException {
124         checkTopLevel(null);
125     }
126
127     public Expression compile(Executable exec) throws XPathException {
128         return null;
129     }
130
131
132     /**
133     * Get the Java class, if this saxon:script element matches the specified URI.
134     * Otherwise return null
135     */

136
137 // private Class getJavaClass(String uri) {
138
// if (language==null) {
139
// // allow for forwards references, but don't bother reporting
140
// // any errors; that will happen when the element is processed
141
// // in its own right.
142
// try {
143
// prepareAttributes();
144
// } catch (TransformerConfigurationException e) {
145
// return null;
146
// }
147
// }
148
// if (language.equals("java") && implementsURI.equals(uri)) {
149
// return javaClass;
150
// } else {
151
// return null;
152
// }
153
// }
154

155 }
156
157 //
158
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
159
// you may not use this file except in compliance with the License. You may obtain a copy of the
160
// License at http://www.mozilla.org/MPL/
161
//
162
// Software distributed under the License is distributed on an "AS IS" basis,
163
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
164
// See the License for the specific language governing rights and limitations under the License.
165
//
166
// The Original Code is: all this file.
167
//
168
// The Initial Developer of the Original Code is Michael H. Kay.
169
//
170
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
171
//
172
// Contributor(s): none.
173
//
174

175
Popular Tags