001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package jsx3.xml;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Wrapper of the native browser XSLT processor.
024: * @author Joe Walker [joe at getahead dot org]
025: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
026: */
027: public class Template extends jsx3.lang.Object {
028: /**
029: * All reverse ajax proxies need context to work from
030: * @param scriptProxy The place we are writing scripts to
031: * @param context The script that got us to where we are now
032: */
033: public Template(Context context, String extension,
034: ScriptProxy scriptProxy) {
035: super (context, extension, scriptProxy);
036: }
037:
038: /**
039: * The instance initializer.
040: * @param objXSL
041: */
042: public Template(jsx3.xml.CdfDocument objXSL) {
043: super ((Context) null, (String) null, (ScriptProxy) null);
044: ScriptBuffer script = new ScriptBuffer();
045: script.appendCall("new Template", objXSL);
046: setInitScript(script);
047: }
048:
049: /**
050: *
051: * @param strName
052: * @param objValue
053: */
054: public void setParam(String strName, jsx3.lang.Object objValue) {
055: ScriptBuffer script = new ScriptBuffer();
056: script.appendCall(getContextPath() + "setParam", strName,
057: objValue);
058: getScriptProxy().addScript(script);
059: }
060:
061: /**
062: *
063: */
064: public void reset() {
065: ScriptBuffer script = new ScriptBuffer();
066: script.appendCall(getContextPath() + "reset");
067: getScriptProxy().addScript(script);
068: }
069:
070: /**
071: *
072: * @param objParams JavaScript object array of name/value pairs. If this parameter is
073: not empty, the transformation will use a paramaterized stylesheet to perform the transformation.
074: */
075: public void setParams(jsx3.lang.Object objParams) {
076: ScriptBuffer script = new ScriptBuffer();
077: script.appendCall(getContextPath() + "setParams", objParams);
078: getScriptProxy().addScript(script);
079: }
080:
081: /**
082: * Performs an XSLT merge. If an error occurs while performing the transform, this method sets the error
083: property of this processor and returns null.
084: * @param objXML
085: * @param bObject
086: * @param callback the result of the transformation
087: */
088: @SuppressWarnings("unchecked")
089: public void transform(jsx3.xml.Node objXML, boolean bObject,
090: org.directwebremoting.proxy.Callback<String> callback) {
091: ScriptBuffer script = new ScriptBuffer();
092: String callbackPrefix = "";
093:
094: if (callback != null) {
095: callbackPrefix = "var reply = ";
096: }
097:
098: script.appendCall(callbackPrefix + getContextPath()
099: + "transform", objXML, bObject);
100:
101: if (callback != null) {
102: String key = org.directwebremoting.extend.CallbackHelper
103: .saveCallback(callback, String.class);
104: script
105: .appendCall("__System.activateCallback", key,
106: "reply");
107: }
108:
109: getScriptProxy().addScript(script);
110: }
111:
112: /**
113: * Performs an XSLT merge. If an error occurs while performing the transform, this method sets the error
114: property of this processor and returns null.
115: * @param objXML
116: * @return if a valid result tree is formed as a result of the transformation
117: */
118: @SuppressWarnings("unchecked")
119: public jsx3.xml.CdfDocument transformToObject(jsx3.xml.Node objXML) {
120: String extension = "transformToObject(\"" + objXML + "\").";
121: try {
122: java.lang.reflect.Constructor<jsx3.xml.CdfDocument> ctor = jsx3.xml.CdfDocument.class
123: .getConstructor(Context.class, String.class,
124: ScriptProxy.class);
125: return ctor.newInstance(this , extension, getScriptProxy());
126: } catch (Exception ex) {
127: throw new IllegalArgumentException("Unsupported type: "
128: + jsx3.xml.CdfDocument.class.getName());
129: }
130: }
131:
132: /**
133: * Performs an XSLT merge. If an error occurs while performing the transform, this method sets the error
134: property of this processor and returns null.
135: * @param objXML
136: * @param returnType The expected return type
137: * @return if a valid result tree is formed as a result of the transformation
138: */
139: @SuppressWarnings("unchecked")
140: public <T> T transformToObject(jsx3.xml.Node objXML,
141: Class<T> returnType) {
142: String extension = "transformToObject(\"" + objXML + "\").";
143: try {
144: java.lang.reflect.Constructor<T> ctor = returnType
145: .getConstructor(Context.class, String.class,
146: ScriptProxy.class);
147: return ctor.newInstance(this , extension, getScriptProxy());
148: } catch (Exception ex) {
149: throw new IllegalArgumentException(
150: "Unsupported return type: " + returnType.getName());
151: }
152: }
153:
154: /**
155: * Returns an error object (a plain JavaScript object) with two properties that the developer can query for:
156:
157: code Ð an integer error code, 0 for no error.
158: description Ð a text description of the error that occurred.
159: */
160: @SuppressWarnings("unchecked")
161: public jsx3.lang.Object getError() {
162: String extension = "getError().";
163: try {
164: java.lang.reflect.Constructor<jsx3.lang.Object> ctor = jsx3.lang.Object.class
165: .getConstructor(Context.class, String.class,
166: ScriptProxy.class);
167: return ctor.newInstance(this , extension, getScriptProxy());
168: } catch (Exception ex) {
169: throw new IllegalArgumentException("Unsupported type: "
170: + jsx3.lang.Object.class.getName());
171: }
172: }
173:
174: /**
175: * Returns an error object (a plain JavaScript object) with two properties that the developer can query for:
176:
177: code Ð an integer error code, 0 for no error.
178: description Ð a text description of the error that occurred.
179: * @param returnType The expected return type
180: */
181: @SuppressWarnings("unchecked")
182: public <T> T getError(Class<T> returnType) {
183: String extension = "getError().";
184: try {
185: java.lang.reflect.Constructor<T> ctor = returnType
186: .getConstructor(Context.class, String.class,
187: ScriptProxy.class);
188: return ctor.newInstance(this , extension, getScriptProxy());
189: } catch (Exception ex) {
190: throw new IllegalArgumentException(
191: "Unsupported return type: " + returnType.getName());
192: }
193: }
194:
195: /**
196: * Returns true if the last operation on this XML entity caused an error.
197: */
198: @SuppressWarnings("unchecked")
199: public void hasError(
200: org.directwebremoting.proxy.Callback<Boolean> callback) {
201: ScriptBuffer script = new ScriptBuffer();
202: String callbackPrefix = "";
203:
204: if (callback != null) {
205: callbackPrefix = "var reply = ";
206: }
207:
208: script.appendCall(callbackPrefix + getContextPath()
209: + "hasError");
210:
211: if (callback != null) {
212: String key = org.directwebremoting.extend.CallbackHelper
213: .saveCallback(callback, Boolean.class);
214: script
215: .appendCall("__System.activateCallback", key,
216: "reply");
217: }
218:
219: getScriptProxy().addScript(script);
220: }
221:
222: }
|