001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2000, 2001, 2002, 2003 Jesse Stockall. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Apache Software Foundation (http://www.apache.org/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
026: * Foundation" must not be used to endorse or promote products derived
027: * from this software without prior written permission. For written
028: * permission, please contact apache@apache.org.
029: *
030: * 5. Products derived from this software may not be called "Apache"
031: * nor may "Apache" appear in their names without prior written
032: * permission of the Apache Group.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
038: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: * SUCH DAMAGE.
046: * ====================================================================
047: *
048: */
049: package org.apache.tools.ant.taskdefs.optional.genjar;
050:
051: import java.io.File;
052: import java.io.FileInputStream;
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.util.jar.Attributes;
056:
057: /**
058: * Represents a directory in the classpath.<p>
059: *
060: * When a directory is located in the classpath, a FileResolver is instantiated that encapsulates
061: * the path and performs searches in that directory. This class is used primarily to allow easy
062: * association of the <i>source directory </i> with the jar entry's attributes.<p>
063: *
064: * When a file is resolved from a JarEntrySpec, Attributes are added for the source file's path and
065: * last modification time.<p>
066: *
067: *
068: *
069: * @author Original Code: <a href="mailto:jake@riggshill.com">John W. Kohler </a>
070: * @author Jesse Stockall
071: * @version $Revision: 1.2 $ $Date: 2003/02/23 10:06:10 $
072: */
073: class FileResolver extends PathResolver {
074: /** the 'base' directory as specified in the classpath */
075: File base = null;
076:
077: /**
078: * constructs a new FileResolver using the given <i>base directory</i>
079: *
080: * @param base a directory at which file searches begin
081: * @param log an ant logging mechanism
082: */
083: FileResolver(File base, Logger log) {
084: super (log);
085:
086: this .base = base;
087: log.verbose("Resolver: " + base);
088: }
089:
090: /**
091: * Nothing to close
092: *
093: * @throws IOException Oops!
094: */
095: public void close() throws IOException {
096: // nothing to close
097: }
098:
099: /**
100: * Resolve the file specified in a JarEntrySpec to a stream.
101: *
102: * @param spec the JarEntrySpec to resolve
103: * @return an InputStream open on the resolved file or null
104: * @exception IOException if opening the stream fails
105: */
106: public InputStream resolve(JarEntrySpec spec) throws IOException {
107: InputStream is = null;
108: File f;
109:
110: //
111: // if the entrySpec already has a source file
112: // associated, use it otherwise attempt to
113: // create one
114: //
115: if ((f = spec.getSourceFile()) == null) {
116: f = new File(base, spec.getJarName());
117: }
118:
119: if (f.exists()) {
120: spec.setSourceFile(f);
121: is = new FileInputStream(f);
122: Attributes atts = spec.getAttributes();
123: atts.putValue(CONTENT_LOC, f.getAbsolutePath());
124: atts.putValue(LAST_MOD, formatDate(f.lastModified()));
125: log.debug(spec.getJarName() + "->" + base);
126: }
127: return is;
128: }
129:
130: /**
131: * Description of the Method
132: *
133: * @param fname Description of the Parameter
134: * @return Description of the Return Value
135: * @throws IOException Description of the Exception
136: */
137: public InputStream resolve(String fname) throws IOException {
138: InputStream is = null;
139: File f = new File(base, fname);
140:
141: if (f.exists()) {
142: is = new FileInputStream(f);
143: log.debug(fname + "->" + base);
144: }
145: return is;
146: }
147: }
148: // vi:set ts=4 sw=4:
|