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.impl;
018: package org.apache.lenya.cms.cocoon.source;
019:
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.Serviceable;
028: import org.apache.avalon.framework.thread.ThreadSafe;
029: import org.apache.excalibur.source.Source;
030: import org.apache.excalibur.source.SourceException;
031: import org.apache.excalibur.source.SourceFactory;
032: import org.apache.excalibur.source.SourceResolver;
033:
034: /** Implementation of a {@link Source} that gets its content from
035: * and ZIP archive.
036: *
037: * A ZIP source can be reached using the zip:// pseudo-protocol. The syntax is
038: * zip://myFile.xml@myZip.zip (zip://[file]@[archive])
039: *
040: * @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
041: * @version CVS $Id: ZipSourceFactory.java 30932 2004-07-29 17:35:38Z vgritsenko $
042: * @since 2.1.4
043: */
044: public class ZipSourceFactory extends AbstractLogEnabled implements
045: SourceFactory, ThreadSafe, Serviceable {
046:
047: protected ServiceManager manager;
048: /**
049: * The URL scheme, including the colon.
050: */
051: public static final String ZIP_SOURCE_SCHEME = "zip:";
052:
053: public Source getSource(String location, Map parameters)
054: throws IOException, MalformedURLException {
055:
056: if (this .getLogger().isDebugEnabled()) {
057: this .getLogger().debug("Processing " + location);
058: }
059:
060: // syntax checks
061: int separatorPos = location.indexOf('@');
062: if (separatorPos == -1) {
063: throw new MalformedURLException("@ required in URI: "
064: + location);
065: }
066: int protocolEnd = location.indexOf("://");
067: if (protocolEnd == -1) {
068: throw new MalformedURLException(
069: "URI does not contain '://' : " + location);
070: }
071:
072: // get the source of the archive and return the ZipSource passing
073: // a source retrieved from the SourceResolver
074: String documentName = location.substring(protocolEnd + 3,
075: separatorPos);
076: Source archive;
077: SourceResolver resolver = null;
078: try {
079: resolver = (SourceResolver) this .manager
080: .lookup(SourceResolver.ROLE);
081: archive = resolver.resolveURI(location
082: .substring(separatorPos + 1));
083: } catch (ServiceException se) {
084: throw new SourceException(
085: "SourceResolver is not available.", se);
086: } finally {
087: this .manager.release(resolver);
088: }
089: return new ZipSource(archive, documentName);
090: }
091:
092: public void release(Source source) {
093: // not necessary here
094: }
095:
096: public void service(ServiceManager manager) throws ServiceException {
097: this.manager = manager;
098: }
099:
100: }
|