KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > SourceUtil


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
50 import org.w3c.dom.DocumentFragment JavaDoc;
51 import org.xml.sax.ContentHandler JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54 import org.xml.sax.helpers.DefaultHandler JavaDoc;
55
56 import javax.xml.transform.OutputKeys JavaDoc;
57 import java.io.ByteArrayOutputStream JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.InputStreamReader JavaDoc;
60 import java.io.OutputStream JavaDoc;
61 import java.io.Reader JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.Map JavaDoc;
65 import java.util.Properties JavaDoc;
66
67 /**
68  * This class contains some utility methods for the source resolving.
69  *
70  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
71  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
72  * @version $Id: SourceUtil.java 178777 2005-05-27 14:02:09Z vgritsenko $
73  */

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             // Should not happen
83
e.printStackTrace();
84         }
85     }
86
87     /** Avoid instantiation */
88     private SourceUtil() {
89     }
90
91     /**
92      * Generates SAX events from the XMLizable and handle SAXException.
93      *
94      * @param source the data
95      */

96     static public void toSAX(XMLizable source,
97                              ContentHandler JavaDoc handler)
98     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
99         try {
100             source.toSAX(handler);
101         } catch (SAXException JavaDoc e) {
102             // Unwrap ProcessingException, IOException, and extreme cases of SAXExceptions.
103
// Handle SourceException.
104
// See also handleSAXException
105
final Exception JavaDoc 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 JavaDoc) {
114                     throw (IOException JavaDoc) cause;
115                 }
116                 if (cause instanceof SAXException JavaDoc) {
117                     throw (SAXException JavaDoc) cause;
118                 }
119             }
120
121             // Throw original SAX exception
122
throw e;
123         }
124     }
125
126     /**
127      * Generates SAX events from the given source.
128      *
129      * <p><b>NOTE</b>: If the implementation can produce lexical events,
130      * care should be taken that <code>handler</code> can actually
131      * directly implement the LexicalHandler interface!</p>
132      *
133      * @param source the data
134      * @throws ProcessingException if no suitable converter is found
135      */

136     static public void toSAX(Source source,
137                              ContentHandler JavaDoc handler)
138     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
139         toSAX(CocoonComponentManager.getSitemapComponentManager(),
140               source, null, handler);
141     }
142
143     /**
144      * Generates SAX events from the given source by using XMLizer.
145      * Current sitemap manager will be used to lookup XMLizer.
146      *
147      * <p><b>NOTE</b>: If the implementation can produce lexical events,
148      * care should be taken that <code>handler</code> can actually
149      * directly implement the LexicalHandler interface!</p>
150      *
151      * @param source the data
152      * @throws ProcessingException if no suitable converter is found
153      */

154     static public void toSAX(Source source,
155                              String JavaDoc mimeTypeHint,
156                              ContentHandler JavaDoc handler)
157     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
158         toSAX(CocoonComponentManager.getSitemapComponentManager(),
159               source, mimeTypeHint, handler);
160     }
161
162     /**
163      * Generates SAX events from the given source by using XMLizer.
164      *
165      * <p><b>NOTE</b>: If the implementation can produce lexical events,
166      * care should be taken that <code>handler</code> can actually
167      * directly implement the LexicalHandler interface!</p>
168      *
169      * @param source the data
170      * @throws ProcessingException if no suitable converter is found
171      * @deprecated Use the {@link #toSAX(ServiceManager, Source, String, ContentHandler)}
172      * method instead.
173      */

