001: /*
002: * Created on Nov 11, 2005
003: */
004: package uk.org.ponder.conversion;
005:
006: import java.util.Enumeration;
007:
008: import uk.org.ponder.arrayutil.ArrayUtil;
009: import uk.org.ponder.beanutil.BeanResolver;
010: import uk.org.ponder.reflect.ReflectiveCache;
011: import uk.org.ponder.saxalizer.mapping.ContainerTypeRegistry;
012: import uk.org.ponder.stringutil.StringList;
013: import uk.org.ponder.util.Denumeration;
014: import uk.org.ponder.util.EnumerationConverter;
015: import uk.org.ponder.util.Logger;
016:
017: /**
018: * An "aggregating" object parser/renderer that is capable of dealing with
019: * vector (array, list) values by repeated invokation of a LeafObjectParser for
020: * each parse/render.
021: *
022: * @author Antranig Basman (antranig@caret.cam.ac.uk)
023: */
024: // Should convert this into an interface, but there is not really any kind of
025: // alternative implementation - all customizability is at the scalarparser
026: // level.
027: public class VectorCapableParser {
028: private StaticLeafParser scalarparser;
029: private ContainerTypeRegistry ctr;
030:
031: public void setScalarParser(StaticLeafParser scalarparser) {
032: this .scalarparser = scalarparser;
033: }
034:
035: /**
036: * Determines whether the supplied object is some kind of "List of Strings"
037: * type, either String[] or StringList.
038: */
039: public static boolean isLOSType(Object o) {
040: Logger.log.info("SAC: " + ArrayUtil.stringArrayClass);
041: return ArrayUtil.stringArrayClass
042: .isAssignableFrom(o.getClass())
043: || o instanceof StringList;
044: }
045:
046: public void setContainerTypeRegistry(ContainerTypeRegistry ctr) {
047: this .ctr = ctr;
048: }
049:
050: /**
051: * Converts some form of multiple Strings into some form of multiple Objects.
052: *
053: * @param stringlist Some form of String collection - either a String[] array,
054: * or any kind of Collection with String elements. {@link isLOSType}
055: * will return true for this argument.
056: * @param target Some kind of collection or array
057: * {@link EnumerationConverter.isDenumerable} will return true for
058: * this argument. The elements of this "collection" will be filled
059: * with parsed values.
060: * @return Either a List or Array of Object according to whether the second
061: * parameter represents a Class or is null.
062: */
063: public Object parse(Object stringlist, Object target,
064: Class elemtype, ReflectiveCache reflectivecache) {
065: // int size = EnumerationConverter.getEnumerableSize(stringlist);
066: // Object togo = arraytype == null? new ArrayList(size) :
067: // Array.newInstance(arraytype, size);
068: Denumeration denum = EnumerationConverter.getDenumeration(
069: target, reflectivecache);
070: Class containeetype = ctr.getContaineeType(target);
071: if (containeetype != null) {
072: elemtype = containeetype;
073: }
074: for (Enumeration elemenum = EnumerationConverter
075: .getEnumeration(stringlist); elemenum.hasMoreElements();) {
076: String elem = (String) elemenum.nextElement();
077: Object converted = scalarparser.parse(elemtype, elem);
078: denum.add(converted);
079: }
080: return target;
081: }
082:
083: /**
084: * Converts some form of multiple objects into some form of multiple Strings.
085: *
086: * @param torenders Either some form of Object array or a Collection.
087: * @param The object into which the rendered objects are to be placed as
088: * Strings - either a String[] array (of the correct size!) or a
089: * Collection.
090: */
091: // This code will go into ValueFixer.
092: public void render(Object torenders, Object toreceive,
093: BeanResolver resolver, ReflectiveCache reflectivecache) {
094: Denumeration denum = EnumerationConverter.getDenumeration(
095: toreceive, reflectivecache);
096: for (Enumeration rendenum = EnumerationConverter
097: .getEnumeration(torenders); rendenum.hasMoreElements();) {
098: Object torender = rendenum.nextElement();
099: String rendered = resolver == null ? scalarparser
100: .render(torender) : resolver.resolveBean(torender);
101: denum.add(rendered);
102: }
103: }
104:
105: }
|