001: package com.sun.portal.app.filesharing.repo;
002:
003: import com.sun.portal.app.filesharing.util.ParameterCheck;
004:
005: import java.text.MessageFormat;
006: import java.io.Serializable;
007:
008: /**
009: * @author Alejandro Abdelnur
010: */
011: public class RepoItem implements Cloneable, Serializable {
012: private static final String SEPARATOR = "/";
013:
014: public static final RepoItem ROOT = new RepoItem();
015:
016: private String _name;
017: private RepoItem _parent;
018: private boolean _isDirectory;
019: private String _creator;
020:
021: private long _created;
022: private long _lastModified;
023: private long _length;
024: private boolean _fromRepo;
025:
026: private RepoItem() {
027: _name = SEPARATOR;
028: _parent = null;
029: _isDirectory = true;
030: _creator = "ROOT";
031: _fromRepo = true;
032: }
033:
034: public RepoItem(String filePath, String creator)
035: throws RepoException {
036: ParameterCheck.checkNotEmpty("filepath", filePath, true);
037: ParameterCheck.checkNotEmpty("creator", creator, true);
038: if (filePath.indexOf(SEPARATOR) == -1) {
039: throw new RepoException("RI00", "Parameter '" + filePath
040: + "' must contain '" + SEPARATOR + "' characters");
041: }
042: if (filePath.indexOf(SEPARATOR) != 0) {
043: throw new RepoException("RI01", "Parameter '" + filePath
044: + "' must begin wiht a '" + SEPARATOR
045: + "' character");
046: }
047: if (filePath.indexOf(SEPARATOR + SEPARATOR) > -1) {
048: throw new RepoException("RI02", "Parameter '" + filePath
049: + "' must not contain consecutive '" + SEPARATOR
050: + "' characters");
051: }
052: if (filePath.equals(SEPARATOR)) {
053: _parent = null;
054: _name = SEPARATOR;
055: _isDirectory = true;
056: _creator = "ROOT";
057: _fromRepo = true;
058: } else {
059: filePath = (filePath.endsWith(SEPARATOR)) ? filePath
060: .substring(0, filePath.length() - 1) : filePath;
061: _parent = getParent(filePath);
062: _name = getName(filePath, filePath.lastIndexOf(SEPARATOR));
063: _creator = creator;
064: }
065: }
066:
067: private RepoItem getParent(String filePath) throws RepoException {
068: if (filePath.equals(SEPARATOR)) {
069: return RepoItem.ROOT;
070: } else {
071: int i = filePath.lastIndexOf(SEPARATOR);
072: return new RepoItem(getPath(filePath, i), "dummy");
073: }
074: }
075:
076: private String getPath(String filePath, int separatorPos) {
077: return filePath.substring(0, separatorPos + 1);
078: }
079:
080: private String getName(String filePath, int separatorPos) {
081: return filePath.substring(separatorPos + 1);
082: }
083:
084: public RepoItem(RepoItem parent, String name, String creator)
085: throws RepoException {
086: this (parent, name, false, creator, 0, 0, 0, false);
087: }
088:
089: public RepoItem(RepoItem parent, String name, String creator,
090: long length) throws RepoException {
091: this (parent, name, false, creator, 0, 0, length, false);
092: }
093:
094: RepoItem(RepoItem parent, String name, boolean isDir,
095: String creator, long created, long lastMod, long length,
096: boolean fromRepo) throws RepoException {
097: ParameterCheck.checkNotNull("parent", parent);
098: ParameterCheck.checkNotEmpty("name", name, true);
099: if (name.indexOf(SEPARATOR) > -1) {
100: throw new RepoException("RI03", "Parameter '" + name
101: + "' cannot contain '" + SEPARATOR + "' characters");
102: }
103: ParameterCheck.checkNotEmpty("creator", creator, true);
104: _parent = parent;
105: _name = name;
106: _isDirectory = isDir;
107: _creator = creator;
108: _created = created;
109: _lastModified = lastMod;
110: _length = length;
111: _fromRepo = fromRepo;
112: }
113:
114: private static final String TO_STRING = "RepoItem: name[{0}] parent[{1}]";
115:
116: public String toString() {
117: return MessageFormat.format(TO_STRING, new Object[] { _name,
118: _parent });
119: }
120:
121: public int hashCode() {
122: return getPath().hashCode();
123: }
124:
125: public boolean equals(Object other) {
126: if (other != null && other instanceof RepoItem) {
127: return ((RepoItem) other).getPath().equals(getPath());
128: }
129: return false;
130: }
131:
132: public Object clone() {
133: try {
134: return new RepoItem(_parent, _name, _isDirectory, _creator,
135: _created, _lastModified, _length, _fromRepo);
136: } catch (RepoException ex) {
137: throw new RuntimeException("It cannot happen");
138: }
139: }
140:
141: public String getName() {
142: return _name;
143: }
144:
145: public RepoItem getParent() {
146: return _parent;
147: }
148:
149: public String getPath() {
150: String path;
151: if (_parent == null) {
152: path = SEPARATOR;
153: } else {
154: if (_parent.equals(ROOT)) {
155: path = SEPARATOR + _name;
156: } else {
157: path = _parent.getPath() + SEPARATOR + _name;
158: }
159: }
160: return path;
161: }
162:
163: public long getLength() {
164: return _length;
165: }
166:
167: public boolean isDirectory() {
168: return _isDirectory;
169: }
170:
171: public long getCreated() {
172: return _created;
173: }
174:
175: public long getLastModified() {
176: return _lastModified;
177: }
178:
179: public String getCreator() {
180: return _creator;
181: }
182:
183: public boolean isFromRepo() {
184: return _fromRepo;
185: }
186: }
|