View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.io.StringReader;
22  
23  import org.springframework.extensions.surf.util.URLDecoder;
24  import org.springframework.extensions.surf.util.URLEncoder;
25  import org.springframework.extensions.webscripts.ui.common.StringUtils;
26  import org.xml.sax.InputSource;
27  
28  import freemarker.ext.dom.NodeModel;
29  
30  /**
31   * Collection of script utility methods for working with strings etc.
32   * This class is immutable.
33   * 
34   * @author Kevin Roast
35   */
36  public class ScriptableUtils
37  {
38      public String stripEncodeUnsafeHTML(String s)
39      {
40          return StringUtils.stripUnsafeHTMLTags(s, true);
41      }
42      
43      public String stripUnsafeHTML(String s)
44      {
45          return StringUtils.stripUnsafeHTMLTags(s, false);
46      }
47      
48      public String replaceLineBreaks(String s)
49      {
50          return StringUtils.replaceLineBreaks(s, true);
51      }
52      
53      public String encodeHTML(String s)
54      {
55          return StringUtils.encode(s);
56      }
57      
58      public String encodeJavaScript(String s)
59      {
60          return StringUtils.encodeJavascript(s);
61      }
62      
63      public String urlEncode(String s)
64      {
65          return URLEncoder.encode(s);
66      }
67  
68      public String urlEncodeComponent(String s)
69      {
70          return URLEncoder.encodeUri(s);
71      }
72  
73      public String urlDecode(String s)
74      {
75          return URLDecoder.decode(s);
76      }
77      
78      /**
79       * Converts an xml string to a freemarker node model
80       * 
81       * @param xml
82       * 
83       * @return freemarker node model
84       */
85      public NodeModel parseXMLNodeModel(String xml)
86      {
87          try
88          {
89              return NodeModel.parse(new InputSource(new StringReader(xml)));
90          }
91          catch (Throwable err)
92          {
93              err.printStackTrace();
94              return null;
95          }
96      }
97      
98  }