001: /* Include.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Sep 28 18:01:03 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import java.util.HashMap;
022: import java.io.Writer;
023: import java.io.IOException;
024:
025: import org.zkoss.lang.Objects;
026: import org.zkoss.lang.Exceptions;
027: import org.zkoss.mesg.Messages;
028: import org.zkoss.util.logging.Log;
029:
030: import org.zkoss.web.Attributes;
031:
032: import org.zkoss.zk.mesg.MZk;
033: import org.zkoss.zk.ui.Desktop;
034: import org.zkoss.zk.ui.Execution;
035: import org.zkoss.zk.ui.UiException;
036: import org.zkoss.zk.ui.WrongValueException;
037: import org.zkoss.zk.ui.sys.UiEngine;
038: import org.zkoss.zk.ui.sys.WebAppCtrl;
039:
040: import org.zkoss.zul.impl.XulElement;
041:
042: /**
043: * Includes the result generated by any servlet.
044: *
045: * <p>Non-XUL extension.
046: *
047: * <p>If the servlet is eventually another ZUL page, the page will be
048: * added to the current desktop when this element is added to
049: * the current desktop.
050: *
051: * @author tomyeh
052: */
053: public class Include extends XulElement {
054: private static final Log log = Log.lookup(Include.class);
055:
056: protected String _src;
057: private boolean _localized;
058:
059: public Include() {
060: }
061:
062: public Include(String src) {
063: setSrc(src);
064: }
065:
066: /** Returns the src.
067: * <p>Default: null.
068: */
069: public String getSrc() {
070: return _src;
071: }
072:
073: /** Sets the src.
074: * <p>If src is changed, the whole component is invalidate.
075: * Thus, you want to smart-update, you have to override this method.
076: *
077: * @param src the source URL. If null or empty, nothing is included.
078: */
079: public void setSrc(String src) throws WrongValueException {
080: if (src != null && src.length() == 0)
081: src = null;
082:
083: if (!Objects.equals(_src, src)) {
084: _src = src;
085: invalidate();
086: }
087: }
088:
089: /** Returns whether the source depends on the current Locale.
090: * If true, it will search xxx_en_US.yyy, xxx_en.yyy and xxx.yyy
091: * for the proper content, where src is assumed to be xxx.yyy.
092: *
093: * <p>Default: false;
094: */
095: public final boolean isLocalized() {
096: return _localized;
097: }
098:
099: /** Sets whether the source depends on the current Locale.
100: */
101: public final void setLocalized(boolean localized) {
102: if (_localized != localized) {
103: _localized = localized;
104: invalidate();
105: }
106: }
107:
108: //-- Component --//
109: /** Default: not childable.
110: */
111: public boolean isChildable() {
112: return false;
113: }
114:
115: public void redraw(Writer out) throws IOException {
116: final UiEngine ueng = ((WebAppCtrl) getDesktop().getWebApp())
117: .getUiEngine();
118: ueng.pushOwner(this );
119: try {
120: out.write("<div id=\"");
121: out.write(getUuid());
122: out.write('"');
123: out.write(getOuterAttrs());
124: out.write(getInnerAttrs());
125: out.write(">\n");
126:
127: if (_src != null && _src.length() > 0)
128: include(out);
129:
130: out.write("\n</div>");
131: } finally {
132: ueng.popOwner();
133: }
134: }
135:
136: private void include(Writer out) throws IOException {
137: final Desktop desktop = getDesktop();
138: final Execution exec = desktop.getExecution();
139: final String src = exec.toAbsoluteURI(_src, false);
140: try {
141: exec.include(out, src, null, 0);
142: } catch (Throwable err) {
143: //though DHtmlLayoutServlet handles exception, we still have to
144: //handle it because src might not be ZUML
145: final String errpg = desktop.getWebApp().getConfiguration()
146: .getErrorPage(desktop.getDeviceType(), err);
147: if (errpg != null) {
148: try {
149: exec.setAttribute("javax.servlet.error.message",
150: Exceptions.getMessage(err));
151: exec.setAttribute("javax.servlet.error.exception",
152: err);
153: exec.setAttribute(
154: "javax.servlet.error.exception_type", err
155: .getClass());
156: exec.setAttribute(
157: "javax.servlet.error.status_code",
158: new Integer(500));
159: exec.include(out, errpg, null, 0);
160: return; //done
161: } catch (IOException ex) { //eat it (connection off)
162: } catch (Throwable ex) {
163: log.warning("Failed to load the error page: "
164: + errpg, ex);
165: }
166: }
167:
168: final String msg = Messages.get(MZk.PAGE_FAILED,
169: new Object[] {
170: src,
171: Exceptions.getMessage(err),
172: Exceptions.formatStackTrace(null, err,
173: null, 6) });
174: final HashMap attrs = new HashMap();
175: attrs.put(Attributes.ALERT_TYPE, "error");
176: attrs.put(Attributes.ALERT, msg);
177: exec.include(out, "~./html/alert.dsp", attrs,
178: Execution.PASS_THRU_ATTR);
179: }
180: }
181: }
|