1 16 package com.blandware.atleap.service.core.impl; 17 18 import com.blandware.atleap.persistence.core.ContentFieldValueDAO; 19 import com.blandware.atleap.persistence.core.MenuDAO; 20 import com.blandware.atleap.common.util.RegExUtil; 21 import com.blandware.atleap.common.util.ConvertUtil; 22 import com.blandware.atleap.common.Constants; 23 import com.blandware.atleap.model.core.ContentFieldValue; 24 import com.blandware.atleap.model.core.ContentField; 25 import com.blandware.atleap.model.core.MenuItem; 26 import com.blandware.atleap.service.core.UtilManager; 27 28 import java.util.List ; 29 import java.util.Iterator ; 30 31 import org.apache.commons.validator.GenericValidator; 32 import org.apache.oro.text.regex.Perl5Compiler; 33 import org.apache.oro.text.regex.MalformedPatternException; 34 35 44 public class UtilManagerImpl extends BaseManagerImpl implements UtilManager { 45 46 49 protected String contextPath = null; 50 51 54 protected String host = null; 55 56 59 protected Integer httpPort = null; 60 61 64 protected Integer httpsPort = null; 65 66 69 protected ContentFieldValueDAO contentFieldValueDAO; 70 71 74 protected MenuDAO menuDAO; 75 76 81 public void setContextPath(String contextPath) { 82 this.contextPath = contextPath; 83 } 84 85 90 public void setHost(String host) { 91 this.host = host; 92 } 93 94 99 public void setHttpPort(Integer httpPort) { 100 this.httpPort = httpPort; 101 } 102 103 108 public void setHttpsPort(Integer httpsPort) { 109 this.httpsPort = httpsPort; 110 } 111 112 117 public void setContentFieldValueDAO(ContentFieldValueDAO contentFieldValueDAO) { 118 this.contentFieldValueDAO = contentFieldValueDAO; 119 } 120 121 126 public void setMenuDAO(MenuDAO menuDAO) { 127 this.menuDAO = menuDAO; 128 } 129 130 141 protected String buildUriRegex(String uriWithoutSlash, String postfix, boolean noExtension) { 142 return RegExUtil.escapeMetasymbols(uriWithoutSlash) 143 + buildLocaleSuffixRegex(noExtension) 144 + RegExUtil.escapeMetasymbols(postfix); 145 } 146 147 154 protected String prepareUri(String uri) { 155 String oldUriWithoutSlash; 156 oldUriWithoutSlash = uri; 157 if (oldUriWithoutSlash.startsWith("/")) { 158 oldUriWithoutSlash = oldUriWithoutSlash.substring("/".length()); 159 } 160 return oldUriWithoutSlash; 161 } 162 163 169 protected String buildLocaleSuffixRegex(boolean noExtension) { 170 return noExtension ? "()" : "(\\.[a-zA-Z]{2})?"; 171 } 172 173 181 protected String computePostfix(boolean noExtension) { 182 String mappingPostfix = Constants.ACTION_MAPPING; 183 if (mappingPostfix.startsWith("*.")) { 184 mappingPostfix = mappingPostfix.substring("*.".length()); 185 } 186 String postfix = (noExtension || GenericValidator.isBlankOrNull(mappingPostfix)) ? "" : "." + mappingPostfix; 187 return postfix; 188 } 189 190 197 protected String buildRwPrefixRegex(String rwPrefix) { 198 if (GenericValidator.isBlankOrNull(rwPrefix)) { 199 rwPrefix = ""; 200 } 201 if (!"/".equals(rwPrefix) && !"".equals(rwPrefix)) { 202 rwPrefix += "/"; 203 } else { 204 rwPrefix = ""; 205 } 206 if (rwPrefix.startsWith("/")) { 207 rwPrefix = rwPrefix.substring(1); 208 } 209 String rwPrefixRegex = "(" + RegExUtil.escapeMetasymbols(rwPrefix) + ")?"; 210 return rwPrefixRegex; 211 } 212 213 218 protected String buildContextPathRegex() { 219 boolean isContextPathPresent = contextPath != null; 220 String context = contextPath; 221 222 String contextPathRegex = "()"; 223 if (isContextPathPresent) { 224 if ("".equals(context)) { 225 context = "/"; 226 } 227 if (!"/".equals(context)) { 228 context += "/"; 229 } 230 contextPathRegex = "(" + RegExUtil.escapeMetasymbols(context) + ")?"; 231 } 232 return contextPathRegex; 233 } 234 235 242 protected String buildHostRegex() { 243 int httpPortNumber = httpPort == null ? 80 : httpPort.intValue(); 245 int httpsPortNumber = httpsPort == null ? 443 : httpsPort.intValue(); 246 247 String httpPortRegex = httpPortNumber == 80 ? "(\\:80)?" : "(\\:" + RegExUtil.escapeMetasymbols(String.valueOf(httpPortNumber)) + ")"; 248 String httpsPortRegex = httpsPortNumber == 443 ? "(\\:443)?" : "(\\:" + RegExUtil.escapeMetasymbols(String.valueOf(httpsPortNumber)) + ")"; 249 String portRegex = "(" + httpPortRegex + "|" + httpsPortRegex + ")"; 250 251 String hostRegex = "((https?\\:\\/\\/" + RegExUtil.escapeMetasymbols(host) + portRegex + ")?)"; 252 return hostRegex; 253 } 254 255 261 protected String buildQueryStringRegex() { 262 return "(\\?[^\"\\s]*)?"; 263 } 264 265 268 public void replaceLinkableObjectUriInLinkedObjects(String oldUri, String newUri, 269 List linkedContentFieldValues, 270 List linkedMenuItems, String rwPrefix, 271 boolean noExtension) { 272 273 String hostRegex = buildHostRegex(); 274 String contextPathRegex = buildContextPathRegex(); 275 String rwPrefixRegex = buildRwPrefixRegex(rwPrefix); 276 277 String oldUriWithoutSlash = prepareUri(oldUri); 279 String newUriWithoutSlash = prepareUri(newUri); 280 281 String postfix = computePostfix(noExtension); 283 284 String uriRegex = buildUriRegex(oldUriWithoutSlash, postfix, noExtension); 286 String regex = "(<([\\w]+)\\b[^>]*\\b(href|src|background)=(\\\")?" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegex + "(" + buildQueryStringRegex() + "(\\\")?[^>]*>)"; 288 String replace = "$1" + newUriWithoutSlash + "$12" + postfix + "$13"; 290 291 for (Iterator i = linkedContentFieldValues.iterator(); i.hasNext();) { 293 ContentFieldValue value = (ContentFieldValue) i.next(); 294 ContentField field = value.getContentField(); 295 byte type = field.getType(); 296 297 String content = null; 299 if (type == ContentField.LINE_TYPE) { 300 content = value.getSimpleValue(); 301 } else { 302 content = ConvertUtil.convertToString(value.getValue()); 303 } 304 try { 306 content = RegExUtil.replaceAll(content, regex, replace, Perl5Compiler.CASE_INSENSITIVE_MASK); 307 } catch (MalformedPatternException e) { 308 log.error("Bad regexp", e); 309 } 310 311 if (type == ContentField.LINE_TYPE) { 313 value.setSimpleValue(content); 314 } else { 315 value.setValue(ConvertUtil.convertToByteArray(content)); 316 } 317 318 contentFieldValueDAO.updateContentFieldValue(value, field, value.getContentLocale()); 320 } 321 322 for (Iterator i = linkedMenuItems.iterator(); i.hasNext();) { 324 MenuItem item = (MenuItem) i.next(); 325 if (item.isDynamic()) { 327 String location = item.getLocation(); 328 String image = item.getImage(); 329 String altImage = item.getAltImage(); 330 if (!GenericValidator.isBlankOrNull(location)) { 331 try { 332 if (location != null) { 333 location = RegExUtil.replaceAll(location, "(" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegex, "$1" + newUriWithoutSlash + "$9" + postfix, Perl5Compiler.CASE_INSENSITIVE_MASK); 334 } 335 String uriRegexWithoutExt = RegExUtil.escapeMetasymbols(oldUriWithoutSlash); 336 if (image != null) { 337 image = RegExUtil.replaceAll(image, "(" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegexWithoutExt, "$1" + newUriWithoutSlash, Perl5Compiler.CASE_INSENSITIVE_MASK); 338 } 339 if (altImage != null) { 340 altImage = RegExUtil.replaceAll(altImage, "(" + hostRegex + contextPathRegex + rwPrefixRegex + ")" + uriRegexWithoutExt, "$1" + newUriWithoutSlash, Perl5Compiler.CASE_INSENSITIVE_MASK); 341 } 342 } catch (MalformedPatternException e) { 343 log.error("Bad regexp", e); 344 } 345 item.setLocation(location); 346 item.setImage(image); 347 item.setAltImage(altImage); 348 menuDAO.updateMenuItem(item, item.getParentItem(), item.getOwner()); 349 } 350 } 351 } 352 } 353 354 357 public void replaceImageSizesInLinkedObjects(String uri, List linkedContentFieldValues, 358 int oldWidth, int oldHeight, 359 int newWidth, int newHeight, 360 String rwPrefix, boolean noExtension) { 361 String hostRegex = buildHostRegex(); 362 String contextPathRegex = buildContextPathRegex(); 363 String rwPrefixRegex = buildRwPrefixRegex(rwPrefix); 364 365 String uriWithoutSlash = prepareUri(uri); 367 368 String postfix = computePostfix(noExtension); 370 371 String uriRegex = buildUriRegex(uriWithoutSlash, postfix, noExtension); 373 String urlRegex = hostRegex + contextPathRegex + rwPrefixRegex + uriRegex; 375 String betweenAttrsRegex = "[^>]*\\b"; 377 String queryStringRegex = buildQueryStringRegex(); 379 String widthRegexL = "(<img\\b" + betweenAttrsRegex + ")width=\\\"?" + oldWidth + "\\\"?(" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?[^>]*>)"; 381 String widthReplaceL = "$1width=\"" + newWidth + "\"$2"; 382 String widthRegexR = "(<img\\b" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?" + betweenAttrsRegex + ")width=\\\"?" + oldWidth + "\\\"?(" + "[^>]*>)"; 383 String widthReplaceR = "$1width=\"" + newWidth + "\"$11"; 384 String heightRegexL = "(<img\\b" + betweenAttrsRegex + ")height=\\\"?" + oldHeight + "\\\"?(" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?[^>]*>)"; 385 String heightReplaceL = "$1height=\"" + newHeight + "\"$2"; 386 String heightRegexR = "(<img\\b" + betweenAttrsRegex + "src=\\\"?" + urlRegex + queryStringRegex + "\\\"?" + betweenAttrsRegex + ")height=\\\"?" + oldHeight + "\\\"?(" + "[^>]*>)"; 387 String heightReplaceR = "$1height=\"" + newHeight + "\"$11"; 388 389 for (Iterator i = linkedContentFieldValues.iterator(); i.hasNext();) { 391 ContentFieldValue contentFieldValue = (ContentFieldValue) i.next(); 392 ContentField contentField = contentFieldValue.getContentField(); 393 byte type = contentField.getType(); 394 String content = null; 395 if (type == ContentField.LINE_TYPE) { 396 content = contentFieldValue.getSimpleValue(); 397 } else { 398 content = ConvertUtil.convertToString(contentFieldValue.getValue()); 399 } 400 try { 401 content = RegExUtil.replaceAll(content, widthRegexL, widthReplaceL, Perl5Compiler.CASE_INSENSITIVE_MASK); 402 content = RegExUtil.replaceAll(content, widthRegexR, widthReplaceR, Perl5Compiler.CASE_INSENSITIVE_MASK); 403 content = RegExUtil.replaceAll(content, heightRegexL, heightReplaceL, Perl5Compiler.CASE_INSENSITIVE_MASK); 404 content = RegExUtil.replaceAll(content, heightRegexR, heightReplaceR, Perl5Compiler.CASE_INSENSITIVE_MASK); 405 } catch (MalformedPatternException e) { 406 log.error("Bad regexp", e); 407 } 408 if (type == ContentField.LINE_TYPE) { 409 contentFieldValue.setSimpleValue(content); 410 } else { 411 contentFieldValue.setValue(ConvertUtil.convertToByteArray(content)); 412 } 413 contentFieldValueDAO.updateContentFieldValue(contentFieldValue, contentField, contentFieldValue.getContentLocale()); 414 } 415 } 416 } 417 | Popular Tags |