001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.util;
016:
017: import java.util.Locale;
018:
019: import org.apache.hivemind.Resource;
020:
021: /**
022: * Abstract implementation of {@link org.apache.hivemind.Resource}.
023: *
024: * <p>Resource instances act as a kind of factory for new instances
025: * of the same type, via {@link #newResource(String)}.
026: *
027: * @author Howard Lewis Ship
028: */
029: public abstract class AbstractResource implements Resource {
030: private String _path;
031: private String _name;
032: private String _folderPath;
033: private Locale _locale;
034:
035: protected AbstractResource(String path) {
036: this (path, null);
037: }
038:
039: protected AbstractResource(String path, Locale locale) {
040: _path = path;
041: _locale = locale;
042: }
043:
044: public String getName() {
045: if (_name == null)
046: split();
047:
048: return _name;
049: }
050:
051: public Resource getRelativeResource(String name) {
052: if (name.startsWith("/")) {
053: if (name.equals(_path))
054: return this ;
055:
056: return newResource(name);
057: }
058:
059: if (_folderPath == null)
060: split();
061:
062: if (name.equals(_name))
063: return this ;
064:
065: return newResource(_folderPath + name);
066: }
067:
068: public String getPath() {
069: return _path;
070: }
071:
072: public Locale getLocale() {
073: return _locale;
074: }
075:
076: protected abstract Resource newResource(String path);
077:
078: private void split() {
079: int lastSlashx = _path.lastIndexOf('/');
080:
081: _folderPath = _path.substring(0, lastSlashx + 1);
082: _name = _path.substring(lastSlashx + 1);
083: }
084:
085: /**
086: * Returns true if the other object is an instance of the
087: * same class, and the paths are equal.
088: *
089: */
090:
091: public boolean equals(Object obj) {
092: if (obj == null)
093: return false;
094:
095: if (obj.getClass().equals(getClass())) {
096: AbstractResource otherLocation = (AbstractResource) obj;
097:
098: return _path.equals(otherLocation._path);
099: }
100:
101: return false;
102: }
103: }
|