1 16 19 package org.apache.xalan.processor; 20 21 import java.io.IOException ; 22 23 import javax.xml.transform.Source ; 24 import javax.xml.transform.TransformerException ; 25 import javax.xml.transform.URIResolver ; 26 import javax.xml.transform.dom.DOMSource ; 27 import javax.xml.transform.sax.SAXSource ; 28 import javax.xml.transform.stream.StreamSource ; 29 30 import org.apache.xalan.res.XSLMessages; 31 import org.apache.xalan.res.XSLTErrorResources; 32 import org.apache.xml.utils.SystemIDResolver; 33 import org.apache.xml.utils.TreeWalker; 34 35 import org.w3c.dom.Node ; 36 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.XMLReader ; 40 import org.xml.sax.helpers.XMLReaderFactory ; 41 42 47 class ProcessorInclude extends XSLTElementProcessor 48 { 49 50 54 private String m_href = null; 55 56 62 public String getHref() 63 { 64 return m_href; 65 } 66 67 72 public void setHref(String baseIdent) 73 { 74 m_href = baseIdent; 76 } 77 78 83 protected int getStylesheetType() 84 { 85 return StylesheetHandler.STYPE_INCLUDE; 86 } 87 88 93 protected String getStylesheetInclErr() 94 { 95 return XSLTErrorResources.ER_STYLESHEET_INCLUDES_ITSELF; 96 } 97 98 117 public void startElement( 118 StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) 119 throws org.xml.sax.SAXException 120 { 121 122 123 setPropertiesFromAttributes(handler, rawName, attributes, this); 124 125 try 126 { 127 String hrefUrl = SystemIDResolver.getAbsoluteURI(getHref(), 128 handler.getBaseIdentifier()); 129 130 if (handler.importStackContains(hrefUrl)) 131 { 132 throw new org.xml.sax.SAXException ( 133 XSLMessages.createMessage( 134 getStylesheetInclErr(), new Object []{ hrefUrl })); } 136 137 handler.pushImportURL(hrefUrl); 138 139 int savedStylesheetType = handler.getStylesheetType(); 140 141 handler.setStylesheetType(this.getStylesheetType()); 142 handler.pushNewNamespaceSupport(); 143 144 try 145 { 146 parse(handler, uri, localName, rawName, attributes); 147 } 148 finally 149 { 150 handler.setStylesheetType(savedStylesheetType); 151 handler.popImportURL(); 152 handler.popNamespaceSupport(); 153 } 154 } 155 catch(TransformerException te) 156 { 157 handler.error(te.getMessage(), te); 158 } 159 } 160 161 176 protected void parse( 177 StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) 178 throws org.xml.sax.SAXException 179 { 180 TransformerFactoryImpl processor = handler.getStylesheetProcessor(); 181 URIResolver uriresolver = processor.getURIResolver(); 182 183 try 184 { 185 Source source = null; 186 187 if (null != uriresolver) 188 { 189 source = uriresolver.resolve(getHref(), 190 handler.getBaseIdentifier()); 191 192 if (null != source && source instanceof DOMSource ) 193 { 194 Node node = ((DOMSource )source).getNode(); 195 196 String systemId = source.getSystemId(); 197 if (systemId == null) 198 { 199 systemId = SystemIDResolver.getAbsoluteURI(getHref(), 200 handler.getBaseIdentifier()); 201 202 } 203 204 TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId); 205 206 try 207 { 208 walker.traverse(node); 209 } 210 catch(org.xml.sax.SAXException se) 211 { 212 throw new TransformerException (se); 213 } 214 return; 215 } 216 } 217 218 if(null == source) 219 { 220 String absURL = SystemIDResolver.getAbsoluteURI(getHref(), 221 handler.getBaseIdentifier()); 222 223 source = new StreamSource (absURL); 224 } 225 226 XMLReader reader = null; 227 228 if(source instanceof SAXSource ) 229 { 230 SAXSource saxSource = (SAXSource )source; 231 reader = saxSource.getXMLReader(); } 233 234 InputSource inputSource = SAXSource.sourceToInputSource(source); 235 236 if (null == reader) 237 { 238 try { 240 javax.xml.parsers.SAXParserFactory factory= 241 javax.xml.parsers.SAXParserFactory.newInstance(); 242 factory.setNamespaceAware( true ); 243 javax.xml.parsers.SAXParser jaxpParser= 244 factory.newSAXParser(); 245 reader=jaxpParser.getXMLReader(); 246 247 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 248 throw new org.xml.sax.SAXException ( ex ); 249 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 250 throw new org.xml.sax.SAXException ( ex1.toString() ); 251 } 252 catch( NoSuchMethodError ex2 ) 253 { 254 } 255 catch (AbstractMethodError ame){} 256 } 257 if (null == reader) 258 reader = XMLReaderFactory.createXMLReader(); 259 260 if (null != reader) 261 { 262 reader.setContentHandler(handler); 263 handler.pushBaseIndentifier(inputSource.getSystemId()); 264 265 try 266 { 267 reader.parse(inputSource); 268 } 269 finally 270 { 271 handler.popBaseIndentifier(); 272 } 273 } 274 } 275 catch (IOException ioe) 276 { 277 handler.error(XSLTErrorResources.ER_IOEXCEPTION, 278 new Object []{ getHref() }, ioe); 279 } 280 catch(TransformerException te) 281 { 282 handler.error(te.getMessage(), te); 283 } 284 } 285 } 286 | Popular Tags |