• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.velocity.util;
2 
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 
22 import org.apache.velocity.runtime.directive.Directive;
23 import org.apache.velocity.runtime.parser.node.Node;
24 import org.apache.velocity.util.introspection.Info;
25 
26 /**
27  * This class provides some methods for dynamically
28  * invoking methods in objects, and some string
29  * manipulation and formatting methods.
30  *
31  *  @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
32  *  @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
33  *  @version $Id$
34  */
35 public class StringUtils
36 {
37     /**
38      * Creates a string that formats the template filename with line number
39      * and column of the given Directive. We use this routine to provide a consistent format for displaying
40      * file errors.
41      * @param directive currrent directive
42      * @return formatted string
43      */
formatFileString(Directive directive)44     public static String formatFileString(Directive directive)
45     {
46         return formatFileString(directive.getTemplateName(), directive.getLine(), directive.getColumn());
47     }
48 
49     /**
50      * Creates a string that formats the template filename with line number
51      * and column of the given Node. We use this routine to provide a consistent format for displaying
52      * file errors.
53      * @param node current node
54      * @return formatted string
55      */
formatFileString(Node node)56     public static String formatFileString(Node node)
57     {
58         return formatFileString(node.getTemplateName(), node.getLine(), node.getColumn());
59     }
60 
61     /**
62      * Simply creates a string that formats the template filename with line number
63      * and column. We use this routine to provide a consistent format for displaying
64      * file errors.
65      * @param info template name, line and column infos
66      * @return formatted string
67      */
formatFileString(Info info)68     public static String formatFileString(Info info)
69     {
70           return formatFileString(info.getTemplateName(), info.getLine(), info.getColumn());
71     }
72 
73     /**
74      * Simply creates a string that formats the template filename with line number
75      * and column. We use this routine to provide a consistent format for displaying
76      * file errors.
77      * @param template File name of template, can be null
78      * @param linenum Line number within the file
79      * @param colnum Column number withing the file at linenum
80      * @return formatted string
81      */
formatFileString(String template, int linenum, int colnum)82     public static String formatFileString(String template, int linenum, int colnum)
83     {
84         StringBuilder buffer = new StringBuilder();
85         if (org.apache.commons.lang3.StringUtils.isEmpty(template))
86         {
87             template = "<unknown template>";
88         }
89         buffer.append(template).append("[line ").append(linenum).append(", column ").append(colnum).append(']');
90         return buffer.toString();
91     }
92 }
93