174     static public void toSAX(ComponentManager manager,
175                              Source source,
176                              String JavaDoc mimeTypeHint,
177                              ContentHandler JavaDoc handler)
178     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
179         if (source instanceof XMLizable) {
180             toSAX((XMLizable) source, handler);
181         } else {
182             String JavaDoc 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     /**
205      * Generates SAX events from the given source
206      *
207      * <p><b>NOTE</b>: If the implementation can produce lexical events,
208      * care should be taken that <code>handler</code> can actually
209      * directly implement the LexicalHandler interface!</p>
210      *
211      * @param source the data
212      * @throws ProcessingException if no suitable converter is found
213      */

214     static public void toSAX(ServiceManager manager,
215                              Source source,
216                              String JavaDoc mimeTypeHint,
217                              ContentHandler JavaDoc handler)
218     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
219         if (source instanceof XMLizable) {
220             toSAX((XMLizable) source, handler);
221         } else {
222             String JavaDoc 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     /**
245      * Generates SAX events from the given source by parsing it.
246      *
247      * <p><b>NOTE</b>: If the implementation can produce lexical events,
248      * care should be taken that <code>handler</code> can actually
249      * directly implement the LexicalHandler interface!</p>
250      *
251      * @param source the data
252      * @throws ProcessingException if no suitable converter is found
253      * @deprecated Use {@link #parse(ServiceManager, Source, ContentHandler)}.
254      */

255     static public void parse(ComponentManager manager,
256                              Source source,
257                              ContentHandler JavaDoc handler)
258     throws SAXException JavaDoc, IOException JavaDoc, 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     /**
277      * Generates character SAX events from the given source.
278      *
279      * @param source The data
280      * @param encoding The character encoding of the data
281      */

282     static public void toCharacters(Source source,
283                                     String JavaDoc encoding,
284                                     ContentHandler JavaDoc handler)
285     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
286         try {
287             Reader JavaDoc r = encoding == null?
288                     new InputStreamReader JavaDoc(source.getInputStream()):
289                     new InputStreamReader JavaDoc(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 JavaDoc e) {
301             handleSAXException(source.getURI(), e);
302         }
303     }
304
305     /**
306      * Generates SAX events from the given source by parsing it.
307      *
308      * <p><b>NOTE</b>: If the implementation can produce lexical events,
309      * care should be taken that <code>handler</code> can actually
310      * directly implement the LexicalHandler interface!</p>
311      *
312      * @param source the data
313      * @throws ProcessingException if no suitable converter is found
314      */

315     static public void parse(ServiceManager manager,
316                              Source source,
317                              ContentHandler JavaDoc handler)
318     throws SAXException JavaDoc, IOException JavaDoc, 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     /**
337      * Generates SAX events from the given source
338      *
339      * <p><b>NOTE</b>: If the implementation can produce lexical events,
340      * care should be taken that <code>handler</code> can actually
341      * directly implement the LexicalHandler interface!</p>
342      *
343      * @param source the data
344      * @throws ProcessingException if no suitable converter is found
345      */

346     static public void toSAX(Source source,
347                              ContentHandler JavaDoc handler,
348                              Parameters typeParameters,
349                              boolean filterDocumentEvent)
350     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
351         // Test for url rewriting
352
if (typeParameters != null
353             && typeParameters.getParameter(URLRewriter.PARAMETER_MODE, null) != null) {
354             handler = new URLRewriter(typeParameters, handler);
355         }
356         String JavaDoc 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     /**
369      * Generates a DOM from the given source
370      * @param source The data
371      *
372      * @return Created DOM document.
373      *
374      * @throws IOException If a io exception occurs.
375      * @throws ProcessingException if no suitable converter is found
376      * @throws SAXException If a SAX exception occurs.
377      */

378     static public Document JavaDoc toDOM(Source source)
379     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
380         DOMBuilder builder = new DOMBuilder();
381
382         toSAX(source, builder);
383
384         Document JavaDoc 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     /**
394      * Generates a DOM from the given source
395      * @param source The data
396      *
397      * @return Created DOM document.
398      *
399      * @throws IOException If a io exception occurs.
400      * @throws ProcessingException if no suitable converter is found
401      * @throws SAXException If a SAX exception occurs.
402      */

403     static public Document JavaDoc toDOM(ServiceManager manager, Source source)
404     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
405         DOMBuilder builder = new DOMBuilder();
406
407         toSAX(manager, source, null, builder);
408
409         Document JavaDoc 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     /**
419      * Generates a DOM from the given source
420      * @param source The data
421      *
422      * @return Created DOM document.
423      *
424      * @throws IOException If a io exception occurs.
425      * @throws ProcessingException if no suitable converter is found
426      * @throws SAXException If a SAX exception occurs.
427      */

428     static public Document JavaDoc toDOM(ServiceManager manager, String JavaDoc mimeTypeHint, Source source)
429     throws SAXException JavaDoc, IOException JavaDoc, ProcessingException {
430         DOMBuilder builder = new DOMBuilder();
431
432         toSAX(manager, source, mimeTypeHint, builder);
433
434         Document JavaDoc 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     /**
444      * Make a ProcessingException from a SourceException.
445      * If the exception is a SourceNotFoundException then a
446      * ResourceNotFoundException is thrown.
447      *
448      * @param se Source exception
449      * @return Created processing exception.
450      */

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     /**
460      * Make a ProcessingException from a SourceException.
461      * If the exception is a SourceNotFoundException then a
462      * ResourceNotFoundException is thrown.
463      *
464      * @param message Additional exception message.
465      * @param se Source exception.
466      * @return Created processing exception.
467      */

468     static public ProcessingException handle(String JavaDoc 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     /**
477      * Handle SAXException catched in Generator's generate method.
478      *
479      * @param source Generator's source
480      * @param e SAXException happened in the generator's generate method.
481      */

482     static public void handleSAXException(String JavaDoc source, SAXException JavaDoc e)
483     throws ProcessingException, IOException JavaDoc, SAXException JavaDoc {
484         final Exception JavaDoc cause = e.getException();
485         if (cause != null) {
486             // Unwrap ProcessingException, IOException, and extreme cases of SAXExceptions.
487
// Handle SourceException.
488
// See also toSax(XMLizable, ContentHandler)
489
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 JavaDoc) {
496                 throw (IOException JavaDoc) cause;
497             }
498             if (cause instanceof SAXException JavaDoc) {
499                 throw (SAXException JavaDoc) cause;
500             }
501             throw new ProcessingException("Could not read resource " +
502                                           source, cause);
503         }
504         throw e;
505     }
506
507     /**
508      * Get an InputSource object
509      *
510      * @param source Source.
511      *
512      * @return Input stream of the source.
513      *
514      * @throws IOException If a io exception occurs.
515      * @throws ProcessingException If an exception occurs during
516      * processing.
517      */

518     static public InputSource JavaDoc getInputSource(Source source)
519     throws IOException JavaDoc, ProcessingException {
520         try {
521             final InputSource JavaDoc newObject = new InputSource JavaDoc(source.getInputStream());
522
523             newObject.setSystemId(source.getURI());
524             return newObject;
525         } catch (SourceException se) {
526             throw handle(se);
527         }
528     }
529
530     /**
531      * Get a <code>Source</code> object
532      *
533      * @param uri URI of the source.
534      * @param typeParameters Type of Source query. Currently, only
535      * <code>method</code> parameter (value typically <code>GET</code> or
536      * <code>POST</code>) is recognized. May be <code>null</code>.
537      * @param resourceParameters Parameters of the source. May be <code>null</code>
538      * @param resolver Resolver for the source.
539      *
540      * @return The resolved source.
541      *
542      * @throws IOException If a io exception occurs.
543      * @throws SAXException If a SAX exception occurs.
544      * @throws SourceException If the source an exception throws.
545      */

546     static public Source getSource(String JavaDoc uri,
547                                    Parameters typeParameters,
548                                    SourceParameters resourceParameters,
549                                    SourceResolver resolver)
550     throws IOException JavaDoc, SAXException JavaDoc, SourceException {
551
552         // first step: encode parameters which are already appended to the url
553
int queryPos = uri.indexOf('?');
554         if (queryPos != -1) {
555             String JavaDoc queryString = uri.substring(queryPos+1);
556             SourceParameters queries = new SourceParameters(queryString);
557
558             if (queries.hasParameters()) {
559                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(uri.substring(0, queryPos));
560                 char separator = '?';
561
562                 Iterator JavaDoc i = queries.getParameterNames();
563                 while (i.hasNext()) {
564                     String JavaDoc current = (String JavaDoc) i.next();
565                     Iterator JavaDoc values = queries.getParameterValues(current);
566                     while (values.hasNext()) {
567                         buffer.append(separator)
568                                 .append(current)
569                                 .append('=')
570                                 .append(NetUtils.encode((String JavaDoc) values.next(), "utf-8"));
571                         separator = '&';
572                     }
573                 }
574                 uri = buffer.toString();
575             }
576         }
577
578         String JavaDoc 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 JavaDoc buf;
592             if (pos == -1) {
593                 buf = new StringBuffer JavaDoc(uri);
594             } else {
595                 buf = new StringBuffer JavaDoc(uri.substring(0, pos));
596             }
597             buf.append(((uri.indexOf('?') == -1) ? '?' : '&'));
598             buf.append(resourceParameters.getEncodedQueryString());
599             uri = buf.toString();
600         }
601
602         Map JavaDoc resolverParameters = new HashMap JavaDoc();
603         resolverParameters.put(SourceResolver.METHOD, method);
604         if (typeParameters != null) {
605             String JavaDoc 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     /**
618      * Write a DOM Fragment to a source.
619      * If the source is a ModifiableSource the interface is used.
620      * If not, the source is invoked with an additional parameter named
621      * "content" containing the XML.
622      *
623      * @param location URI of the Source
624      * @param typeParameters Type of Source query. Currently, only
625      * <code>method</code> parameter (value typically <code>GET</code> or
626      * <code>POST</code>) is recognized. May be <code>null</code>.
627      * @param parameters Parameters (e.g. URL params) of the source.
628      * May be <code>null</code>
629      * @param frag DOM fragment to serialize to the Source
630      * @param resolver Resolver for the source.
631      * @param serializerName The serializer to use
632      *
633      * @throws ProcessingException
634      */

635     public static void writeDOM(String JavaDoc location,
636                                 Parameters typeParameters,
637                                 SourceParameters parameters,
638                                 DocumentFragment JavaDoc frag,
639                                 SourceResolver resolver,
640                                 String JavaDoc 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 JavaDoc 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                             } catch (Exception JavaDoc ignore) {
675                             }
676                         }
677                         if (selector != null) {
678                             selector.release(serializer);
679                             manager.release(selector);
680                         }
681                     }
682                 } else {
683                     Properties JavaDoc props = XMLUtils.createPropertiesForXML(false);
684                     props.put(OutputKeys.ENCODING, "ISO-8859-1");
685                     final String JavaDoc content = XMLUtils.serializeNode(frag, props);
686                     OutputStream JavaDoc oStream = ws.getOutputStream();
687
688                     oStream.write(content.getBytes());
689                     oStream.flush();
690                     oStream.close();
691                 }
692             } else {
693                 String JavaDoc content;
694                 if (null != serializerName) {
695                     ComponentManager manager = CocoonComponentManager.getSitemapComponentManager();
696
697                     ComponentSelector selector = null;
698                     Serializer serializer = null;
699                     ByteArrayOutputStream JavaDoc oStream = new ByteArrayOutputStream JavaDoc();
700                     try {
701                         selector = (ComponentSelector)manager.lookup(Serializer.ROLE + "Selector");
702                         serializer = (Serializer)selector.select(serializerName);
703                         serializer.setOutputStream(oStream);
704                         serializer.startDocument();
705                         DOMStreamer streamer = new DOMStreamer(serializer);
706                         streamer.stream(frag);
707                         serializer.endDocument();
708                     } catch (ComponentException e) {
709                         throw new ProcessingException("Unable to lookup serializer.", e);
710                     } finally {
711                         if (oStream != null) {
712                             oStream.flush();
713                             try {
714                                 oStream.close();
715                             } catch (Exception JavaDoc ignore) {
716                             }
717                         }
718                         if (selector != null) {
719                             selector.release(serializer);
720                             manager.release(selector);
721                         }
722                     }
723                     content = oStream.toString();
724                 } else {
725                     Properties JavaDoc props = XMLUtils.createPropertiesForXML(false);
726                     props.put(OutputKeys.ENCODING, "ISO-8859-1");
727                     content = XMLUtils.serializeNode(frag, props);
728                 }
729
730                 if (parameters == null) {
731                     parameters = new SourceParameters();
732                 } else {
733                     parameters = (SourceParameters) parameters.clone();
734                 }
735                 parameters.setSingleParameterValue("content", content);
736
737                 source = SourceUtil.getSource(location, typeParameters,
738                                               parameters, resolver);
739                 SourceUtil.toSAX(source, new DefaultHandler JavaDoc());
740             }
741         } catch (SourceException e) {
742             throw SourceUtil.handle(e);
743         } catch (IOException JavaDoc e) {
744             throw new ProcessingException(e);
745         } catch (SAXException JavaDoc e) {
746             throw new ProcessingException(e);
747         } finally {
748             resolver.release(source);
749         }
750     }
751
752     /**
753      * Read a DOM Fragment from a source
754      *
755      * @param location URI of the Source
756      * @param typeParameters Type of Source query. Currently, only
757      * <code>method</code> parameter (value typically <code>GET</code> or
758      * <code>POST</code>) is recognized. May be <code>null</code>.
759      * @param parameters Parameters (e.g. URL params) of the source.
760      * May be <code>null</code>
761      * @param resolver Resolver for the source.
762      *
763      * @return DOM <code>DocumentFragment</code> constructed from the specified
764      * source.
765      *
766      * @throws ProcessingException
767      */

