1 28 29 package com.opencms.htmlconverter; 30 31 import org.opencms.staticexport.CmsLinkManager; 32 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.ArrayList ; 36 import java.util.HashSet ; 37 38 import org.w3c.dom.NamedNodeMap ; 39 import org.w3c.dom.Node ; 40 41 50 final class CmsHtmlConverterTools { 51 52 59 protected boolean checkTag(String NodeName, HashSet tags) { 60 if (tags.contains(new String ("*"))) { 62 return true; 63 } 64 if (tags.contains(new String (NodeName))) { 65 return true; 66 } 67 return false; 68 } 69 70 78 protected String scanContent(String testString, ArrayList rStrings) { 79 CmsHtmlConverterObjectReplaceContent testObject = new CmsHtmlConverterObjectReplaceContent(); 80 String 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 97 protected String scanString(String testString, ArrayList rStrings) { 98 CmsHtmlConverterObjectReplaceStrings testObject = new CmsHtmlConverterObjectReplaceStrings(); 99 String 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 116 protected String scanChar(String testString, ArrayList rStrings) { 117 CmsHtmlConverterObjectReplaceExtendedChars testObject = new CmsHtmlConverterObjectReplaceExtendedChars(); 118 String 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 136 protected String replaceString(String testString, String searchString, String replaceItem) { 137 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 returnString = new StringBuffer (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 163 protected String scanNodeAttrs(Node node, String attrName) { 164 NamedNodeMap 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 182 protected String modifyParameter(URL orgUrl, String parameter, String prefix, String relativeRoot) { 183 try { 184 URL myURL = new URL (parameter); 185 parameter = myURL.getFile(); 186 String reference = myURL.getRef(); 187 if (reference != null) { 188 parameter += "#"+reference; 189 } 190 } catch (MalformedURLException e) { 191 if (!parameter.startsWith("/")) { 192 try { 194 URL newUrl = new URL (orgUrl, parameter); 195 parameter = newUrl.getFile(); 196 String reference = newUrl.getRef(); 197 if (reference != null) { 198 parameter += "#"+reference; 199 } 200 } catch (MalformedURLException exc) { 201 } 203 } 204 } 205 if (prefix != null && !"".equals(prefix)) { 207 if (parameter.startsWith(prefix)) { 208 parameter = parameter.substring(prefix.length()); 209 } 210 } 211 if ((relativeRoot != null) && parameter.startsWith(relativeRoot)) { 213 String 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 231 protected boolean shouldReplaceUrl(URL orgUrl, String valueParam, String servletUri) { 232 233 if (valueParam != null) { 236 if (valueParam.indexOf("<link>") != -1 || valueParam.indexOf("<LINK>") != -1) { 237 return false; 238 } 239 if (valueParam.startsWith("#")) { 240 return false; 242 } 243 if (valueParam.toLowerCase().startsWith("javascript:")) { 244 return false; 246 } 247 } 248 if (orgUrl == null) { 249 return false; 250 } 251 252 if (valueParam == null) { 253 return false; 254 } 255 256 URL paramUrl = null; 257 String protocol = null; 258 259 String spec = valueParam.trim(); 261 262 int index = spec.indexOf(":"); 264 if (index >= 0) { 265 String 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 paramUrl = new URL (spec); 286 if (protocol == null) { 287 protocol = paramUrl.getProtocol(); 288 } 289 } catch (MalformedURLException 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 311 protected String reconstructTag(String replace, Node node, String param, String quotationMark) { 312 StringBuffer tempString = new StringBuffer (""); 313 tempString.append("<"); 314 tempString.append(node.getNodeName()); 315 NamedNodeMap 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 |