| 1 16 package org.apache.cocoon.components.source; 17 18 import org.apache.avalon.framework.component.Component; 19 import org.apache.avalon.framework.component.ComponentException; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.avalon.framework.component.ComponentSelector; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.avalon.framework.service.ServiceException; 24 import org.apache.avalon.framework.service.ServiceManager; 25 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.ResourceNotFoundException; 28 import org.apache.cocoon.components.CocoonComponentManager; 29 import org.apache.cocoon.serialization.Serializer; 30 import org.apache.cocoon.util.NetUtils; 31 import org.apache.cocoon.xml.IncludeXMLConsumer; 32 import org.apache.cocoon.xml.XMLUtils; 33 import org.apache.cocoon.xml.dom.DOMBuilder; 34 import org.apache.cocoon.xml.dom.DOMStreamer; 35 36 import org.apache.excalibur.source.ModifiableSource; 37 import org.apache.excalibur.source.Source; 38 import org.apache.excalibur.source.SourceException; 39 import org.apache.excalibur.source.SourceNotFoundException; 40 import org.apache.excalibur.source.SourceParameters; 41 import org.apache.excalibur.source.SourceResolver; 42 import org.apache.excalibur.xml.sax.SAXParser; 43 import org.apache.excalibur.xml.sax.XMLizable; 44 import org.apache.excalibur.xmlizer.XMLizer; 45 import org.apache.regexp.RE; 46 import org.apache.regexp.RECompiler; 47 import org.apache.regexp.REProgram; 48 import org.apache.regexp.RESyntaxException; 49 import org.w3c.dom.Document ; 50 import org.w3c.dom.DocumentFragment ; 51 import org.xml.sax.ContentHandler ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.helpers.DefaultHandler ; 55 56 import javax.xml.transform.OutputKeys ; 57 import java.io.ByteArrayOutputStream ; 58 import java.io.IOException ; 59 import java.io.InputStreamReader ; 60 import java.io.OutputStream ; 61 import java.io.Reader ; 62 import java.util.HashMap ; 63 import java.util.Iterator ; 64 import java.util.Map ; 65 import java.util.Properties ; 66 67 74 public final class SourceUtil { 75 76 private static REProgram uripattern = null; 77 78 static { 79 try { 80 uripattern = new RECompiler().compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"); 81 } catch (RESyntaxException e) { 82 e.printStackTrace(); 84 } 85 } 86 87 88 private SourceUtil() { 89 } 90 91 96 static public void toSAX(XMLizable source, 97 ContentHandler handler) 98 throws SAXException , IOException , ProcessingException { 99 try { 100 source.toSAX(handler); 101 } catch (SAXException e) { 102 final Exception cause = e.getException(); 106 if (cause != null) { 107 if (cause instanceof SourceException) { 108 throw handle((SourceException) cause); 109 } 110 if (cause instanceof ProcessingException) { 111 throw (ProcessingException) cause; 112 } 113 if (cause instanceof IOException ) { 114 throw (IOException ) cause; 115 } 116 if (cause instanceof SAXException ) { 117 throw (SAXException ) cause; 118 } 119 } 120 121 throw e; 123 } 124 } 125 126 136 static public void toSAX(Source source, 137 ContentHandler handler) 138 throws SAXException , IOException , ProcessingException { 139 toSAX(CocoonComponentManager.getSitemapComponentManager(), 140 source, null, handler); 141 } 142 143 154 static public void toSAX(Source source, 155 String mimeTypeHint, 156 ContentHandler handler) 157 throws SAXException , IOException , ProcessingException { 158 toSAX(CocoonComponentManager.getSitemapComponentManager(), 159 source, mimeTypeHint, handler); 160 } 161 162 174 static public void toSAX(ComponentManager manager, 175 Source source, 176 String mimeTypeHint, 177 ContentHandler handler) 178 throws SAXException , IOException , ProcessingException { 179 if (source instanceof XMLizable) { 180 toSAX((XMLizable) source, handler); 181 } else { 182 String mimeType = source.getMimeType(); 183 if (null == mimeType) { 184 mimeType = mimeTypeHint; 185 } 186 187 XMLizer xmlizer = null; 188 try { 189 xmlizer = (XMLizer) manager.lookup(XMLizer.ROLE); 190 xmlizer.toSAX(source.getInputStream(), 191 mimeType, 192 source.getURI(), 193 handler); 194 } catch (SourceException e) { 195 throw SourceUtil.handle(e); 196 } catch (ComponentException e) { 197 throw new ProcessingException("Exception during streaming source.", e); 198 } finally { 199 manager.release((Component) xmlizer); 200 } 201 } 202 } 203 204 214 static public void toSAX(ServiceManager manager, 215 Source source, 216 String mimeTypeHint, 217 ContentHandler handler) 218 throws SAXException , IOException , ProcessingException { 219 if (source instanceof XMLizable) { 220 toSAX((XMLizable) source, handler); 221 } else { 222 String mimeType = source.getMimeType(); 223 if (null == mimeType) { 224 mimeType = mimeTypeHint; 225 } 226 227 XMLizer xmlizer = null; 228 try { 229 xmlizer = (XMLizer) manager.lookup(XMLizer.ROLE); 230 xmlizer.toSAX(source.getInputStream(), 231 mimeType, 232 source.getURI(), 233 handler); 234 } catch (SourceException e) { 235 throw SourceUtil.handle(e); 236 } catch (ServiceException e) { 237 throw new ProcessingException("Exception during streaming source.", e); 238 } finally { 239 manager.release(xmlizer); 240 } 241 } 242 } 243 244 255 static public void parse(ComponentManager manager, 256 Source source, 257 ContentHandler handler) 258 throws SAXException , IOException , ProcessingException { 259 if (source instanceof XMLizable) { 260 toSAX((XMLizable) source, handler); 261 } else { 262 SAXParser parser = null; 263 try { 264 parser = (SAXParser) manager.lookup(SAXParser.ROLE); 265 parser.parse(getInputSource(source), handler); 266 } catch (SourceException e) { 267 throw SourceUtil.handle(e); 268 } catch (ComponentException e) { 269 throw new ProcessingException("Exception during parsing source.", e); 270 } finally { 271 manager.release((Component) parser); 272 } 273 } 274 } 275 276 282 static public void toCharacters(Source source, 283 String encoding, 284 ContentHandler handler) 285 throws SAXException , IOException , ProcessingException { 286 try { 287 Reader r = encoding == null? 288 new InputStreamReader (source.getInputStream()): 289 new InputStreamReader (source.getInputStream(), encoding); 290 291 int len; 292 char[] chr = new char[4096]; 293 try { 294 while ((len = r.read(chr)) > 0) { 295 handler.characters(chr, 0, len); 296 } 297 } finally { 298 r.close(); 299 } 300 } catch (SAXException e) { 301 handleSAXException(source.getURI(), e); 302 } 303 } 304 305 315 static public void parse(ServiceManager manager, 316 Source source, 317 ContentHandler handler) 318 throws SAXException , IOException , ProcessingException { 319 if (source instanceof XMLizable) { 320 toSAX((XMLizable) source, handler); 321 } else { 322 SAXParser parser = null; 323 try { 324 parser = (SAXParser) manager.lookup(SAXParser.ROLE); 325 parser.parse(getInputSource(source), handler); 326 } catch (SourceException e) { 327 throw SourceUtil.handle(e); 328 } catch (ServiceException e) { 329 throw new ProcessingException("Exception during parsing source.", e); 330 } finally { 331 manager.release(parser); 332 } 333 } 334 } 335 336 346 static public void toSAX(Source source, 347 ContentHandler handler, 348 Parameters typeParameters, 349 boolean filterDocumentEvent) 350 throws SAXException , IOException , ProcessingException { 351 if (typeParameters != null 353 && typeParameters.getParameter(URLRewriter.PARAMETER_MODE, null) != null) { 354 handler = new URLRewriter(typeParameters, handler); 355 } 356 String mimeTypeHint = null; 357 if (typeParameters != null) { 358 mimeTypeHint = typeParameters.getParameter("mime-type", mimeTypeHint); 359 } 360 if (filterDocumentEvent) { 361 IncludeXMLConsumer filter = new IncludeXMLConsumer(handler); 362 toSAX(source, mimeTypeHint, filter); 363 } else { 364 toSAX(source, mimeTypeHint, handler); 365 } 366 } 367 368 378 static public Document toDOM(Source source) 379 throws SAXException , IOException , ProcessingException { 380 DOMBuilder builder = new DOMBuilder(); 381 382 toSAX(source, builder); 383 384 Document document = builder.getDocument(); 385 if (document == null) { 386 throw new ProcessingException("Could not build DOM for '" + 387 source.getURI() + "'"); 388 } 389 390 return document; 391 } 392 393 403 static public Document toDOM(ServiceManager manager, Source source) 404 throws SAXException , IOException , ProcessingException { 405 DOMBuilder builder = new DOMBuilder(); 406 407 toSAX(manager, source, null, builder); 408 409 Document document = builder.getDocument(); 410 if (document == null) { 411 throw new ProcessingException("Could not build DOM for '" + 412 source.getURI() + "'"); 413 } 414 415 return document; 416 } 417 418 428 static public Document toDOM(ServiceManager manager, String mimeTypeHint, Source source) 429 throws SAXException , IOException , ProcessingException { 430 DOMBuilder builder = new DOMBuilder(); 431 432 toSAX(manager, source, mimeTypeHint, builder); 433 434 Document document = builder.getDocument(); 435 if (document == null) { 436 throw new ProcessingException("Could not build DOM for '" + 437 source.getURI() + "'"); 438 } 439 440 return document; 441 } 442 443 451 static public ProcessingException handle(SourceException se) { 452 if (se instanceof SourceNotFoundException) { 453 return new ResourceNotFoundException("Resource not found.", se); 454 } 455 return new ProcessingException("Exception during source resolving.", 456 se); 457 } 458 459 468 static public ProcessingException handle(String message, 469 SourceException se) { 470 if (se instanceof SourceNotFoundException) { 471 return new ResourceNotFoundException(message, se); 472 } 473 return new ProcessingException(message, se); 474 } 475 476 482 static public void handleSAXException(String source, SAXException e) 483 throws ProcessingException, IOException , SAXException { 484 final Exception cause = e.getException(); 485 if (cause != null) { 486 if (cause instanceof SourceException) { 490 throw handle((SourceException) cause); 491 } 492 if (cause instanceof ProcessingException) { 493 throw (ProcessingException) cause; 494 } 495 if (cause instanceof IOException ) { 496 throw (IOException ) cause; 497 } 498 if (cause instanceof SAXException ) { 499 throw (SAXException ) cause; 500 } 501 throw new ProcessingException("Could not read resource " + 502 source, cause); 503 } 504 throw e; 505 } 506 507 518 static public InputSource getInputSource(Source source) 519 throws IOException , ProcessingException { 520 try { 521 final InputSource newObject = new InputSource (source.getInputStream()); 522 523 newObject.setSystemId(source.getURI()); 524 return newObject; 525 } catch (SourceException se) { 526 throw handle(se); 527 } 528 } 529 530 546 static public Source getSource(String uri, 547 Parameters typeParameters, 548 SourceParameters resourceParameters, 549 SourceResolver resolver) 550 throws IOException , SAXException , SourceException { 551 552 int queryPos = uri.indexOf('?'); 554 if (queryPos != -1) { 555 String queryString = uri.substring(queryPos+1); 556 SourceParameters queries = new SourceParameters(queryString); 557 558 if (queries.hasParameters()) { 559 StringBuffer buffer = new StringBuffer (uri.substring(0, queryPos)); 560 char separator = '?'; 561 562 Iterator i = queries.getParameterNames(); 563 while (i.hasNext()) { 564 String current = (String ) i.next(); 565 Iterator values = queries.getParameterValues(current); 566 while (values.hasNext()) { 567 buffer.append(separator) 568 .append(current) 569 .append('=') 570 .append(NetUtils.encode((String ) values.next(), "utf-8")); 571 separator = '&'; 572 } 573 } 574 uri = buffer.toString(); 575 } 576 } 577 578 String method = ((typeParameters!=null) 579 ? typeParameters.getParameter("method", "GET") 580 : "GET"); 581 if (method.equalsIgnoreCase("POST") && 582 (resourceParameters == null || 583 !resourceParameters.hasParameters())) { 584 method = "GET"; 585 } 586 587 if (uri.startsWith("cocoon:") && resourceParameters != null && 588 resourceParameters.hasParameters()) { 589 int pos = uri.indexOf(";jsessionid="); 590 591 StringBuffer buf; 592 if (pos == -1) { 593 buf = new StringBuffer (uri); 594 } else { 595 buf = new StringBuffer (uri.substring(0, pos)); 596 } 597 buf.append(((uri.indexOf('?') == -1) ? '?' : '&')); 598 buf.append(resourceParameters.getEncodedQueryString()); 599 uri = buf.toString(); 600 } 601 602 Map resolverParameters = new HashMap (); 603 resolverParameters.put(SourceResolver.METHOD, method); 604 if (typeParameters != null) { 605 String encoding = typeParameters.getParameter("encoding", 606 System.getProperty("file.encoding", "ISO-8859-1")); 607 if (encoding != null && !"".equals(encoding)) { 608 resolverParameters.put(SourceResolver.URI_ENCODING, encoding); 609 } 610 } 611 resolverParameters.put(SourceResolver.URI_PARAMETERS, 612 resourceParameters); 613 614 return resolver.resolveURI(uri, null, resolverParameters); 615 } 616 617 635 public static void writeDOM(String location, 636 Parameters typeParameters, 637 SourceParameters parameters, 638 DocumentFragment frag, 639 SourceResolver resolver, 640 String serializerName) 641 throws ProcessingException { 642 Source source = null; 643 644 try { 645 source = SourceUtil.getSource(location, typeParameters, 646 parameters, resolver); 647 if (source instanceof ModifiableSource) { 648 ModifiableSource ws = (ModifiableSource) source; 649 650 frag.normalize(); 651 652 if (null != serializerName) { 653 ComponentManager manager = CocoonComponentManager.getSitemapComponentManager(); 654 655 ComponentSelector selector = null; 656 Serializer serializer = null; 657 OutputStream oStream = null; 658 try { 659 selector = (ComponentSelector)manager.lookup(Serializer.ROLE + "Selector"); 660 serializer = (Serializer)selector.select(serializerName); 661 oStream = ws.getOutputStream(); 662 serializer.setOutputStream(oStream); 663 serializer.startDocument(); 664 DOMStreamer streamer = new DOMStreamer(serializer); 665 streamer.stream(frag); 666 serializer.endDocument(); 667 } catch (ComponentException e) { 668 throw new ProcessingException("Unable to lookup serializer.", e); 669 } finally { 670 if (oStream != null) { 671 oStream.flush(); 672 try { 673 oStream.close(); 674  
|