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.graph.properties.basic; 023 024 import net.jcip.annotations.Immutable; 025 import org.jboss.dna.common.text.TextEncoder; 026 import org.jboss.dna.common.util.CheckArg; 027 import org.jboss.dna.common.util.HashCode; 028 import org.jboss.dna.graph.properties.Name; 029 import org.jboss.dna.graph.properties.NamespaceRegistry; 030 import org.jboss.dna.graph.properties.Path; 031 032 /** 033 * A basic implementation of {@link Name}. 034 * 035 * @author Randall Hauch 036 * @author John Verhaeg 037 */ 038 @Immutable 039 public class BasicName implements Name { 040 041 /** 042 */ 043 private static final long serialVersionUID = -1737537720336990144L; 044 private final String namespaceUri; 045 private final String localName; 046 private final int hc; 047 048 public BasicName( String namespaceUri, 049 String localName ) { 050 CheckArg.isNotEmpty(localName, "localName"); 051 this.namespaceUri = namespaceUri != null ? namespaceUri.trim() : ""; 052 this.localName = localName != null ? localName.trim() : ""; 053 this.hc = HashCode.compute(this.namespaceUri, this.localName); 054 } 055 056 /** 057 * {@inheritDoc} 058 */ 059 public String getLocalName() { 060 return this.localName; 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 public String getNamespaceUri() { 067 return this.namespaceUri; 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 public String getString() { 074 return getString(Path.DEFAULT_ENCODER); 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 public String getString( TextEncoder encoder ) { 081 if (this.getNamespaceUri().length() == 0) { 082 if (this.getLocalName().equals(Path.SELF)) return Path.SELF; 083 if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT; 084 } 085 if (encoder == null) encoder = Path.DEFAULT_ENCODER; 086 return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName); 087 } 088 089 /** 090 * {@inheritDoc} 091 */ 092 public String getString( NamespaceRegistry namespaceRegistry ) { 093 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry"); 094 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true); 095 if (prefix != null && prefix.length() != 0) { 096 return prefix + ":" + this.localName; 097 } 098 return this.localName; 099 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 public String getString( NamespaceRegistry namespaceRegistry, 105 TextEncoder encoder ) { 106 // This is the most-often used method, so implement it directly 107 CheckArg.isNotNull(namespaceRegistry, "namespaceRegistry"); 108 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true); 109 if (prefix != null && prefix.length() != 0) { 110 return encoder.encode(prefix) + ":" + encoder.encode(this.localName); 111 } 112 return this.localName; 113 } 114 115 /** 116 * {@inheritDoc} 117 * 118 * @see org.jboss.dna.graph.properties.Name#getString(org.jboss.dna.graph.properties.NamespaceRegistry, 119 * org.jboss.dna.common.text.TextEncoder, org.jboss.dna.common.text.TextEncoder) 120 */ 121 public String getString( NamespaceRegistry namespaceRegistry, 122 TextEncoder encoder, 123 TextEncoder delimiterEncoder ) { 124 if (namespaceRegistry == null) { 125 if (this.getNamespaceUri().length() == 0) { 126 if (this.getLocalName().equals(Path.SELF)) return Path.SELF; 127 if (this.getLocalName().equals(Path.PARENT)) return Path.PARENT; 128 } 129 if (encoder == null) encoder = Path.DEFAULT_ENCODER; 130 if (delimiterEncoder != null) { 131 return delimiterEncoder.encode("{") + encoder.encode(this.namespaceUri) + delimiterEncoder.encode("}") 132 + encoder.encode(this.localName); 133 } 134 return "{" + encoder.encode(this.namespaceUri) + "}" + encoder.encode(this.localName); 135 136 } 137 String prefix = namespaceRegistry.getPrefixForNamespaceUri(this.namespaceUri, true); 138 if (prefix != null && prefix.length() != 0) { 139 String delim = delimiterEncoder != null ? delimiterEncoder.encode(":") : ":"; 140 return encoder.encode(prefix) + delim + encoder.encode(this.localName); 141 } 142 return this.localName; 143 } 144 145 /** 146 * {@inheritDoc} 147 */ 148 public int compareTo( Name that ) { 149 if (that == this) return 0; 150 int diff = this.getNamespaceUri().compareTo(that.getNamespaceUri()); 151 if (diff != 0) return diff; 152 diff = this.getLocalName().compareTo(that.getLocalName()); 153 return diff; 154 } 155 156 /** 157 * {@inheritDoc} 158 */ 159 @Override 160 public int hashCode() { 161 return this.hc; 162 } 163 164 /** 165 * {@inheritDoc} 166 */ 167 @Override 168 public boolean equals( Object obj ) { 169 if (obj == this) return true; 170 if (obj instanceof Name) { 171 Name that = (Name)obj; 172 if (!this.getNamespaceUri().equals(that.getNamespaceUri())) return false; 173 return this.getLocalName().equals(that.getLocalName()); 174 } 175 return false; 176 } 177 178 /** 179 * {@inheritDoc} 180 */ 181 @Override 182 public String toString() { 183 return "{" + this.namespaceUri + "}" + this.localName; 184 } 185 186 }