View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.repository.util;
25  
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  import net.jcip.annotations.Immutable;
29  import org.modeshape.common.util.HashCode;
30  import org.modeshape.repository.RepositoryI18n;
31  
32  /**
33   * An immutable representation of a path to a node within the named workspace of a named repository source.
34   */
35  @Immutable
36  public class RepositoryNodePath {
37  
38      protected static final Pattern PATTERN = Pattern.compile("([^:/]):(/.*)");
39  
40      public static RepositoryNodePath parse( String path,
41                                              String repositorySourceName,
42                                              String defaultRepositoryWorkspaceName ) {
43          Matcher matcher = PATTERN.matcher(path);
44          if (matcher.matches()) {
45              try {
46                  return new RepositoryNodePath(repositorySourceName, matcher.group(1), matcher.group(2));
47              } catch (Throwable t) {
48                  throw new IllegalArgumentException(RepositoryI18n.invalidRepositoryNodePath.text(path, t.getMessage()));
49              }
50          }
51          return new RepositoryNodePath(repositorySourceName, defaultRepositoryWorkspaceName, path);
52  
53      }
54  
55      private final String repositorySourceName;
56      private final String workspaceName;
57      private final String nodePath;
58      private final int hc;
59  
60      public RepositoryNodePath( String repositorySourceName,
61                                 String workspaceName,
62                                 String nodePath ) {
63          this.repositorySourceName = repositorySourceName;
64          this.workspaceName = workspaceName;
65          this.nodePath = nodePath;
66          this.hc = HashCode.compute(this.repositorySourceName, this.workspaceName, this.nodePath);
67      }
68  
69      /**
70       * @return nodePath
71       */
72      public String getNodePath() {
73          return this.nodePath;
74      }
75  
76      /**
77       * @return repositoryName
78       */
79      public String getRepositorySourceName() {
80          return this.repositorySourceName;
81      }
82  
83      /**
84       * @return the workspace name
85       */
86      public String getWorkspaceName() {
87          return this.workspaceName;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public int hashCode() {
95          return this.hc;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public boolean equals( Object obj ) {
103         if (obj == this) return true;
104         if (obj instanceof RepositoryNodePath) {
105             RepositoryNodePath that = (RepositoryNodePath)obj;
106             if (!this.repositorySourceName.equals(that.repositorySourceName)) return false;
107             if (!this.workspaceName.equals(that.workspaceName)) return false;
108             if (!this.nodePath.equals(that.nodePath)) return false;
109             return true;
110         }
111         return false;
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String toString() {
119         return this.repositorySourceName + ":" + this.workspaceName + ":" + this.nodePath;
120     }
121 }