768     public static DocumentFragment JavaDoc readDOM(String JavaDoc location,
769                                            Parameters typeParameters,
770                                            SourceParameters parameters,
771                                            SourceResolver resolver)
772     throws ProcessingException {
773
774         Source source = null;
775         try {
776             source = SourceUtil.getSource(location, typeParameters, parameters, resolver);
777             Document JavaDoc doc = SourceUtil.toDOM(source);
778
779             DocumentFragment JavaDoc fragment = doc.createDocumentFragment();
780             fragment.appendChild(doc.getDocumentElement());
781
782             return fragment;
783         } catch (SourceException e) {
784             throw SourceUtil.handle(e);
785         } catch (IOException JavaDoc e) {
786             throw new ProcessingException(e);
787         } catch (SAXException JavaDoc e) {
788             throw new ProcessingException(e);
789         } finally {
790             resolver.release(source);
791         }
792     }
793
794     /**
795      * Return the scheme of a URI. Just as there are many different methods
796      * of access to resources, there are a variety of schemes for identifying
797      * such resources.
798      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
799      *
800      * @param uri Uniform resource identifier.
801      *
802      * @return Scheme of the URI.
803      */

804     public static String JavaDoc getScheme(String JavaDoc uri) {
805         RE re = new RE(uripattern);
806         if (re.match(uri)) {
807             return re.getParen(2);
808         } else {
809             throw new IllegalArgumentException JavaDoc("'" + uri +
810                                                "' is not a correct URI");
811         }
812     }
813
814     /**
815      * Return the authority of a URI. This authority is
816      * typically defined by an Internet-based server or a scheme-specific
817      * registry of naming authorities
818      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
819      *
820      * @param uri Uniform resource identifier.
821      *
822      * @return Scheme of the URI.
823      */

