001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.repository.util;
025    
026    import java.util.regex.Matcher;
027    import java.util.regex.Pattern;
028    import net.jcip.annotations.Immutable;
029    import org.jboss.dna.common.util.HashCode;
030    import org.jboss.dna.repository.RepositoryI18n;
031    
032    /**
033     * @author Randall Hauch
034     */
035    @Immutable
036    public class RepositoryNodePath {
037    
038        protected static final Pattern PATTERN = Pattern.compile("([^:/]):(/.*)");
039    
040        public static RepositoryNodePath parse( String path, String defaultRepositoryWorkspaceName ) {
041            Matcher matcher = PATTERN.matcher(path);
042            if (matcher.matches()) {
043                try {
044                    return new RepositoryNodePath(matcher.group(1), matcher.group(2));
045                } catch (Throwable t) {
046                    throw new IllegalArgumentException(RepositoryI18n.invalidRepositoryNodePath.text(path, t.getMessage()));
047                }
048            }
049            return new RepositoryNodePath(defaultRepositoryWorkspaceName, path);
050    
051        }
052    
053        private final String repositoryName;
054        private final String nodePath;
055        private final int hc;
056    
057        public RepositoryNodePath( String repositoryName, String nodePath ) {
058            this.repositoryName = repositoryName;
059            this.nodePath = nodePath;
060            this.hc = HashCode.compute(this.repositoryName, this.nodePath);
061        }
062    
063        /**
064         * @return nodePath
065         */
066        public String getNodePath() {
067            return this.nodePath;
068        }
069    
070        /**
071         * @return repositoryName
072         */
073        public String getRepositoryWorkspaceName() {
074            return this.repositoryName;
075        }
076    
077        /**
078         * {@inheritDoc}
079         */
080        @Override
081        public int hashCode() {
082            return this.hc;
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        @Override
089        public boolean equals( Object obj ) {
090            if (obj == this) return true;
091            if (obj instanceof RepositoryNodePath) {
092                RepositoryNodePath that = (RepositoryNodePath)obj;
093                if (!this.repositoryName.equals(that.repositoryName)) return false;
094                if (!this.nodePath.equals(that.nodePath)) return false;
095                return true;
096            }
097            return false;
098        }
099    
100        /**
101         * {@inheritDoc}
102         */
103        @Override
104        public String toString() {
105            return this.repositoryName + ":" + this.nodePath;
106        }
107    }