001: /*
002: * Created on Oct 25, 2005
003: */
004: package uk.org.ponder.rsf.componentprocessor;
005:
006: import uk.org.ponder.conversion.StaticLeafParser;
007: import uk.org.ponder.htmlutil.HTMLUtil;
008: import uk.org.ponder.rsf.components.UIComponent;
009: import uk.org.ponder.rsf.components.UIInitBlock;
010: import uk.org.ponder.rsf.components.UIInternalLink;
011: import uk.org.ponder.rsf.components.UIOutput;
012: import uk.org.ponder.rsf.uitype.UITypes;
013: import uk.org.ponder.rsf.viewstate.AnyViewParameters;
014: import uk.org.ponder.rsf.viewstate.InternalURLRewriter;
015: import uk.org.ponder.rsf.viewstate.RawViewParameters;
016: import uk.org.ponder.rsf.viewstate.ViewParameters;
017: import uk.org.ponder.rsf.viewstate.ViewParamsInterceptor;
018: import uk.org.ponder.rsf.viewstate.ViewStateHandler;
019:
020: public class ViewParamsFixer implements ComponentProcessor {
021: private ViewStateHandler viewstatehandler;
022: private InternalURLRewriter inturlrewriter;
023: private ViewParamsInterceptor environmentalInterceptor;
024:
025: public void setEnvironmentalInterceptor(
026: ViewParamsInterceptor environmentalInterceptor) {
027: this .environmentalInterceptor = environmentalInterceptor;
028: }
029:
030: public void setViewStateHandler(ViewStateHandler viewstatehandler) {
031: this .viewstatehandler = viewstatehandler;
032: }
033:
034: public void setInternalURLRewriter(
035: InternalURLRewriter inturlrewriter) {
036: this .inturlrewriter = inturlrewriter;
037: }
038:
039: public void processComponent(UIComponent toprocesso) {
040: if (toprocesso instanceof UIInternalLink) {
041: UIInternalLink toprocess = (UIInternalLink) toprocesso;
042: // any navigation link is assumed to interrupt flow session, so set
043: // IUPS parameters to null.
044: if (toprocess.target == null) {
045: toprocess.target = new UIOutput();
046: }
047: if (toprocess.viewparams != null) {
048: toprocess.target.setValue(viewstatehandler
049: .getFullURL(toprocess.viewparams));
050: } else {
051: String target = toprocess.target.getValue();
052: if (target == null || UITypes.isPlaceholder(target)) {
053: throw new IllegalArgumentException(
054: "UIInternalLink with fullID "
055: + toprocesso.getFullID()
056: + " discovered with neither ViewParameters nor URL");
057: }
058: toprocess.target.setValue(inturlrewriter
059: .rewriteRenderURL(target));
060: }
061: } else if (toprocesso instanceof UIInitBlock) {
062: UIInitBlock toprocess = (UIInitBlock) toprocesso;
063: String[] rendered = new String[toprocess.arguments.length];
064: for (int i = 0; i < toprocess.arguments.length; ++i) {
065: rendered[i] = convertInitArgument(toprocess.arguments[i]);
066: }
067: toprocess.markup = HTMLUtil.emitJavascriptCall(
068: toprocess.functionname, rendered);
069: }
070: }
071:
072: private String convertInitArgument(Object object) {
073: // TODO: upgrade this to an implementation capable of rendering JSON
074: StaticLeafParser parser = StaticLeafParser.instance();
075: if (parser.isLeafType(object.getClass())) {
076: return parser.render(object);
077: }
078: if (object instanceof UIComponent) {
079: return ((UIComponent) object).getFullID();
080: }
081: if (object instanceof AnyViewParameters) {
082: AnyViewParameters viewparams = (AnyViewParameters) object;
083: if (object instanceof ViewParameters) {
084: AnyViewParameters intercepted = environmentalInterceptor
085: .adjustViewParameters((ViewParameters) viewparams);
086: if (intercepted != null) {
087: viewparams = intercepted;
088: }
089: }
090: if (viewparams instanceof ViewParameters) {
091: return viewstatehandler
092: .getFullURL((ViewParameters) viewparams);
093: }
094: object = viewparams;
095: }
096: if (object instanceof RawViewParameters) {
097: return ((RawViewParameters) object).URL;
098: }
099: throw new IllegalArgumentException(
100: "Cannot render init block argument of unrecognised "
101: + object.getClass());
102: }
103: }
|