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.graph.property.basic;
25  
26  import net.jcip.annotations.Immutable;
27  import org.modeshape.common.text.TextEncoder;
28  import org.modeshape.common.util.CheckArg;
29  import org.modeshape.common.util.HashCode;
30  import org.modeshape.graph.property.Name;
31  import org.modeshape.graph.property.NamespaceRegistry;
32  import org.modeshape.graph.property.Path;
33  
34  /**
35   * A basic implementation of {@link Name}.
36   */
37  @Immutable
38  public class BasicName implements Name {
39  
40      private String trimNonEmptyStrings( String value ) {
41          if (value == null) return null;
42          String trimmed = value.trim();
43          return trimmed.length() == 0 ? value : trimmed;
44      }
45  
46      /**
47       */
48      private static final long serialVersionUID = -1737537720336990144L;
49      private final String namespaceUri;
50      private final String localName;
51      private final int hc;
52  
53      public BasicName( String namespaceUri,
54                        String localName ) {
55          CheckArg.isNotNull(localName, "localName");
56          this.namespaceUri = namespaceUri != null ? namespaceUri.trim().intern() : "";
57          this.localName = trimNonEmptyStrings(localName);
58          this.hc = HashCode.compute(this.namespaceUri, this.localName);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      public String getLocalName() {
65          return this.localName;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public String getNamespaceUri() {
72          return this.namespaceUri;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      public String getString() {
79          return getString(Path.DEFAULT_ENCODER);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      public String getString( TextEncoder encoder ) {
86          if (this.getNamespaceUri().length() == 0) {
87              if (this.getLocalName().equals(Path.SELF)) return Path.SELF;
88              if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT;
89          }
90          if (encoder == null) encoder = Path.DEFAULT_ENCODER;
91  
92          if (namespaceUri.length() > 0) {
93              return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName);
94          }
95          return encoder.encode(this.localName);
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     public String getString( NamespaceRegistry namespaceRegistry ) {
102         CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
103         String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
104         if (prefix != null && prefix.length() != 0) {
105             return prefix + ":" + this.localName;
106         }
107         return this.localName;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public String getString( NamespaceRegistry namespaceRegistry,
114                              TextEncoder encoder ) {
115         // This is the most-often used method, so implement it directly
116         CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry");
117         String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
118         if (prefix != null && prefix.length() != 0) {
119             return encoder.encode(prefix) + ":" + encoder.encode(this.localName);
120         }
121         return encoder.encode(this.localName);
122     }
123 
124     /**
125      * {@inheritDoc}
126      * 
127      * @see org.modeshape.graph.property.Name#getString(org.modeshape.graph.property.NamespaceRegistry,
128      *      org.modeshape.common.text.TextEncoder, org.modeshape.common.text.TextEncoder)
129      */
130     public String getString( NamespaceRegistry namespaceRegistry,
131                              TextEncoder encoder,
132                              TextEncoder delimiterEncoder ) {
133         if (namespaceRegistry == null) {
134             if (this.getNamespaceUri().length() == 0) {
135                 if (this.getLocalName().equals(Path.SELF)) return Path.SELF;
136                 if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT;
137             }
138             if (encoder == null) encoder = Path.DEFAULT_ENCODER;
139             if (delimiterEncoder != null) {
140                 return delimiterEncoder.encode("{") + encoder.encode(this.namespaceUri) + delimiterEncoder.encode("}")
141                        + encoder.encode(this.localName);
142             }
143             return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName);
144 
145         }
146         if (encoder == null) encoder = Path.DEFAULT_ENCODER;
147         String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true);
148         if (prefix != null && prefix.length() != 0) {
149             String delim = delimiterEncoder != null ? delimiterEncoder.encode(":") : ":";
150             return encoder.encode(prefix) + delim + encoder.encode(this.localName);
151         }
152         return encoder.encode(this.localName);
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public int compareTo( Name that ) {
159         if (that == this) return 0;
160         int diff = this.getLocalName().compareTo(that.getLocalName());
161         if (diff != 0) return diff;
162         diff = this.getNamespaceUri().compareTo(that.getNamespaceUri());
163         return diff;
164     }
165 
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public int hashCode() {
171         return this.hc;
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public boolean equals( Object obj ) {
179         if (obj == this) return true;
180         if (obj instanceof Name) {
181             Name that = (Name)obj;
182             if (!this.getLocalName().equals(that.getLocalName())) return false;
183             if (!this.getNamespaceUri().equals(that.getNamespaceUri())) return false;
184             return true;
185         }
186         return false;
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     @Override
193     public String toString() {
194         return "{" + this.namespaceUri + "}" + this.localName;
195     }
196 
197 }