1 16 19 package org.apache.xalan.processor; 20 21 import java.util.Vector ; 22 23 import javax.xml.transform.TransformerConfigurationException ; 24 import javax.xml.transform.TransformerException ; 25 26 import org.apache.xalan.res.XSLMessages; 27 import org.apache.xalan.res.XSLTErrorResources; 28 import org.apache.xalan.templates.Constants; 29 import org.apache.xalan.templates.ElemExtensionCall; 30 import org.apache.xalan.templates.ElemLiteralResult; 31 import org.apache.xalan.templates.ElemTemplate; 32 import org.apache.xalan.templates.ElemTemplateElement; 33 import org.apache.xalan.templates.Stylesheet; 34 import org.apache.xalan.templates.StylesheetRoot; 35 import org.apache.xalan.templates.XMLNSDecl; 36 import org.apache.xml.utils.SAXSourceLocator; 37 import org.apache.xpath.XPath; 38 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.Locator ; 41 import org.xml.sax.helpers.AttributesImpl ; 42 43 54 public class ProcessorLRE extends ProcessorTemplateElem 55 { 56 57 66 public void startElement( 67 StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) 68 throws org.xml.sax.SAXException 69 { 70 71 try 72 { 73 ElemTemplateElement p = handler.getElemTemplateElement(); 74 boolean excludeXSLDecl = false; 75 boolean isLREAsStyleSheet = false; 76 77 if (null == p) 78 { 79 80 XSLTElementProcessor lreProcessor = handler.popProcessor(); 82 XSLTElementProcessor stylesheetProcessor = 83 handler.getProcessorFor(Constants.S_XSLNAMESPACEURL, "stylesheet", 84 "xsl:stylesheet"); 85 86 handler.pushProcessor(lreProcessor); 87 88 Stylesheet stylesheet; 89 try 90 { 91 stylesheet = new StylesheetRoot(handler.getSchema(), handler.getStylesheetProcessor().getErrorListener()); 92 } 93 catch(TransformerConfigurationException tfe) 94 { 95 throw new TransformerException (tfe); 96 } 97 98 SAXSourceLocator slocator = new SAXSourceLocator(); 101 Locator locator = handler.getLocator(); 102 if(null != locator) 103 { 104 slocator.setLineNumber(locator.getLineNumber()); 105 slocator.setColumnNumber(locator.getColumnNumber()); 106 slocator.setPublicId(locator.getPublicId()); 107 slocator.setSystemId(locator.getSystemId()); 108 } 109 stylesheet.setLocaterInfo(slocator); 110 stylesheet.setPrefixes(handler.getNamespaceSupport()); 111 handler.pushStylesheet(stylesheet); 112 113 isLREAsStyleSheet = true; 114 115 AttributesImpl stylesheetAttrs = new AttributesImpl (); 116 AttributesImpl lreAttrs = new AttributesImpl (); 117 int n = attributes.getLength(); 118 119 for (int i = 0; i < n; i++) 120 { 121 String attrLocalName = attributes.getLocalName(i); 122 String attrUri = attributes.getURI(i); 123 String value = attributes.getValue(i); 124 125 if ((null != attrUri) && attrUri.equals(Constants.S_XSLNAMESPACEURL)) 126 { 127 stylesheetAttrs.addAttribute(null, attrLocalName, attrLocalName, 128 attributes.getType(i), 129 attributes.getValue(i)); 130 } 131 else if ((attrLocalName.startsWith("xmlns:") || attrLocalName.equals( 132 "xmlns")) && value.equals(Constants.S_XSLNAMESPACEURL)) 133 { 134 135 } 137 else 138 { 139 lreAttrs.addAttribute(attrUri, attrLocalName, 140 attributes.getQName(i), 141 attributes.getType(i), 142 attributes.getValue(i)); 143 } 144 } 145 146 attributes = lreAttrs; 147 148 try{ 152 stylesheetProcessor.setPropertiesFromAttributes(handler, "stylesheet", 153 stylesheetAttrs, stylesheet); 154 } 155 catch (Exception e) 156 { 157 164 if (stylesheet.getDeclaredPrefixes() == null || 165 !declaredXSLNS(stylesheet)) 166 { 167 throw new org.xml.sax.SAXException (XSLMessages.createWarning(XSLTErrorResources.WG_OLD_XSLT_NS, null)); 168 } 169 else 170 { 171 throw new org.xml.sax.SAXException (e); 172 } 173 } 174 handler.pushElemTemplateElement(stylesheet); 175 176 ElemTemplate template = new ElemTemplate(); 177 178 appendAndPush(handler, template); 179 180 XPath rootMatch = new XPath("/", stylesheet, stylesheet, XPath.MATCH, 181 handler.getStylesheetProcessor().getErrorListener()); 182 183 template.setMatch(rootMatch); 184 185 stylesheet.setTemplate(template); 187 188 p = handler.getElemTemplateElement(); 189 excludeXSLDecl = true; 190 } 191 192 XSLTElementDef def = getElemDef(); 193 Class classObject = def.getClassObject(); 194 boolean isExtension = false; 195 boolean isComponentDecl = false; 196 boolean isUnknownTopLevel = false; 197 198 while (null != p) 199 { 200 201 if (p instanceof ElemLiteralResult) 203 { 204 ElemLiteralResult parentElem = (ElemLiteralResult) p; 205 206 isExtension = parentElem.containsExtensionElementURI(uri); 207 } 208 else if (p instanceof Stylesheet) 209 { 210 Stylesheet parentElem = (Stylesheet) p; 211 212 isExtension = parentElem.containsExtensionElementURI(uri); 213 214 if ((false == isExtension) && (null != uri) 215 && (uri.equals(Constants.S_BUILTIN_EXTENSIONS_URL) 216 || uri.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))) 217 { 218 isComponentDecl = true; 219 } 220 else 221 { 222 isUnknownTopLevel = true; 223 } 224 } 225 226 if (isExtension) 227 break; 228 229 p = p.getParentElem(); 230 } 231 232 ElemTemplateElement elem = null; 233 234 try 235 { 236 if (isExtension) 237 { 238 239 elem = new ElemExtensionCall(); 241 } 242 else if (isComponentDecl) 243 { 244 elem = (ElemTemplateElement) classObject.newInstance(); 245 } 246 else if (isUnknownTopLevel) 247 { 248 249 elem = (ElemTemplateElement) classObject.newInstance(); 251 } 252 else 253 { 254 elem = (ElemTemplateElement) classObject.newInstance(); 255 } 256 257 elem.setDOMBackPointer(handler.getOriginatingNode()); 258 elem.setLocaterInfo(handler.getLocator()); 259 elem.setPrefixes(handler.getNamespaceSupport(), excludeXSLDecl); 260 261 if (elem instanceof ElemLiteralResult) 262 { 263 ((ElemLiteralResult) elem).setNamespace(uri); 264 ((ElemLiteralResult) elem).setLocalName(localName); 265 ((ElemLiteralResult) elem).setRawName(rawName); 266 ((ElemLiteralResult) elem).setIsLiteralResultAsStylesheet( 267 isLREAsStyleSheet); 268 } 269 } 270 catch (InstantiationException ie) 271 { 272 handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, ie); } 274 catch (IllegalAccessException iae) 275 { 276 handler.error(XSLTErrorResources.ER_FAILED_CREATING_ELEMLITRSLT, null, iae); } 278 279 setPropertiesFromAttributes(handler, rawName, attributes, elem); 280 281 if (!isExtension && (elem instanceof ElemLiteralResult)) 283 { 284 isExtension = 285 ((ElemLiteralResult) elem).containsExtensionElementURI(uri); 286 287 if (isExtension) 288 { 289 290 elem = new ElemExtensionCall(); 292 293 elem.setLocaterInfo(handler.getLocator()); 294 elem.setPrefixes(handler.getNamespaceSupport()); 295 ((ElemLiteralResult) elem).setNamespace(uri); 296 ((ElemLiteralResult) elem).setLocalName(localName); 297 ((ElemLiteralResult) elem).setRawName(rawName); 298 setPropertiesFromAttributes(handler, rawName, attributes, elem); 299 } 300 } 301 302 appendAndPush(handler, elem); 303 } 304 catch(TransformerException te) 305 { 306 throw new org.xml.sax.SAXException (te); 307 } 308 } 309 310 321 public void endElement( 322 StylesheetHandler handler, String uri, String localName, String rawName) 323 throws org.xml.sax.SAXException 324 { 325 326 ElemTemplateElement elem = handler.getElemTemplateElement(); 327 328 if (elem instanceof ElemLiteralResult) 329 { 330 if (((ElemLiteralResult) elem).getIsLiteralResultAsStylesheet()) 331 { 332 handler.popStylesheet(); 333 } 334 } 335 336 super.endElement(handler, uri, localName, rawName); 337 } 338 339 private boolean declaredXSLNS(Stylesheet stylesheet) 340 { 341 Vector declaredPrefixes = stylesheet.getDeclaredPrefixes(); 342 int n = declaredPrefixes.size(); 343 344 for (int i = 0; i < n; i++) 345 { 346 XMLNSDecl decl = (XMLNSDecl) declaredPrefixes.elementAt(i); 347 if(decl.getURI().equals(Constants.S_XSLNAMESPACEURL)) 348 return true; 349 } 350 return false; 351 } 352 } 353 | Popular Tags |