Пример #1
0
        /// <summary>
        /// Get an InputStream so that the Runtime can build a
        /// template with it.
        /// </summary>
        /// <param name="name">name of template to get</param>
        /// <returns>InputStream containing the template
        /// @throws ResourceNotFoundException if template not found
        /// in the file template path.
        /// </returns>
        public override Stream getResourceStream(String templateName)
        {
            lock (this) {
                String template = null;
                int    size     = paths.Count;

                for (int i = 0; i < size; i++)
                {
                    String path = (String)
                                  paths[i];

                    // Make sure we have a valid templateName.
                    if (templateName == null || templateName.Length == 0)
                    {
                        // If we don't get a properly formed templateName
                        // then there's not much we can do. So
                        // we'll forget about trying to search
                        // any more paths for the template.
                        throw new ResourceNotFoundException("Need to specify a file name or file path!");
                    }

                    template = StringUtils.normalizePath(templateName)
                    ;
                    if (template == null || template.Length == 0)
                    {
                        String msg = "File resource error : argument " + template + " contains .. and may be trying to access " + "content outside of template root.  Rejected.";

                        rsvc.error("FileResourceLoader : " + msg)
                        ;

                        throw new ResourceNotFoundException(msg);
                    }

                    // if a / leads off, then just nip that :)
                    if (template.StartsWith("/"))
                    {
                        template = template.Substring(1)
                        ;
                    }

                    Stream inputStream = findTemplate(path, template)
                    ;

                    if (inputStream != null)
                    {
                        // Store the path that this template came
                        // from so that we can check its modification
                        // time.
                        SupportClass.PutElement(templatePaths, templateName, path);
                        return(inputStream);
                    }
                }

                // We have now searched all the paths for
                // templates and we didn't find anything so
                // throw an exception.
                String msg2 = "FileResourceLoader Error: cannot find resource " + template;
                throw new ResourceNotFoundException(msg2);
            }
        }
Пример #2
0
        /// <summary> Handle logging.
        /// *
        /// </summary>
        /// <param name="String">message to log
        ///
        /// </param>
        private void log(int level, System.Object message)
        {
            System.String out_Renamed;

            /*
             *  now,  see if the logging stacktrace is on
             *  and modify the message to suit
             */
            //UPGRADE_NOTE: Exception 'java.lang.Throwable' was converted to ' ' which has different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1100"'
            if (showStackTrace() && (message is System.Exception || message is System.Exception))
            {
                //UPGRADE_NOTE: Exception 'java.lang.Throwable' was converted to ' ' which has different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1100"'
                out_Renamed = StringUtils.stackTrace((System.Exception)message);
            }
            else
            {
                //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"'
                out_Renamed = message.ToString();
            }

            /*
             *  just log it, as we are guaranteed now to have some
             *  kind of logger - save the if()
             */
            logSystem.LogVelocityMessage(level, out_Renamed);
        }
Пример #3
0
            internal virtual void  parseTree(InternalContextAdapter ica)
            {
                try {
                    //UPGRADE_ISSUE: The equivalent of constructor 'java.io.BufferedReader.BufferedReader' is incompatible with the expected type in C#. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1109"'
                    System.IO.TextReader br = new System.IO.StringReader(macrobody);

                    nodeTree = Enclosing_Instance.rsvc.parse(br, "VM:" + macroname, true);
                    nodeTree.init(ica, null);
                } catch (System.Exception e) {
                    Enclosing_Instance.rsvc.error("VelocimacroManager.parseTree() : exception " + macroname + " : " + StringUtils.stackTrace(e));
                }
            }
Пример #4
0
        /// <summary>  does the housekeeping upon creationg.  If a dynamic type
        /// it needs to make an AST for further get()/set() operations
        /// Anything else is constant.
        /// </summary>
        private void  setup()
        {
            switch (type)
            {
            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTINTEGERRANGE:
            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTREFERENCE:
            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTOBJECTARRAY:
            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTSTRINGLITERAL:
            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTTEXT: {
                /*
                 *  dynamic types, just render
                 */

                constant = false;

                try {
                    /*
                     *  fakie : wrap in  directive to get the parser to treat our args as args
                     *   it doesn't matter that #include() can't take all these types, because we
                     *   just want the parser to consider our arg as a Directive/VM arg rather than
                     *   as if inline in schmoo
                     */

                    System.String buff = "#include(" + callerReference + " ) ";

                    //ByteArrayInputStream inStream = new ByteArrayInputStream( buff.getBytes() );

                    //UPGRADE_ISSUE: The equivalent of constructor 'java.io.BufferedReader.BufferedReader' is incompatible with the expected type in C#. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1109"'
                    System.IO.TextReader br = new System.IO.StringReader(buff);

                    nodeTree = rsvc.parse(br, "VMProxyArg:" + callerReference, true);

                    /*
                     *  now, our tree really is the first DirectiveArg(), and only one
                     */

                    nodeTree = (SimpleNode)nodeTree.jjtGetChild(0).jjtGetChild(0);

                    /*
                     * sanity check
                     */

                    if (nodeTree != null && nodeTree.Type != type)
                    {
                        rsvc.error("VMProxyArg.setup() : programmer error : type doesn't match node type.");
                    }

                    /*
                     *  init.  We can do this as they are only references
                     */

                    nodeTree.init(null, rsvc);
                } catch (System.Exception e) {
                    rsvc.error("VMProxyArg.setup() : exception " + callerReference + " : " + StringUtils.stackTrace(e));
                }

                break;
            }



            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTTRUE: {
                constant     = true;
                staticObject = true;
                break;
            }



            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTFALSE: {
                constant     = true;
                staticObject = false;
                break;
            }



            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTNUMBERLITERAL: {
                constant     = true;
                staticObject = System.Int32.Parse(callerReference);
                break;
            }



            case NVelocity.Runtime.Parser.ParserTreeConstants.JJTWORD: {
                /*
                 *  this is technically an error...
                 */

                rsvc.error("Unsupported arg type : " + callerReference + "  You most likely intended to call a VM with a string literal, so enclose with ' or \" characters. (VMProxyArg.setup())");
                constant     = true;
                staticObject = new System.String(callerReference.ToCharArray());

                break;
            }



            default: {
                rsvc.error(" VMProxyArg.setup() : unsupported type : " + callerReference);
            }
            break;
            }
        }
