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 repositorySourceName, String defaultRepositoryWorkspaceName ) {
041            Matcher matcher = PATTERN.matcher(path);
042            if (matcher.matches()) {
043                try {
044                    return new RepositoryNodePath(repositorySourceName, 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(repositorySourceName, defaultRepositoryWorkspaceName, path);
050    
051        }
052    
053        private final String repositorySourceName;
054        private final String workspaceName;
055        private final String nodePath;
056        private final int hc;
057    
058        public RepositoryNodePath( String repositorySourceName, String workspaceName, String nodePath ) {
059            this.repositorySourceName = repositorySourceName;
060            this.workspaceName = workspaceName;
061            this.nodePath = nodePath;
062            this.hc = HashCode.compute(this.repositorySourceName, this.workspaceName, this.nodePath);
063        }
064    
065        /**
066         * @return nodePath
067         */
068        public String getNodePath() {
069            return this.nodePath;
070        }
071    
072        /**
073         * @return repositoryName
074         */
075        public String getRepositorySourceName() {
076            return this.repositorySourceName;
077        }
078    
079        /**
080         * 
081         * @return the workspace name
082         */
083        public String getWorkspaceName() {
084            return this.workspaceName;
085        }
086        
087        /**
088         * {@inheritDoc}
089         */
090        @Override
091        public int hashCode() {
092            return this.hc;
093        }
094    
095        /**
096         * {@inheritDoc}
097         */
098        @Override
099        public boolean equals( Object obj ) {
100            if (obj == this) return true;
101            if (obj instanceof RepositoryNodePath) {
102                RepositoryNodePath that = (RepositoryNodePath)obj;
103                if (!this.repositorySourceName.equals(that.repositorySourceName)) return false;
104                if (!this.workspaceName.equals(that.workspaceName)) return false;
105                if (!this.nodePath.equals(that.nodePath)) return false;
106                return true;
107            }
108            return false;
109        }
110    
111        /**
112         * {@inheritDoc}
113         */
114        @Override
115        public String toString() {
116            return this.repositorySourceName + ":" + this.workspaceName + ":" + this.nodePath;
117        }
118    }