KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > source > SourceUtil


1 /*
2  * Copyright 1999-2004 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  */

17
18 package org.apache.lenya.cms.cocoon.source;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import org.apache.commons.io.CopyUtils;
25 import org.apache.commons.io.output.ByteArrayOutputStream;
26 import org.apache.excalibur.source.ModifiableSource;
27 import org.apache.excalibur.source.Source;
28 import org.apache.excalibur.source.SourceException;
29 import org.apache.excalibur.source.SourceResolver;
30
31 /**
32  * @version $Id: SourceUtil.java 149325 2005-02-01 00:08:51Z gregor $
33  */

34 public final class SourceUtil {
35
36     /**
37      * <p>Copies one Source to another using a source buffer i.e.
38      * the source Source is buffered before it is copied to its final destination.</p>
39      * <p>The optional buffering is sometimes useful, if the source Source somehow depends on
40      * the destination Source. This situation may occur e.g. if source Source is a Cocoon pipeline.</p>
41      * <p><em>NOTE:</em>o.a.e..s.SourceUtil.copy does not close streams on an exception!!</p>
42      * @param source
43      * @param destination
44      * @param useBuffer If true, the source data will be read into a buffer before it is written to the final destination.
45      * @throws IOException If an error occures.
46      */

47     public static void copy(Source source, ModifiableSource destination, boolean useBuffer)
48     throws IOException JavaDoc {
49         InputStream JavaDoc sourceInputStream = null;
50         OutputStream JavaDoc destOutputStream = null;
51         try {
52             sourceInputStream = source.getInputStream();
53             destOutputStream = destination.getOutputStream();
54             if(useBuffer) {
55                 final ByteArrayOutputStream sourceBos = new ByteArrayOutputStream();
56                 CopyUtils.copy(sourceInputStream, sourceBos);
57                 CopyUtils.copy(sourceBos.toByteArray(), destOutputStream);
58             }
59             else
60                 CopyUtils.copy(sourceInputStream, destOutputStream);
61         } finally {
62             if(destOutputStream != null) {
63                 destOutputStream.flush();
64                 destOutputStream.close();
65             }
66             if(sourceInputStream != null) {
67                 sourceInputStream.close();
68             }
69         }
70     }
71     
72     /**
73      * Copies one Source to another.
74      * The source Source is optionally buffered.
75      * @param resolver The SourceResolver to use for lookin up Sources.
76      * @param sourceUri The source to be copied.
77      * @param destUri The URI to copy to.
78      * @param useBuffer If true, the source Source is buffered before copied to the final destination.
79      * @throws IOException If an error occures.
80      * @throws SourceException If the destination is not modifiable.
81      * @see #copy(Source, ModifiableSource, boolean)
82      */

83     public static void copy(SourceResolver resolver, String JavaDoc sourceUri, String JavaDoc destUri, boolean useBuffer)
84     throws IOException JavaDoc, SourceException {
85         Source source = null;
86         Source dest = null;
87         try {
88             source = resolver.resolveURI(sourceUri);
89             dest = resolver.resolveURI(destUri);
90             
91             if(!(dest instanceof ModifiableSource))
92                 throw new SourceException("Destination '"+ dest.getURI() + "' is not modifiable.");
93             
94             copy(source, (ModifiableSource) dest, useBuffer);
95         }
96         finally {
97             if(source != null)
98                 resolver.release(source);
99             if(dest != null)
100                 resolver.release(dest);
101         }
102     }
103     
104     /**
105      * Copies a Source without buffering.
106      * @param resolver A SourceResolver instance.
107      * @param sourceUri The source URI to copy from.
108      * @param destUri The destination URI to copy to.
109      * @throws IOException If an error occures.
110      * @see #copy(SourceResolver, String, String, boolean)
111      */

112     public static void copy(SourceResolver resolver, String JavaDoc sourceUri, String JavaDoc destUri)
113     throws IOException JavaDoc {
114         copy(resolver, sourceUri, destUri, false);
115     }
116 }
117
Popular Tags