Пример #5
0
        /// <summary>
        /// parses the macro.  We need to do this here, at init time, or else
        /// the local-scope template feature is hard to get to work :)
        /// </summary>
        private void  parseTree(System.String[] callArgs)
        {
            try {
                //UPGRADE_ISSUE: The equivalent of constructor 'java.io.BufferedReader.BufferedReader' is incompatible with the expected type in C#. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1109"'
                System.IO.TextReader br = new System.IO.StringReader(macroBody);

                /*
                 *  now parse the macro - and don't dump the namespace
                 */

                nodeTree = rsvc.parse(br, namespace_Renamed, false);

                /*
                 *  now, to make null references render as proper schmoo
                 *  we need to tweak the tree and change the literal of
                 *  the appropriate references
                 *
                 *  we only do this at init time, so it's the overhead
                 *  is irrelevant
                 */
                Hashtable hm = new Hashtable();

                for (int i = 1; i < argArray.Length; i++)
                {
                    System.String arg = callArgs[i - 1];

                    /*
                     *  if the calling arg is indeed a reference
                     *  then we add to the map.  We ignore other
                     *  stuff
                     */
                    if (arg[0] == '$')
                    {
                        hm[argArray[i]] = arg;
                    }
                }

                /*
                 *  now make one of our reference-munging visitor, and
                 *  let 'er rip
                 */
                VMReferenceMungeVisitor v = new VMReferenceMungeVisitor(hm);
                nodeTree.jjtAccept(v, null);
            } catch (System.Exception e) {
                rsvc.error("VelocimacroManager.parseTree() : exception " + macroName + " : " + StringUtils.stackTrace(e));
            }
        }
Пример #6
0
        /// <summary> Return name of this Velocimacro.
        /// </summary>

        /// <summary> Velocimacros are always LINE
        /// type directives.
        /// </summary>

        /// <summary>   sets the directive name of this VM
        /// </summary>

        /// <summary>  sets the array of arguments specified in the macro definition
        /// </summary>


        /// <summary>  returns the number of ars needed for this VM
        /// </summary>

        /// <summary>   Sets the orignal macro body.  This is simply the cat of the macroArray, but the
        /// Macro object creates this once during parsing, and everyone shares it.
        /// Note : it must not be modified.
        /// </summary>


        /// <summary>   Renders the macro using the context
        /// </summary>
        public override bool render(InternalContextAdapter context, System.IO.TextWriter writer, INode node)
        {
            try {
                /*
                 *  it's possible the tree hasn't been parsed yet, so get
                 *  the VMManager to parse and init it
                 */

                if (nodeTree != null)
                {
                    if (!init_Renamed_Field)
                    {
                        nodeTree.init(context, rsvc);
                        init_Renamed_Field = true;
                    }

                    /*
                     *  wrap the current context and add the VMProxyArg objects
                     */

                    VMContext vmc = new VMContext(context, rsvc);

                    for (int i = 1; i < argArray.Length; i++)
                    {
                        /*
                         *  we can do this as VMProxyArgs don't change state. They change
                         *  the context.
                         */

                        VMProxyArg arg = (VMProxyArg)proxyArgHash[argArray[i]];
                        vmc.AddVMProxyArg(arg);
                    }

                    /*
                     *  now render the VM
                     */

                    nodeTree.render(vmc, writer);
                }
                else
                {
                    rsvc.error("VM error : " + macroName + ". Null AST");
                }
            } 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("VelocimacroProxy.render() : exception VM = #" + macroName + "() : " + StringUtils.stackTrace(e));
            }

            return(true);
        }
Пример #7
0
        /// <summary>
        /// Gets the loader specified in the configuration file.
        /// </summary>
        /// <returns>TemplateLoader</returns>
        public static ResourceLoader getLoader(RuntimeServices rs, System.String loaderClassName)
        {
            ResourceLoader loader = null;

            try {
                // since properties are parsed into arrays with commas, something else needed to be used
                loaderClassName = loaderClassName.Replace(';', ',');
                Type   loaderType = System.Type.GetType(loaderClassName);
                Object o          = System.Activator.CreateInstance(loaderType);
                loader = (ResourceLoader)o;

                rs.info("Resource Loader Instantiated: " + loader.GetType().FullName);

                return(loader);
            } catch (System.Exception e) {
                rs.error("Problem instantiating the template loader.\n" + "Look at your properties file and make sure the\n" + "name of the template loader is correct. Here is the\n" + "error: " + StringUtils.stackTrace(e));
                throw new System.Exception("Problem initializing template loader: " + loaderClassName + "\nError is: " + StringUtils.stackTrace(e));
            }
        }