1 31 32 package org.opencms.util; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProperty; 36 import org.opencms.file.CmsResource; 37 import org.opencms.flex.CmsFlexController; 38 import org.opencms.i18n.CmsMessageContainer; 39 import org.opencms.i18n.CmsMessages; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.CmsLog; 42 import org.opencms.main.OpenCms; 43 44 import java.util.Arrays ; 45 import java.util.Collections ; 46 import java.util.HashMap ; 47 import java.util.List ; 48 import java.util.Map ; 49 50 import javax.servlet.jsp.PageContext ; 51 52 import org.apache.commons.logging.Log; 53 54 67 public class CmsMacroResolver implements I_CmsMacroResolver { 68 69 70 public static final String KEY_CURRENT_TIME = "currenttime"; 71 72 73 public static final String KEY_CURRENT_USER_CITY = "currentuser.city"; 74 75 76 public static final String KEY_CURRENT_USER_COUNTRY = "currentuser.country"; 77 78 79 public static final String KEY_CURRENT_USER_EMAIL = "currentuser.email"; 80 81 82 public static final String KEY_CURRENT_USER_FIRSTNAME = "currentuser.firstname"; 83 84 85 public static final String KEY_CURRENT_USER_FULLNAME = "currentuser.fullname"; 86 87 88 public static final String KEY_CURRENT_USER_LASTNAME = "currentuser.lastname"; 89 90 91 public static final String KEY_CURRENT_USER_NAME = "currentuser.name"; 92 93 94 public static final String KEY_CURRENT_USER_STREET = "currentuser.street"; 95 96 97 public static final String KEY_CURRENT_USER_ZIP = "currentuser.zip"; 98 99 100 public static final String KEY_LOCALIZED_PREFIX = "key."; 101 102 103 public static final String KEY_OPENCMS = "opencms."; 104 105 106 public static final String KEY_PAGE_CONTEXT = "pageContext."; 107 108 109 public static final String KEY_PROPERTY = "property."; 110 111 112 public static final String KEY_PROPERTY_ELEMENT = "elementProperty."; 113 114 115 public static final String KEY_REQUEST_ENCODING = "request.encoding"; 116 117 118 public static final String KEY_REQUEST_FOLDER = "request.folder"; 119 120 121 public static final String KEY_REQUEST_LOCALE = "request.locale"; 122 123 124 public static final String KEY_REQUEST_PARAM = "param."; 125 126 127 public static final String KEY_REQUEST_URI = "request.uri"; 128 129 130 public static final String KEY_VALIDATION_PATH = "validation.path"; 131 132 133 public static final String KEY_VALIDATION_REGEX = "validation.regex"; 134 135 136 public static final String KEY_VALIDATION_VALUE = "validation.value"; 137 138 139 public static final String [] VALUE_NAME_ARRAY = {"uri", "filename", "folder", "default.encoding"}; 140 141 142 public static final List VALUE_NAMES = Collections.unmodifiableList(Arrays.asList(VALUE_NAME_ARRAY)); 143 144 145 private static final Log LOG = CmsLog.getLog(CmsMacroResolver.class); 146 147 148 protected Map m_additionalMacros; 149 150 151 protected CmsObject m_cms; 152 153 154 protected PageContext m_jspPageContext; 155 156 157 protected boolean m_keepEmptyMacros; 158 159 160 protected CmsMessages m_messages; 161 162 163 protected String m_resourceName; 164 165 173 public static String formatMacro(String input) { 174 175 StringBuffer result = new StringBuffer (input.length() + 4); 176 result.append(I_CmsMacroResolver.MACRO_DELIMITER_NEW); 177 result.append(I_CmsMacroResolver.MACRO_START_NEW); 178 result.append(input); 179 result.append(I_CmsMacroResolver.MACRO_END_NEW); 180 return result.toString(); 181 } 182 183 192 public static boolean isMacro(String input) { 193 194 if (CmsStringUtil.isEmpty(input) || (input.length() < 3)) { 195 return false; 196 } 197 198 return (((input.charAt(0) == I_CmsMacroResolver.MACRO_DELIMITER) && ((input.charAt(1) == I_CmsMacroResolver.MACRO_START) && (input.charAt(input.length() - 1) == I_CmsMacroResolver.MACRO_END))) || ((input.charAt(0) == I_CmsMacroResolver.MACRO_DELIMITER_NEW) && ((input.charAt(1) == I_CmsMacroResolver.MACRO_START_NEW) && (input.charAt(input.length() - 1) == I_CmsMacroResolver.MACRO_END_NEW)))); 199 } 200 201 209 public static boolean isMacro(String input, String macroName) { 210 211 if (isMacro(input)) { 212 return input.substring(2, input.length() - 1).equals(macroName); 213 } 214 return false; 215 } 216 217 222 public static CmsMacroResolver newInstance() { 223 224 return new CmsMacroResolver(); 225 } 226 227 243 public static String resolveMacros(String input, CmsObject cms, CmsMessages messages) { 244 245 CmsMacroResolver resolver = new CmsMacroResolver(); 246 resolver.m_cms = cms; 247 resolver.m_messages = messages; 248 resolver.m_keepEmptyMacros = true; 249 return resolver.resolveMacros(input); 250 } 251 252 268 public static String resolveMacros(final String input, I_CmsMacroResolver resolver) { 269 270 if ((input == null) || (input.length() < 3)) { 271 return input; 273 } 274 275 int pn = input.indexOf(I_CmsMacroResolver.MACRO_DELIMITER_NEW); 276 int po = input.indexOf(I_CmsMacroResolver.MACRO_DELIMITER); 277 278 if ((po == -1) && (pn == -1)) { 279 return input; 281 } 282 283 int len = input.length(); 284 StringBuffer result = new StringBuffer (len << 1); 285 int np, pp1, pp2, e; 286 String macro, value; 287 boolean keep = resolver.isKeepEmptyMacros(); 288 boolean resolvedNone = true; 289 char ds, de; 290 int p; 291 292 if ((po == -1) || ((pn > -1) && (pn < po))) { 293 p = pn; 294 ds = I_CmsMacroResolver.MACRO_START_NEW; 295 de = I_CmsMacroResolver.MACRO_END_NEW; 296 } else { 297 p = po; 298 ds = I_CmsMacroResolver.MACRO_START; 299 de = I_CmsMacroResolver.MACRO_END; 300 } 301 302 result.append(input.substring(0, p)); 304 do { 305 pp1 = p + 1; 306 pp2 = pp1 + 1; 307 if (pp2 >= len) { 308 result.append(input.substring(p, len)); 310 break; 311 } 312 if ((pn > -1) && (pn < pp1)) { 314 pn = input.indexOf(I_CmsMacroResolver.MACRO_DELIMITER_NEW, pp1); 315 } 316 if ((po > -1) && (po < pp1)) { 317 po = input.indexOf(I_CmsMacroResolver.MACRO_DELIMITER, pp1); 318 } 319 if ((po == -1) && (pn == -1)) { 320 np = len; 322 } else { 323 if ((po == -1) || ((pn > -1) && (pn < po))) { 325 np = pn; 326 } else { 327 np = po; 328 } 329 } 330 char st = input.charAt(pp1); 332 if (st == ds) { 333 e = input.indexOf(de, p); 335 if ((e > 0) && (e < np)) { 336 macro = input.substring(pp2, e); 338 value = resolver.getMacroValue(macro); 340 e++; 341 if (value != null) { 342 result.append(value); 344 resolvedNone = false; 345 } else if (keep) { 346 result.append(input.substring(p, e)); 348 } 349 } else { 350 e = p; 352 } 353 } else { 354 e = p; 356 } 357 if (np == pn) { 359 ds = I_CmsMacroResolver.MACRO_START_NEW; 360 de = I_CmsMacroResolver.MACRO_END_NEW; 361 } else { 362 ds = I_CmsMacroResolver.MACRO_START; 363 de = I_CmsMacroResolver.MACRO_END; 364 } 365 result.append(input.substring(e, np)); 367 p = np; 369 } while (p < len); 370 371 if (resolvedNone && keep) { 372 return input; 374 } 375 376 return result.toString(); 378 } 379 380 390 public static String stripMacro(String input) { 391 392 if (isMacro(input)) { 393 return input.substring(2, input.length() - 1); 394 } 395 return null; 396 } 397 398 404 public void addMacro(String key, String value) { 405 406 if (m_additionalMacros == null) { 407 m_additionalMacros = new HashMap (); 409 } 410 m_additionalMacros.put(key, value); 411 } 412 413 416 public String getMacroValue(String macro) { 417 418 if (m_messages != null) { 419 if (macro.startsWith(CmsMacroResolver.KEY_LOCALIZED_PREFIX)) { 420 String keyName = macro.substring(CmsMacroResolver.KEY_LOCALIZED_PREFIX.length()); 421 return m_messages.keyWithParams(keyName); 422 } 423 } 424 425 if (m_jspPageContext != null) { 426 427 if (macro.startsWith(CmsMacroResolver.KEY_REQUEST_PARAM)) { 428 macro = macro.substring(CmsMacroResolver.KEY_REQUEST_PARAM.length()); 430 return m_jspPageContext.getRequest().getParameter(macro); 431 } 432 433 if (macro.startsWith(CmsMacroResolver.KEY_PAGE_CONTEXT)) { 434 macro = macro.substring(CmsMacroResolver.KEY_PAGE_CONTEXT.length()); 436 int scope = m_jspPageContext.getAttributesScope(macro); 437 return m_jspPageContext.getAttribute(macro, scope).toString(); 438 } 439 440 if ((m_cms != null) && macro.startsWith(CmsMacroResolver.KEY_PROPERTY_ELEMENT)) { 441 442 444 macro = macro.substring(CmsMacroResolver.KEY_PROPERTY_ELEMENT.length()); 445 CmsFlexController controller = CmsFlexController.getController(m_jspPageContext.getRequest()); 446 try { 447 CmsProperty property = m_cms.readPropertyObject( 448 controller.getCurrentRequest().getElementUri(), 449 macro, 450 false); 451 if (property != CmsProperty.getNullProperty()) { 452 return property.getValue(); 453 } 454 } catch (CmsException e) { 455 if (LOG.isWarnEnabled()) { 456 LOG.warn(Messages.get().getBundle().key( 457 Messages.LOG_PROPERTY_READING_FAILED_2, 458 macro, 459 controller.getCurrentRequest().getElementUri()), e); 460 } 461 } 462 } 463 } 464 465 if (m_cms != null) { 466 467 if (macro.startsWith(CmsMacroResolver.KEY_PROPERTY)) { 468 macro = macro.substring(CmsMacroResolver.KEY_PROPERTY.length()); 470 try { 471 CmsProperty property = m_cms.readPropertyObject(m_cms.getRequestContext().getUri(), macro, true); 472 if (property != CmsProperty.getNullProperty()) { 473 return property.getValue(); 474 } 475 } catch (CmsException e) { 476 if (LOG.isWarnEnabled()) { 477 CmsMessageContainer message = Messages.get().container( 478 Messages.LOG_PROPERTY_READING_FAILED_2, 479 macro, 480 m_cms.getRequestContext().getUri()); 481 LOG.warn(message.key(), e); 482 } 483 } 484 485 } 486 487 if (macro.startsWith(CmsMacroResolver.KEY_OPENCMS)) { 488 489 491 String originalKey = macro; 492 macro = macro.substring(CmsMacroResolver.KEY_OPENCMS.length()); 493 int index = VALUE_NAMES.indexOf(macro); 494 String value = null; 495 496 switch (index) { 497 case 0: 498 value = m_cms.getRequestContext().getUri(); 500 break; 501 case 1: 502 value = m_resourceName; 504 break; 505 case 2: 506 value = m_cms.getRequestContext().getFolderUri(); 508 break; 509 case 3: 510 value = OpenCms.getSystemInfo().getDefaultEncoding(); 512 break; 513 default: 514 value = originalKey; 516 break; 517 } 518 519 return value; 520 } 521 522 if (CmsMacroResolver.KEY_CURRENT_USER_NAME.equals(macro)) { 523 return m_cms.getRequestContext().currentUser().getName(); 525 } 526 527 if (CmsMacroResolver.KEY_CURRENT_USER_FIRSTNAME.equals(macro)) { 528 return m_cms.getRequestContext().currentUser().getFirstname(); 530 } 531 532 if (CmsMacroResolver.KEY_CURRENT_USER_LASTNAME.equals(macro)) { 533 return m_cms.getRequestContext().currentUser().getLastname(); 535 } 536 537 if (CmsMacroResolver.KEY_CURRENT_USER_FULLNAME.equals(macro)) { 538 return m_cms.getRequestContext().currentUser().getFullName(); 540 } 541 542 if (CmsMacroResolver.KEY_CURRENT_USER_EMAIL.equals(macro)) { 543 return m_cms.getRequestContext().currentUser().getEmail(); 545 } 546 547 if (CmsMacroResolver.KEY_CURRENT_USER_STREET.equals(macro)) { 548 return m_cms.getRequestContext().currentUser().getAddress(); 550 } 551 552 if (CmsMacroResolver.KEY_CURRENT_USER_ZIP.equals(macro)) { 553 return m_cms.getRequestContext().currentUser().getZipcode(); 555 } 556 557 if (CmsMacroResolver.KEY_CURRENT_USER_COUNTRY.equals(macro)) { 558 return m_cms.getRequestContext().currentUser().getCountry(); 560 } 561 562 if (CmsMacroResolver.KEY_CURRENT_USER_CITY.equals(macro)) { 563 return m_cms.getRequestContext().currentUser().getCity(); 565 } 566 567 if (CmsMacroResolver.KEY_REQUEST_URI.equals(macro)) { 568 return m_cms.getRequestContext().getUri(); 570 } 571 572 if (CmsMacroResolver.KEY_REQUEST_FOLDER.equals(macro)) { 573 return CmsResource.getParentFolder(m_cms.getRequestContext().getUri()); 575 } 576 577 if (CmsMacroResolver.KEY_REQUEST_ENCODING.equals(macro)) { 578 return m_cms.getRequestContext().getEncoding(); 580 } 581 582 if (CmsMacroResolver.KEY_REQUEST_LOCALE.equals(macro)) { 583 return m_cms.getRequestContext().getLocale().toString(); 585 } 586 587 } 588 589 if (CmsMacroResolver.KEY_CURRENT_TIME.equals(macro)) { 590 return String.valueOf(System.currentTimeMillis()); 592 } 593 594 if (m_additionalMacros != null) { 595 return (String )m_additionalMacros.get(macro); 596 } 597 598 return null; 599 } 600 601 604 public boolean isKeepEmptyMacros() { 605 606 return m_keepEmptyMacros; 607 } 608 609 617 public String resolveMacros(String input) { 618 619 String result = input; 620 621 if (input != null) { 622 String lastResult; 623 do { 624 lastResult = result; 626 result = CmsMacroResolver.resolveMacros(result, this); 628 } while (!result.equals(lastResult)); 630 } 631 632 return result; 634 } 635 636 645 public CmsMacroResolver setAdditionalMacros(Map additionalMacros) { 646 647 m_additionalMacros = additionalMacros; 648 return this; 649 } 650 651 658 public CmsMacroResolver setCmsObject(CmsObject cms) { 659 660 m_cms = cms; 661 return this; 662 } 663 664 671 public CmsMacroResolver setJspPageContext(PageContext jspPageContext) { 672 673 m_jspPageContext = jspPageContext; 674 return this; 675 } 676 677 687 public CmsMacroResolver setKeepEmptyMacros(boolean keepEmptyMacros) { 688 689 m_keepEmptyMacros = keepEmptyMacros; 690 return this; 691 } 692 693 701 public CmsMacroResolver setMessages(CmsMessages messages) { 702 703 m_messages = messages; 704 return this; 705 } 706 707 714 public CmsMacroResolver setResourceName(String resourceName) { 715 716 m_resourceName = resourceName; 717 return this; 718 } 719 } | Popular Tags |