001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.util; 029 030import java.io.Serializable; 031 032/** 033 * Simple data holder class which stores a path in a normalized form.<p> 034 * 035 * This is mostly useful when using paths as map keys, when you are not sure if the paths you are processing have a trailing slash 036 * or not.<p> 037 * 038 * The paths are stored in the form '/foo/bar/baz', i.e. they include a leading but no trailing slash, except in the case of the root path '/'. 039 */ 040public class CmsPath implements Serializable, Comparable<CmsPath> { 041 042 /** Serial version id. */ 043 private static final long serialVersionUID = 1L; 044 045 /** The internal path string. */ 046 private String m_pathStr; 047 048 /** 049 * Creates a new instance. 050 * 051 * @param pathStr the path string 052 */ 053 public CmsPath(String pathStr) { 054 055 m_pathStr = normalize(pathStr); 056 } 057 058 /** 059 * Checks if two (absolute) paths are equal in normalized form. 060 * 061 * @param path1 the first path 062 * @param path2 the second path 063 * @return true if the paths are logically equal 064 */ 065 public static boolean equal(String path1, String path2) { 066 067 if ((path1 == null) && (path2 == null)) { 068 return true; 069 } 070 if ((path1 == null) || (path2 == null)) { 071 return false; 072 } 073 return normalize(path1).equals(normalize(path2)); 074 075 } 076 077 /** 078 * Normalizes a path. 079 * 080 * <p>The normalized path always has a leading slash, but never a trailing slash if it's more than one character long. 081 * 082 * @param pathStr the path to normalize 083 * @return the normalized path 084 */ 085 private static String normalize(String pathStr) { 086 087 String result = null; 088 if (pathStr.equals("") || pathStr.equals("/")) { 089 result = "/"; 090 } else { 091 result = CmsFileUtil.removeTrailingSeparator(pathStr); 092 if (!result.startsWith("/")) { 093 result = "/" + result; 094 } 095 } 096 return result; 097 } 098 099 /** 100 * Gets the path as a string. 101 * 102 * @return the path as a string. 103 */ 104 public String asString() { 105 106 return m_pathStr; 107 } 108 109 /** 110 * @see java.lang.Comparable#compareTo(java.lang.Object) 111 */ 112 public int compareTo(CmsPath o) { 113 114 return m_pathStr.compareTo(o.m_pathStr); 115 } 116 117 /** 118 * @see java.lang.Object#equals(java.lang.Object) 119 */ 120 @Override 121 public boolean equals(Object obj) { 122 123 if (!(obj instanceof CmsPath)) { 124 return false; 125 } 126 return ((CmsPath)obj).m_pathStr.equals(m_pathStr); 127 } 128 129 /** 130 * @see java.lang.Object#hashCode() 131 */ 132 @Override 133 public int hashCode() { 134 135 return m_pathStr.hashCode(); 136 } 137 138 /** 139 * Returns true if this path is a prefix of the path given as parameter. 140 * 141 * @param path a path 142 * @return true if this path is a prefix of the parameter 143 */ 144 public boolean isPrefixOf(CmsPath path) { 145 146 return isPrefixOfStr(path.asString()); 147 148 } 149 150 /** 151 * Returns true if this path is a prefix path of the given path 152 * 153 * @param path a path 154 * @return true if the path represented by this object is a prefix of the given path 155 */ 156 public boolean isPrefixOfStr(String path) { 157 158 return CmsStringUtil.isPrefixPath(m_pathStr, path); 159 160 } 161 162 /** 163 * @see java.lang.Object#toString() 164 */ 165 @Override 166 public String toString() { 167 168 return "CmsPath[" + asString() + "]"; 169 } 170 171}