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 ; 8 import java.net.MalformedURLException ; 9 import java.net.URL ; 10 import java.net.URLClassLoader ; 11 import java.util.StringTokenizer ; 12 13 16 17 public class SaxonScript extends StyleElement { 18 19 private Class javaClass = null; 20 private String implementsURI = null; 21 private String language = null; 22 23 public void prepareAttributes() throws XPathException { 24 25 String languageAtt = null; 26 String implementsAtt = null; 27 String srcAtt = null; 28 String 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 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 className = srcAtt.substring(5); 73 74 if (archiveAtt==null) { 75 try { 76 javaClass = getConfiguration().getClass(className, false, null); 77 } catch (TransformerException err) { 78 compileError(err); 79 return; 80 } 81 } else { 82 URL base; 83 try { 84 base = new URL (getBaseURI()); 85 } catch (MalformedURLException err) { 86 compileError("Invalid base URI " + getBaseURI()); 87 return; 88 } 89 StringTokenizer st = new StringTokenizer (archiveAtt); 90 int count = 0; 91 while (st.hasMoreTokens()) { 92 count++; 93 st.nextToken(); 94 } 95 URL [] urls = new URL [count]; 96 count = 0; 97 st = new StringTokenizer (archiveAtt); 98 while (st.hasMoreTokens()) { 99 String s = st.nextToken(); 100 try { 101 urls[count++] = new URL (base, s); 102 } catch (MalformedURLException err) { 103 compileError("Invalid URL " + s); 104 return; 105 } 106 } 107 try { 108 javaClass = new URLClassLoader (urls).loadClass(className); 109 } catch (java.lang.ClassNotFoundException err) { 110 compileError("Cannot find class " + className + " in the specified archive" 111 + (count>1 ? "s" : "")); 112 } catch (java.lang.NoClassDefFoundError err2) { 113 compileError("Cannot use the archive attribute with this Java VM"); 114 } 115 } 116 } else { 117 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 136 137 155 } 156 157 175 | Popular Tags |