001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.wfs.xml;
006:
007: import org.geotools.feature.AttributeType;
008: import org.geotools.feature.type.ProfileImpl;
009: import org.opengis.feature.type.Name;
010: import org.opengis.feature.type.Schema;
011: import java.util.ArrayList;
012: import java.util.Collections;
013: import java.util.Comparator;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Set;
017:
018: /**
019: * A special profile of a pariticular {@link Schema} which maintains a unique
020: * mapping of java class to xml schema type.
021: *
022: * @author Justin Deoliveira, The Open Planning Project
023: *
024: */
025: public class TypeMappingProfile /*extends ProfileImpl*/{
026:
027: /**
028: * Set of profiles to do mappings from.
029: */
030: Set/*<Profile>*/profiles;
031:
032: // public TypeMappingProfile(Schema schema, Set profile) {
033: // super(schema, profile);
034: // }
035:
036: public TypeMappingProfile(Set profiles) {
037: this .profiles = profiles;
038: }
039:
040: /**
041: * Obtains the {@link AttributeType} mapped to a particular class.
042: * <p>
043: * If an exact match cannot be made, then those types which are supertypes
044: * of clazz are examined.
045: * </p>
046: * @param clazz The class.
047: *
048: * @return The AttributeType, or <code>null</code> if no atttribute
049: * type mapped to <code>clazz</code>
050: */
051: public AttributeType type(Class clazz) {
052: ArrayList assignable = new ArrayList();
053:
054: for (Iterator p = profiles.iterator(); p.hasNext();) {
055: ProfileImpl profile = (ProfileImpl) p.next();
056:
057: for (Iterator i = profile.values().iterator(); i.hasNext();) {
058: AttributeType type = (AttributeType) i.next();
059:
060: if (type.getType().isAssignableFrom(clazz)) {
061: assignable.add(type);
062: }
063:
064: if (clazz.equals(type.getType())) {
065: return type;
066: }
067: }
068: }
069:
070: if (assignable.isEmpty()) {
071: return null;
072: }
073:
074: if (assignable.size() == 1) {
075: return (AttributeType) assignable.get(0);
076: } else {
077: //sort
078: Comparator comparator = new Comparator() {
079: public int compare(Object o1, Object o2) {
080: AttributeType a1 = (AttributeType) o1;
081: AttributeType a2 = (AttributeType) o2;
082:
083: if (a1.getType().equals(a2.getType())) {
084: return 0;
085: }
086:
087: if (a1.getType().isAssignableFrom(a2.getType())) {
088: return 1;
089: }
090:
091: return -1;
092: }
093: };
094:
095: Collections.sort(assignable, comparator);
096:
097: if (!assignable.get(0).equals(assignable.get(1))) {
098: return (AttributeType) assignable.get(0);
099: }
100: }
101:
102: return null;
103: }
104:
105: /**
106: * Obtains the {@link Name} of the {@link AttributeType} mapped
107: * to a particular class.
108: *
109: * @param clazz The class.
110: *
111: * @return The Name, or <code>null</code> if no atttribute type mapped
112: * to <code>clazz</code>
113: */
114: public Name name(Class clazz) {
115: ArrayList assignable = new ArrayList();
116:
117: for (Iterator p = profiles.iterator(); p.hasNext();) {
118: ProfileImpl profile = (ProfileImpl) p.next();
119:
120: for (Iterator i = profile.entrySet().iterator(); i
121: .hasNext();) {
122: Map.Entry entry = (Map.Entry) i.next();
123: AttributeType type = (AttributeType) entry.getValue();
124:
125: if (type.getType().isAssignableFrom(clazz)) {
126: assignable.add(entry);
127: }
128:
129: if (clazz.equals(type.getType())) {
130: return (Name) entry.getKey();
131: }
132: }
133: }
134:
135: if (assignable.isEmpty()) {
136: return null;
137: }
138:
139: if (assignable.size() == 1) {
140: return (Name) ((Map.Entry) assignable.get(0)).getKey();
141: } else {
142: //sort
143: Comparator comparator = new Comparator() {
144: public int compare(Object o1, Object o2) {
145: Map.Entry e1 = (Map.Entry) o1;
146: Map.Entry e2 = (Map.Entry) o2;
147:
148: AttributeType a1 = (AttributeType) e1.getValue();
149: AttributeType a2 = (AttributeType) e2.getValue();
150:
151: if (a1.getType().equals(a2.getType())) {
152: return 0;
153: }
154:
155: if (a1.getType().isAssignableFrom(a2.getType())) {
156: return 1;
157: }
158:
159: return -1;
160: }
161: };
162:
163: Collections.sort(assignable, comparator);
164:
165: Map.Entry e1 = (Map.Entry) assignable.get(0);
166: Map.Entry e2 = (Map.Entry) assignable.get(1);
167: AttributeType a1 = (AttributeType) e1.getValue();
168: AttributeType a2 = (AttributeType) e2.getValue();
169:
170: if (!a1.getType().equals(a2.getType())) {
171: return (Name) e1.getKey();
172: }
173: }
174:
175: return null;
176: }
177: }
|