001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.feature;
017:
018: import java.net.URI;
019: import java.util.Arrays;
020:
021: import org.geotools.factory.FactoryCreator;
022: import org.geotools.factory.FactoryRegistry;
023: import org.geotools.factory.FactoryRegistryException;
024:
025: /**
026: * Replaced with use of FeatureTypeBuilder to follow standard pattern
027: * naming conventions.
028: *
029: * @deprecated Please use FeatureTypeBuilder
030: * @author Ian Schneider
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/FeatureTypeFactory.java $
032: * @version $Id: FeatureTypeFactory.java 22347 2006-10-24 01:57:23Z jdeolive $
033: */
034: public abstract class FeatureTypeFactory extends FeatureTypeBuilder {
035: /**
036: * An empty public constructor. Subclasses should not provide a
037: * constructor.
038: */
039: public FeatureTypeFactory() {
040: // no op constructor
041: }
042:
043: /**
044: * The service registry for this manager.
045: * Will be initialized only when first needed.
046: */
047: private static FactoryRegistry registry;
048:
049: /**
050: * Returns the service registry. The registry will be created the first
051: * time this method is invoked.
052: */
053: private static FactoryRegistry getServiceRegistry() {
054: assert Thread.holdsLock(FeatureTypeFactory.class);
055: if (registry == null) {
056: registry = new FactoryCreator(Arrays
057: .asList(new Class[] { FeatureTypeFactory.class }));
058: }
059: return registry;
060: }
061:
062: /**
063: * The most specific way to create a new FeatureType.
064: *
065: * @param types The AttributeTypes to create the FeatureType with.
066: * @param name The typeName of the FeatureType. Required, may not be null.
067: * @param ns The namespace of the FeatureType. Optional, may be null.
068: * @param isAbstract True if this created type should be abstract.
069: * @param superTypes A Collection of types the FeatureType will inherit
070: * from. Currently, all types inherit from feature in the opengis
071: * namespace.
072: *
073: * @return A new FeatureType created from the given arguments.
074: *
075: * @throws FactoryConfigurationError If there are problems creating a
076: * factory.
077: * @throws SchemaException If the AttributeTypes provided are invalid in
078: * some way.
079: */
080: public static FeatureType newFeatureType(AttributeType[] types,
081: String name, URI ns, boolean isAbstract,
082: FeatureType[] super Types) throws FactoryRegistryException,
083: SchemaException {
084: return newFeatureType(types, name, ns, isAbstract, super Types,
085: null);
086: }
087:
088: /**
089: * The most specific way to create a new FeatureType.
090: *
091: * @param types The AttributeTypes to create the FeatureType with.
092: * @param name The typeName of the FeatureType. Required, may not be null.
093: * @param ns The namespace of the FeatureType. Optional, may be null.
094: * @param isAbstract True if this created type should be abstract.
095: * @param superTypes A Collection of types the FeatureType will inherit
096: * from. Currently, all types inherit from feature in the opengis
097: * namespace.
098: *
099: * @return A new FeatureType created from the given arguments.
100: *
101: * @throws FactoryRegistryException If there are problems creating a
102: * factory.
103: * @throws SchemaException If the AttributeTypes provided are invalid in
104: * some way.
105: */
106: public static FeatureType newFeatureType(AttributeType[] types,
107: String name, URI ns, boolean isAbstract,
108: FeatureType[] super Types, AttributeType defaultGeometry)
109: throws FactoryRegistryException, SchemaException {
110: FeatureTypeFactory factory = newInstance(name);
111: factory.addTypes(types);
112: factory.setNamespace(ns);
113: factory.setAbstract(isAbstract);
114: if (defaultGeometry != null)
115: factory
116: .setDefaultGeometry((GeometryAttributeType) defaultGeometry);
117:
118: if (super Types != null) {
119: factory.setSuperTypes(Arrays.asList(super Types));
120: }
121:
122: return factory.getFeatureType();
123: }
124:
125: /**
126: * The most specific way to create a new FeatureType.
127: *
128: * @param types The AttributeTypes to create the FeatureType with.
129: * @param name The typeName of the FeatureType. Required, may not be null.
130: * @param ns The namespace of the FeatureType. Optional, may be null.
131: * @param isAbstract True if this created type should be abstract.
132: * @param superTypes A Collection of types the FeatureType will inherit
133: * from. Currently, all types inherit from feature in the opengis
134: * namespace.
135: *
136: * @return A new FeatureType created from the given arguments.
137: *
138: * @throws FactoryRegistryException If there are problems creating a
139: * factory.
140: * @throws SchemaException If the AttributeTypes provided are invalid in
141: * some way.
142: */
143: public static FeatureType newFeatureType(AttributeType[] types,
144: String name, URI ns, boolean isAbstract,
145: FeatureType[] super Types,
146: GeometryAttributeType defaultGeometry)
147: throws FactoryRegistryException, SchemaException {
148: FeatureTypeFactory factory = newInstance(name);
149: factory.addTypes(types);
150: factory.setNamespace(ns);
151: factory.setAbstract(isAbstract);
152:
153: if (super Types != null) {
154: factory.setSuperTypes(Arrays.asList(super Types));
155: }
156:
157: if (defaultGeometry != null) {
158: factory.setDefaultGeometry(defaultGeometry);
159: }
160:
161: return factory.getFeatureType();
162: }
163:
164: /**
165: * Create a new FeatureType with the given AttributeTypes. A short cut for
166: * calling <code>newFeatureType(types,name,ns,isAbstract,null)</code>.
167: *
168: * @param types The AttributeTypes to create the FeatureType with.
169: * @param name The typeName of the FeatureType. Required, may not be null.
170: * @param ns The namespace of the FeatureType. Optional, may be null.
171: * @param isAbstract True if this created type should be abstract.
172: *
173: * @return A new FeatureType created from the given arguments.
174: *
175: * @throws FactoryRegistryException If there are problems creating a
176: * factory.
177: * @throws SchemaException If the AttributeTypes provided are invalid in
178: * some way.
179: */
180: public static FeatureType newFeatureType(AttributeType[] types,
181: String name, URI ns, boolean isAbstract)
182: throws FactoryRegistryException, SchemaException {
183: return newFeatureType(types, name, ns, isAbstract, null);
184: }
185:
186: /**
187: * Create a new FeatureType with the given AttributeTypes. A short cut for
188: * calling <code>newFeatureType(types,name,ns,false,null)</code>.
189: *
190: * @param types The AttributeTypes to create the FeatureType with.
191: * @param name The typeName of the FeatureType. Required, may not be null.
192: * @param ns The namespace of the FeatureType. Optional, may be null.
193: *
194: * @return A new FeatureType created from the given arguments.
195: *
196: * @throws FactoryRegistryException If there are problems creating a
197: * factory.
198: * @throws SchemaException If the AttributeTypes provided are invalid in
199: * some way.
200: */
201: public static FeatureType newFeatureType(AttributeType[] types,
202: String name, URI ns) throws FactoryRegistryException,
203: SchemaException {
204: return newFeatureType(types, name, ns, false);
205: }
206:
207: /**
208: * Create a new FeatureType with the given AttributeTypes. A short cut for
209: * calling <code>newFeatureType(types,name,null,false,null)</code>. Useful
210: * for test cases or datasources which may not allow a namespace.
211: *
212: * @param types The AttributeTypes to create the FeatureType with.
213: * @param name The typeName of the FeatureType. Required, may not be null.
214: *
215: * @return A new FeatureType created from the given arguments.
216: *
217: * @throws FactoryRegistryException If there are problems creating a
218: * factory.
219: * @throws SchemaException If the AttributeTypes provided are invalid in
220: * some way.
221: */
222: public static FeatureType newFeatureType(AttributeType[] types,
223: String name) throws FactoryRegistryException,
224: SchemaException {
225: return newFeatureType(types, name,
226: FeatureTypes.DEFAULT_NAMESPACE, false);
227: }
228:
229: /**
230: * Create a new FeatureTypeFactory with the given typeName.
231: *
232: * @param name The typeName of the feature to create.
233: *
234: * @return A new FeatureTypeFactory instance.
235: *
236: * @throws FactoryRegistryException If there exists a configuration error.
237: */
238: public static synchronized FeatureTypeFactory newInstance(
239: String name) throws FactoryRegistryException {
240: FeatureTypeFactory factory = (FeatureTypeFactory) getServiceRegistry()
241: .getServiceProvider(FeatureTypeFactory.class, null,
242: null, null);
243: FeatureTypeFactory newFactory;
244: try {
245: newFactory = (FeatureTypeFactory) factory.getClass()
246: .newInstance();
247: } catch (InstantiationException e) {
248: throw new FactoryRegistryException(e.getMessage(), e);
249: } catch (IllegalAccessException e) {
250: throw new FactoryRegistryException(e.getMessage(), e);
251: }
252: newFactory.setName(name);
253:
254: return newFactory;
255: }
256:
257: /**
258: * Create a FeatureTypeFactory which contains all of the AttributeTypes
259: * from the given FeatureType. This is simply a convenience method for<br>
260: * <code><pre>
261: * FeatureTypeFactory factory = FeatureTypeFactory.newInstace();
262: * factory.importType(yourTypeHere);
263: * factory.setName(original.getName());
264: * factory.setNamespace(original.getNamespace());
265: * factory.setNillable(original.isNillable());
266: * factory.setDefaultGeometry(original.getDefaultGeometry());
267: * </pre></code>
268: *
269: * @param original The FeatureType to obtain information from.
270: *
271: * @return A new FeatureTypeFactory which is initialized with the state of
272: * the original FeatureType.
273: *
274: * @throws FactoryRegistryException If a FeatureTypeFactory cannot be
275: * found.
276: */
277: public static FeatureTypeFactory createTemplate(FeatureType original)
278: throws FactoryRegistryException {
279:
280: FeatureTypeFactory builder = FeatureTypeFactory
281: .newInstance(original.getTypeName());
282: builder.importType(original);
283: builder.setNamespace(original.getNamespace());
284: builder.setDefaultGeometry(original.getDefaultGeometry());
285:
286: FeatureType[] ancestors = original.getAncestors();
287:
288: if (ancestors != null) {
289: builder.setSuperTypes(Arrays.asList(ancestors));
290: }
291:
292: return builder;
293: }
294:
295: /**
296: * Returns a string representation of this factory.
297: *
298: * @return The string representing this factory.
299: */
300: public String toString() {
301: String types = "";
302:
303: for (int i = 0, ii = getAttributeCount(); i < ii; i++) {
304: types += get(i);
305:
306: if (i < ii) {
307: types += " , ";
308: }
309: }
310:
311: return "FeatureTypeFactory(" + getClass().getName() + ") [ "
312: + types + " ]";
313: }
314:
315: }
|