1 16 19 package org.apache.xalan.lib; 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.OutputStream ; 24 import java.util.Hashtable ; 25 26 import javax.xml.transform.Result ; 27 import javax.xml.transform.TransformerException ; 28 import javax.xml.transform.stream.StreamResult ; 29 30 import org.apache.xalan.extensions.XSLProcessorContext; 31 import org.apache.xalan.res.XSLTErrorResources; 32 import org.apache.xalan.templates.ElemExtensionCall; 33 import org.apache.xalan.templates.OutputProperties; 34 import org.apache.xalan.transformer.TransformerImpl; 35 import org.apache.xpath.XPath; 36 import org.apache.xpath.objects.XObject; 37 import org.apache.xml.serializer.SerializationHandler; 38 import org.xml.sax.ContentHandler ; 39 40 122 public class Redirect 123 { 124 127 protected Hashtable m_formatterListeners = new Hashtable (); 128 129 132 protected Hashtable m_outputStreams = new Hashtable (); 133 134 138 public static final boolean DEFAULT_APPEND_OPEN = false; 139 140 144 public static final boolean DEFAULT_APPEND_WRITE = false; 145 146 149 public void open(XSLProcessorContext context, ElemExtensionCall elem) 150 throws java.net.MalformedURLException , 151 java.io.FileNotFoundException , 152 java.io.IOException , 153 javax.xml.transform.TransformerException 154 { 155 String fileName = getFilename(context, elem); 156 Object flistener = m_formatterListeners.get(fileName); 157 if(null == flistener) 158 { 159 String mkdirsExpr 160 = elem.getAttribute ("mkdirs", context.getContextNode(), 161 context.getTransformer()); 162 boolean mkdirs = (mkdirsExpr != null) 163 ? (mkdirsExpr.equals("true") || mkdirsExpr.equals("yes")) : true; 164 165 String appendExpr = elem.getAttribute("append", context.getContextNode(), context.getTransformer()); 167 boolean append = (appendExpr != null) 168 ? (appendExpr.equals("true") || appendExpr.equals("yes")) : DEFAULT_APPEND_OPEN; 169 170 Object ignored = makeFormatterListener(context, elem, fileName, true, mkdirs, append); 171 } 172 } 173 174 178 public void write(XSLProcessorContext context, ElemExtensionCall elem) 179 throws java.net.MalformedURLException , 180 java.io.FileNotFoundException , 181 java.io.IOException , 182 javax.xml.transform.TransformerException 183 { 184 String fileName = getFilename(context, elem); 185 Object flObject = m_formatterListeners.get(fileName); 186 ContentHandler formatter; 187 boolean inTable = false; 188 if(null == flObject) 189 { 190 String mkdirsExpr 191 = ((ElemExtensionCall)elem).getAttribute ("mkdirs", 192 context.getContextNode(), 193 context.getTransformer()); 194 boolean mkdirs = (mkdirsExpr != null) 195 ? (mkdirsExpr.equals("true") || mkdirsExpr.equals("yes")) : true; 196 197 String appendExpr = elem.getAttribute("append", context.getContextNode(), context.getTransformer()); 199 boolean append = (appendExpr != null) 200 ? (appendExpr.equals("true") || appendExpr.equals("yes")) : DEFAULT_APPEND_WRITE; 201 202 formatter = makeFormatterListener(context, elem, fileName, true, mkdirs, append); 203 } 204 else 205 { 206 inTable = true; 207 formatter = (ContentHandler )flObject; 208 } 209 210 TransformerImpl transf = context.getTransformer(); 211 212 transf.executeChildTemplates(elem, 213 context.getContextNode(), 214 context.getMode(), formatter); 215 216 if(!inTable) 217 { 218 OutputStream ostream = (OutputStream )m_outputStreams.get(fileName); 219 if(null != ostream) 220 { 221 try 222 { 223 formatter.endDocument(); 224 } 225 catch(org.xml.sax.SAXException se) 226 { 227 throw new TransformerException (se); 228 } 229 ostream.close(); 230 m_outputStreams.remove(fileName); 231 m_formatterListeners.remove(fileName); 232 } 233 } 234 } 235 236 237 240 public void close(XSLProcessorContext context, ElemExtensionCall elem) 241 throws java.net.MalformedURLException , 242 java.io.FileNotFoundException , 243 java.io.IOException , 244 javax.xml.transform.TransformerException 245 { 246 String fileName = getFilename(context, elem); 247 Object formatterObj = m_formatterListeners.get(fileName); 248 if(null != formatterObj) 249 { 250 ContentHandler fl = (ContentHandler )formatterObj; 251 try 252 { 253 fl.endDocument(); 254 } 255 catch(org.xml.sax.SAXException se) 256 { 257 throw new TransformerException (se); 258 } 259 OutputStream ostream = (OutputStream )m_outputStreams.get(fileName); 260 if(null != ostream) 261 { 262 ostream.close(); 263 m_outputStreams.remove(fileName); 264 } 265 m_formatterListeners.remove(fileName); 266 } 267 } 268 269 272 private String getFilename(XSLProcessorContext context, ElemExtensionCall elem) 273 throws java.net.MalformedURLException , 274 java.io.FileNotFoundException , 275 java.io.IOException , 276 javax.xml.transform.TransformerException 277 { 278 String fileName; 279 String fileNameExpr 280 = ((ElemExtensionCall)elem).getAttribute ("select", 281 context.getContextNode(), 282 context.getTransformer()); 283 if(null != fileNameExpr) 284 { 285 org.apache.xpath.XPathContext xctxt 286 = context.getTransformer().getXPathContext(); 287 XPath myxpath = new XPath(fileNameExpr, elem, xctxt.getNamespaceContext(), XPath.SELECT); 288 XObject xobj = myxpath.execute(xctxt, context.getContextNode(), elem); 289 fileName = xobj.str(); 290 if((null == fileName) || (fileName.length() == 0)) 291 { 292 fileName = elem.getAttribute ("file", 293 context.getContextNode(), 294 context.getTransformer()); 295 } 296 } 297 else 298 { 299 fileName = elem.getAttribute ("file", context.getContextNode(), 300 context.getTransformer()); 301 } 302 if(null == fileName) 303 { 304 context.getTransformer().getMsgMgr().error(elem, elem, 305 context.getContextNode(), 306 XSLTErrorResources.ER_REDIRECT_COULDNT_GET_FILENAME); 307 } 309 return fileName; 310 } 311 312 private String urlToFileName(String base) 316 { 317 if(null != base) 318 { 319 if(base.startsWith("file:////")) 320 { 321 base = base.substring(7); 322 } 323 else if(base.startsWith("file:///")) 324 { 325 base = base.substring(6); 326 } 327 else if(base.startsWith("file://")) 328 { 329 base = base.substring(5); } 331 else if(base.startsWith("file:/")) 332 { 333 base = base.substring(5); 334 } 335 else if(base.startsWith("file:")) 336 { 337 base = base.substring(4); 338 } 339 } 340 return base; 341 } 342 343 346 private ContentHandler makeFormatterListener(XSLProcessorContext context, 347 ElemExtensionCall elem, 348 String fileName, 349 boolean shouldPutInTable, 350 boolean mkdirs, 351 boolean append) 352 throws java.net.MalformedURLException , 353 java.io.FileNotFoundException , 354 java.io.IOException , 355 javax.xml.transform.TransformerException 356 { 357 File file = new File (fileName); 358 TransformerImpl transformer = context.getTransformer(); 359 String base; 361 if(!file.isAbsolute()) 362 { 363 370 Result outputTarget = transformer.getOutputTarget(); 371 if ( (null != outputTarget) && ((base = outputTarget.getSystemId()) != null) ) { 372 base = urlToFileName(base); 373 } 374 else 375 { 376 base = urlToFileName(transformer.getBaseURLOfSource()); 377 } 378 379 if(null != base) 380 { 381 File baseFile = new File (base); 382 file = new File (baseFile.getParent(), fileName); 383 } 384 } 386 387 if(mkdirs) 388 { 389 String dirStr = file.getParent(); 390 if((null != dirStr) && (dirStr.length() > 0)) 391 { 392 File dir = new File (dirStr); 393 dir.mkdirs(); 394 } 395 } 396 397 OutputProperties format = transformer.getOutputFormat(); 400 401 FileOutputStream ostream = new FileOutputStream (file.getPath(), append); 408 409 try 410 { 411 SerializationHandler flistener = 412 transformer.createSerializationHandler(new StreamResult (ostream), 413 format); 414 try 415 { 416 flistener.startDocument(); 417 } 418 catch(org.xml.sax.SAXException se) 419 { 420 throw new TransformerException (se); 421 } 422 if(shouldPutInTable) 423 { 424 m_outputStreams.put(fileName, ostream); 425 m_formatterListeners.put(fileName, flistener); 426 } 427 return flistener; 428 } 429 catch(TransformerException te) 430 { 431 throw new javax.xml.transform.TransformerException (te); 432 } 433 434 } 435 } 436 | Popular Tags |