Source code: com/opencms/flex/util/CmsStringSubstitution.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/util/Attic/CmsStringSubstitution.java,v $
3 * Date : $Date: 2003/04/09 14:04:09 $
4 * Version: $Revision: 1.6 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2002 - 2003 Alkacon Software (http://www.alkacon.com)
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about Alkacon Software, please see the
22 * company website: http://www.alkacon.com
23 *
24 * For further information about OpenCms, please see the
25 * project website: http://www.opencms.org
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32 package com.opencms.flex.util;
33
34 import com.opencms.workplace.I_CmsWpConstants;
35
36 import org.apache.oro.text.perl.MalformedPerl5PatternException;
37 import org.apache.oro.text.perl.Perl5Util;
38
39 /**
40 * Provides a String substitution functionality
41 * with Perl regular expressions.<p>
42 *
43 * @author Andreas Zahner (a.zahner@alkacon.com)
44 * @version $Revision: 1.6 $
45 * @since 5.0
46 */
47 public class CmsStringSubstitution {
48
49 /** DEBUG flag */
50 private static final int DEBUG = 0;
51
52 // static members, will only be initilized once, for performance reasons
53 private static String contextSearch = null;
54 private static String contextReplace = null;
55
56 /**
57 * Default constructor (empty).<p>
58 */
59 public CmsStringSubstitution() {}
60
61 /**
62 * Substitutes searchString in content with replaceItem.<p>
63 *
64 * @param content the content which is scanned
65 * @param searchString the String which is searched in content
66 * @param replaceItem the new String which replaces searchString
67 * @return String the substituted String
68 */
69 public static String substitute(String content, String searchString, String replaceItem) {
70 return substitutePerl(content, escapePattern(searchString), escapePattern(replaceItem), "g");
71 }
72
73 /**
74 * Substitutes first occurance of searchString in content with replaceItem.<p>
75 *
76 * @param content the content which is scanned
77 * @param searchString the String which is searched in content
78 * @param replaceItem the new String which replaces searchString
79 * @return String the substituted String
80 */
81 public static String substituteFirst(String content, String searchString, String replaceItem) {
82 return substitutePerl(content, escapePattern(searchString), escapePattern(replaceItem), "");
83 }
84
85 /**
86 * Substitutes searchString in content with replaceItem.<p>
87 *
88 * @param content the content which is scanned
89 * @param searchString the String which is searched in content
90 * @param replaceItem the new String which replaces searchString
91 * @param occurences must be a "g" if all occurences of searchString shall be replaced
92 * @return String the substituted String
93 */
94 public static String substitutePerl(String content, String searchString, String replaceItem, String occurences) {
95 String translationRule = "s#"+searchString+"#"+replaceItem+"#"+occurences;
96 Perl5Util perlUtil = new Perl5Util();
97 try {
98 return perlUtil.substitute(translationRule, content);
99 }
100 catch(MalformedPerl5PatternException e){
101 if (DEBUG>0) System.err.println("[CmsStringSubstitution]: "+e.toString());
102 }
103 return content;
104 }
105
106 /**
107 * Substitutes the OpenCms context path (e.g. /opencms/opencms/) in a HTML page with a
108 * special variable so that the content also runs if the context path of the server changes.<p>
109 *
110 * @param htmlContent the HTML to replace the context path in
111 * @return the HTML with the replaced context path
112 */
113 public static String substituteContextPath(String htmlContent, String context) {
114 if (contextSearch == null) {
115 contextSearch = "([^\\w/])" + context;
116 contextReplace = "$1" + CmsStringSubstitution.escapePattern(I_CmsWpConstants.C_MACRO_OPENCMS_CONTEXT) + "/";
117 }
118 return CmsStringSubstitution.substitutePerl(htmlContent, contextSearch, contextReplace, "g");
119 }
120
121 /**
122 * Escapes a String so it may be used as a Perl5 regular expression.<p>
123 *
124 * This method replaces the following characters in a String:<br>
125 * <code>{}[]()\$^.*+/</code>
126 *
127 *
128 * @param source the string to escape
129 * @return the escaped string
130 */
131 public static String escapePattern(String source) {
132 if (DEBUG>0) System.err.println("[CmsStringSubstitution]: escaping String: "+source);
133 if (source == null) return null;
134 StringBuffer result = new StringBuffer(source.length()*2);
135 for(int i = 0;i < source.length(); ++i) {
136 char ch = source.charAt(i);
137 switch (ch) {
138 case '\\' :
139 result.append("\\\\");
140 break;
141 case '/' :
142 result.append("\\/");
143 break;
144 case '$' :
145 result.append("\\$");
146 break;
147 case '^' :
148 result.append("\\^");
149 break;
150 case '.' :
151 result.append("\\.");
152 break;
153 case '*' :
154 result.append("\\*");
155 break;
156 case '+' :
157 result.append("\\+");
158 break;
159 case '|' :
160 result.append("\\|");
161 break;
162 case '?' :
163 result.append("\\?");
164 break;
165 case '{' :
166 result.append("\\{");
167 break;
168 case '}' :
169 result.append("\\}");
170 break;
171 case '[' :
172 result.append("\\[");
173 break;
174 case ']' :
175 result.append("\\]");
176 break;
177 case '(' :
178 result.append("\\(");
179 break;
180 case ')' :
181 result.append("\\)");
182 break;
183 default :
184 result.append(ch);
185 }
186 }
187 if (DEBUG>0) System.err.println("[CmsStringSubstitution]: escaped String to: "+result.toString());
188 return new String(result);
189 }
190 }