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        if (pathStr.equals("") || pathStr.equals("/")) {
056            m_pathStr = "/";
057        } else {
058            m_pathStr = CmsFileUtil.removeTrailingSeparator(pathStr);
059            if (!m_pathStr.startsWith("/")) {
060                m_pathStr = "/" + m_pathStr;
061            }
062        }
063    }
064
065    /**
066     * Gets the path as a string.
067     *
068     * @return the path as a string.
069     */
070    public String asString() {
071
072        return m_pathStr;
073    }
074
075    /**
076     * @see java.lang.Comparable#compareTo(java.lang.Object)
077     */
078    public int compareTo(CmsPath o) {
079
080        return m_pathStr.compareTo(o.m_pathStr);
081    }
082
083    /**
084     * @see java.lang.Object#equals(java.lang.Object)
085     */
086    @Override
087    public boolean equals(Object obj) {
088
089        if (!(obj instanceof CmsPath)) {
090            return false;
091        }
092        return ((CmsPath)obj).m_pathStr.equals(m_pathStr);
093    }
094
095    /**
096     * @see java.lang.Object#hashCode()
097     */
098    @Override
099    public int hashCode() {
100
101        return m_pathStr.hashCode();
102    }
103
104    /**
105     * Returns true if this path is a prefix of the path given as parameter.
106     *
107     * @param path a path
108     * @return true if this path is a prefix of the parameter
109     */
110    public boolean isPrefixOf(CmsPath path) {
111
112        return isPrefixOfStr(path.asString());
113
114    }
115
116    /**
117     * Returns true if this path is a prefix path of the given path
118     *
119     * @param path a path
120     * @return true if the path represented by this object is a prefix of the given path
121     */
122    public boolean isPrefixOfStr(String path) {
123
124        return CmsStringUtil.isPrefixPath(m_pathStr, path);
125
126    }
127
128    /**
129     * @see java.lang.Object#toString()
130     */
131    @Override
132    public String toString() {
133
134        return "CmsPath[" + asString() + "]";
135    }
136
137}