001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.framework.repository;
018:
019: import java.net.*;
020: import java.io.*;
021:
022: public class FileResource extends AbstractResource {
023:
024: File file;
025: Repository repository;
026: String name;
027: String shortName;
028: String baseName;
029:
030: public FileResource(File file) {
031: this (file, null);
032: }
033:
034: protected FileResource(File file, Repository repository) {
035: this .file = file;
036:
037: this .repository = repository;
038: name = file.getAbsolutePath();
039: shortName = file.getName();
040: // base name is short name with extension cut off
041: int lastDot = shortName.lastIndexOf(".");
042: baseName = (lastDot == -1) ? shortName : shortName.substring(0,
043: lastDot);
044: }
045:
046: public String getName() {
047: return name;
048: }
049:
050: public String getShortName() {
051: return shortName;
052: }
053:
054: public String getBaseName() {
055: return baseName;
056: }
057:
058: public InputStream getInputStream() {
059: try {
060: return new FileInputStream(file);
061: } catch (FileNotFoundException ex) {
062: return null;
063: }
064: }
065:
066: public URL getUrl() {
067: try {
068: return new URL("file:" + file.getAbsolutePath());
069: } catch (MalformedURLException ex) {
070: return null;
071: }
072: }
073:
074: public long lastModified() {
075: return file.lastModified();
076: }
077:
078: public String getContent(String encoding) throws IOException {
079: InputStream in = getInputStream();
080: int size = (int) file.length();
081: byte[] buf = new byte[size];
082: int read = 0;
083: while (read < size) {
084: int r = in.read(buf, read, size - read);
085: if (r == -1)
086: break;
087: read += r;
088: }
089: in.close();
090: return encoding == null ? new String(buf) : new String(buf,
091: encoding);
092: }
093:
094: public String getContent() throws IOException {
095: return getContent(null);
096: }
097:
098: public long getLength() {
099: return file.length();
100: }
101:
102: public boolean exists() {
103: return file.exists();
104: }
105:
106: public Repository getRepository() {
107: return repository;
108: }
109:
110: public int hashCode() {
111: return 17 + name.hashCode();
112: }
113:
114: public boolean equals(Object obj) {
115: return obj instanceof FileResource
116: && name.equals(((FileResource) obj).name);
117: }
118:
119: public String toString() {
120: return getName();
121: }
122: }
|