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 { 031 private String _path; 032 private String _name; 033 private String _folderPath; 034 private Locale _locale; 035 036 protected AbstractResource(String path) 037 { 038 this(path, null); 039 } 040 041 protected AbstractResource(String path, Locale locale) 042 { 043 _path = path; 044 _locale = locale; 045 } 046 047 public String getName() 048 { 049 if (_name == null) 050 split(); 051 052 return _name; 053 } 054 055 public Resource getRelativeResource(String name) 056 { 057 if (name.startsWith("/")) 058 { 059 if (name.equals(_path)) 060 return this; 061 062 return newResource(name); 063 } 064 065 if (_folderPath == null) 066 split(); 067 068 if (name.equals(_name)) 069 return this; 070 071 return newResource(_folderPath + name); 072 } 073 074 public String getPath() 075 { 076 return _path; 077 } 078 079 public Locale getLocale() 080 { 081 return _locale; 082 } 083 084 085 protected abstract Resource newResource(String path); 086 087 private void split() 088 { 089 int lastSlashx = _path.lastIndexOf('/'); 090 091 _folderPath = _path.substring(0, lastSlashx + 1); 092 _name = _path.substring(lastSlashx + 1); 093 } 094 095 096 /** 097 * Returns true if the other object is an instance of the 098 * same class, and the paths are equal. 099 * 100 */ 101 102 public boolean equals(Object obj) 103 { 104 if (obj == null) 105 return false; 106 107 if (obj.getClass().equals(getClass())) 108 { 109 AbstractResource otherLocation = (AbstractResource) obj; 110 111 return _path.equals(otherLocation._path); 112 } 113 114 return false; 115 } 116 }