824     public static String JavaDoc getAuthority(String JavaDoc uri) {
825         RE re = new RE(uripattern);
826         if (re.match(uri)) {
827             return re.getParen(4);
828         } else {
829             throw new IllegalArgumentException JavaDoc("'" + uri +
830                                                "' is not a correct URI");
831         }
832     }
833
834     /**
835      * Return the path of a URI. The path contains data, specific to the
836      * authority (or the scheme if there is no authority component),
837      * identifying the resource within the scope of that scheme and authority
838      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
839      *
840      * @param uri Uniform resource identifier.
841      *
842      * @return Path of the URI.
843      */

844     public static String JavaDoc getPath(String JavaDoc uri) {
845         RE re = new RE(uripattern);
846         if (re.match(uri)) {
847             return re.getParen(5);
848         } else {
849             throw new IllegalArgumentException JavaDoc("'" + uri +
850                                                "' is not a correct URI");
851         }
852     }
853
854     /**
855      * Return the path of a URI, if the URI can't contains a authority.
856      * This implementation differ to the RFC 2396.
857      *
858      * @param uri Uniform resource identifier.
859      *
860      * @return Path of the URI.
861      */

862     public static String JavaDoc getPathWithoutAuthority(String JavaDoc uri) {
863         RE re = new RE(uripattern);
864         if (re.match(uri)) {
865             return re.getParen(4) + re.getParen(5);
866         } else {
867             throw new IllegalArgumentException JavaDoc("'" + uri +
868                                                "' is not a correct URI");
869         }
870     }
871
872     /**
873      * Return the query of a URI. The query is a string of information to
874      * be interpreted by the resource
875      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
876      *
877      * @param uri Uniform resource identifier.
878      *
879      * @return Query of the URI.
880      */

881     public static String JavaDoc getQuery(String JavaDoc uri) {
882         RE re = new RE(uripattern);
883         if (re.match(uri)) {
884             return re.getParen(7);
885         } else {
886             throw new IllegalArgumentException JavaDoc("'" + uri +
887                                                "' is not a correct URI");
888         }
889     }
890
891     /**
892      * Return the fragment of a URI. When a URI reference is used to perform
893      * a retrieval action on the identified resource, the optional fragment
894      * identifier, consists of additional reference information to be
895      * interpreted by the user agent after the retrieval action has been
896      * successfully completed
897      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
898      *
899      * @param uri Uniform resource identifier.
900      *
901      * @return Fragment of the URI.
902      */

903     public static String JavaDoc getFragment(String JavaDoc uri) {
904         RE re = new RE(uripattern);
905         if (re.match(uri)) {
906             return re.getParen(9);
907         } else {
908             throw new IllegalArgumentException JavaDoc("'" + uri +
909                                                "' is not a correct URI");
910         }
911     }
912 }
913
Popular Tags