1 package com.icl.saxon.style; 2 import com.icl.saxon.tree.AttributeCollection; 3 import com.icl.saxon.*; 4 import com.icl.saxon.om.*; 5 import com.icl.saxon.tree.*; 6 7 import javax.xml.transform.*; 8 import org.xml.sax.Locator ; 9 import org.xml.sax.SAXException ; 10 import java.util.*; 11 12 18 19 public class StyleNodeFactory implements NodeFactory { 20 21 Hashtable userStyles = new Hashtable(); 22 NamePool namePool; 23 StandardNames sn; 24 25 public StyleNodeFactory(NamePool pool) { 26 27 namePool = pool; 28 sn = pool.getStandardNames(); 29 } 30 31 public StandardNames getStandardNames() { 32 return namePool.getStandardNames(); 33 } 34 35 43 44 public ElementImpl makeElementNode( 45 NodeInfo parent, 46 int nameCode, 47 AttributeCollection attlist, 48 int[] namespaces, 49 int namespacesUsed, 50 Locator locator, 51 int sequence) 52 { 53 boolean toplevel = (parent instanceof XSLStyleSheet); 55 56 String baseURI = null; 57 int lineNumber = -1; 58 59 if (locator!=null) { 60 baseURI = locator.getSystemId(); 61 lineNumber = locator.getLineNumber(); 62 } 63 64 int f = nameCode&0xfffff; 65 StyleElement e = makeXSLElement(f); 66 67 if (e != null) { 68 try { 69 e.setNamespaceDeclarations(namespaces, namespacesUsed); 70 e.initialise(nameCode, attlist, parent, baseURI, lineNumber, sequence); 71 if (e instanceof XSLStyleSheet) { 72 e.processExtensionElementAttribute(sn.EXTENSION_ELEMENT_PREFIXES); 73 e.processExcludedNamespaces(sn.EXCLUDE_RESULT_PREFIXES); 74 e.processVersionAttribute(sn.VERSION); 75 } 76 } catch (TransformerException err) { 77 e.setValidationError(err, StyleElement.REPORT_ALWAYS); 78 } 79 return e; 80 81 } else { 82 83 short uriCode = namePool.getURICode(nameCode); 84 String localname = namePool.getLocalName(nameCode); 85 86 Class assumedClass = LiteralResultElement.class; 87 88 92 StyleElement temp = null; 93 boolean assumedSaxonElement = false; 94 95 97 if (uriCode == Namespace.EXSLT_FUNCTIONS_CODE) { 98 temp = makeExsltFunctionsElement(f); 99 if (temp!=null) { 100 assumedClass = temp.getClass(); 101 assumedSaxonElement = true; 102 } 103 104 } else if (uriCode == Namespace.SAXON_CODE) { 105 temp = makeSaxonElement(f); 106 if (temp!=null) { 107 assumedClass = temp.getClass(); 108 assumedSaxonElement = true; 109 } 110 } 111 if (temp==null) { 112 temp = new LiteralResultElement(); 113 } 114 115 temp.setNamespaceDeclarations(namespaces, namespacesUsed); 116 117 try { 118 temp.initialise(nameCode, attlist, parent, baseURI, lineNumber, sequence); 119 temp.processExtensionElementAttribute(sn.XSL_EXTENSION_ELEMENT_PREFIXES); 120 temp.processExcludedNamespaces(sn.XSL_EXCLUDE_RESULT_PREFIXES); 121 temp.processVersionAttribute(sn.XSL_VERSION); 122 } catch (TransformerException err) { 123 temp.setValidationError(err, StyleElement.REPORT_UNLESS_FORWARDS_COMPATIBLE); 124 } 125 126 128 TransformerException reason = null; 129 Class actualClass = LiteralResultElement.class; 130 131 if (uriCode == Namespace.XSLT_CODE) { 132 reason = new TransformerConfigurationException("Unknown XSLT element: " + localname); 133 actualClass = AbsentExtensionElement.class; 134 temp.setValidationError(reason, StyleElement.REPORT_UNLESS_FORWARDS_COMPATIBLE); 135 } else if (uriCode == Namespace.SAXON_CODE || uriCode == Namespace.EXSLT_FUNCTIONS_CODE) { 136 if (toplevel || temp.isExtensionNamespace(uriCode)) { 137 if (assumedSaxonElement) { 138 actualClass = assumedClass; 140 } else { 141 actualClass = AbsentExtensionElement.class; 142 reason = new TransformerConfigurationException( 143 "Unknown Saxon extension element: " + localname); 144 temp.setValidationError(reason, StyleElement.REPORT_IF_INSTANTIATED); 145 } 146 } else { 147 actualClass = LiteralResultElement.class; 148 } 149 } else if (temp.isExtensionNamespace(uriCode) && !toplevel) { 150 Integer nameKey = new Integer (nameCode&0xfffff); 151 152 actualClass = (Class )userStyles.get(nameKey); 153 if (actualClass==null) { 154 ExtensionElementFactory factory = getFactory(uriCode); 155 if (factory != null) { 156 actualClass = factory.getExtensionClass(localname); 157 if (actualClass != null) { 158 userStyles.put(nameKey, actualClass); } 160 } 161 if (actualClass == null) { 162 163 168 actualClass = AbsentExtensionElement.class; 169 reason = new TransformerConfigurationException("Unknown extension element"); 170 temp.setValidationError(reason, StyleElement.REPORT_IF_INSTANTIATED); 171 } 172 } 173 } else { 174 actualClass = LiteralResultElement.class; 175 } 176 177 StyleElement node; 178 if (!actualClass.equals(assumedClass)) { 179 try { 180 node = (StyleElement)actualClass.newInstance(); 181 } catch (java.lang.InstantiationException err1) { 185 throw new TransformerFactoryConfigurationError(err1, "Failed to create instance of " + actualClass.getName()); 186 } catch (java.lang.IllegalAccessException err2) { 187 throw new TransformerFactoryConfigurationError(err2, "Failed to access class " + actualClass.getName()); 188 } 189 node.substituteFor(temp); } else { 191 node = temp; } 193 return node; 194 } 195 } 196 197 200 201 private StyleElement makeXSLElement(int f) { 202 203 StyleElement e = null; 204 205 if (f==sn.XSL_APPLY_IMPORTS) e=new XSLApplyImports(); 206 else if (f==sn.XSL_APPLY_TEMPLATES) e=new XSLApplyTemplates(); 207 else if (f==sn.XSL_ATTRIBUTE) e=new XSLAttribute(); 208 else if (f==sn.XSL_ATTRIBUTE_SET) e=new XSLAttributeSet(); 209 else if (f==sn.XSL_CALL_TEMPLATE) e=new XSLCallTemplate(); 210 else if (f==sn.XSL_CHOOSE) e=new XSLChoose(); 211 else if (f==sn.XSL_COMMENT) e=new XSLComment(); 212 else if (f==sn.XSL_COPY) e=new XSLCopy(); 213 else if (f==sn.XSL_COPY_OF) e=new XSLCopyOf(); 214 else if (f==sn.XSL_DECIMAL_FORMAT) e=new XSLDecimalFormat(); 215 else if (f==sn.XSL_DOCUMENT) e=new XSLDocument(); 216 else if (f==sn.XSL_ELEMENT) e=new XSLElement(); 217 else if (f==sn.XSL_FALLBACK) e=new XSLFallback(); 218 else if (f==sn.XSL_FOR_EACH) e=new XSLForEach(); 219 else if (f==sn.XSL_IF) e=new XSLIf(); 220 else if (f==sn.XSL_IMPORT) e=new XSLImport(); 221 else if (f==sn.XSL_INCLUDE) e=new XSLInclude(); 222 else if (f==sn.XSL_KEY) e=new XSLKey(); 223 else if (f==sn.XSL_MESSAGE) e=new XSLMessage(); 224 else if (f==sn.XSL_NUMBER) e=new XSLNumber(); 225 else if (f==sn.XSL_NAMESPACE_ALIAS) e=new XSLNamespaceAlias(); 226 else if (f==sn.XSL_OTHERWISE) e=new XSLOtherwise(); 227 else if (f==sn.XSL_OUTPUT) e=new XSLOutput(); 228 else if (f==sn.XSL_PARAM) e=new XSLParam(); 229 else if (f==sn.XSL_PRESERVE_SPACE) e=new XSLPreserveSpace(); 230 else if (f==sn.XSL_PROCESSING_INSTRUCTION) e=new XSLProcessingInstruction(); 231 else if (f==sn.XSL_SCRIPT) e=new XSLScript(); 232 else if (f==sn.XSL_SORT) e=new XSLSort(); 233 else if (f==sn.XSL_STRIP_SPACE) e=new XSLPreserveSpace(); 234 else if (f==sn.XSL_STYLESHEET) e=new XSLStyleSheet(); 235 else if (f==sn.XSL_TEMPLATE) e=new XSLTemplate(); 236 else if (f==sn.XSL_TEXT) e=new XSLText(); 237 else if (f==sn.XSL_TRANSFORM) e=new XSLStyleSheet(); 238 else if (f==sn.XSL_VALUE_OF) e=new XSLValueOf(); 239 else if (f==sn.XSL_VARIABLE) e=new XSLVariable(); 240 else if (f==sn.XSL_WITH_PARAM) e=new XSLWithParam(); 241 else if (f==sn.XSL_WHEN) e=new XSLWhen(); 242 243 return e; 244 } 245 246 249 250 private StyleElement makeSaxonElement(int f) { 251 252 StyleElement e = null; 253 254 if (f==sn.SAXON_ASSIGN) e=new SAXONAssign(); 255 else if (f==sn.SAXON_ENTITY_REF) e=new SAXONEntityRef(); 256 else if (f==sn.SAXON_DOCTYPE) e=new SAXONDoctype(); 257 else if (f==sn.SAXON_FUNCTION) e=new SAXONFunction(); 258 else if (f==sn.SAXON_GROUP) e=new SAXONGroup(); 259 else if (f==sn.SAXON_HANDLER) e=new SAXONHandler(); 260 else if (f==sn.SAXON_ITEM) e=new SAXONItem(); 261 else if (f==sn.SAXON_OUTPUT) e=new XSLDocument(); else if (f==sn.SAXON_PREVIEW) e=new SAXONPreview(); 263 else if (f==sn.SAXON_RETURN) e=new SAXONReturn(); 264 else if (f==sn.SAXON_SCRIPT) e=new XSLScript(); else if (f==sn.SAXON_WHILE) e=new SAXONWhile(); 266 return e; 267 } 268 269 272 273 private StyleElement makeExsltFunctionsElement(int f) { 274 275 if (f==sn.EXSLT_FUNC_FUNCTION) return new SAXONFunction(); 276 else if (f==sn.EXSLT_FUNC_RESULT) return new SAXONReturn(); 277 278 return null; 279 } 280 281 285 286 private ExtensionElementFactory getFactory(short uriCode) { 287 String uri = namePool.getURIFromNamespaceCode(uriCode); 288 int lastSlash = uri.lastIndexOf('/'); 289 if (lastSlash<0 || lastSlash==uri.length()-1) { 290 return null; 291 } 292 String factoryClass = uri.substring(lastSlash+1); 293 ExtensionElementFactory factory; 294 295 try { 296 factory = (ExtensionElementFactory)Loader.getInstance(factoryClass); 297 } catch (Exception err) { 298 return null; 299 } 300 return factory; 301 } 302 303 306 307 public boolean isElementAvailable(String uri, String localName) { 308 int fingerprint = namePool.getFingerprint(uri, localName); 309 if (uri.equals(Namespace.XSLT)) { 310 if (fingerprint==-1) return false; StyleElement e = makeXSLElement(fingerprint); 312 if (e!=null) return e.isInstruction(); 313 } 314 315 if (uri.equals(Namespace.SAXON)) { 316 if (fingerprint==-1) return false; StyleElement e = makeSaxonElement(fingerprint); 318 if (e!=null) return e.isInstruction(); 319 } 320 321 short uriCode = namePool.getCodeForURI(uri); 322 ExtensionElementFactory factory = getFactory(uriCode); 323 if (factory==null) return false; 324 Class actualClass = factory.getExtensionClass(localName); 325 return (actualClass != null); 326 327 } 328 329 330 331 332 } 333 334 | Popular Tags |