⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 generator.java

📁 velocity官方工具包 包括各种JAR包 示例 文档等
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param path      path to the output file
     * @param encoding  output encoding
     * @return A Writer for this generator.
     * @throws Exception
     */
    public Writer getWriter(String path, String encoding) throws Exception {
        Writer writer;
        if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) {
            writer = new FileWriter(path);
        }
        else
        {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding));
        }
        return writer;
    }

    /**
     * Returns a template, based on encoding and path.
     *
     * @param templateName  name of the template
     * @param encoding      template encoding
     * @return A Template.
     * @throws Exception
     */
    public Template getTemplate(String templateName, String encoding) throws Exception {
        Template template;
        if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) {
            template = ve.getTemplate(templateName);
        }
        else {
            template = ve.getTemplate(templateName, encoding);
        }
        return template;
    }

    /**
     * Parse an input and write the output to an output file.  If the
     * output file parameter is null or an empty string the result is
     * returned as a string object.  Otherwise an empty string is returned.
     *
     * @param inputTemplate input template
     * @param outputFile output file
     * @return The parsed file.
     * @throws Exception
     */
    public String parse (String inputTemplate, String outputFile)
        throws Exception
    {
        return parse(inputTemplate, outputFile, null, null);
    }

    /**
     * Parse an input and write the output to an output file.  If the
     * output file parameter is null or an empty string the result is
     * returned as a string object.  Otherwise an empty string is returned.
     * You can add objects to the context with the objs Hashtable.
     *
     * @param inputTemplate input template
     * @param outputFile output file
     * @param objectID id for object to be placed in the control context
     * @param object object to be placed in the context
     * @return String generated output from velocity
     * @throws Exception
     */
    public String parse (String inputTemplate,
                         String outputFile,
                         String objectID,
                         Object object)
        throws Exception
    {
        return parse(inputTemplate, null, outputFile, null, objectID, object);
    }
    /**
     * Parse an input and write the output to an output file.  If the
     * output file parameter is null or an empty string the result is
     * returned as a string object.  Otherwise an empty string is returned.
     * You can add objects to the context with the objs Hashtable.
     *
     * @param inputTemplate input template
     * @param inputEncoding template encoding
     * @param outputFile output file
     * @param outputEncoding outputEncoding encoding of output file
     * @param objectID id for object to be placed in the control context
     * @param object object to be placed in the context
     * @return String generated output from velocity
     * @throws Exception
     */
    public String parse (String inputTemplate,
                         String inputEncoding,
                         String outputFile,
                         String outputEncoding,
                         String objectID,
                         Object object)
        throws Exception
    {
        if (objectID != null && object != null)
        {
            controlContext.put(objectID, object);
        }

        Template template = getTemplate(inputTemplate, inputEncoding != null ? inputEncoding : this.inputEncoding);

        if (outputFile == null || outputFile.equals(""))
        {
            StringWriter sw = new StringWriter();
            template.merge (controlContext,sw);
            return sw.toString();
        }
        else
        {
            Writer writer = null;

            if (writers.get(outputFile) == null)
            {
                /*
                 * We have never seen this file before so create
                 * a new file writer for it.
                 */
                writer = getWriter(
                            getOutputPath() + File.separator + outputFile,
                            outputEncoding != null ? outputEncoding : this.outputEncoding
                         );

                /*
                 * Place the file writer in our collection
                 * of file writers.
                 */
                writers.put(outputFile, writer);
            }
            else
            {
                writer = (Writer) writers.get(outputFile);
            }

            VelocityContext vc = new VelocityContext( controlContext );
            template.merge (vc,writer);

            // commented because it is closed in shutdown();
            //fw.close();

            return "";
        }
    }

    /**
     * Parse the control template and merge it with the control
     * context. This is the starting point in texen.
     *
     * @param controlTemplate control template
     * @param controlContext control context
     * @return String generated output
     * @throws Exception
     */
    public String parse (String controlTemplate, Context controlContext)
        throws Exception
    {
        this.controlContext = controlContext;
        fillContextDefaults(this.controlContext);
        fillContextProperties(this.controlContext);

        Template template = getTemplate(controlTemplate, inputEncoding);
        StringWriter sw = new StringWriter();
        template.merge (controlContext,sw);

        return sw.toString();
    }


    /**
     * Create a new context and fill it with the elements of the
     * objs Hashtable.  Default objects and objects that comes from
     * the properties of this Generator object is also added.
     *
     * @param objs objects to place in the control context
     * @return Context context filled with objects
     */
    protected Context getContext (Hashtable objs)
    {
        fillContextHash (controlContext,objs);
        return controlContext;
    }

    /**
     * Add all the contents of a Hashtable to the context.
     *
     * @param context context to fill with objects
     * @param objs source of objects
     */
    protected void fillContextHash (Context context, Hashtable objs)
    {
        Enumeration enumeration = objs.keys();
        while (enumeration.hasMoreElements())
        {
            String key = enumeration.nextElement().toString();
            context.put (key, objs.get(key));
        }
    }

    /**
     * Add properties that will aways be in the context by default
     *
     * @param context control context to fill with default values.
     */
    protected void fillContextDefaults (Context context)
    {
        context.put ("generator", instance);
        context.put ("outputDirectory", getOutputPath());
    }

    /**
     * Add objects to the context from the current properties.
     *
     * @param context control context to fill with objects
     *                that are specified in the default.properties
     *                file
     */
    protected void fillContextProperties (Context context)
    {
        Enumeration enumeration = props.propertyNames();

        while (enumeration.hasMoreElements())
        {
            String nm = (String) enumeration.nextElement();
            if (nm.startsWith ("context.objects."))
            {

                String contextObj = props.getProperty (nm);
                int colon = nm.lastIndexOf ('.');
                String contextName = nm.substring (colon+1);

                try
                {
                    Object o = ClassUtils.getNewInstance(contextObj);
                    context.put (contextName,o);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    //TO DO: Log Something Here
                }
            }
        }
    }

    /**
     * Properly shut down the generator, right now
     * this is simply flushing and closing the file
     * writers that we have been holding on to.
     */
    public void shutdown()
    {
        Iterator iterator = writers.values().iterator();

        while(iterator.hasNext())
        {
            Writer writer = (Writer) iterator.next();

            try
            {
                writer.flush();
            }
            catch (IOException e)
            {
                /* do nothing */
            }

            try
            {
                writer.close();
            }
            catch (IOException e)
            {
                /* do nothing */
            }
        }
        // clear the file writers cache
        writers.clear();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -