001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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: package org.geotools.util;
018:
019: // J2SE dependencies
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Locale;
023:
024: // OpenGIS dependencies
025: import org.opengis.metadata.Identifier;
026: import org.opengis.util.GenericName;
027: import org.opengis.util.InternationalString;
028:
029: // Geotools dependencies
030: import org.geotools.resources.Utilities;
031:
032: /**
033: * A factory for {@link GenericName} objects.
034: *
035: * @since 2.1
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/NameFactory.java $
037: * @version $Id: NameFactory.java 22443 2006-10-27 20:47:22Z desruisseaux $
038: * @author Martin Desruisseaux
039: */
040: public final class NameFactory {
041: /**
042: * Do not allows instantiation of instance of this class.
043: */
044: private NameFactory() {
045: }
046:
047: /**
048: * Constructs a generic name from a fully qualified name
049: * and the default separator character.
050: *
051: * @param name The fully qualified name.
052: */
053: public static GenericName create(final String name) {
054: return create(name,
055: org.geotools.util.GenericName.DEFAULT_SEPARATOR);
056: }
057:
058: /**
059: * Constructs a generic name from a fully qualified name
060: * and the specified separator character.
061: *
062: * @param name The fully qualified name.
063: * @param separator The separator character.
064: */
065: public static GenericName create(final String name,
066: final char separator) {
067: final List names = new ArrayList();
068: int lower = 0;
069: while (true) {
070: final int upper = name.indexOf(separator, lower);
071: if (upper >= 0) {
072: names.add(name.substring(lower, upper));
073: lower = upper + 1;
074: } else {
075: names.add(name.substring(lower));
076: break;
077: }
078: }
079: return create((String[]) names
080: .toArray(new String[names.size()]), separator);
081: }
082:
083: /**
084: * Constructs a generic name from an array of local names and the default separator character.
085: * If any of the specified names is an {@link InternationalString}, then the
086: * <code>{@linkplain InternationalString#toString(Locale) toString}(null)</code>
087: * method will be used in order to fetch an unlocalized name. Otherwise, the
088: * <code>{@linkplain CharSequence#toString toString}()</code> method will be used.
089: *
090: * @param names The local names as an array of strings or international strings.
091: * This array must contains at least one element.
092: */
093: public static GenericName create(final CharSequence[] names) {
094: return create(names,
095: org.geotools.util.GenericName.DEFAULT_SEPARATOR);
096: }
097:
098: /**
099: * Constructs a generic name from an array of local names and the specified separator character.
100: * If any of the specified names is an {@link InternationalString}, then the
101: * <code>{@linkplain InternationalString#toString(Locale) toString}(null)</code>
102: * method will be used in order to fetch an unlocalized name. Otherwise, the
103: * <code>{@linkplain CharSequence#toString toString}()</code> method will be used.
104: *
105: * @param names The local names as an array of strings.
106: * This array must contains at least one element.
107: * @param separator The separator character to use.
108: */
109: public static GenericName create(final CharSequence[] names,
110: final char separator) {
111: return create(names, names.length, separator);
112: }
113:
114: /**
115: * Constructs a generic name from an array of local names
116: * and the specified separator character.
117: *
118: * @param names The local names as an array of strings.
119: * @param length The valid length of {@code names} array.
120: * @param separator The separator character to use.
121: */
122: private static GenericName create(final CharSequence[] names,
123: final int length, final char separator) {
124: if (length <= 0) {
125: throw new IllegalArgumentException(String.valueOf(length));
126: }
127: if (length == 1) {
128: return new LocalName(names[0]);
129: }
130: return new ScopedName(create(names, length - 1, separator),
131: separator, names[length - 1]);
132: }
133:
134: /**
135: * Returns the specified name in an array. The {@code value} may be either a {@link String},
136: * {@code String[]}, {@link GenericName} or {@code GenericName[]}. This method is used in
137: * {@link org.geotools.referencing.AbstractIdentifiedObject} constructors.
138: *
139: * @param value The object to cast into an array of generic names.
140: * @return The generic names.
141: * @throws ClassCastException if {@code value} can't be cast.
142: */
143: public static GenericName[] toArray(final Object value)
144: throws ClassCastException {
145: if (value instanceof GenericName[]) {
146: return (GenericName[]) value;
147: }
148: if (value instanceof GenericName) {
149: return new GenericName[] { (GenericName) value };
150: }
151: if (value instanceof CharSequence) {
152: return new GenericName[] { create(value.toString()) };
153: }
154: if (value instanceof CharSequence[]) {
155: final CharSequence[] values = (CharSequence[]) value;
156: final GenericName[] names = new GenericName[values.length];
157: for (int i = 0; i < values.length; i++) {
158: final CharSequence v = values[i];
159: names[i] = (v instanceof GenericName) ? (GenericName) v
160: : create(v.toString());
161: }
162: return names;
163: }
164: if (value instanceof Identifier[]) {
165: final Identifier[] values = (Identifier[]) value;
166: final GenericName[] names = new GenericName[values.length];
167: for (int i = 0; i < values.length; i++) {
168: final Identifier v = values[i];
169: names[i] = (v instanceof GenericName) ? (GenericName) v
170: : create(v.getCode());
171: }
172: return names;
173: }
174: // TODO: localize
175: throw new ClassCastException("Cannot convert "
176: + Utilities.getShortClassName(value)
177: + " to GenericName[]");
178: }
179: }
|