001: /*
002: * Created on Dec 3, 2006
003: */
004: package uk.org.ponder.beanutil.support;
005:
006: import java.util.Iterator;
007: import java.util.Set;
008:
009: import uk.org.ponder.beanutil.BeanLocator;
010: import uk.org.ponder.beanutil.BeanModelAlterer;
011: import uk.org.ponder.beanutil.IterableBeanLocator;
012: import uk.org.ponder.conversion.StaticLeafParser;
013: import uk.org.ponder.util.ObstinateMap;
014: import uk.org.ponder.util.ObstinateSet;
015: import uk.org.ponder.util.UniversalRuntimeException;
016:
017: /** Converts arbitrary EL-traversable material to standard Maps - that is,
018: * abstracts away the mechanism of property access, via by BeanLocator or
019: * physical bean properties/fields, and renders them all as Map key/values.
020: *
021: * @author Antranig Basman (antranig@caret.cam.ac.uk)
022: *
023: */
024:
025: public class ObstinateTravellingMap extends ObstinateMap {
026: private Object seed;
027: private BeanModelAlterer bma;
028: private StaticLeafParser staticLeafParser;
029: private boolean wraptrunks;
030: private String seedpath;
031:
032: public void init() {
033: if (seedpath != null) {
034: seed = bma.getBeanValue(seedpath, seed, null);
035: }
036: }
037:
038: public void setSeed(Object seed) {
039: this .seed = seed;
040: }
041:
042: public void setSeedPath(String seedpath) {
043: this .seedpath = seedpath;
044: }
045:
046: public void setBeanModelAlterer(BeanModelAlterer bma) {
047: this .bma = bma;
048: }
049:
050: public void setStaticLeafParser(StaticLeafParser staticLeafParser) {
051: this .staticLeafParser = staticLeafParser;
052: }
053:
054: public void setWrapTrunks(boolean wraptrunks) {
055: this .wraptrunks = wraptrunks;
056: }
057:
058: public boolean containsKey(Object key) {
059: Object child = bma.getBeanValue((String) key, seed, null);
060: return child != null;
061: }
062:
063: public Set keySet() {
064: return new ObstinateSet() {
065:
066: public boolean contains(final Object o) {
067: return containsKey(o);
068: }
069:
070: public Iterator iterator() {
071: if (seed instanceof IterableBeanLocator) {
072: return ((IterableBeanLocator) seed).iterator();
073: } else
074: throw new UniversalRuntimeException(
075: "Wrapped bean of " + seed.getClass()
076: + " is not iterable");
077: }
078: };
079: }
080:
081: public Object get(Object key) {
082: Object child = bma.getBeanValue((String) key, seed, null);
083: return wrapChild(child);
084: }
085:
086: private Object wrapChild(Object child) {
087: boolean wrap = false;
088: if (child instanceof BeanLocator) {
089: wrap = true;
090: } else if (staticLeafParser.isLeafType(child.getClass())) {
091: wrap = false;
092: } else
093: wrap = wraptrunks;
094:
095: if (wrap) {
096: ObstinateTravellingMap togo = new ObstinateTravellingMap();
097: togo.setBeanModelAlterer(bma);
098: togo.setStaticLeafParser(staticLeafParser);
099: togo.setWrapTrunks(wraptrunks);
100: togo.setSeed(child);
101: togo.init();
102: return togo;
103: } else
104: return child;
105: }
106:
107: }
|