KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > htmlconverter > CmsHtmlConverterTools


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/htmlconverter/CmsHtmlConverterTools.java,v $
3 * Date : $Date: 2005/05/17 13:47:32 $
4 * Version: $Revision: 1.1 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
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 OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */

28
29 package com.opencms.htmlconverter;
30
31 import org.opencms.staticexport.CmsLinkManager;
32
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.HashSet JavaDoc;
37
38 import org.w3c.dom.NamedNodeMap JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40
41 /**
42  * Various methods used by CmsHtmlConverter to
43  * check, remove or replace tags and Strings.<p>
44  *
45  * @author Andreas Zahner
46  * @version 1.0
47  *
48  * @deprecated Will not be supported past the OpenCms 6 release.
49  */

50 final class CmsHtmlConverterTools {
51
52     /**
53      * Tests if a set of tags contains the specified tag.&nbsp;This method is used to remove tags, blocks and to check inlinetags.<p>
54      *
55      * @param NodeName String with tagname
56      * @param tags set of tags
57      * @return true if NodeName is found in tags, otherwise false.
58      */

59     protected boolean checkTag(String JavaDoc NodeName, HashSet JavaDoc tags) {
60         // cw09.09.2003 added general qualifier *
61
if (tags.contains(new String JavaDoc("*"))) {
62             return true;
63         }
64         if (tags.contains(new String JavaDoc(NodeName))) {
65             return true;
66         }
67         return false;
68     }
69
70     /**
71      * Scans a String for substrings specified in a list of Strings and replaces them, returns a String where all replacements are done;
72      * this method is needed for replacement run #1.<p>
73      *
74      * @param testString is the String which has to be scanned
75      * @param rStrings lists all subStrings which have to be replaced
76      * @return String with replaced content
77      */

78     protected String JavaDoc scanContent(String JavaDoc testString, ArrayList JavaDoc rStrings) {
79         CmsHtmlConverterObjectReplaceContent testObject = new CmsHtmlConverterObjectReplaceContent();
80         String JavaDoc searchString, replaceItem;
81         for (int i=0; i<rStrings.size(); i++) {
82             testObject=(CmsHtmlConverterObjectReplaceContent)(rStrings.get(i));
83             searchString=testObject.getSearchString();
84             replaceItem=testObject.getReplaceItem();
85             testString=replaceString(testString, searchString, replaceItem);
86         }
87         return testString;
88     }
89
90     /**
91      * Scans a String for substrings specified in a list of Strings and replaces them, returns a String where all replacements are done.<p>
92      *
93      * @param testString is the String which has to be scanned
94      * @param rStrings lists all subStrings which have to be replaced
95      * @return String with replaced content
96      */

97     protected String JavaDoc scanString(String JavaDoc testString, ArrayList JavaDoc rStrings) {
98         CmsHtmlConverterObjectReplaceStrings testObject = new CmsHtmlConverterObjectReplaceStrings();
99         String JavaDoc searchString, replaceItem;
100         for (int i=0; i<rStrings.size(); i++) {
101             testObject=(CmsHtmlConverterObjectReplaceStrings)(rStrings.get(i));
102             searchString=testObject.getSearchString();
103             replaceItem=testObject.getReplaceItem();
104             testString=replaceString(testString, searchString, replaceItem);
105         }
106         return testString;
107     }
108
109     /**
110      * Scans a String and replaces umlauts and other special characters.<p>
111      *
112      * @param testString is the String which has to be scanned
113      * @param rStrings lists all subStrings wich have to be replaced
114      * @return String with replaced special characters
115      */

116     protected String JavaDoc scanChar(String JavaDoc testString, ArrayList JavaDoc rStrings) {
117         CmsHtmlConverterObjectReplaceExtendedChars testObject = new CmsHtmlConverterObjectReplaceExtendedChars();
118         String JavaDoc searchString, replaceItem;
119         for (int i=0; i<rStrings.size(); i++) {
120             testObject=(CmsHtmlConverterObjectReplaceExtendedChars)(rStrings.get(i));
121             searchString=testObject.getSearchString();
122             replaceItem=testObject.getReplaceItem();
123             testString=replaceString(testString, searchString, replaceItem);
124         }
125         return testString;
126     }
127
128     /**
129      * Method to replace a subString with replaceItem.<p>
130      *
131      * @param testString the original String
132      * @param searchString the subString that has to be replaced
133      * @param replaceItem the String that replaces searchString
134      * @return String with replaced subStrings
135      */

136     protected String JavaDoc replaceString(String JavaDoc testString, String JavaDoc searchString, String JavaDoc replaceItem) {
137         /* if searchString isn't in testString, return (better performance) */
138         if (testString.indexOf(searchString) == -1) {
139             return testString;
140         }
141         int tempIndex = 0;
142         int searchLen = searchString.length();
143         int searchIndex = testString.indexOf(searchString);
144         StringBuffer JavaDoc returnString = new StringBuffer JavaDoc(testString.length());
145         while (searchIndex != -1) {
146             returnString.append(testString.substring(0, searchIndex));
147             returnString.append(replaceItem);
148             tempIndex = searchIndex+searchLen;
149             testString = testString.substring(tempIndex);
150             searchIndex = testString.indexOf(searchString);
151         }
152         returnString.append(testString);
153         return returnString.toString();
154     }
155
156     /**
157      * Method to scan attributes of a node and return the value.<p>
158      *
159      * @param node the node which is tested
160      * @param attrName String with attribute name
161      * @return String with attribute value
162      */

163     protected String JavaDoc scanNodeAttrs(Node JavaDoc node, String JavaDoc attrName) {
164         NamedNodeMap JavaDoc attrs = node.getAttributes();
165         for (int i = attrs.getLength()-1; i >= 0; i--) {
166             if (attrName.equalsIgnoreCase(attrs.item(i).getNodeName())) {
167                 return attrs.item(i).getNodeValue();
168             }
169         }
170         return "";
171     }
172
173     /**
174      * TODO: add javadoc.
175      *
176      * @param orgUrl the original url
177      * @param parameter the parameter
178      * @param prefix the prefix
179      * @param relativeRoot the relative root
180      * @return the modified parameter
181      */

182     protected String JavaDoc modifyParameter(URL JavaDoc orgUrl, String JavaDoc parameter, String JavaDoc prefix, String JavaDoc relativeRoot) {
183         try {
184             URL JavaDoc myURL = new URL JavaDoc(parameter);
185             parameter = myURL.getFile();
186             String JavaDoc reference = myURL.getRef();
187             if (reference != null) {
188                 parameter += "#"+reference;
189             }
190         } catch (MalformedURLException JavaDoc e) {
191             if (!parameter.startsWith("/")) {
192                 //this is a relative link
193
try {
194                     URL JavaDoc newUrl = new URL JavaDoc(orgUrl, parameter);
195                     parameter = newUrl.getFile();
196                     String JavaDoc reference = newUrl.getRef();
197                     if (reference != null) {
198                         parameter += "#"+reference;
199                     }
200                 } catch (MalformedURLException JavaDoc exc) {
201                     // ignore
202
}
203             }
204         }
205         // remove the servletprefix
206
if (prefix != null && !"".equals(prefix)) {
207             if (parameter.startsWith(prefix)) {
208                 parameter = parameter.substring(prefix.length());
209             }
210         }
211         // check if we need a relative path for this uri
212
if ((relativeRoot != null) && parameter.startsWith(relativeRoot)) {
213             // uri is located in the relative root folder
214
String JavaDoc source = orgUrl.getFile();
215             if (source.startsWith(prefix)) {
216                 source = source.substring(prefix.length());
217             }
218             parameter = CmsLinkManager.getRelativeUri(source, parameter);
219         }
220         return parameter;
221     }
222
223     /**
224      * TODO: add javadoc.
225      *
226      * @param orgUrl the original url
227      * @param valueParam the value parameter
228      * @param servletUri the servlet uri
229      * @return flag to indicate if the url should be replaced
230      */

231     protected boolean shouldReplaceUrl(URL JavaDoc orgUrl, String JavaDoc valueParam, String JavaDoc servletUri) {
232
233         // HACK: if this link has already a link tag in it don't replace it
234
// this is only for a special project and should be removed sometime...
235
if (valueParam != null) {
236             if (valueParam.indexOf("<link>") != -1 || valueParam.indexOf("<LINK>") != -1) {
237                 return false;
238             }
239             if (valueParam.startsWith("#")) {
240                 // its an anchor in the same page
241
return false;
242             }
243             if (valueParam.toLowerCase().startsWith("javascript:")) {
244                 // it is a javascript (or Javasript or JavaScript or ...)
245
return false;
246             }
247         }
248         if (orgUrl == null) {
249             return false;
250         }
251
252         if (valueParam == null) {
253             return false;
254         }
255
256         URL JavaDoc paramUrl = null;
257         String JavaDoc protocol = null;
258
259         // replace protocol by "http://"
260
String JavaDoc spec = valueParam.trim();
261
262         // get protocol
263
int index = spec.indexOf(":");
264         if (index >= 0) {
265             String JavaDoc tmpProtocol = spec.substring(0, index).trim().toLowerCase();
266             if ((tmpProtocol.length() > 0) && Character.isLetter(tmpProtocol.charAt(0))) {
267                 char c;
268                 boolean isValid = true;
269                 for (int i = 1; i < tmpProtocol.length(); i++) {
270                     c = tmpProtocol.charAt(i);
271                     if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' && c != '-') {
272                         isValid = false;
273                         break;
274                     }
275                 }
276                 if (isValid) {
277                     spec = "http" + spec.substring(index);
278                     protocol = tmpProtocol;
279                 }
280             }
281         }
282
283         try {
284             // parse URL
285
paramUrl = new URL JavaDoc(spec);
286             if (protocol == null) {
287                 protocol = paramUrl.getProtocol();
288             }
289         } catch (MalformedURLException JavaDoc e) {
290             return true;
291         }
292
293         if (orgUrl.getProtocol().equalsIgnoreCase(protocol)
294             && orgUrl.getHost().equalsIgnoreCase(paramUrl.getHost())
295             && paramUrl.getFile().startsWith(servletUri)
296             ) {
297             return (!(paramUrl.getFile() == null || "".equals(paramUrl.getFile())));
298         }
299         return false;
300     }
301         
302     /**
303      * TODO: add javadoc comment.
304      *
305      * @param replace tbd
306      * @param node tbd
307      * @param param tbd
308      * @param quotationMark tbd
309      * @return tbd
310      */

311     protected String JavaDoc reconstructTag(String JavaDoc replace, Node JavaDoc node, String JavaDoc param, String JavaDoc quotationMark) {
312         StringBuffer JavaDoc tempString = new StringBuffer JavaDoc("");
313         tempString.append("<");
314         tempString.append(node.getNodeName());
315         NamedNodeMap JavaDoc attrs = node.getAttributes();
316         for (int i = attrs.getLength()-1; i >= 0; i--) {
317             tempString.append(" ");
318             if (attrs.item(i).getNodeName().equals(param)) {
319                 tempString.append(param);
320                 tempString.append("=");
321                 tempString.append(quotationMark);
322                 tempString.append(replace);
323             } else {
324                 tempString.append(attrs.item(i).getNodeName());
325                 tempString.append("=");
326                 tempString.append(quotationMark);
327                 tempString.append(attrs.item(i).getNodeValue());
328             }
329             tempString.append(quotationMark);
330         }
331         tempString.append(">");
332         return tempString.toString();
333     }
334 }
Popular Tags