View Javadoc

1   /*
2    * $Id: VelocityStrutsUtil.java 474191 2006-11-13 08:30:40Z mrdon $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.util;
22  
23  import java.io.CharArrayWriter;
24  import java.io.IOException;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts2.views.velocity.VelocityManager;
30  import org.apache.velocity.app.VelocityEngine;
31  import org.apache.velocity.context.Context;
32  import org.apache.velocity.exception.MethodInvocationException;
33  import org.apache.velocity.exception.ParseErrorException;
34  import org.apache.velocity.exception.ResourceNotFoundException;
35  
36  import com.opensymphony.xwork2.util.ValueStack;
37  
38  
39  /***
40   * Struts velocity related util.
41   *
42   */
43  public class VelocityStrutsUtil extends StrutsUtil {
44  
45      private Context ctx;
46      private VelocityEngine velocityEngine;
47  
48      public VelocityStrutsUtil(VelocityEngine engine, Context ctx, ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
49          super(stack, request, response);
50          this.ctx = ctx;
51          this.velocityEngine = engine;
52      }
53  
54      public String evaluate(String expression) throws IOException, ResourceNotFoundException, MethodInvocationException, ParseErrorException {
55          CharArrayWriter writer = new CharArrayWriter();
56          velocityEngine.evaluate(ctx, writer, "Error parsing " + expression, expression);
57  
58          return writer.toString();
59      }
60  
61  }