1 31 32 package org.opencms.workplace.tools.content.check; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResource; 37 import org.opencms.main.CmsException; 38 import org.opencms.main.CmsLog; 39 import org.opencms.util.CmsStringUtil; 40 import org.opencms.workplace.tools.I_CmsToolHandler; 41 import org.opencms.xml.content.CmsXmlContent; 42 import org.opencms.xml.content.CmsXmlContentFactory; 43 44 import java.util.ArrayList ; 45 import java.util.List ; 46 import java.util.Locale ; 47 import java.util.regex.Pattern ; 48 49 import org.apache.commons.logging.Log; 50 51 70 public class CmsContentCheckProperty extends A_CmsContentCheck implements I_CmsContentCheck, I_CmsToolHandler { 71 72 73 private static final String CONFIGURATION = CmsContentCheck.VFS_PATH_PLUGIN_FOLDER 74 + "propertycheck/configuration.xml"; 75 76 77 private static final String DIALOG_PARAMETER = "property"; 78 79 80 private static final String ICONPATH = "tools/contenttools/icons/big/contentcheck_property_configuration.png"; 81 82 83 private static final Log LOG = CmsLog.getLog(CmsContentCheckProperty.class); 84 85 86 private static final String NAME = "Property Check"; 87 88 89 private static final String XPATH_EMPTY = "empty"; 90 91 92 private static final String XPATH_ERROR = "error"; 93 94 95 private static final String XPATH_FILENAME = "filename"; 96 97 98 private static final String XPATH_LENGTH = "length"; 99 100 101 private static final String XPATH_PROPERTYNAME = "propertyname"; 102 103 104 private static final String XPATH_TYPE = "type"; 105 106 107 private static final String XPATH_VALUE = "value"; 108 109 110 private static final String XPATH_WARNUING = "warning"; 111 112 113 private boolean m_active = true; 114 115 116 private CmsObject m_cms; 117 118 119 private List m_configuredErrorChecks = null; 120 121 122 private List m_configuredWarningChecks = null; 123 124 125 private Locale m_locale; 126 127 131 public CmsContentCheckResource executeContentCheck(CmsObject cms, CmsContentCheckResource testResource) 132 throws CmsException { 133 134 getConfiguration(); 135 if (LOG.isDebugEnabled()) { 136 LOG.debug(Messages.get().getBundle().key( 137 Messages.LOG_DEBUG_PROPERTY_CONFIGURED_ERRORS_2, 138 new Object [] {testResource.getResourceName(), m_configuredErrorChecks})); 139 } 140 List errors = processProperties(m_configuredErrorChecks, testResource); 142 if (errors.size() > 0) { 143 testResource.addErrors(errors); 144 } 145 if (LOG.isDebugEnabled()) { 146 LOG.debug(Messages.get().getBundle().key( 147 Messages.LOG_DEBUG_PROPERTY_CONFIGURED_WARNINGS_2, 148 testResource.getResourceName(), 149 m_configuredErrorChecks)); 150 } 151 List warnings = processProperties(m_configuredWarningChecks, testResource); 153 if (warnings.size() > 0) { 154 testResource.addWarnings(warnings); 155 } 156 return testResource; 157 } 158 159 163 public String getDialogParameterName() { 164 165 return DIALOG_PARAMETER; 166 } 167 168 171 public String getHelpText() { 172 173 return Messages.get().getBundle().key(Messages.GUI_CHECKCONTENT_CONFIGURATION_PROPERTY_HELP_0); 174 } 175 176 179 public String getIconPath() { 180 181 return ICONPATH; 182 } 183 184 187 public String getLink() { 188 189 return "/system/workplace/views/admin/admin-editor.jsp?resource=/system/workplace/admin/contenttools/check/plugin/propertycheck/configuration.xml"; 190 } 191 192 195 public List getMessageBundles() { 196 197 List messages = new ArrayList (); 198 messages.add(org.opencms.workplace.tools.content.check.Messages.get().getBundleName()); 199 return messages; 200 } 201 202 205 public String getName() { 206 207 return NAME; 208 } 209 210 213 public String getPath() { 214 215 return "/contenttools/checkconfig/checkproperty"; 216 } 217 218 221 public float getPosition() { 222 223 return 1; 224 } 225 226 229 public String getShortName() { 230 231 return NAME; 232 } 233 234 237 public void init(CmsObject cms) { 238 239 m_cms = cms; 240 m_locale = new Locale ("en"); 241 } 242 243 248 public boolean isActive() { 249 250 return m_active; 251 } 252 253 260 public void setActive(boolean value) { 261 262 m_active = value; 263 } 264 265 270 private void getConfiguration() throws CmsException { 271 272 if (m_configuredErrorChecks == null || m_configuredWarningChecks == null) { 273 CmsResource res = m_cms.readResource(CONFIGURATION); 275 if (LOG.isDebugEnabled()) { 276 LOG.debug(Messages.get().getBundle().key( 277 Messages.LOG_DEBUG_PROPERTY_CONFIG_FILENAME_1, 278 res.getRootPath())); 279 } 280 CmsFile file = CmsFile.upgrade(res, m_cms); 281 if (LOG.isDebugEnabled()) { 282 LOG.debug(Messages.get().getBundle().key( 283 Messages.LOG_DEBUG_PROPERTY_CONFIG_FILE_1, 284 new String (file.getContents()))); 285 } 286 CmsXmlContent configuration = CmsXmlContentFactory.unmarshal(m_cms, file); 287 288 m_configuredErrorChecks = getConfiguredChecks(configuration, XPATH_ERROR); 290 m_configuredWarningChecks = getConfiguredChecks(configuration, XPATH_WARNUING); 291 } 292 } 293 294 301 private List getConfiguredChecks(CmsXmlContent configuration, String xpath) { 302 303 List checks = new ArrayList (); 304 305 if (LOG.isDebugEnabled()) { 306 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_PROPERTY_CONFIG_XPATH_2, xpath, m_locale)); 307 } 308 309 int size = configuration.getIndexCount(xpath, m_locale); 310 for (int i = 1; i <= size; i++) { 311 String propertyname = configuration.getValue(xpath + "[" + i + "]/" + XPATH_PROPERTYNAME, m_locale).getStringValue( 313 m_cms); 314 String type = configuration.getValue(xpath + "[" + i + "]/" + XPATH_TYPE, m_locale).getStringValue(m_cms); 315 String empty = configuration.getValue(xpath + "[" + i + "]/" + XPATH_EMPTY, m_locale).getStringValue(m_cms); 316 String filename = configuration.getValue(xpath + "[" + i + "]/" + XPATH_FILENAME, m_locale).getStringValue( 317 m_cms); 318 String length = configuration.getValue(xpath + "[" + i + "]/" + XPATH_LENGTH, m_locale).getStringValue( 319 m_cms); 320 int values = configuration.getIndexCount(xpath + "[" + i + "]/" + XPATH_VALUE, m_locale); 321 322 324 CmsContentCheckProperetyObject propObject = new CmsContentCheckProperetyObject(); 326 327 if (CmsStringUtil.isNotEmpty(propertyname)) { 328 propObject.setPropertyname(propertyname); 329 } 330 if (CmsStringUtil.isNotEmpty(type)) { 331 propObject.setType(type); 332 } 333 if (CmsStringUtil.isNotEmpty(empty)) { 334 propObject.setEmpty(empty.equals("true")); 335 } 336 if (CmsStringUtil.isNotEmpty(filename)) { 337 propObject.setFilename(filename.equals("true")); 338 } 339 if (CmsStringUtil.isNotEmpty(length)) { 340 propObject.setLength(new Integer (length).intValue()); 341 } 342 if (values > 0) { 343 List valueList = new ArrayList (); 344 for (int j = 1; j <= values; j++) { 345 String value = configuration.getValue( 346 xpath + "[" + i + "]/" + XPATH_VALUE + "[" + j + "]", 347 m_locale).getStringValue(m_cms); 348 if (CmsStringUtil.isNotEmpty(value)) { 349 valueList.add(value); 350 } 351 } 352 propObject.setValue(valueList); 353 } 354 355 if (LOG.isDebugEnabled()) { 356 LOG.debug(Messages.get().getBundle().key( 357 Messages.LOG_DEBUG_PROPERTY_CONFIG_PROPERTY_3, 358 new Integer (i), 359 new Integer (size), 360 propObject)); 361 } 362 363 checks.add(propObject); 364 } 365 return checks; 366 } 367 368 377 private List processProperties(List properties, CmsContentCheckResource testResource) { 378 379 List results = new ArrayList (); 380 381 if (LOG.isDebugEnabled()) { 382 LOG.debug(Messages.get().getBundle().key( 383 Messages.LOG_DEBUG_PROPERTY_RESOURCE_1, 384 testResource.getResourceName())); 385 } 386 387 for (int i = 0; i < properties.size(); i++) { 389 try { 390 CmsContentCheckProperetyObject propObject = (CmsContentCheckProperetyObject)properties.get(i); 391 392 if (LOG.isDebugEnabled()) { 393 LOG.debug(Messages.get().getBundle().key( 394 Messages.LOG_DEBUG_PROPERTY_PROPERTY_1, 395 propObject.toString())); 396 } 397 398 if ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_BOTH)) 400 || ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_FILE) && (testResource.getResource().isFile()))) 401 || ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_FOLDER) && (testResource.getResource().isFolder()))) 402 403 ) { 404 405 String prop = m_cms.readPropertyObject( 407 testResource.getResource(), 408 propObject.getPropertyname(), 409 false).getValue(); 410 411 if (LOG.isDebugEnabled()) { 412 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_PROPERTY_VALUE_1, prop)); 413 } 414 415 if (LOG.isDebugEnabled()) { 416 LOG.debug(Messages.get().getBundle().key( 417 Messages.LOG_DEBUG_PROPERTY_ISEMPTYCHECK_1, 418 new Boolean (propObject.isEmpty()))); 419 } 420 421 if (propObject.isEmpty() && CmsStringUtil.isEmpty(prop)) { 423 results.add(Messages.get().getBundle().key( 424 Messages.ERR_CHECK_NO_PROPERTYNAME_1, 425 propObject.getPropertyname())); 426 if (LOG.isDebugEnabled()) { 427 LOG.debug(Messages.get().getBundle().key( 428 Messages.ERR_CHECK_NO_PROPERTYNAME_1, 429 propObject.getPropertyname())); 430 } 431 } 432 433 if (LOG.isDebugEnabled()) { 434 LOG.debug(Messages.get().getBundle().key( 435 Messages.LOG_DEBUG_PROPERTY_ISFILENAME_1, 436 new Boolean (propObject.isFilename()))); 437 } 438 439 if (!CmsStringUtil.isEmpty(prop)) { 441 if (propObject.isFilename() 442 && testResource.getResource().getName().toLowerCase().startsWith(prop.toLowerCase())) { 443 results.add(Messages.get().getBundle().key( 444 Messages.ERR_CHECK_CONTAINS_FILENAME_2, 445 propObject.getPropertyname(), 446 prop)); 447 if (LOG.isDebugEnabled()) { 448 LOG.debug(Messages.get().getBundle().key( 449 Messages.ERR_CHECK_CONTAINS_FILENAME_2, 450 propObject.getPropertyname(), 451 prop)); 452 } 453 } 454 455 if (LOG.isDebugEnabled()) { 456 LOG.debug(Messages.get().getBundle().key( 457 Messages.LOG_DEBUG_PROPERTY_CHECKLENGTH_2, 458 new Integer (propObject.getLength()), 459 new Integer (prop.length()))); 460 } 461 if (propObject.getLength() > -1) { 463 if (prop.length() < propObject.getLength()) { 464 results.add(Messages.get().getBundle().key( 465 Messages.ERR_CHECK_TOO_SHORT_3, 466 propObject.getPropertyname(), 467 prop, 468 new Integer (prop.length()))); 469 if (LOG.isDebugEnabled()) { 470 LOG.debug(Messages.get().getBundle().key( 471 Messages.ERR_CHECK_TOO_SHORT_3, 472 propObject.getPropertyname(), 473 prop, 474 new Integer (prop.length()))); 475 } 476 } 477 } 478 479 if (propObject.getValue().size() > 0) { 481 for (int j = 0; j < propObject.getValue().size(); j++) { 482 483 String regex = new String ((String )propObject.getValue().get(j)); 484 485 boolean matchResult = true; 486 if (regex.charAt(0) == '!') { 487 matchResult = false; 489 regex = regex.substring(1); 490 } 491 String matchValue = prop; 492 boolean match = Pattern.matches(regex, matchValue); 493 494 if (LOG.isDebugEnabled()) { 495 LOG.debug(Messages.get().getBundle().key( 496 Messages.LOG_DEBUG_PROPERTY_MATCHPATTERN_2, 497 regex, 498 matchValue)); 499 } 500 501 if (matchResult != match) { 502 results.add(Messages.get().getBundle().key( 503 Messages.ERR_CHECK_MATCH_3, 504 propObject.getPropertyname(), 505 prop, 506 propObject.getValue().get(j))); 507 if (LOG.isDebugEnabled()) { 508 LOG.debug(Messages.get().getBundle().key( 509 Messages.ERR_CHECK_MATCH_3, 510 propObject.getPropertyname(), 511 prop, 512 propObject.getValue().get(j))); 513 } 514 } 515 } 516 } 517 } 518 } 519 520 } catch (CmsException e) { 521 LOG.error(Messages.get().getBundle().key( 522 Messages.LOG_ERROR_PROCESSING_PROPERTIES_2, 523 testResource.getResourceName(), 524 e)); 525 } 526 } 527 528 return results; 529 } 530 } | Popular Tags |