001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/datatypes/QualifiedName.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.datatypes;
044:
045: import java.io.Serializable;
046: import java.net.URI;
047: import java.net.URISyntaxException;
048:
049: import org.deegree.framework.util.StringTools;
050:
051: /**
052: * This class represent a qualified name for something. A name is thought to be built from an
053: * optional prefix and/or a local name E.g.: <BR>- deegree - pre:deegree <BR>
054: * a name may be located within a namespace assigned to the names prefix (or as default namespace if
055: * the name has not prefix).
056: *
057: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
058: * @author last edited by: $Author: apoth $
059: *
060: * @version $Revision: 10522 $, $Date: 2008-03-07 03:09:06 -0800 (Fri, 07 Mar 2008) $
061: */
062: public class QualifiedName implements Serializable {
063:
064: private static final long serialVersionUID = 5551137348397905772L;
065:
066: private String prefix = null;
067:
068: private String localName = null;
069:
070: private URI namespace = null;
071:
072: private String s = null;
073:
074: /**
075: * @param name
076: * local name/simple (without prefix)
077: */
078: public QualifiedName(String name) {
079: if (name.indexOf('{') > -1) {
080: try {
081: namespace = new URI(StringTools.extractStrings(name,
082: "{", "}")[0]);
083: } catch (URISyntaxException e) {
084: }
085: int pos = name.lastIndexOf(':');
086: localName = name.substring(pos + 1);
087: } else if (name.indexOf(':') > -1) {
088: String[] tmp = StringTools.toArray(name, ":", false);
089: if (tmp.length == 2) {
090: prefix = tmp[0];
091: localName = tmp[1];
092: } else {
093: localName = name;
094: }
095: } else {
096: this .localName = name;
097: }
098: buildString();
099: }
100:
101: /**
102: * @param name
103: * complete name including a prefix
104: * @param namespace
105: * namespace the name is located within
106: */
107: public QualifiedName(String name, URI namespace) {
108: if (name.indexOf(':') > -1) {
109: String[] tmp = StringTools.toArray(name, ":", false);
110: prefix = tmp[0];
111: this .localName = tmp[1];
112: } else {
113: this .localName = name;
114: }
115: this .namespace = namespace;
116: buildString();
117: }
118:
119: /**
120: * @param prefix
121: * @param localName
122: * local/simple name (e.g. deegree)
123: * @param namespace
124: * namespace the name is located within
125: */
126: public QualifiedName(String prefix, String localName, URI namespace) {
127: this .prefix = prefix;
128: this .localName = localName;
129: this .namespace = namespace;
130: buildString();
131: }
132:
133: private void buildString() {
134: StringBuffer sb = new StringBuffer(50);
135: if (prefix != null && prefix.length() != 0) {
136: sb.append(prefix).append(':');
137: }
138: sb.append(localName);
139: s = sb.toString();
140: }
141:
142: /**
143: * returns a string representation of a QualifiedName. prefix and local name are separated by
144: * ':'
145: *
146: * @return string representation of a QualifiedName
147: * @deprecated use
148: * @see #getFormattedString() or
149: * @see #getPrefixedName() instead
150: */
151: @Deprecated
152: public String getAsString() {
153: return s;
154: }
155:
156: /**
157: * returns a string representation of a QualifiedName. prefix and local name are separated by
158: * ':'. If the Prefix is null, the sole localname will be returned.
159: *
160: * @return string representation of a QualifiedName
161: */
162: public String getPrefixedName() {
163: return s;
164: }
165:
166: /**
167: * @return a QualifiedName as a formatted string. If a QualifiedName has a namespace the
168: * returned format is:<br>
169: * {namespace}:localName. <br>
170: * Otherwise just a String representation of this qualified name will be returned. Which
171: * means, that if the prefix is not null (allthough not bound to namespace) the result
172: * String will be: <br>
173: * PRE_FIX:localName.<br>
174: * If the Prefix is null, the sole localname will be returned.
175: */
176: public String getFormattedString() {
177: if (namespace != null) {
178: return StringTools.concat(100, "{", namespace, "}:",
179: localName);
180: }
181: return s;
182: }
183:
184: /**
185: * returns the names prefix
186: *
187: * @return the names prefix
188: */
189: public String getPrefix() {
190: return prefix;
191: }
192:
193: /**
194: * returns the local part of the name
195: *
196: * @return the local part of the name
197: */
198: public String getLocalName() {
199: return localName;
200: }
201:
202: /**
203: * returns the namespace the name is located within (may be null)
204: *
205: * @return the namespace the name is located within (may be null)
206: */
207: public URI getNamespace() {
208: return namespace;
209: }
210:
211: /**
212: * @param ns
213: * the namespace to checkfor
214: * @return true if the given namespace equals this qualified name's namespace. If the given ns
215: * is null and the namespace is null, this method will also return true.
216: */
217: public boolean isInNamespace(URI ns) {
218: if (ns == null) {
219: if (this .namespace == null) {
220: return true;
221: }
222: return false;
223: }
224: return ns.equals(this .namespace);
225: }
226:
227: @Override
228: public String toString() {
229: StringBuffer result = new StringBuffer(150);
230: result.append(this .s);
231: if (this .prefix != null && this .prefix.length() > 0) {
232: result.append(" (");
233: result.append(this .prefix);
234: result.append("=");
235: if (this .namespace == null
236: || this .namespace.toASCIIString().length() == 0) {
237: result.append("not bound to a namespace");
238: } else {
239: result.append(this .namespace.toASCIIString());
240: }
241: result.append(")");
242: }
243: return result.toString();
244: }
245:
246: @Override
247: public int hashCode() {
248: return (this .namespace + this .localName).hashCode();
249: }
250:
251: @Override
252: public boolean equals(Object o) {
253: // return false in the case that the object is null
254: // or isn't an instance of QualifiedName
255: if (o == null || !(o instanceof QualifiedName)) {
256: return false;
257: }
258:
259: QualifiedName other = (QualifiedName) o;
260: if (localName.equals(other.getLocalName())) {
261: if ((namespace != null && namespace.equals(other
262: .getNamespace()))
263: || (namespace == null && other.getNamespace() == null)) {
264: return true;
265: }
266: }
267: return false;
268: }
269: }
|