001: /*
002: * Created on Sep 22, 2004
003: */
004: package uk.org.ponder.saxalizer;
005:
006: import java.util.Map;
007:
008: import uk.org.ponder.beanutil.support.IndexedPropertyAccessor;
009: import uk.org.ponder.conversion.StaticLeafParser;
010: import uk.org.ponder.reflect.JDKReflectiveCache;
011: import uk.org.ponder.reflect.ReflectiveCache;
012: import uk.org.ponder.saxalizer.mapping.ClassNameManager;
013: import uk.org.ponder.saxalizer.mapping.ContainerTypeRegistry;
014: import uk.org.ponder.saxalizer.mapping.DefaultMapperInferrer;
015: import uk.org.ponder.saxalizer.mapping.SAXalizerMapper;
016: import uk.org.ponder.saxalizer.mapping.SAXalizerMapperInferrer;
017:
018: /**
019: * A reflective mapping context used to infer mappings of Java objects
020: * to and from serial representations, for example to XML or EL expressions.
021: * <p>If a <code>SAXalizerMapperInferrer</code> has been
022: * provided, it will be asked to synthesize a "default" mapping
023: * based on reflection of the class.
024: * </ol>
025: * @author Antranig Basman (antranig@caret.cam.ac.uk)
026: *
027: */
028: public class SAXalizerMappingContext {
029: public SAXalizerMapperInferrer inferrer;
030: public StaticLeafParser saxleafparser;
031: public ClassNameManager classnamemanager = ClassNameManager
032: .instance();
033: private ReflectiveCache reflectivecache;
034: private IndexedPropertyAccessor indexedPropertyAccessor;
035:
036: public IndexedPropertyAccessor getIndexedPropertyAccessor() {
037: return indexedPropertyAccessor;
038: }
039:
040: public void setIndexedPropertyAccessor(
041: IndexedPropertyAccessor indexedPropertyAccessor) {
042: this .indexedPropertyAccessor = indexedPropertyAccessor;
043: }
044:
045: public void setStaticLeafParser(StaticLeafParser saxleafparser) {
046: this .saxleafparser = saxleafparser;
047: }
048:
049: public void setDefaultInferrer(SAXalizerMapperInferrer inferrer) {
050: this .inferrer = inferrer;
051: }
052:
053: public void setChainedInferrer(SAXalizerMapperInferrer inferrer) {
054: inferrer.setChainedInferrer(this .inferrer);
055: this .inferrer = inferrer;
056: }
057:
058: public void setReflectiveCache(ReflectiveCache reflectivecache) {
059: this .reflectivecache = reflectivecache;
060: mapper = new SAXalizerMapper(reflectivecache
061: .getConcurrentMap(1));
062: methodanalysers = reflectivecache.getConcurrentMap(1);
063: }
064:
065: public ReflectiveCache getReflectiveCache() {
066: return reflectivecache;
067: }
068:
069: public SAXalizerMapper mapper;
070: // this is a Hashtable of Classes to MethodAnalysers
071: private Map methodanalysers;
072:
073: public MethodAnalyser getAnalyser(Class clazz) {
074: MethodAnalyser togo = (MethodAnalyser) methodanalysers
075: .get(clazz);
076: if (togo == null) {
077: togo = MethodAnalyser.constructMethodAnalyser(clazz, this );
078: methodanalysers.put(clazz, togo);
079: }
080: return togo;
081: }
082:
083: private void putAnalyser(Class clazz, MethodAnalyser analyser) {
084: methodanalysers.put(clazz, analyser);
085: }
086:
087: private SAXalizerMappingContext(boolean systemwide) {
088: saxleafparser = StaticLeafParser.instance();
089: classnamemanager = ClassNameManager.instance();
090: DefaultMapperInferrer definferrer = new DefaultMapperInferrer();
091: definferrer
092: .setContainerTypeRegistry(new ContainerTypeRegistry());
093: inferrer = definferrer;
094: setReflectiveCache(new JDKReflectiveCache());
095: }
096:
097: public SAXalizerMappingContext() {
098: saxleafparser = new StaticLeafParser();
099: classnamemanager = new ClassNameManager();
100: }
101:
102: private static SAXalizerMappingContext instance = new SAXalizerMappingContext(
103: true);
104:
105: /** Returns a JVM-wide context to be used where no specialised
106: * context is supplied to the SAXalizer or DeSAXalizer. This
107: * context may NOT be used in conjunction with any dynamic mapping
108: * information, only static or default-inferred mappings are permitted.
109: * @return
110: */
111: public static SAXalizerMappingContext instance() {
112: return instance;
113: }
114: }
|