/src/main/ed/appserver/templates/djang10/Node.java

https://github.com/babble/babble · Java · 268 lines · 202 code · 44 blank · 22 comment · 14 complexity · 482be1317a284c43a29cffe911b89ea4 MD5 · raw file

  1. /**
  2. * Copyright (C) 2008 10gen Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package ed.appserver.templates.djang10;
  17. import ed.js.Encoding;
  18. import ed.js.JSArray;
  19. import ed.js.JSFunction;
  20. import ed.js.JSObject;
  21. import ed.js.JSObjectBase;
  22. import ed.js.JSString;
  23. import ed.js.engine.Scope;
  24. import ed.js.func.JSFunctionCalls0;
  25. import ed.js.func.JSFunctionCalls1;
  26. import ed.js.func.JSFunctionCalls2;
  27. import ed.log.Logger;
  28. public class Node extends JSObjectBase {
  29. public Node() {
  30. super(CONSTRUCTOR);
  31. }
  32. protected Node(Constructor constructor) {
  33. super(constructor);
  34. }
  35. private static JSString render(Scope scope, JSObject thisObj, Context context) {
  36. Printer.RedirectedPrinter printer = new Printer.RedirectedPrinter();
  37. ((JSFunction)thisObj.get("__render")).call(scope, context, printer);
  38. return printer.getJSString();
  39. }
  40. private static void __render(Scope scope, JSObject thisObj, Context context, JSFunction printer) {
  41. JSString result = (JSString) ((JSFunction)thisObj.get("render")).call(scope, context);
  42. printer.call(scope, result);
  43. }
  44. private static NodeList get_nodes_by_type(Scope scope, JSObject thisObj, JSFunction constructor) {
  45. NodeList nodelist = new NodeList();
  46. Object obj = thisObj;
  47. while(obj instanceof JSObject) {
  48. if(obj == constructor.getPrototype()) {
  49. nodelist.add(thisObj);
  50. break;
  51. }
  52. obj = ((JSObject)obj).get("__proto__");
  53. }
  54. //recurse through children
  55. JSArray children = (JSArray) thisObj.get("nodelist");
  56. if (children != null) {
  57. for (Object childObj : children) {
  58. JSFunction child_fn = (JSFunction)((JSObject)childObj).get("get_nodes_by_type");
  59. JSArray childNodes = (JSArray)child_fn.callAndSetThis(scope, childObj, new Object[] { constructor });
  60. nodelist.addAll(childNodes);
  61. }
  62. }
  63. return nodelist;
  64. }
  65. private static String toString_() {
  66. return "<Node>";
  67. }
  68. //Functors
  69. private static class renderFunc extends JSFunctionCalls1 {
  70. public Object call(Scope scope, Object contextObj, Object[] extra) {
  71. JSObject thisObj = (JSObject)scope.getThis();
  72. Context context = (Context) contextObj;
  73. return render(scope, thisObj, context);
  74. }
  75. }
  76. private static class __renderFunc extends JSFunctionCalls2 {
  77. public Object call(Scope scope, Object contextObj, Object printerObj, Object[] extra) {
  78. JSObject thisObj = (JSObject)scope.getThis();
  79. Context context = (Context)contextObj;
  80. JSFunction printer = (JSFunction)printerObj;
  81. __render(scope, thisObj, context, printer);
  82. return null;
  83. }
  84. }
  85. private static class get_nodes_by_typeFunc extends JSFunctionCalls1 {
  86. public Object call(Scope scope, Object constructorObj, Object[] extra) {
  87. JSObject thisObj = (JSObject)scope.getThis();
  88. JSFunction constructor = (JSFunction)constructorObj;
  89. return get_nodes_by_type(scope, thisObj, constructor);
  90. }
  91. }
  92. private static class toString_Func extends JSFunctionCalls0 {
  93. public toString_Func(Scope scope, String name) {
  94. super(scope, name);
  95. }
  96. public Object call(Scope scope, Object[] extra) {
  97. return toString_();
  98. }
  99. }
  100. //Constructor
  101. public static Constructor CONSTRUCTOR = new Constructor();
  102. public static class Constructor extends JSFunctionCalls0 {
  103. public Object call(Scope scope, Object[] extra) {
  104. //noop
  105. return null;
  106. }
  107. public JSObject newOne() {
  108. return new Node();
  109. };
  110. protected void init() {
  111. _prototype.set("render", new renderFunc());
  112. _prototype.set("__render", new __renderFunc());
  113. _prototype.set("get_nodes_by_type", new get_nodes_by_typeFunc());
  114. _prototype.set("toString", new toString_Func(Scope.newGlobal().child(), "toString"));
  115. }
  116. };
  117. //Text Node
  118. public static class TextNode extends Node {
  119. public TextNode(JSString text) {
  120. super(CONSTRUCTOR);
  121. this.set("text", text);
  122. }
  123. public static Node.Constructor CONSTRUCTOR = new Constructor() {
  124. public Object call(Scope scope, Object[] extra) {
  125. JSObject node = (JSObject)scope.getThis();
  126. node.set("text", extra[0]);
  127. return null;
  128. }
  129. protected void init() {
  130. super.init();
  131. _prototype.set("__proto__", Node.CONSTRUCTOR.getPrototype());
  132. _prototype.set("__render", new JSFunctionCalls2() {
  133. public Object call(Scope scope, Object contextObj, Object printerObj, Object[] extra) {
  134. JSObject thisObj = (JSObject)scope.getThis();
  135. ((JSFunction)printerObj).call(scope, thisObj.get("text"));
  136. return null;
  137. }
  138. });
  139. _prototype.set("toString", new JSFunctionCalls0(Scope.newGlobal().child(), "toString") {
  140. public Object call(Scope scope, Object[] extra) {
  141. JSObject thisObj = (JSObject)scope.getThis();
  142. String str = String.valueOf(thisObj.get("text"));
  143. str = str.replaceAll("\\s+", " ");
  144. str = str.substring(0, Math.min(20, str.length()));
  145. return "<Text Node: \"" + str + "\">";
  146. };
  147. });
  148. }
  149. public JSObject newOne() {
  150. return new TextNode(null);
  151. }
  152. };
  153. }
  154. public static class VariableNode extends Node {
  155. private final Logger log;
  156. public VariableNode(FilterExpression expression) {
  157. super(CONSTRUCTOR);
  158. log = Logger.getRoot().getChild("djang10").getChild("VariableNode");
  159. this.set("expression", expression);
  160. }
  161. private static void __render(Scope scope, JSObject thisObj, Context context, JSFunction passedInPrinter) {
  162. FilterExpression expr = (FilterExpression)thisObj.get("expression");
  163. boolean isAutoEscape = context.get("autoescape") != Boolean.FALSE;
  164. Object oldGlobalPrinter = scope.get( "print" );
  165. Printer.RedirectedPrinter printer = new Printer.RedirectedPrinter();
  166. scope.put("print", printer, false);
  167. Object result;
  168. try {
  169. result = expr.resolve(scope, context);
  170. }
  171. finally {
  172. scope.put( "print" , oldGlobalPrinter, false );
  173. }
  174. if(result != null && !"".equals(result))
  175. printer.call(scope, result);
  176. String output = printer.getJSString().toString();
  177. boolean needsEscape = isAutoEscape && (printer.is_safe() != Boolean.TRUE);
  178. needsEscape = needsEscape || (printer.is_safe() == Boolean.FALSE);
  179. if(needsEscape)
  180. output = Encoding._escapeHTML(output);
  181. passedInPrinter.call(scope, new JSString(output));
  182. }
  183. private static String toString(JSObject thisObj) {
  184. FilterExpression expr = (FilterExpression)thisObj.get("expression");
  185. return "<Variable Node: \"" + expr.toString() + "\">";
  186. }
  187. //Functors
  188. private static class __renderFunc extends JSFunctionCalls2 {
  189. public Object call(Scope scope, Object contextObj, Object printerObj, Object[] extra) {
  190. JSObject thisObj = (JSObject)scope.getThis();
  191. Context context = (Context)contextObj;
  192. JSFunction printer = (JSFunction) printerObj;
  193. __render(scope, thisObj, context, printer);
  194. return null;
  195. }
  196. };
  197. private static class toStringFunc extends JSFunctionCalls0 {
  198. public toStringFunc(Scope scope, String name) {
  199. super(scope, name);
  200. }
  201. public Object call(Scope scope, Object[] extra) {
  202. JSObject thisObj = (JSObject)scope.getThis();
  203. return VariableNode.toString(thisObj);
  204. }
  205. };
  206. //Constructor
  207. public static final Node.Constructor CONSTRUCTOR = new Constructor() {
  208. public Object call(Scope scope, Object[] extra) {
  209. JSObject thisObj = (JSObject)scope.getThis();
  210. thisObj.set("expression", extra[0]);
  211. return null;
  212. }
  213. protected void init() {
  214. super.init();
  215. _prototype.set("__proto__", Node.CONSTRUCTOR.getPrototype());
  216. _prototype.set("__render", new __renderFunc());
  217. _prototype.set("toString", new toStringFunc(Scope.newGlobal().child(), "toString"));
  218. }
  219. public JSObject newOne() {
  220. return new VariableNode(null);
  221. }
  222. };
  223. }
  224. }