⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmscontentcheckproperty.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Gets the configuration of the property check.<p>
     *
     *@throws CmsException if an error occurs reading the configuration
     */
    private void getConfiguration() throws CmsException {

        if (m_configuredErrorChecks == null || m_configuredWarningChecks == null) {
            // get the configuration file
            CmsResource res = m_cms.readResource(CONFIGURATION);
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_DEBUG_PROPERTY_CONFIG_FILENAME_1,
                    res.getRootPath()));
            }
            CmsFile file = CmsFile.upgrade(res, m_cms);
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_DEBUG_PROPERTY_CONFIG_FILE_1,
                    new String(file.getContents())));
            }
            CmsXmlContent configuration = CmsXmlContentFactory.unmarshal(m_cms, file);

            // now extract the configured error checks from it
            m_configuredErrorChecks = getConfiguredChecks(configuration, XPATH_ERROR);
            m_configuredWarningChecks = getConfiguredChecks(configuration, XPATH_WARNUING);
        }
    }

    /**
     * Reads the configuration for a given xpath and stored all results in a list.<p>
     * 
     * @param configuration the configuration to read from
     * @param xpath the xpath prefix
     * @return list of CmsContentCheckProperetyObject objects
     */
    private List getConfiguredChecks(CmsXmlContent configuration, String xpath) {

        List checks = new ArrayList();

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_PROPERTY_CONFIG_XPATH_2, xpath, m_locale));
        }

        int size = configuration.getIndexCount(xpath, m_locale);
        for (int i = 1; i <= size; i++) {
            // extract the values from the configuration
            String propertyname = configuration.getValue(xpath + "[" + i + "]/" + XPATH_PROPERTYNAME, m_locale).getStringValue(
                m_cms);
            String type = configuration.getValue(xpath + "[" + i + "]/" + XPATH_TYPE, m_locale).getStringValue(m_cms);
            String empty = configuration.getValue(xpath + "[" + i + "]/" + XPATH_EMPTY, m_locale).getStringValue(m_cms);
            String filename = configuration.getValue(xpath + "[" + i + "]/" + XPATH_FILENAME, m_locale).getStringValue(
                m_cms);
            String length = configuration.getValue(xpath + "[" + i + "]/" + XPATH_LENGTH, m_locale).getStringValue(
                m_cms);
            int values = configuration.getIndexCount(xpath + "[" + i + "]/" + XPATH_VALUE, m_locale);

            //String value = configuration.getValue(xpath + "[" + i + "]/" + XPATH_VALUE, m_locale).getStringValue(m_cms);

            // store them in the CmsContentCheckProperetyObject obejct for f黵ther processing
            CmsContentCheckProperetyObject propObject = new CmsContentCheckProperetyObject();

            if (CmsStringUtil.isNotEmpty(propertyname)) {
                propObject.setPropertyname(propertyname);
            }
            if (CmsStringUtil.isNotEmpty(type)) {
                propObject.setType(type);
            }
            if (CmsStringUtil.isNotEmpty(empty)) {
                propObject.setEmpty(empty.equals("true"));
            }
            if (CmsStringUtil.isNotEmpty(filename)) {
                propObject.setFilename(filename.equals("true"));
            }
            if (CmsStringUtil.isNotEmpty(length)) {
                propObject.setLength(new Integer(length).intValue());
            }
            if (values > 0) {
                List valueList = new ArrayList();
                for (int j = 1; j <= values; j++) {
                    String value = configuration.getValue(
                        xpath + "[" + i + "]/" + XPATH_VALUE + "[" + j + "]",
                        m_locale).getStringValue(m_cms);
                    if (CmsStringUtil.isNotEmpty(value)) {
                        valueList.add(value);
                    }
                }
                propObject.setValue(valueList);
            }

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_DEBUG_PROPERTY_CONFIG_PROPERTY_3,
                    new Integer(i),
                    new Integer(size),
                    propObject));
            }

            checks.add(propObject);
        }
        return checks;
    }

    /**
     * Processes a list of CmsContentCheckProperetyObject and runs all available tests on them.<p>
     * 
     * All errors or warnings found are collected in a list returned to the calling method.
     * 
     * @param properties list of CmsContentCheckProperetyObject to process
     * @param testResource the CmsContentCheckResource to run all tests on
     * @return list of Strings containing either errors or warinings
     */
    private List processProperties(List properties, CmsContentCheckResource testResource) {

        List results = new ArrayList();

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(
                Messages.LOG_DEBUG_PROPERTY_RESOURCE_1,
                testResource.getResourceName()));
        }

        //loop through all property tests
        for (int i = 0; i < properties.size(); i++) {
            try {
                CmsContentCheckProperetyObject propObject = (CmsContentCheckProperetyObject)properties.get(i);

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(
                        Messages.LOG_DEBUG_PROPERTY_PROPERTY_1,
                        propObject.toString()));
                }

                // check if this test must be done for thies kind of resource
                if ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_BOTH))
                    || ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_FILE) && (testResource.getResource().isFile())))
                    || ((propObject.getType().equals(CmsContentCheckProperetyObject.TYPE_FOLDER) && (testResource.getResource().isFolder())))

                ) {

                    // read the property
                    String prop = m_cms.readPropertyObject(
                        testResource.getResource(),
                        propObject.getPropertyname(),
                        false).getValue();

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_PROPERTY_VALUE_1, prop));
                    }

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(
                            Messages.LOG_DEBUG_PROPERTY_ISEMPTYCHECK_1,
                            new Boolean(propObject.isEmpty())));
                    }

                    // test if the property is empty
                    if (propObject.isEmpty() && CmsStringUtil.isEmpty(prop)) {
                        results.add(Messages.get().getBundle().key(
                            Messages.ERR_CHECK_NO_PROPERTYNAME_1,
                            propObject.getPropertyname()));
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(Messages.get().getBundle().key(
                                Messages.ERR_CHECK_NO_PROPERTYNAME_1,
                                propObject.getPropertyname()));
                        }
                    }

                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(
                            Messages.LOG_DEBUG_PROPERTY_ISFILENAME_1,
                            new Boolean(propObject.isFilename())));
                    }

                    // test if the property does not start with the filename
                    if (!CmsStringUtil.isEmpty(prop)) {
                        if (propObject.isFilename()
                            && testResource.getResource().getName().toLowerCase().startsWith(prop.toLowerCase())) {
                            results.add(Messages.get().getBundle().key(
                                Messages.ERR_CHECK_CONTAINS_FILENAME_2,
                                propObject.getPropertyname(),
                                prop));
                            if (LOG.isDebugEnabled()) {
                                LOG.debug(Messages.get().getBundle().key(
                                    Messages.ERR_CHECK_CONTAINS_FILENAME_2,
                                    propObject.getPropertyname(),
                                    prop));
                            }
                        }

                        if (LOG.isDebugEnabled()) {
                            LOG.debug(Messages.get().getBundle().key(
                                Messages.LOG_DEBUG_PROPERTY_CHECKLENGTH_2,
                                new Integer(propObject.getLength()),
                                new Integer(prop.length())));
                        }
                        // test if the minmal property length is valid
                        if (propObject.getLength() > -1) {
                            if (prop.length() < propObject.getLength()) {
                                results.add(Messages.get().getBundle().key(
                                    Messages.ERR_CHECK_TOO_SHORT_3,
                                    propObject.getPropertyname(),
                                    prop,
                                    new Integer(prop.length())));
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug(Messages.get().getBundle().key(
                                        Messages.ERR_CHECK_TOO_SHORT_3,
                                        propObject.getPropertyname(),
                                        prop,
                                        new Integer(prop.length())));
                                }
                            }
                        }

                        // test if the value matches a regex
                        if (propObject.getValue().size() > 0) {
                            for (int j = 0; j < propObject.getValue().size(); j++) {

                                String regex = new String((String)propObject.getValue().get(j));

                                boolean matchResult = true;
                                if (regex.charAt(0) == '!') {
                                    // negate the pattern
                                    matchResult = false;
                                    regex = regex.substring(1);
                                }
                                String matchValue = prop;
                                boolean match = Pattern.matches(regex, matchValue);

                                if (LOG.isDebugEnabled()) {
                                    LOG.debug(Messages.get().getBundle().key(
                                        Messages.LOG_DEBUG_PROPERTY_MATCHPATTERN_2,
                                        regex,
                                        matchValue));
                                }

                                if (matchResult != match) {
                                    results.add(Messages.get().getBundle().key(
                                        Messages.ERR_CHECK_MATCH_3,
                                        propObject.getPropertyname(),
                                        prop,
                                        propObject.getValue().get(j)));
                                    if (LOG.isDebugEnabled()) {
                                        LOG.debug(Messages.get().getBundle().key(
                                            Messages.ERR_CHECK_MATCH_3,
                                            propObject.getPropertyname(),
                                            prop,
                                            propObject.getValue().get(j)));
                                    }
                                }
                            }
                        }
                    }
                }

            } catch (CmsException e) {
                LOG.error(Messages.get().getBundle().key(
                    Messages.LOG_ERROR_PROCESSING_PROPERTIES_2,
                    testResource.getResourceName(),
                    e));
            }
        }

        return results;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -