001: package net.sf.saxon.style;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.instruct.Executable;
005: import net.sf.saxon.om.AttributeCollection;
006: import net.sf.saxon.trans.XPathException;
007:
008: import javax.xml.transform.TransformerException;
009: import java.net.MalformedURLException;
010: import java.net.URL;
011: import java.net.URLClassLoader;
012: import java.util.StringTokenizer;
013:
014: /**
015: * A saxon:script element in the stylesheet.
016: */
017:
018: public class SaxonScript extends StyleElement {
019:
020: private Class javaClass = null;
021: private String implements URI = null;
022: private String language = null;
023:
024: public void prepareAttributes() throws XPathException {
025:
026: String languageAtt = null;
027: String implements Att = null;
028: String srcAtt = null;
029: String archiveAtt = null;
030:
031: AttributeCollection atts = getAttributeList();
032:
033: for (int a = 0; a < atts.getLength(); a++) {
034: int nc = atts.getNameCode(a);
035: String f = getNamePool().getClarkName(nc);
036: if (f == StandardNames.LANGUAGE) {
037: languageAtt = atts.getValue(a).trim();
038: } else if (f == StandardNames.IMPLEMENTS_PREFIX) {
039: implements Att = atts.getValue(a).trim();
040: } else if (f == StandardNames.SRC) {
041: srcAtt = atts.getValue(a).trim();
042: } else if (f == StandardNames.ARCHIVE) {
043: archiveAtt = atts.getValue(a).trim();
044: } else {
045: checkUnknownAttribute(nc);
046: }
047: }
048: if (implements Att == null) {
049: reportAbsence("implements-prefix");
050: return;
051: }
052: implements URI = getURIForPrefix(implements Att, false);
053: if (implements URI == null) {
054: undeclaredNamespaceError(implements Att, null);
055: return;
056: }
057:
058: if (languageAtt == null) {
059: reportAbsence("language");
060: return;
061: }
062: language = languageAtt;
063:
064: if (language.equals("java")) {
065: if (srcAtt == null) {
066: compileError("For java, the src attribute is mandatory");
067: return;
068: }
069: if (!srcAtt.startsWith("java:")) {
070: compileError("The src attribute must be a URI of the form java:full.class.Name");
071: return;
072: }
073: String className = srcAtt.substring(5);
074:
075: if (archiveAtt == null) {
076: try {
077: javaClass = getConfiguration().getClass(className,
078: false, null);
079: } catch (TransformerException err) {
080: compileError(err);
081: return;
082: }
083: } else {
084: URL base;
085: try {
086: base = new URL(getBaseURI());
087: } catch (MalformedURLException err) {
088: compileError("Invalid base URI " + getBaseURI());
089: return;
090: }
091: StringTokenizer st = new StringTokenizer(archiveAtt);
092: int count = 0;
093: while (st.hasMoreTokens()) {
094: count++;
095: st.nextToken();
096: }
097: URL[] urls = new URL[count];
098: count = 0;
099: st = new StringTokenizer(archiveAtt);
100: while (st.hasMoreTokens()) {
101: String s = st.nextToken();
102: try {
103: urls[count++] = new URL(base, s);
104: } catch (MalformedURLException err) {
105: compileError("Invalid URL " + s);
106: return;
107: }
108: }
109: try {
110: javaClass = new URLClassLoader(urls)
111: .loadClass(className);
112: } catch (java.lang.ClassNotFoundException err) {
113: compileError("Cannot find class " + className
114: + " in the specified archive"
115: + (count > 1 ? "s" : ""));
116: } catch (java.lang.NoClassDefFoundError err2) {
117: compileError("Cannot use the archive attribute with this Java VM");
118: }
119: }
120: } else {
121: // language != java
122: compileError("The only language supported for Saxon extension functions is 'java'");
123: }
124: getPrincipalStylesheet().declareJavaClass(implements URI,
125: javaClass);
126: }
127:
128: public void validate() throws XPathException {
129: checkTopLevel(null);
130: }
131:
132: public Expression compile(Executable exec) throws XPathException {
133: return null;
134: }
135:
136: /**
137: * Get the Java class, if this saxon:script element matches the specified URI.
138: * Otherwise return null
139: */
140:
141: // private Class getJavaClass(String uri) {
142: // if (language==null) {
143: // // allow for forwards references, but don't bother reporting
144: // // any errors; that will happen when the element is processed
145: // // in its own right.
146: // try {
147: // prepareAttributes();
148: // } catch (TransformerConfigurationException e) {
149: // return null;
150: // }
151: // }
152: // if (language.equals("java") && implementsURI.equals(uri)) {
153: // return javaClass;
154: // } else {
155: // return null;
156: // }
157: // }
158: }
159:
160: //
161: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
162: // you may not use this file except in compliance with the License. You may obtain a copy of the
163: // License at http://www.mozilla.org/MPL/
164: //
165: // Software distributed under the License is distributed on an "AS IS" basis,
166: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
167: // See the License for the specific language governing rights and limitations under the License.
168: //
169: // The Original Code is: all this file.
170: //
171: // The Initial Developer of the Original Code is Michael H. Kay.
172: //
173: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
174: //
175: // Contributor(s): none.
176: //
|