Source Code Cross Referenced for SourceUtil.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » cocoon » components » source » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.cocoon.components.source 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.cocoon.components.source;
018:
019:        import org.apache.avalon.framework.component.Component;
020:        import org.apache.avalon.framework.component.ComponentException;
021:        import org.apache.avalon.framework.component.ComponentManager;
022:        import org.apache.avalon.framework.component.ComponentSelector;
023:        import org.apache.avalon.framework.parameters.Parameters;
024:        import org.apache.avalon.framework.service.ServiceException;
025:        import org.apache.avalon.framework.service.ServiceManager;
026:
027:        import org.apache.cocoon.ProcessingException;
028:        import org.apache.cocoon.ResourceNotFoundException;
029:        import org.apache.cocoon.components.CocoonComponentManager;
030:        import org.apache.cocoon.serialization.Serializer;
031:        import org.apache.cocoon.util.NetUtils;
032:        import org.apache.cocoon.xml.IncludeXMLConsumer;
033:        import org.apache.cocoon.xml.XMLUtils;
034:        import org.apache.cocoon.xml.dom.DOMBuilder;
035:        import org.apache.cocoon.xml.dom.DOMStreamer;
036:
037:        import org.apache.excalibur.source.ModifiableSource;
038:        import org.apache.excalibur.source.Source;
039:        import org.apache.excalibur.source.SourceException;
040:        import org.apache.excalibur.source.SourceNotFoundException;
041:        import org.apache.excalibur.source.SourceParameters;
042:        import org.apache.excalibur.source.SourceResolver;
043:        import org.apache.excalibur.xml.sax.SAXParser;
044:        import org.apache.excalibur.xml.sax.XMLizable;
045:        import org.apache.excalibur.xmlizer.XMLizer;
046:        import org.apache.regexp.RE;
047:        import org.apache.regexp.RECompiler;
048:        import org.apache.regexp.REProgram;
049:        import org.apache.regexp.RESyntaxException;
050:        import org.w3c.dom.Document;
051:        import org.w3c.dom.DocumentFragment;
052:        import org.xml.sax.ContentHandler;
053:        import org.xml.sax.InputSource;
054:        import org.xml.sax.SAXException;
055:        import org.xml.sax.helpers.DefaultHandler;
056:
057:        import javax.xml.transform.OutputKeys;
058:        import java.io.ByteArrayOutputStream;
059:        import java.io.IOException;
060:        import java.io.InputStreamReader;
061:        import java.io.OutputStream;
062:        import java.io.Reader;
063:        import java.util.HashMap;
064:        import java.util.Iterator;
065:        import java.util.Map;
066:        import java.util.Properties;
067:
068:        /**
069:         * This class contains some utility methods for the source resolving.
070:         *
071:         * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
072:         * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
073:         * @version $Id: SourceUtil.java 485224 2006-12-10 17:24:05Z cziegeler $
074:         */
075:        public final class SourceUtil {
076:
077:            private static REProgram uripattern = null;
078:
079:            static {
080:                try {
081:                    uripattern = new RECompiler()
082:                            .compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$");
083:                } catch (RESyntaxException e) {
084:                    // Should not happen
085:                    e.printStackTrace();
086:                }
087:            }
088:
089:            /** Avoid instantiation */
090:            private SourceUtil() {
091:            }
092:
093:            /**
094:             * Generates SAX events from the XMLizable and handle SAXException.
095:             *
096:             * @param  source    the data
097:             */
098:            static public void toSAX(XMLizable source, ContentHandler handler)
099:                    throws SAXException, IOException, ProcessingException {
100:                try {
101:                    source.toSAX(handler);
102:                } catch (SAXException e) {
103:                    // Unwrap ProcessingException, IOException, and extreme cases of SAXExceptions.
104:                    // Handle SourceException.
105:                    // See also handleSAXException
106:                    final Exception cause = e.getException();
107:                    if (cause != null) {
108:                        if (cause instanceof  SourceException) {
109:                            throw handle((SourceException) cause);
110:                        }
111:                        if (cause instanceof  ProcessingException) {
112:                            throw (ProcessingException) cause;
113:                        }
114:                        if (cause instanceof  IOException) {
115:                            throw (IOException) cause;
116:                        }
117:                        if (cause instanceof  SAXException) {
118:                            throw (SAXException) cause;
119:                        }
120:                    }
121:
122:                    // Throw original SAX exception
123:                    throw e;
124:                }
125:            }
126:
127:            /**
128:             * Generates SAX events from the given source.
129:             *
130:             * <p><b>NOTE</b>: If the implementation can produce lexical events,
131:             * care should be taken that <code>handler</code> can actually
132:             * directly implement the LexicalHandler interface!</p>
133:             *
134:             * @param  source    the data
135:             * @throws ProcessingException if no suitable converter is found
136:             */
137:            static public void toSAX(Source source, ContentHandler handler)
138:                    throws SAXException, IOException, 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, String mimeTypeHint,
155:                    ContentHandler handler) throws SAXException, IOException,
156:                    ProcessingException {
157:                toSAX(CocoonComponentManager.getSitemapComponentManager(),
158:                        source, mimeTypeHint, handler);
159:            }
160:
161:            /**
162:             * Generates SAX events from the given source by using XMLizer.
163:             *
164:             * <p><b>NOTE</b>: If the implementation can produce lexical events,
165:             * care should be taken that <code>handler</code> can actually
166:             * directly implement the LexicalHandler interface!</p>
167:             *
168:             * @param  source    the data
169:             * @throws ProcessingException if no suitable converter is found
170:             * @deprecated Use the {@link #toSAX(ServiceManager, Source, String, ContentHandler)}
171:             *             method instead.
172:             */
173:            static public void toSAX(ComponentManager manager, Source source,
174:                    String mimeTypeHint, ContentHandler handler)
175:                    throws SAXException, IOException, ProcessingException {
176:                if (source instanceof  XMLizable) {
177:                    toSAX((XMLizable) source, handler);
178:                } else {
179:                    String mimeType = source.getMimeType();
180:                    if (null == mimeType) {
181:                        mimeType = mimeTypeHint;
182:                    }
183:
184:                    XMLizer xmlizer = null;
185:                    try {
186:                        xmlizer = (XMLizer) manager.lookup(XMLizer.ROLE);
187:                        xmlizer.toSAX(source.getInputStream(), mimeType, source
188:                                .getURI(), handler);
189:                    } catch (SourceException e) {
190:                        throw SourceUtil.handle(e);
191:                    } catch (ComponentException e) {
192:                        throw new ProcessingException(
193:                                "Exception during streaming source.", e);
194:                    } finally {
195:                        manager.release((Component) xmlizer);
196:                    }
197:                }
198:            }
199:
200:            /**
201:             * Generates SAX events from the given source
202:             *
203:             * <p><b>NOTE</b>: If the implementation can produce lexical events,
204:             * care should be taken that <code>handler</code> can actually
205:             * directly implement the LexicalHandler interface!</p>
206:             *
207:             * @param  source    the data
208:             * @throws ProcessingException if no suitable converter is found
209:             */
210:            static public void toSAX(ServiceManager manager, Source source,
211:                    String mimeTypeHint, ContentHandler handler)
212:                    throws SAXException, IOException, ProcessingException {
213:                if (source instanceof  XMLizable) {
214:                    toSAX((XMLizable) source, handler);
215:                } else {
216:                    String mimeType = source.getMimeType();
217:                    if (null == mimeType) {
218:                        mimeType = mimeTypeHint;
219:                    }
220:
221:                    XMLizer xmlizer = null;
222:                    try {
223:                        xmlizer = (XMLizer) manager.lookup(XMLizer.ROLE);
224:                        xmlizer.toSAX(source.getInputStream(), mimeType, source
225:                                .getURI(), handler);
226:                    } catch (SourceException e) {
227:                        throw SourceUtil.handle(e);
228:                    } catch (ServiceException e) {
229:                        throw new ProcessingException(
230:                                "Exception during streaming source.", e);
231:                    } finally {
232:                        manager.release(xmlizer);
233:                    }
234:                }
235:            }
236:
237:            /**
238:             * Generates SAX events from the given source by parsing it.
239:             *
240:             * <p><b>NOTE</b>: If the implementation can produce lexical events,
241:             * care should be taken that <code>handler</code> can actually
242:             * directly implement the LexicalHandler interface!</p>
243:             *
244:             * @param  source    the data
245:             * @throws ProcessingException if no suitable converter is found
246:             * @deprecated Use {@link #parse(ServiceManager, Source, ContentHandler)}.
247:             */
248:            static public void parse(ComponentManager manager, Source source,
249:                    ContentHandler handler) throws SAXException, IOException,
250:                    ProcessingException {
251:                if (source instanceof  XMLizable) {
252:                    toSAX((XMLizable) source, handler);
253:                } else {
254:                    SAXParser parser = null;
255:                    try {
256:                        parser = (SAXParser) manager.lookup(SAXParser.ROLE);
257:                        parser.parse(getInputSource(source), handler);
258:                    } catch (SourceException e) {
259:                        throw SourceUtil.handle(e);
260:                    } catch (ComponentException e) {
261:                        throw new ProcessingException(
262:                                "Exception during parsing source.", e);
263:                    } finally {
264:                        manager.release((Component) parser);
265:                    }
266:                }
267:            }
268:
269:            /**
270:             * Generates character SAX events from the given source.
271:             *
272:             * @param source The data
273:             * @param encoding The character encoding of the data
274:             */
275:            static public void toCharacters(Source source, String encoding,
276:                    ContentHandler handler) throws SAXException, IOException,
277:                    ProcessingException {
278:                try {
279:                    Reader r = encoding == null ? new InputStreamReader(source
280:                            .getInputStream()) : new InputStreamReader(source
281:                            .getInputStream(), encoding);
282:
283:                    int len;
284:                    char[] chr = new char[4096];
285:                    try {
286:                        while ((len = r.read(chr)) > 0) {
287:                            handler.characters(chr, 0, len);
288:                        }
289:                    } finally {
290:                        r.close();
291:                    }
292:                } catch (SAXException e) {
293:                    handleSAXException(source.getURI(), e);
294:                }
295:            }
296:
297:            /**
298:             * Generates SAX events from the given source by parsing it.
299:             *
300:             * <p><b>NOTE</b>: If the implementation can produce lexical events,
301:             * care should be taken that <code>handler</code> can actually
302:             * directly implement the LexicalHandler interface!</p>
303:             *
304:             * @param  source    the data
305:             * @throws ProcessingException if no suitable converter is found
306:             */
307:            static public void parse(ServiceManager manager, Source source,
308:                    ContentHandler handler) throws SAXException, IOException,
309:                    ProcessingException {
310:                if (source instanceof  XMLizable) {
311:                    toSAX((XMLizable) source, handler);
312:                } else {
313:                    SAXParser parser = null;
314:                    try {
315:                        parser = (SAXParser) manager.lookup(SAXParser.ROLE);
316:                        parser.parse(getInputSource(source), handler);
317:                    } catch (SourceException e) {
318:                        throw SourceUtil.handle(e);
319:                    } catch (ServiceException e) {
320:                        throw new ProcessingException(
321:                                "Exception during parsing source.", e);
322:                    } finally {
323:                        manager.release(parser);
324:                    }
325:                }
326:            }
327:
328:            /**
329:             * Generates SAX events from the given source
330:             *
331:             * <p><b>NOTE</b>: If the implementation can produce lexical events,
332:             * care should be taken that <code>handler</code> can actually
333:             * directly implement the LexicalHandler interface!</p>
334:             *
335:             * @param  source    the data
336:             * @throws ProcessingException if no suitable converter is found
337:             */
338:            static public void toSAX(Source source, ContentHandler handler,
339:                    Parameters typeParameters, boolean filterDocumentEvent)
340:                    throws SAXException, IOException, ProcessingException {
341:                // Test for url rewriting
342:                if (typeParameters != null
343:                        && typeParameters.getParameter(
344:                                URLRewriter.PARAMETER_MODE, null) != null) {
345:                    handler = new URLRewriter(typeParameters, handler);
346:                }
347:                String mimeTypeHint = null;
348:                if (typeParameters != null) {
349:                    mimeTypeHint = typeParameters.getParameter("mime-type",
350:                            mimeTypeHint);
351:                }
352:                if (filterDocumentEvent) {
353:                    IncludeXMLConsumer filter = new IncludeXMLConsumer(handler);
354:                    toSAX(source, mimeTypeHint, filter);
355:                } else {
356:                    toSAX(source, mimeTypeHint, handler);
357:                }
358:            }
359:
360:            /**
361:             * Generates a DOM from the given source
362:             * @param source The data
363:             *
364:             * @return Created DOM document.
365:             *
366:             * @throws IOException If a io exception occurs.
367:             * @throws ProcessingException if no suitable converter is found
368:             * @throws SAXException If a SAX exception occurs.
369:             */
370:            static public Document toDOM(Source source) throws SAXException,
371:                    IOException, ProcessingException {
372:                DOMBuilder builder = new DOMBuilder();
373:
374:                toSAX(source, builder);
375:
376:                Document document = builder.getDocument();
377:                if (document == null) {
378:                    throw new ProcessingException("Could not build DOM for '"
379:                            + source.getURI() + "'");
380:                }
381:
382:                return document;
383:            }
384:
385:            /**
386:             * Generates a DOM from the given source
387:             * @param source The data
388:             *
389:             * @return Created DOM document.
390:             *
391:             * @throws IOException If a io exception occurs.
392:             * @throws ProcessingException if no suitable converter is found
393:             * @throws SAXException If a SAX exception occurs.
394:             */
395:            static public Document toDOM(ServiceManager manager, Source source)
396:                    throws SAXException, IOException, ProcessingException {
397:                DOMBuilder builder = new DOMBuilder();
398:
399:                toSAX(manager, source, null, builder);
400:
401:                Document document = builder.getDocument();
402:                if (document == null) {
403:                    throw new ProcessingException("Could not build DOM for '"
404:                            + source.getURI() + "'");
405:                }
406:
407:                return document;
408:            }
409:
410:            /**
411:             * Generates a DOM from the given source
412:             * @param source The data
413:             *
414:             * @return Created DOM document.
415:             *
416:             * @throws IOException If a io exception occurs.
417:             * @throws ProcessingException if no suitable converter is found
418:             * @throws SAXException If a SAX exception occurs.
419:             */
420:            static public Document toDOM(ServiceManager manager,
421:                    String mimeTypeHint, Source source) throws SAXException,
422:                    IOException, ProcessingException {
423:                DOMBuilder builder = new DOMBuilder();
424:
425:                toSAX(manager, source, mimeTypeHint, builder);
426:
427:                Document document = builder.getDocument();
428:                if (document == null) {
429:                    throw new ProcessingException("Could not build DOM for '"
430:                            + source.getURI() + "'");
431:                }
432:
433:                return document;
434:            }
435:
436:            /**
437:             * Make a ProcessingException from a SourceException.
438:             * If the exception is a SourceNotFoundException then a
439:             * ResourceNotFoundException is thrown.
440:             *
441:             * @param se Source exception
442:             * @return Created processing exception.
443:             */
444:            static public ProcessingException handle(SourceException se) {
445:                if (se instanceof  SourceNotFoundException) {
446:                    return new ResourceNotFoundException("Resource not found.",
447:                            se);
448:                }
449:                return new ProcessingException(
450:                        "Exception during source resolving.", se);
451:            }
452:
453:            /**
454:             * Make a ProcessingException from a SourceException.
455:             * If the exception is a SourceNotFoundException then a
456:             * ResourceNotFoundException is thrown.
457:             *
458:             * @param message Additional exception message.
459:             * @param se Source exception.
460:             * @return Created processing exception.
461:             */
462:            static public ProcessingException handle(String message,
463:                    SourceException se) {
464:                if (se instanceof  SourceNotFoundException) {
465:                    return new ResourceNotFoundException(message, se);
466:                }
467:                return new ProcessingException(message, se);
468:            }
469:
470:            /**
471:             * Handle SAXException catched in Generator's generate method.
472:             *
473:             * @param source Generator's source
474:             * @param e SAXException happened in the generator's generate method.
475:             */
476:            static public void handleSAXException(String source, SAXException e)
477:                    throws ProcessingException, IOException, SAXException {
478:                final Exception cause = e.getException();
479:                if (cause != null) {
480:                    // Unwrap ProcessingException, IOException, and extreme cases of SAXExceptions.
481:                    // Handle SourceException.
482:                    // See also toSax(XMLizable, ContentHandler)
483:                    if (cause instanceof  SourceException) {
484:                        throw handle((SourceException) cause);
485:                    }
486:                    if (cause instanceof  ProcessingException) {
487:                        throw (ProcessingException) cause;
488:                    }
489:                    if (cause instanceof  IOException) {
490:                        throw (IOException) cause;
491:                    }
492:                    if (cause instanceof  SAXException) {
493:                        throw (SAXException) cause;
494:                    }
495:                    throw new ProcessingException("Could not read resource "
496:                            + source, cause);
497:                }
498:                throw e;
499:            }
500:
501:            /**
502:             * Get an InputSource object
503:             *
504:             * @param source Source.
505:             *
506:             * @return Input stream of the source.
507:             *
508:             * @throws IOException If a io exception occurs.
509:             * @throws ProcessingException If an exception occurs during
510:             *                             processing.
511:             */
512:            static public InputSource getInputSource(Source source)
513:                    throws IOException, ProcessingException {
514:                try {
515:                    final InputSource newObject = new InputSource(source
516:                            .getInputStream());
517:
518:                    newObject.setSystemId(source.getURI());
519:                    return newObject;
520:                } catch (SourceException se) {
521:                    throw handle(se);
522:                }
523:            }
524:
525:            /**
526:             * Get a <code>Source</code> object
527:             *
528:             * @param uri URI of the source.
529:             * @param typeParameters Type of Source query.  Currently, only
530:             * <code>method</code> parameter (value typically <code>GET</code> or
531:             * <code>POST</code>) is recognized.  May be <code>null</code>.
532:             * @param resourceParameters Parameters of the source.  May be <code>null</code>
533:             * @param resolver Resolver for the source.
534:             *
535:             * @return The resolved source.
536:             *
537:             * @throws IOException If a io exception occurs.
538:             * @throws SAXException If a SAX exception occurs.
539:             * @throws SourceException If the source an exception throws.
540:             */
541:            static public Source getSource(String uri,
542:                    Parameters typeParameters,
543:                    SourceParameters resourceParameters, SourceResolver resolver)
544:                    throws IOException, SAXException, SourceException {
545:
546:                // first step: encode parameters which are already appended to the url
547:                int queryPos = uri.indexOf('?');
548:                if (queryPos != -1) {
549:                    String queryString = uri.substring(queryPos + 1);
550:                    SourceParameters queries = new SourceParameters(queryString);
551:
552:                    if (queries.hasParameters()) {
553:                        StringBuffer buffer = new StringBuffer(uri.substring(0,
554:                                queryPos));
555:                        char separator = '?';
556:
557:                        Iterator i = queries.getParameterNames();
558:                        while (i.hasNext()) {
559:                            String current = (String) i.next();
560:                            Iterator values = queries
561:                                    .getParameterValues(current);
562:                            while (values.hasNext()) {
563:                                buffer.append(separator).append(current)
564:                                        .append('=').append(
565:                                                NetUtils.encode((String) values
566:                                                        .next(), "utf-8"));
567:                                separator = '&';
568:                            }
569:                        }
570:                        uri = buffer.toString();
571:                    }
572:                }
573:
574:                String method = ((typeParameters != null) ? typeParameters
575:                        .getParameter("method", "GET") : "GET");
576:                if (method.equalsIgnoreCase("POST")
577:                        && (resourceParameters == null || !resourceParameters
578:                                .hasParameters())) {
579:                    method = "GET";
580:                }
581:
582:                if (uri.startsWith("cocoon:") && resourceParameters != null
583:                        && resourceParameters.hasParameters()) {
584:                    int pos = uri.indexOf(";jsessionid=");
585:
586:                    StringBuffer buf;
587:                    if (pos == -1) {
588:                        buf = new StringBuffer(uri);
589:                    } else {
590:                        buf = new StringBuffer(uri.substring(0, pos));
591:                    }
592:                    buf.append(((uri.indexOf('?') == -1) ? '?' : '&'));
593:                    buf.append(resourceParameters.getEncodedQueryString());
594:                    uri = buf.toString();
595:                }
596:
597:                Map resolverParameters = new HashMap();
598:                resolverParameters.put(SourceResolver.METHOD, method);
599:                if (typeParameters != null) {
600:                    String encoding = typeParameters.getParameter("encoding",
601:                            System.getProperty("file.encoding", "ISO-8859-1"));
602:                    if (encoding != null && !"".equals(encoding)) {
603:                        resolverParameters.put(SourceResolver.URI_ENCODING,
604:                                encoding);
605:                    }
606:                }
607:                resolverParameters.put(SourceResolver.URI_PARAMETERS,
608:                        resourceParameters);
609:
610:                return resolver.resolveURI(uri, null, resolverParameters);
611:            }
612:
613:            /**
614:             * Write a DOM Fragment to a source.
615:             * If the source is a ModifiableSource the interface is used.
616:             * If not, the source is invoked with an additional parameter named
617:             * "content" containing the XML.
618:             *
619:             * @param location URI of the Source
620:             * @param typeParameters Type of Source query.  Currently, only
621:             * <code>method</code> parameter (value typically <code>GET</code> or
622:             * <code>POST</code>) is recognized.  May be <code>null</code>.
623:             * @param parameters Parameters (e.g. URL params) of the source.
624:             * May be <code>null</code>
625:             * @param frag DOM fragment to serialize to the Source
626:             * @param resolver Resolver for the source.
627:             * @param serializerName The serializer to use
628:             * @throws ProcessingException
629:             * @deprecated This method will be removed in future versions.
630:             */
631:            public static void writeDOM(String location,
632:                    Parameters typeParameters, SourceParameters parameters,
633:                    DocumentFragment frag, SourceResolver resolver,
634:                    String serializerName) throws ProcessingException {
635:                Source source = null;
636:
637:                try {
638:                    source = SourceUtil.getSource(location, typeParameters,
639:                            parameters, resolver);
640:                    if (source instanceof  ModifiableSource) {
641:                        ModifiableSource ws = (ModifiableSource) source;
642:
643:                        frag.normalize();
644:
645:                        if (null != serializerName) {
646:                            ComponentManager manager = CocoonComponentManager
647:                                    .getSitemapComponentManager();
648:
649:                            ComponentSelector selector = null;
650:                            Serializer serializer = null;
651:                            OutputStream oStream = null;
652:                            try {
653:                                selector = (ComponentSelector) manager
654:                                        .lookup(Serializer.ROLE + "Selector");
655:                                serializer = (Serializer) selector
656:                                        .select(serializerName);
657:                                oStream = ws.getOutputStream();
658:                                serializer.setOutputStream(oStream);
659:                                serializer.startDocument();
660:                                DOMStreamer streamer = new DOMStreamer(
661:                                        serializer);
662:                                streamer.stream(frag);
663:                                serializer.endDocument();
664:                            } catch (ComponentException e) {
665:                                throw new ProcessingException(
666:                                        "Unable to lookup serializer.", e);
667:                            } finally {
668:                                if (oStream != null) {
669:                                    oStream.flush();
670:                                    try {
671:                                        oStream.close();
672:                                    } catch (Exception ignore) {
673:                                    }
674:                                }
675:                                if (selector != null) {
676:                                    selector.release(serializer);
677:                                    manager.release(selector);
678:                                }
679:                            }
680:                        } else {
681:                            Properties props = XMLUtils
682:                                    .createPropertiesForXML(false);
683:                            props.put(OutputKeys.ENCODING, "ISO-8859-1");
684:                            final String content = XMLUtils.serializeNode(frag,
685:                                    props);
686:                            OutputStream oStream = ws.getOutputStream();
687:
688:                            oStream.write(content.getBytes());
689:                            oStream.flush();
690:                            oStream.close();
691:                        }
692:                    } else {
693:                        String content;
694:                        if (null != serializerName) {
695:                            ComponentManager manager = CocoonComponentManager
696:                                    .getSitemapComponentManager();
697:
698:                            ComponentSelector selector = null;
699:                            Serializer serializer = null;
700:                            ByteArrayOutputStream oStream = new ByteArrayOutputStream();
701:                            try {
702:                                selector = (ComponentSelector) manager
703:                                        .lookup(Serializer.ROLE + "Selector");
704:                                serializer = (Serializer) selector
705:                                        .select(serializerName);
706:                                serializer.setOutputStream(oStream);
707:                                serializer.startDocument();
708:                                DOMStreamer streamer = new DOMStreamer(
709:                                        serializer);
710:                                streamer.stream(frag);
711:                                serializer.endDocument();
712:                            } catch (ComponentException e) {
713:                                throw new ProcessingException(
714:                                        "Unable to lookup serializer.", e);
715:                            } finally {
716:                                oStream.flush();
717:                                try {
718:                                    oStream.close();
719:                                } catch (Exception ignore) {
720:                                    // do nothing
721:                                }
722:                                if (selector != null) {
723:                                    selector.release(serializer);
724:                                    manager.release(selector);
725:                                }
726:                            }
727:                            content = oStream.toString();
728:                        } else {
729:                            Properties props = XMLUtils
730:                                    .createPropertiesForXML(false);
731:                            props.put(OutputKeys.ENCODING, "ISO-8859-1");
732:                            content = XMLUtils.serializeNode(frag, props);
733:                        }
734:
735:                        if (parameters == null) {
736:                            parameters = new SourceParameters();
737:                        } else {
738:                            parameters = (SourceParameters) parameters.clone();
739:                        }
740:                        parameters.setSingleParameterValue("content", content);
741:
742:                        source = SourceUtil.getSource(location, typeParameters,
743:                                parameters, resolver);
744:                        SourceUtil.toSAX(source, new DefaultHandler());
745:                    }
746:                } catch (SourceException e) {
747:                    throw SourceUtil.handle(e);
748:                } catch (IOException e) {
749:                    throw new ProcessingException(e);
750:                } catch (SAXException e) {
751:                    throw new ProcessingException(e);
752:                } finally {
753:                    resolver.release(source);
754:                }
755:            }
756:
757:            /**
758:             * Read a DOM Fragment from a source
759:             *
760:             * @param location URI of the Source
761:             * @param typeParameters Type of Source query. Currently, only
762:             *        <code>method</code> parameter (value typically <code>GET</code> or
763:             *        <code>POST</code>) is recognized. May be <code>null</code>.
764:             * @param parameters Parameters (e.g. URL params) of the source.
765:             *        May be <code>null</code>
766:             * @param resolver Resolver for the source.
767:             *
768:             * @return DOM <code>DocumentFragment</code> constructed from the specified
769:             *         source.
770:             * @throws ProcessingException
771:             * @deprecated This method will be removed in future versions.
772:             */
773:            public static DocumentFragment readDOM(String location,
774:                    Parameters typeParameters, SourceParameters parameters,
775:                    SourceResolver resolver) throws ProcessingException {
776:
777:                Source source = null;
778:                try {
779:                    source = SourceUtil.getSource(location, typeParameters,
780:                            parameters, resolver);
781:                    Document doc = SourceUtil.toDOM(source);
782:
783:                    DocumentFragment fragment = doc.createDocumentFragment();
784:                    fragment.appendChild(doc.getDocumentElement());
785:
786:                    return fragment;
787:                } catch (SourceException e) {
788:                    throw SourceUtil.handle(e);
789:                } catch (IOException e) {
790:                    throw new ProcessingException(e);
791:                } catch (SAXException e) {
792:                    throw new ProcessingException(e);
793:                } finally {
794:                    resolver.release(source);
795:                }
796:            }
797:
798:            /**
799:             * Return the scheme of a URI. Just as there are many different methods
800:             * of access to resources, there are a variety of schemes for identifying
801:             * such resources.
802:             * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
803:             *
804:             * @param uri Uniform resource identifier.
805:             *
806:             * @return Scheme of the URI.
807:             */
808:            public static String getScheme(String uri) {
809:                RE re = new RE(uripattern);
810:                if (re.match(uri)) {
811:                    return re.getParen(2);
812:                } else {
813:                    throw new IllegalArgumentException("'" + uri
814:                            + "' is not a correct URI");
815:                }
816:            }
817:
818:            /**
819:             * Return the authority of a URI. This authority is
820:             * typically defined by an Internet-based server or a scheme-specific
821:             * registry of naming authorities
822:             * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
823:             *
824:             * @param uri Uniform resource identifier.
825:             *
826:             * @return Scheme of the URI.
827:             */
828:            public static String getAuthority(String uri) {
829:                RE re = new RE(uripattern);
830:                if (re.match(uri)) {
831:                    return re.getParen(4);
832:                } else {
833:                    throw new IllegalArgumentException("'" + uri
834:                            + "' is not a correct URI");
835:                }
836:            }
837:
838:            /**
839:             * Return the path of a URI. The path contains data, specific to the
840:             * authority (or the scheme if there is no authority component),
841:             * identifying the resource within the scope of that scheme and authority
842:             * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
843:             *
844:             * @param uri Uniform resource identifier.
845:             *
846:             * @return Path of the URI.
847:             */
848:            public static String getPath(String uri) {
849:                RE re = new RE(uripattern);
850:                if (re.match(uri)) {
851:                    return re.getParen(5);
852:                } else {
853:                    throw new IllegalArgumentException("'" + uri
854:                            + "' is not a correct URI");
855:                }
856:            }
857:
858:            /**
859:             * Return the path of a URI, if the URI can't contains a authority.
860:             * This implementation differ to the RFC 2396.
861:             *
862:             * @param uri Uniform resource identifier.
863:             *
864:             * @return Path of the URI.
865:             */
866:            public static String getPathWithoutAuthority(String uri) {
867:                RE re = new RE(uripattern);
868:                if (re.match(uri)) {
869:                    return re.getParen(4) + re.getParen(5);
870:                } else {
871:                    throw new IllegalArgumentException("'" + uri
872:                            + "' is not a correct URI");
873:                }
874:            }
875:
876:            /**
877:             * Return the query of a URI. The query is a string of information to
878:             * be interpreted by the resource
879:             * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
880:             *
881:             * @param uri Uniform resource identifier.
882:             *
883:             * @return Query of the URI.
884:             */
885:            public static String getQuery(String uri) {
886:                RE re = new RE(uripattern);
887:                if (re.match(uri)) {
888:                    return re.getParen(7);
889:                } else {
890:                    throw new IllegalArgumentException("'" + uri
891:                            + "' is not a correct URI");
892:                }
893:            }
894:
895:            /**
896:             * Return the fragment of a URI. When a URI reference is used to perform
897:             * a retrieval action on the identified resource, the optional fragment
898:             * identifier, consists of additional reference information to be
899:             * interpreted by the user agent after the retrieval action has been
900:             * successfully completed
901:             * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
902:             *
903:             * @param uri Uniform resource identifier.
904:             *
905:             * @return Fragment of the URI.
906:             */
907:            public static String getFragment(String uri) {
908:                RE re = new RE(uripattern);
909:                if (re.match(uri)) {
910:                    return re.getParen(9);
911:                } else {
912:                    throw new IllegalArgumentException("'" + uri
913:                            + "' is not a correct URI");
914:                }
915:            }
916:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.