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 ; 10 import java.net.URL ; 11 import java.net.URLClassLoader ; 12 import java.net.MalformedURLException ; 13 14 17 18 public class XSLScript extends StyleElement { 19 20 private Class javaClass = null; 21 private String implementsURI = null; 22 private String language = null; 23 24 public void prepareAttributes() throws TransformerConfigurationException { 25 26 String languageAtt = null; 27 String implementsAtt = null; 28 String srcAtt = null; 29 String 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 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 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 base; 90 try { 91 base = new URL (getBaseURI()); 92 } catch (MalformedURLException err) { 93 compileError("Invalid base URI " + getBaseURI()); 94 return; 95 } 96 StringTokenizer st = new StringTokenizer (archiveAtt); 97 int count = 0; 98 while (st.hasMoreTokens()) { 99 count++; 100 st.nextToken(); 101 } 102 URL [] urls = new URL [count]; 103 count = 0; 104 st = new StringTokenizer (archiveAtt); 105 while (st.hasMoreTokens()) { 106 String s = (String )st.nextToken(); 107 try { 108 urls[count++] = new URL (base, s); 109 } catch (MalformedURLException err) { 110 compileError("Invalid URL " + s); 111 return; 112 } 113 } 114 try { 115 javaClass = new URLClassLoader (urls).loadClass(className); 116 } catch (java.lang.ClassNotFoundException err) { 117 compileError("Cannot find class " + className + " in the specified archive" 118 + (count>1 ? "s" : "")); 119 } catch (java.lang.NoClassDefFoundError 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 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 144 145 public Class getJavaClass(String uri) throws TransformerException { 146 if (language==null) { 147 prepareAttributes(); 149 } 150 if (language.equals("java") && implementsURI.equals(uri)) { 151 return javaClass; 152 } else { 153 return null; 154 } 155 } 156 157 } 158 159 | Popular Tags |