/// <summary> /// Initializes a new instance of the <see cref="NVelocityViewContextAdapter"/> class. /// </summary> /// <param name="componentName">Name of the component.</param> /// <param name="parentNode">The parent node.</param> /// <param name="viewEngine">The view engine.</param> /// <param name="renderer">The view renderer.</param> public NVelocityViewContextAdapter(String componentName, INode parentNode, IViewEngine viewEngine, IViewRenderer renderer) { this.componentName = componentName; this.parentNode = parentNode; this.viewEngine = viewEngine; this.renderer = renderer; }
/// <summary> /// How this directive is to be rendered /// </summary> public override bool Render(IInternalContextAdapter context, TextWriter writer, INode node) { if (context == null) { throw new MonoRailException("context is null"); } if (contextAdapter == null) { throw new MonoRailException("contextAdapter is null"); } if (contextAdapter.ContextVars == null) { throw new MonoRailException("contextAdapter.ContextVars is null"); } // foreach(DictionaryEntry entry in contextAdapter.ContextVars) // { // context.Put(entry.Key.ToString(), entry.Value); // } for(var i=0; i < savedNode.ChildrenCount; i++) { var childNode = savedNode.GetChild(i); childNode.Render(context, writer); } return true; }
/// <summary> Returns an array of the literal rep of the AST /// </summary> private static IList getASTAsStringArray(Node rootNode) { /* * this assumes that we are passed in the root * node of the code block */ Token t = rootNode.FirstToken; Token tLast = rootNode.LastToken; /* * now, run down the part of the tree bounded by * our first and last tokens */ ArrayList list = new ArrayList(); t = rootNode.FirstToken; while (t != tLast) { list.Add(NodeUtils.tokenLiteral(t)); t = t.next; } /* * make sure we get the last one... */ list.Add(NodeUtils.tokenLiteral(t)); return(list); }
public override bool render(InternalContextAdapter context, TextWriter writer, INode node) { INode bodyNode = null; IDictionary componentParams = CreateParametersAndFigureOutBlockNode(context, node); if (bodyNodeIndex < node.jjtGetNumChildren()) { bodyNode = node.jjtGetChild(bodyNodeIndex); } NVelocityViewContextAdapter contextAdapter = new NVelocityViewContextAdapter( componentName, context, writer, bodyNode, componentParams); IRailsEngineContext railsContext = (IRailsEngineContext)context.Get("context"); component.Init(railsContext, contextAdapter); component.Render(); if (contextAdapter.ViewToRender != null) { return(RenderComponentView(context, writer, contextAdapter)); } return(true); }
/// <summary> /// How this directive is to be rendered /// </summary> public override bool Render(IInternalContextAdapter context, TextWriter writer, INode node) { for(int i=0; i < savedNode.ChildrenCount; i++) { INode childNode = savedNode.GetChild(i); childNode.Render(context, writer); } return true; }
public override void init(RuntimeServices rs, InternalContextAdapter context, Node node) { base.init(rs, context, node); /* * again, don't do squat. We want the AST of the macro * block to hang off of this but we don't want to * init it... it's useless... */ return ; }
private IDictionary CreateParametersAndFigureOutBlockNode(InternalContextAdapter context, INode node) { int childrenCount = node.jjtGetNumChildren(); if (childrenCount > 1) { bodyNodeIndex = childrenCount - 1; INode lastNode = node.jjtGetChild(childrenCount - 1); if (lastNode.Type == ParserTreeConstants.JJTBLOCK) { childrenCount--; } ArrayList list = new ArrayList(); for (int i = 1; i < childrenCount; i++) { if (i == 1) { INode withNode = node.jjtGetChild(1); String withName = withNode.FirstToken.image; if (!"with".Equals(withName)) { String message = String.Format("A #component directive with parameters must use the keyword 'with' - component {0}", componentName); throw new RailsException(message); } continue; } INode paramNode = node.jjtGetChild(i); Object value = paramNode.Value(context); String stValue = (value as String); if (stValue == null) { String message = String.Format("Could not evaluate parameter {0} to a String for component {1}", i, componentName); throw new RailsException(message); } list.Add(stValue); } return((new DictHelper()).CreateDict((String[])list.ToArray(typeof(String)))); } return(new Hashtable(0)); }
/// <summary> /// Used by Parser.java to process VMs withing the parsing process /// /// processAndRegister() doesn't actually render the macro to the output /// Processes the macro body into the internal representation used by the /// VelocimacroProxy objects, and if not currently used, adds it /// to the macro Factory /// </summary> public static void processAndRegister(RuntimeServices rs, Node node, System.String sourceTemplate) { /* * There must be at least one arg to #macro, * the name of the VM. Note that 0 following * args is ok for naming blocks of HTML */ int numArgs = node.jjtGetNumChildren(); /* * this number is the # of args + 1. The + 1 * is for the block tree */ if (numArgs < 2) { /* * error - they didn't name the macro or * define a block */ rs.error("#macro error : Velocimacro must have name as 1st " + "argument to #macro()"); return; } /* * get the arguments to the use of the VM */ System.String[] argArray = getArgArray(node); /* * now, try and eat the code block. Pass the root. */ IList macroArray = getASTAsStringArray(node.jjtGetChild(numArgs - 1)); /* * make a big string out of our macro */ System.Text.StringBuilder temp = new System.Text.StringBuilder(); for (int i = 0; i < macroArray.Count; i++) { temp.Append(macroArray[i]); } System.String macroBody = temp.ToString(); /* * now, try to add it. The Factory controls permissions, * so just give it a whack... */ bool bRet = rs.addVelocimacro(argArray[0], macroBody, argArray, sourceTemplate); return; }
/// <summary> creates an array containing the literal /// strings in the macro arguement /// </summary> private static System.String[] getArgArray(Node node) { /* * remember : this includes the block tree */ int numArgs = node.jjtGetNumChildren(); numArgs--; // avoid the block tree... System.String[] argArray = new System.String[numArgs]; int i = 0; /* * eat the args */ while (i < numArgs) { argArray[i] = node.jjtGetChild(i).FirstToken.image; /* * trim off the leading $ for the args after the macro name. * saves everyone else from having to do it */ if (i > 0) { if (argArray[i].StartsWith("$")) { argArray[i] = argArray[i].Substring(1, (argArray[i].Length) - (1)); } } i++; } if (debugMode) { System.Console.Out.WriteLine("Macro.getArgArray() : #args = " + numArgs); System.Console.Out.Write(argArray[0] + "("); for (i = 1; i < numArgs; i++) { System.Console.Out.Write(" " + argArray[i]); } System.Console.Out.WriteLine(" )"); } return(argArray); }
/// <summary> /// Used by Parser.java to process VMs withing the parsing process /// /// processAndRegister() doesn't actually render the macro to the output /// Processes the macro body into the internal representation used by the /// VelocimacroProxy objects, and if not currently used, adds it /// to the macro Factory /// </summary> public static void processAndRegister(RuntimeServices rs, Node node, System.String sourceTemplate) { /* * There must be at least one arg to #macro, * the name of the VM. Note that 0 following * args is ok for naming blocks of HTML */ int numArgs = node.jjtGetNumChildren(); /* * this number is the # of args + 1. The + 1 * is for the block tree */ if (numArgs < 2) { /* * error - they didn't name the macro or * define a block */ rs.error("#macro error : Velocimacro must have name as 1st " + "argument to #macro()"); return ; } /* * get the arguments to the use of the VM */ System.String[] argArray = getArgArray(node); /* * now, try and eat the code block. Pass the root. */ IList macroArray = getASTAsStringArray(node.jjtGetChild(numArgs - 1)); /* * make a big string out of our macro */ System.Text.StringBuilder temp = new System.Text.StringBuilder(); for (int i = 0; i < macroArray.Count; i++) temp.Append(macroArray[i]); System.String macroBody = temp.ToString(); /* * now, try to add it. The Factory controls permissions, * so just give it a whack... */ bool bRet = rs.addVelocimacro(argArray[0], macroBody, argArray, sourceTemplate); return ; }
public NVelocityViewContextAdapter(String componentName, InternalContextAdapter context, TextWriter writer, INode bodyNode, IDictionary componentParams) { this.context = context; this.componentName = componentName; this.writer = writer; this.bodyNode = bodyNode; this.componentParams = componentParams; // TODO: Implement IEnumerable on NVelocity context // foreach(String key in context.Keys) // { // contextVars[key] = context.Get(key); // } }
public override void init(RuntimeServices rs, InternalContextAdapter context, INode node) { base.init(rs, context, node); INode compNameNode = node.jjtGetChild(0); if (compNameNode == null) { String message = String.Format("You must specify the component name on the #component directive"); throw new RailsException(message); } componentName = compNameNode.FirstToken.image; if (componentName == null) { String message = String.Format("Could not obtain component name from the #component directive"); throw new RailsException(message); } component = viewComponentFactory.Create(componentName); }
/// <summary> How this directive is to be rendered /// </summary> public abstract bool render(InternalContextAdapter context, System.IO.TextWriter writer, INode node);
/// <summary>for log msg purposes /// </summary> /// <summary>for log msg purposes /// </summary> /// <summary> How this directive is to be initialized. /// </summary> public virtual void init(RuntimeServices rs, InternalContextAdapter context, INode node) { rsvc = rs; // int i, k = node.jjtGetNumChildren(); //for (i = 0; i < k; i++) // node.jjtGetChild(i).init(context, rs); }
/// <summary> Return name of this directive. /// </summary> /// <summary> Return type of this directive. /// </summary> /// <summary> render() doesn't do anything in the final output rendering. /// There is no output from a #macro() directive. /// </summary> public override bool render(InternalContextAdapter context, System.IO.TextWriter writer, Node node) { /* * do nothing : We never render. The VelocimacroProxy object does that */ return true; }
/// <summary> Returns an array of the literal rep of the AST /// </summary> private static IList getASTAsStringArray(Node rootNode) { /* * this assumes that we are passed in the root * node of the code block */ Token t = rootNode.FirstToken; Token tLast = rootNode.LastToken; /* * now, run down the part of the tree bounded by * our first and last tokens */ ArrayList list = new ArrayList(); t = rootNode.FirstToken; while (t != tLast) { list.Add(NodeUtils.tokenLiteral(t)); t = t.next; } /* * make sure we get the last one... */ list.Add(NodeUtils.tokenLiteral(t)); return list; }
public override void init(RuntimeServices rs, InternalContextAdapter context, Node node) { base.init(rs, context, node); /* * again, don't do squat. We want the AST of the macro * block to hang off of this but we don't want to * init it... it's useless... */ return; }
/// <summary> /// How this directive is to be rendered /// </summary> public override bool Render(IInternalContextAdapter context, TextWriter writer, INode node) { if (context == null) { throw new MonoRailException("context is null"); } if (contextAdapter == null) { throw new MonoRailException("contextAdapter is null"); } if (contextAdapter.ContextVars == null) { throw new MonoRailException("contextAdapter.ContextVars is null"); } // foreach(DictionaryEntry entry in contextAdapter.ContextVars) // { // context.Put(entry.Key.ToString(), entry.Value); // } for (var i = 0; i < savedNode.ChildrenCount; i++) { var childNode = savedNode.GetChild(i); childNode.Render(context, writer); } return(true); }
/// <summary> Return name of this directive. /// </summary> /// <summary> Return type of this directive. /// </summary> /// <summary> iterates through the argument list and renders every /// argument that is appropriate. Any non appropriate /// arguments are logged, but render() continues. /// </summary> public override bool render(InternalContextAdapter context, System.IO.TextWriter writer, Node node) { /* * did we get an argument? */ if (node.jjtGetChild(0) == null) { rsvc.error("#parse() error : null argument"); return false; } /* * does it have a value? If you have a null reference, then no. */ System.Object value_Renamed = node.jjtGetChild(0).value_Renamed(context); if (value_Renamed == null) { rsvc.error("#parse() error : null argument"); return false; } /* * get the path */ //UPGRADE_TODO: The equivalent in .NET for method 'java.Object.toString' may return a different value. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1043"' System.String arg = value_Renamed.ToString(); /* * see if we have exceeded the configured depth. * If it isn't configured, put a stop at 20 just in case. */ System.Object[] templateStack = context.TemplateNameStack; if (templateStack.Length >= rsvc.getInt(NVelocity.Runtime.RuntimeConstants_Fields.PARSE_DIRECTIVE_MAXDEPTH, 20)) { System.Text.StringBuilder path = new System.Text.StringBuilder(); for (int i = 0; i < templateStack.Length; ++i) { path.Append(" > " + templateStack[i]); } rsvc.error("Max recursion depth reached (" + templateStack.Length + ")" + " File stack:" + path); return false; } Resource current = context.CurrentResource; /* * get the resource, and assume that we use the encoding of the current template * the 'current resource' can be null if we are processing a stream.... */ System.String encoding = null; if (current != null) { encoding = current.Encoding; } else { encoding = (System.String) rsvc.getProperty(NVelocity.Runtime.RuntimeConstants_Fields.INPUT_ENCODING); } /* * now use the Runtime resource loader to get the template */ Template t = null; try { t = rsvc.getTemplate(arg, encoding); } catch (ResourceNotFoundException rnfe) { /* * the arg wasn't found. Note it and throw */ rsvc.error("#parse(): cannot find template '" + arg + "', called from template " + context.CurrentTemplateName + " at (" + Line + ", " + Column + ")"); throw rnfe; } catch (ParseErrorException pee) { /* * the arg was found, but didn't parse - syntax error * note it and throw */ rsvc.error("#parse(): syntax error in #parse()-ed template '" + arg + "', called from template " + context.CurrentTemplateName + " at (" + Line + ", " + Column + ")"); throw pee; } catch (System.Exception e) { rsvc.error("#parse() : arg = " + arg + ". Exception : " + e); return false; } /* * and render it */ try { context.PushCurrentTemplateName(arg); ((SimpleNode) t.Data).render(context, writer); } catch (System.Exception e) { /* * if it's a MIE, it came from the render.... throw it... */ if (e is MethodInvocationException) { throw (MethodInvocationException) e; } rsvc.error("Exception rendering #parse( " + arg + " ) : " + e); return false; } finally { context.PopCurrentTemplateName(); } return true; }
/// <summary> Return name of this directive. /// </summary> /// <summary> Return type of this directive. /// </summary> /// <summary> iterates through the argument list and renders every /// argument that is appropriate. Any non appropriate /// arguments are logged, but render() continues. /// </summary> public override bool render(InternalContextAdapter context, System.IO.TextWriter writer, Node node) { /* * did we get an argument? */ if (node.jjtGetChild(0) == null) { rsvc.error("#parse() error : null argument"); return(false); } /* * does it have a value? If you have a null reference, then no. */ System.Object value_Renamed = node.jjtGetChild(0).value_Renamed(context); if (value_Renamed == null) { rsvc.error("#parse() error : null argument"); return(false); } /* * get the path */ //UPGRADE_TODO: The equivalent in .NET for method 'java.Object.toString' may return a different value. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1043"' System.String arg = value_Renamed.ToString(); /* * see if we have exceeded the configured depth. * If it isn't configured, put a stop at 20 just in case. */ System.Object[] templateStack = context.TemplateNameStack; if (templateStack.Length >= rsvc.getInt(NVelocity.Runtime.RuntimeConstants_Fields.PARSE_DIRECTIVE_MAXDEPTH, 20)) { System.Text.StringBuilder path = new System.Text.StringBuilder(); for (int i = 0; i < templateStack.Length; ++i) { path.Append(" > " + templateStack[i]); } rsvc.error("Max recursion depth reached (" + templateStack.Length + ")" + " File stack:" + path); return(false); } Resource current = context.CurrentResource; /* * get the resource, and assume that we use the encoding of the current template * the 'current resource' can be null if we are processing a stream.... */ System.String encoding = null; if (current != null) { encoding = current.Encoding; } else { encoding = (System.String)rsvc.getProperty(NVelocity.Runtime.RuntimeConstants_Fields.INPUT_ENCODING); } /* * now use the Runtime resource loader to get the template */ Template t = null; try { t = rsvc.getTemplate(arg, encoding); } catch (ResourceNotFoundException rnfe) { /* * the arg wasn't found. Note it and throw */ rsvc.error("#parse(): cannot find template '" + arg + "', called from template " + context.CurrentTemplateName + " at (" + Line + ", " + Column + ")"); throw rnfe; } catch (ParseErrorException pee) { /* * the arg was found, but didn't parse - syntax error * note it and throw */ rsvc.error("#parse(): syntax error in #parse()-ed template '" + arg + "', called from template " + context.CurrentTemplateName + " at (" + Line + ", " + Column + ")"); throw pee; } catch (System.Exception e) { rsvc.error("#parse() : arg = " + arg + ". Exception : " + e); return(false); } /* * and render it */ try { context.PushCurrentTemplateName(arg); ((SimpleNode)t.Data).render(context, writer); } catch (System.Exception e) { /* * if it's a MIE, it came from the render.... throw it... */ if (e is MethodInvocationException) { throw (MethodInvocationException)e; } rsvc.error("Exception rendering #parse( " + arg + " ) : " + e); return(false); } finally { context.PopCurrentTemplateName(); } return(true); }
/// <summary> creates an array containing the literal /// strings in the macro arguement /// </summary> private static System.String[] getArgArray(Node node) { /* * remember : this includes the block tree */ int numArgs = node.jjtGetNumChildren(); numArgs--; // avoid the block tree... System.String[] argArray = new System.String[numArgs]; int i = 0; /* * eat the args */ while (i < numArgs) { argArray[i] = node.jjtGetChild(i).FirstToken.image; /* * trim off the leading $ for the args after the macro name. * saves everyone else from having to do it */ if (i > 0) { if (argArray[i].StartsWith("$")) argArray[i] = argArray[i].Substring(1, (argArray[i].Length) - (1)); } i++; } if (debugMode) { System.Console.Out.WriteLine("Macro.getArgArray() : #args = " + numArgs); System.Console.Out.Write(argArray[0] + "("); for (i = 1; i < numArgs; i++) System.Console.Out.Write(" " + argArray[i]); System.Console.Out.WriteLine(" )"); } return argArray; }
/// <summary> /// How this directive is to be initialized. /// </summary> public override void Init(IRuntimeServices rs, IInternalContextAdapter context, INode node) { base.Init(rs, context, node); savedNode = node; }
/// <summary> Return name of this directive. /// </summary> /// <summary> Return type of this directive. /// </summary> /// <summary> render() doesn't do anything in the final output rendering. /// There is no output from a #macro() directive. /// </summary> public override bool render(InternalContextAdapter context, System.IO.TextWriter writer, Node node) { /* * do nothing : We never render. The VelocimacroProxy object does that */ return(true); }
/** * Break directive does not actually do any rendering. * * This directive throws a BreakException (RuntimeException) which * signals foreach directive to break out of the loop. Note that this * directive does not verify that it is being called inside a foreach * loop. * * @param context * @param writer * @param node * @return true if the directive rendered successfully. * @throws IOException * @throws MethodInvocationException * @throws ResourceNotFoundException * @throws ParseErrorException */ public override bool Render(IInternalContextAdapter context, TextWriter writer, NVelocity.Runtime.Parser.Node.INode node) { throw new BreakException(); }