001: /*
002: * Created on Feb 5, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.vfny.geoserver.global.xml;
008:
009: import java.util.HashSet;
010: import java.util.Iterator;
011: import java.util.Set;
012:
013: /**
014: * NameSpaceTranslator purpose.
015: * <p>
016: * Helps perform translation between element names, definition names
017: * and their java types for a particular namespace and namespace prefix.
018: * </p>
019: * <p>
020: * Each name space translator should contain a list of name space
021: * elements for their particular prefix. This loading should not be
022: * completed lazily to avoid performance lags at run time. When ever
023: * posible constants should alos be used for performance purposes.
024: * </p>
025: * <p>
026: * USE:
027: * <code>
028: * NameSpaceTranslator nst = NameSpaceTranslatorFactor.getInstance().getNameSpaceTranslator("xs");
029: * Class cls = nst.getElement("string").getJavaClass();
030: * ...
031: * Object obj // contains some data, what can it be represented as?
032: * String elementName = ((NameSpaceElement)nst.getElements(obj).toArray()[0]).getTypeRefName();
033: * </code>
034: * </p>
035: * @author dzwiers, Refractions Research, Inc.
036: * @author $Author: dmzwiers $ (last modification)
037: * @version $Id: NameSpaceTranslator.java 6412 2007-03-30 20:42:25Z saul.farber $
038: */
039: public abstract class NameSpaceTranslator {
040: /** the prefix for this translator instance*/
041: private String prefix;
042:
043: /**
044: * NameSpaceTranslator constructor.
045: * <p>
046: * Creates an instance of this translator for the given prefix.
047: * </p>
048: * @param prefix The prefix for which this tranlator will tranlate.
049: * A null prefix will affect the NameSpaceElements returned by the
050: * access methods.
051: * @see NameSpaceElement(String)
052: */
053: public NameSpaceTranslator(String prefix) {
054: this .prefix = prefix;
055: }
056:
057: /**
058: * Retrive all elements that can be used with the provided type.
059: * <p>
060: * Looks for Elements who's Class objects, or the parents of the
061: * Class object are compatible with this class object.
062: * </p>
063: * @param type Class the class to attempt to find related elements for.
064: * @return Set a set of associated NameSpaceElements
065: */
066: public Set getAssociatedTypes(Class type) {
067: if (type == null) {
068: return null;
069: }
070:
071: HashSet r = new HashSet();
072: Set elems = getElements();
073: Iterator i = elems.iterator();
074:
075: while (i.hasNext()) {
076: NameSpaceElement nse = (NameSpaceElement) i.next();
077:
078: if (nse != null) {
079: Class cls = nse.getJavaClass();
080:
081: if ((cls != null) && cls.isAssignableFrom(type)
082: && !cls.equals(Object.class)) {
083: r.add(nse);
084: }
085: }
086: }
087:
088: return r;
089: }
090:
091: /**
092: * Looks for Elements who's name is the same or a super set of this name.
093: * <p>
094: * (ie. name.indexOf(type)!=-1)
095: * </p>
096: * @param type String the class to attempt to find related elements for.
097: * @return Set a set of associated NameSpaceElements
098: * @see String.indexOf(String)
099: */
100: public Set getAssociatedTypes(String type) {
101: if (type == null) {
102: return null;
103: }
104:
105: HashSet r = new HashSet();
106: Set elems = getElements();
107: Iterator i = elems.iterator();
108:
109: while (i.hasNext()) {
110: NameSpaceElement nse = (NameSpaceElement) i.next();
111:
112: if (nse != null) {
113: String name = nse.getTypeRefName();
114:
115: if ((name != null) && (name.indexOf(type) != -1)) {
116: r.add(nse);
117: }
118:
119: name = nse.getTypeDefName();
120:
121: if ((name != null) && (name.indexOf(type) != -1)) {
122: r.add(nse);
123: }
124: }
125: }
126:
127: return r;
128: }
129:
130: /**
131: * isValidDefinition purpose.
132: * <p>
133: * checks to see if the definition provided is found in the list of
134: * elements for this namespace.
135: * </p>
136: * @param definition The definition name to check for, may be either definition or prefix:definition.
137: * @return true when found, false otherwise.
138: */
139: public boolean isValidDefinition(String definition) {
140: if ((definition == null) || (definition == "")) {
141: return false;
142: }
143:
144: Set elems = getElements();
145: Iterator i = elems.iterator();
146:
147: while (i.hasNext()) {
148: NameSpaceElement nse = (NameSpaceElement) i.next();
149:
150: if (nse == null) {
151: continue;
152: }
153:
154: String def = nse.getTypeDefName();
155:
156: if ((def != null) && def.equals(definition)) {
157: return true;
158: }
159:
160: def = nse.getQualifiedTypeDefName();
161:
162: if ((def != null) && def.equals(definition)) {
163: return true;
164: }
165: }
166:
167: return false;
168: }
169:
170: /**
171: * isValidTypeRef purpose.
172: * <p>
173: * checks to see if the reference provided is found in the list of
174: * elements for this namespace.
175: * </p>
176: * @param definition The reference name to check for, may be either reference or prefix:reference.
177: * @return true when found, false otherwise.
178: */
179: public boolean isValidTypeRef(String type) {
180: if ((type == null) || (type == "")) {
181: return false;
182: }
183:
184: Set elems = getElements();
185: Iterator i = elems.iterator();
186:
187: while (i.hasNext()) {
188: NameSpaceElement nse = (NameSpaceElement) i.next();
189:
190: if (nse == null) {
191: continue;
192: }
193:
194: String tp = nse.getTypeRefName();
195:
196: if ((tp != null) && tp.equals(type)) {
197: return true;
198: }
199:
200: tp = nse.getQualifiedTypeRefName();
201:
202: if ((tp != null) && tp.equals(type)) {
203: return true;
204: }
205: }
206:
207: return false;
208: }
209:
210: /**
211: * getElements purpose.
212: * <p>
213: * returns the set of elements.
214: * </p>
215: * @return Set
216: */
217: public abstract Set getElements();
218:
219: /**
220: * getElements purpose.
221: * <p>
222: * Returns a set of all elements with the exact class specified.
223: * </p>
224: * @param type Class the class of elements to get
225: * @return Set
226: */
227: public Set getElements(Class type) {
228: if (type == null) {
229: return null;
230: }
231:
232: HashSet r = new HashSet();
233: Set elems = getElements();
234: Iterator i = elems.iterator();
235:
236: while (i.hasNext()) {
237: NameSpaceElement nse = (NameSpaceElement) i.next();
238:
239: if ((nse != null) && type.equals(nse.getJavaClass())) {
240: r.add(nse);
241: }
242: }
243:
244: return r;
245: }
246:
247: /**
248: * Gets an element definition by name.
249: *
250: * @param name The name of the element definition
251: * @return NameSpaceElement
252: */
253: public NameSpaceElement getElement(String name) {
254: if (name == null) {
255: return null;
256: }
257:
258: Set elems = getElements();
259: Iterator i = elems.iterator();
260:
261: while (i.hasNext()) {
262: NameSpaceElement nse = (NameSpaceElement) i.next();
263:
264: if (nse != null) {
265: if (name.equals(nse.getTypeRefName())) {
266: return nse;
267: }
268:
269: if (name.equals(nse.getTypeDefName())) {
270: return nse;
271: }
272:
273: if (name.equals(nse.getQualifiedTypeRefName())) {
274: return nse;
275: }
276:
277: if (name.equals(nse.getQualifiedTypeDefName())) {
278: return nse;
279: }
280: }
281: }
282:
283: return null;
284: }
285:
286: /**
287: * Gets the default element for the class type passed in. Note that this
288: * is a bit hacky, as it doesn't not depend on a real namespace map, but
289: * on careful assignment of the NamespaceElements, so that each class only
290: * has one that returns true for isDefault(). Sorry for the hackiness,
291: * I need to get a release out.
292: */
293: public NameSpaceElement getDefaultElement(Class type) {
294: Set posibilities = getElements(type);
295:
296: //System.out.println("getting default for type: " + type + " = " + posibilities);
297: if (posibilities.size() == 0) {
298: return null;
299: }
300:
301: Iterator i = posibilities.iterator();
302:
303: while (i.hasNext()) {
304: NameSpaceElement nse = (NameSpaceElement) i.next();
305:
306: if (nse != null) {
307: if (nse.isDefault()) {
308: return nse;
309: }
310: }
311: }
312:
313: return null;
314: }
315:
316: /**
317: * Gets an element definition by name.
318: *
319: * @param name The name of the element definition
320: * @return NameSpaceElement
321: */
322: public NameSpaceElement getElement(Class type, String name) {
323: if (type == null) {
324: return null;
325: }
326:
327: Set posibilities = getElements(type);
328:
329: if (posibilities.size() == 0) {
330: return null;
331: }
332:
333: if (posibilities.size() == 1) {
334: return (NameSpaceElement) posibilities.toArray()[0];
335: }
336:
337: if (name == null) {
338: return (NameSpaceElement) posibilities.toArray()[0];
339: }
340:
341: Iterator i = posibilities.iterator();
342:
343: while (i.hasNext()) {
344: NameSpaceElement nse = (NameSpaceElement) i.next();
345:
346: if (nse != null) {
347: if (name.equals(nse.getTypeRefName())) {
348: return nse;
349: }
350:
351: if (name.equals(nse.getTypeDefName())) {
352: return nse;
353: }
354:
355: if (name.equals(nse.getQualifiedTypeRefName())) {
356: return nse;
357: }
358:
359: if (name.equals(nse.getQualifiedTypeDefName())) {
360: return nse;
361: }
362:
363: if (nse.isDefault()) {
364: return nse;
365: }
366: }
367: }
368:
369: return (NameSpaceElement) posibilities.toArray()[0];
370: }
371:
372: /**
373: * getNameSpace purpose.
374: * <p>
375: * Returns the current namespace. Should be implemented as a constant.
376: * </p>
377: * @return String
378: */
379: public abstract String getNameSpace();
380:
381: /**
382: * getPrefix purpose.
383: * <p>
384: * Returns the prefix that this namespace represents.
385: * </p>
386: * @return String the prefix, null if it does not exist
387: */
388: public final String getPrefix() {
389: return prefix;
390: }
391: }
|