001: /*
002: * Created on May 17, 2006
003: */
004: package uk.org.ponder.rsf.renderer.decorator;
005:
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: import org.springframework.beans.BeansException;
010: import org.springframework.context.ApplicationContext;
011: import org.springframework.context.ApplicationContextAware;
012:
013: import uk.org.ponder.rsf.components.decorators.DecoratorList;
014: import uk.org.ponder.rsf.components.decorators.UIDecorator;
015: import uk.org.ponder.rsf.content.ContentTypeInfo;
016: import uk.org.ponder.stringutil.StringList;
017: import uk.org.ponder.util.Logger;
018:
019: public class DecoratorManager implements ApplicationContextAware {
020: // decorators index by decorator class - has subindex by content type.
021: // if no match on exact type, go first for "" and then finally for any found.
022: private Map byClass = new HashMap();
023:
024: private ContentTypeInfo contenttypeinfo;
025:
026: public void setContentTypeInfo(ContentTypeInfo contenttypeinfo) {
027: this .contenttypeinfo = contenttypeinfo;
028: }
029:
030: public void decorate(DecoratorList decorators, String tagname,
031: Map attrmap) {
032: if (decorators == null)
033: return;
034: String requestcontent = contenttypeinfo.get().typename;
035: for (int i = 0; i < decorators.size(); ++i) {
036: UIDecorator dec = decorators.decoratorAt(i);
037: DecoratorRenderer renderer = lookupRenderer(dec,
038: requestcontent);
039: if (renderer == null) {
040: if (Logger.log.isDebugEnabled()) {
041: Logger.log
042: .debug("Unable to find renderer for decorator "
043: + dec.getClass()
044: + " and content type "
045: + requestcontent);
046: }
047: } else {
048: renderer.modifyAttributes(dec, tagname, attrmap);
049: }
050: }
051: }
052:
053: private DecoratorRenderer lookupRenderer(UIDecorator dec,
054: String requestcontent) {
055: Map typemap = getTypeMap(dec);
056: if (typemap == null)
057: return null;
058:
059: DecoratorRenderer togo = (DecoratorRenderer) typemap
060: .get(requestcontent);
061: if (togo == null) {
062: togo = (DecoratorRenderer) typemap.get("");
063: }
064: if (togo == null) {
065: togo = (DecoratorRenderer) typemap.values().iterator()
066: .next();
067: }
068: return togo;
069: }
070:
071: private Map getTypeMap(UIDecorator dec) {
072: Class clazz = dec.getClass();
073: while (clazz != null && clazz != UIDecorator.class) {
074: Map typemap = (Map) byClass.get(clazz);
075: if (typemap != null && !typemap.isEmpty()) {
076: return typemap;
077: }
078: clazz = clazz.getSuperclass();
079: }
080: return null;
081: }
082:
083: public void setApplicationContext(
084: ApplicationContext applicationContext)
085: throws BeansException {
086: String decorators[] = applicationContext.getBeanNamesForType(
087: DecoratorRenderer.class, false, false);
088: for (int i = 0; i < decorators.length; ++i) {
089: DecoratorRenderer renderer = (DecoratorRenderer) applicationContext
090: .getBean(decorators[i]);
091:
092: Class declass = renderer.getRenderedType();
093: Map typemap = (Map) byClass.get(declass);
094: if (typemap == null) {
095: typemap = new HashMap();
096: byClass.put(declass, typemap);
097: }
098:
099: StringList contents = StringList.fromString(renderer
100: .getContentTypes());
101:
102: for (int j = 0; j < contents.size(); ++j) {
103: String content = contents.stringAt(j).trim();
104: if (content.length() > 0) {
105: typemap.put(content, renderer);
106: }
107: }
108:
109: if (contents.size() == 0) {
110: typemap.put("", renderer);
111: }
112:
113: }
114:
115: }
116: }
|