KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > ZipSourceFactory


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

17 package org.apache.cocoon.components.source.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.logger.AbstractLogEnabled;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.avalon.framework.thread.ThreadSafe;
28 import org.apache.excalibur.source.Source;
29 import org.apache.excalibur.source.SourceException;
30 import org.apache.excalibur.source.SourceFactory;
31 import org.apache.excalibur.source.SourceResolver;
32
33
34 /**
35   * ATTENTION: The ZIP protocol is also part of Cocoon 2.1.4 (scratchpad). When
36   * Forrest uses this version or higher this file can be removed!!! (RP)
37   *
38   * Implementation of a {@link Source} that gets its content from
39   * and ZIP archive.
40   *
41   * A ZIP source can be reached using the zip:// pseudo-protocol. The syntax is
42   * zip://myFile.xml@myZip.zip (zip://[file]@[archive])
43   *
44   * @since 0.6
45   */

46 public class ZipSourceFactory extends AbstractLogEnabled
47     implements SourceFactory, ThreadSafe, Serviceable {
48
49     protected ServiceManager manager;
50     public static final String JavaDoc ZIP_SOURCE_SCHEME = "zip:";
51
52     public Source getSource(String JavaDoc location, Map JavaDoc parameters)
53         throws IOException JavaDoc, MalformedURLException JavaDoc {
54
55         if ( this.getLogger().isDebugEnabled() ) {
56             this.getLogger().debug("Processing " + location);
57         }
58         
59         // syntax checks
60
int separatorPos = location.indexOf('@');
61         if (separatorPos == -1) {
62             throw new MalformedURLException JavaDoc("@ required in URI: " + location);
63         }
64         int protocolEnd = location.indexOf("://");
65         if (protocolEnd == -1) {
66             throw new MalformedURLException JavaDoc("URI does not contain '://' : " + location);
67         }
68
69         // get the source of the archive and return the ZipSource passing
70
// a source retrieved from the SourceResolver
71
String JavaDoc documentName = location.substring(protocolEnd + 3, separatorPos);
72         Source archive;
73         SourceResolver resolver = null;
74         try {
75             resolver = (SourceResolver)this.manager.lookup( SourceResolver.ROLE );
76             archive = resolver.resolveURI(location.substring(separatorPos + 1));
77         } catch (ServiceException se) {
78             throw new SourceException("SourceResolver is not available.", se);
79         } finally {
80             this.manager.release(resolver);
81         }
82         return new ZipSource(archive, documentName);
83     }
84
85
86     public void release(Source source) {
87         // not necessary here
88
}
89
90     public void service(ServiceManager manager) throws ServiceException {
91         this.manager = manager;
92     }
93
94 }
95
Popular Tags