001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software 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     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.repository.util;
023    
024    import java.util.regex.Matcher;
025    import java.util.regex.Pattern;
026    import net.jcip.annotations.Immutable;
027    import org.jboss.dna.common.util.HashCode;
028    import org.jboss.dna.repository.RepositoryI18n;
029    
030    /**
031     * @author Randall Hauch
032     */
033    @Immutable
034    public class RepositoryNodePath {
035    
036        protected static final Pattern PATTERN = Pattern.compile("([^:/]):(/.*)");
037    
038        public static RepositoryNodePath parse( String path, String defaultRepositoryWorkspaceName ) {
039            Matcher matcher = PATTERN.matcher(path);
040            if (matcher.matches()) {
041                try {
042                    return new RepositoryNodePath(matcher.group(1), matcher.group(2));
043                } catch (Throwable t) {
044                    throw new IllegalArgumentException(RepositoryI18n.invalidRepositoryNodePath.text(path, t.getMessage()));
045                }
046            }
047            return new RepositoryNodePath(defaultRepositoryWorkspaceName, path);
048    
049        }
050    
051        private final String repositoryName;
052        private final String nodePath;
053        private final int hc;
054    
055        public RepositoryNodePath( String repositoryName, String nodePath ) {
056            this.repositoryName = repositoryName;
057            this.nodePath = nodePath;
058            this.hc = HashCode.compute(this.repositoryName, this.nodePath);
059        }
060    
061        /**
062         * @return nodePath
063         */
064        public String getNodePath() {
065            return this.nodePath;
066        }
067    
068        /**
069         * @return repositoryName
070         */
071        public String getRepositoryWorkspaceName() {
072            return this.repositoryName;
073        }
074    
075        /**
076         * {@inheritDoc}
077         */
078        @Override
079        public int hashCode() {
080            return this.hc;
081        }
082    
083        /**
084         * {@inheritDoc}
085         */
086        @Override
087        public boolean equals( Object obj ) {
088            if (obj == this) return true;
089            if (obj instanceof RepositoryNodePath) {
090                RepositoryNodePath that = (RepositoryNodePath)obj;
091                if (!this.repositoryName.equals(that.repositoryName)) return false;
092                if (!this.nodePath.equals(that.nodePath)) return false;
093                return true;
094            }
095            return false;
096        }
097    
098        /**
099         * {@inheritDoc}
100         */
101        @Override
102        public String toString() {
103            return this.repositoryName + ":" + this.nodePath;
104        }
105    }