001: package org.zkoss.jsf.zul.impl;
002:
003: import java.util.Collection;
004:
005: import javax.servlet.jsp.JspException;
006:
007: import org.zkoss.lang.Classes;
008: import org.zkoss.util.CollectionsX;
009: import org.zkoss.zk.ui.Component;
010: import org.zkoss.zk.ui.UiException;
011: import org.zkoss.zk.ui.util.Composer;
012: import org.zkoss.zk.ui.util.ComposerExt;
013:
014: /**
015: *
016: * @author Dennis Chen
017: *
018: */
019: public class ComposerHandler {
020:
021: private Composer composer;
022: private ComposerExt composerExt;
023:
024: /**
025: *
026: * @param apply could be String Collection or a {@link Composer}
027: */
028: /*package*/ComposerHandler(Object apply) {
029: composer = parseAppliedComposer(apply);
030: if (composer != null)
031: composerExt = composer instanceof ComposerExt ? (ComposerExt) composer
032: : null;
033: }
034:
035: private static Composer parseAppliedComposer(Object o) {
036: if (null == o)
037: return null;
038: try {
039: if (o instanceof String) {
040: final String s = (String) o;
041: if (s.indexOf(',') >= 0)
042: o = CollectionsX.parse(null, s, ',');
043: }
044:
045: if (o instanceof Collection) {
046: final Collection c = (Collection) o;
047: int sz = c.size();
048: switch (sz) {
049: case 0:
050: return null;
051: case 1:
052: o = c.iterator().next();
053: break;
054: default:
055: o = c.toArray(new Object[sz]);
056: break;
057: }
058: }
059:
060: if (o instanceof Object[]) {
061: final Object[] cs = (Object[]) o;
062: switch (cs.length) {
063: case 0:
064: return null;
065: case 1:
066: o = cs[0];
067: break;
068: default:
069: return new MultiComposer(cs);
070: }
071: }
072:
073: if (o instanceof String)
074: o = Classes.newInstanceByThread(((String) o).trim());
075: else if (o instanceof Class)
076: o = ((Class) o).newInstance();
077:
078: if (o instanceof Composer)
079: return (Composer) o;
080: } catch (Exception ex) {
081: throw UiException.Aide.wrap(ex);
082: }
083: return null;
084: }
085:
086: /**
087: * Fire contained {@link ComposerExt}'s {@link ComposerExt#doBeforeComposeChildren(Component)} method.
088: * @param comp the component that should be composed.
089: * @throws Exception if {@link ComposerExt#doBeforeComposeChildren(Component)} failed.
090: */
091: public void doBeforeComposeChildren(Component comp)
092: throws Exception {
093: if (composerExt != null)
094: composerExt.doBeforeComposeChildren(comp);
095: }
096:
097: /**
098: * Fire contained {@link Composer}'s {@link Composer#doAfterCompose(Component)} method.
099: * @param comp the component that should be composed.
100: * @throws JspException if {@link Composer#doAfterCompose(Component)} failed.
101: */
102: public void doAfterCompose(Component comp) throws JspException {
103: try {
104: if (composer != null)
105: composer.doAfterCompose(comp);
106: } catch (Exception e) {
107: throw new JspException(e);
108: }
109: }
110:
111: /**
112: *
113: * @param e the error that composer should handled
114: * @return true if {@link ComposerExt#doCatch(Throwable)} return's true, false otherwise.
115: */
116: public boolean doCatch(Throwable e) {
117: boolean flag = false;
118: try {
119: if (composerExt != null)
120: flag = composerExt.doCatch(e);
121: } catch (Exception e1) {
122: StackTraceElement[] oriArr = e.getStackTrace();
123: StackTraceElement[] erArr = e1.getStackTrace();
124: StackTraceElement[] newErrArr = new StackTraceElement[oriArr.length
125: + erArr.length];
126: System.arraycopy(newErrArr, 0, oriArr, 0, oriArr.length);
127: System.arraycopy(newErrArr, oriArr.length, erArr, 0,
128: erArr.length);
129: e.setStackTrace(newErrArr);
130: }
131: return flag;
132: }
133:
134: /**
135: *
136: * @throws JspException if {@link ComposerExt#doFinally()} fail.
137: */
138: public void doFinally() throws Exception {
139: if (composerExt != null) {
140: try {
141: composerExt.doFinally();
142: } catch (Exception e) {
143: throw new JspException(e);
144: }
145: }
146: }
147:
148: }//end of class...
|