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

📄 cmselementrename.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                // read the property from the template file
                elements = getCms().readPropertyObject(
                    currentTemplate,
                    CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS,
                    false).getValue(null);
            } catch (CmsException e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn(e.getLocalizedMessage());
                }
            }
            if (elements != null) {
                // elements are defined on template file, merge with available elements
                StringTokenizer T = new StringTokenizer(elements, ",");
                while (T.hasMoreTokens()) {
                    String currentElement = T.nextToken();
                    String niceName = null;
                    boolean mandatory = false;
                    int sepIndex = currentElement.indexOf("|");
                    if (sepIndex != -1) {
                        // nice name found for current element, extract it
                        niceName = currentElement.substring(sepIndex + 1);
                        currentElement = currentElement.substring(0, sepIndex);
                    }
                    if (currentElement.endsWith("*")) {
                        // element is mandatory
                        mandatory = true;
                        currentElement = currentElement.substring(0, currentElement.length() - 1);
                    }

                    CmsDialogElement element = new CmsDialogElement(currentElement, niceName, mandatory, true, false);
                    templateElements.add(element);
                }
            }
        }

        return templateElements;
    }

    /**
     * Returns a list of xml pages from the specified folder.<p>
     * 
     * @return a list of xml pages from the specified folder
     */
    private List getXmlPages() {

        boolean isRecursive = Boolean.valueOf(getParamRecursive()).booleanValue();
        // filterdefinition to read only the required resources 
        CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeXmlPage.getStaticTypeId());
        // trying to read the resources
        List xmlPages = null;

        try {
            xmlPages = getCms().readResources(getParamResource(), filter, isRecursive);
        } catch (CmsException e) {
            if (LOG.isErrorEnabled()) {
                LOG.error(e);
            }
        }

        return xmlPages;
    }

    /**
     * Checks if the specified element/locale of the given page has a content.<p>
     * 
     * @param page the xml page
     * @param element the element name
     * @param locale the locale
     * @return false if the specified element/locale of the given page has a content; otherwise true
     */
    private boolean isEmptyElement(CmsXmlPage page, String element, Locale locale) {

        CmsXmlHtmlValue xmlHtmlValue = (CmsXmlHtmlValue)page.getValue(element, locale);
        if (CmsStringUtil.isNotEmpty(xmlHtmlValue.getPlainText(getCms()))) {
            return false;
        }

        return true;
    }

    /** 
     * Checks if the selected new element is valid for the selected template.<p>
     * 
     * @param page the xml page
     * @param element the element name
     * 
     * @return true if ALL_TEMPLATES selected or the element is valid for the selected template; otherwise false
     */
    private boolean isValidElement(CmsXmlPage page, String element) {

        CmsFile file = page.getFile();
        String template;
        try {
            template = getCms().readPropertyObject(
                getCms().getSitePath(file),
                CmsPropertyDefinition.PROPERTY_TEMPLATE,
                true).getValue(null);
        } catch (CmsException e) {
            return false;
        }

        return isValidTemplateElement(template, element);
    }

    /** 
     * Checks if the selected new element is valid for the selected template.<p>
     * 
     * @param element the element name
     * 
     * @return true if ALL_TEMPLATES selected or the element is valid for the selected template; otherwise false
     */
    private boolean isValidElement(String element) {

        boolean validateNewElement = Boolean.valueOf(getParamValidateNewElement()).booleanValue();
        if (ALL.equals(getParamTemplate()) || !validateNewElement) {
            return true;
        }

        return isValidTemplateElement(getParamTemplate(), element);
    }

    /**
     * Check if the given template includes the specified element.<p>
     *  
     * @param template the template
     * @param element the element name
     * @return true if the template includes the given element
     */
    private boolean isValidTemplateElement(String template, String element) {

        List elements = new ArrayList(getTemplateElements(template));
        if (elements != null) {
            Iterator i = elements.iterator();
            while (i.hasNext()) {
                CmsDialogElement currElement = (CmsDialogElement)i.next();
                if (element.equals(currElement.getName())) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Performs the main element rename operation on the filtered resources.<p>
     * 
     * @param xmlPages the list of xml pages
     * @param locale the locale specifying the xmlpage node to perform the operation on
     */
    private void performRenameOperation(List xmlPages, Locale locale) {

        // partial localized (stopped due to low prio).
        boolean removeEmptyElements = Boolean.valueOf(getParamRemoveEmptyElements()).booleanValue();
        boolean validateNewElement = Boolean.valueOf(getParamValidateNewElement()).booleanValue();
        // the list including at least one resource
        if (xmlPages != null && xmlPages.size() > 0) {
            m_report.println(
                Messages.get().container(Messages.RPT_RENAME_LANG_1, locale.getLanguage()),
                I_CmsReport.FORMAT_HEADLINE);
            // if user has not selected ALL templates, then retain pages with specified template
            if (!ALL.equals(getParamTemplate())) {
                xmlPages = getRetainedPagesWithTemplate(xmlPages);
            }
            int m = 0;
            int n = xmlPages.size();
            // loop over remained pages
            Iterator i = xmlPages.iterator();
            while (i.hasNext()) {
                m++;
                CmsXmlPage page = null;
                try {
                    // next file from the list
                    CmsResource res = (CmsResource)i.next();
                    CmsFile file;

                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_SUCCESSION_2,
                        String.valueOf(m),
                        String.valueOf(n)), I_CmsReport.FORMAT_NOTE);
                    m_report.print(Messages.get().container(Messages.RPT_PROCESSING_PAGE_0), I_CmsReport.FORMAT_NOTE);
                    m_report.print(org.opencms.report.Messages.get().container(
                        org.opencms.report.Messages.RPT_ARGUMENT_1,
                        getCms().getSitePath(res)));
                    m_report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));

                    try {
                        file = getCms().readFile(getCms().getSitePath(res), CmsResourceFilter.IGNORE_EXPIRATION);
                    } catch (CmsException e2) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error(e2);
                        }
                        m_report.println(e2);
                        continue;
                    }
                    // try unmarshaling to xml page
                    try {
                        page = CmsXmlPageFactory.unmarshal(getCms(), file);
                    } catch (CmsXmlException e) {
                        m_report.println(e);
                        continue;
                    }

                    // check if the source element exists in the page
                    if (!page.hasValue(getParamOldElement(), locale)) {
                        m_report.println(
                            Messages.get().container(Messages.RPT_NONEXISTANT_ELEM_1, getParamOldElement()),
                            I_CmsReport.FORMAT_NOTE);
                        continue;
                    }

                    // check if the target element already exists in the page
                    if (page.hasValue(getParamNewElement(), locale)) {
                        // the page contains already the new element with speicific content. 
                        // renaming the old will invalid the xml page
                        m_report.println(
                            Messages.get().container(Messages.RPT_NEW_ELEM_EXISTS_0),
                            I_CmsReport.FORMAT_NOTE);
                        continue;
                    }

                    if (validateNewElement) {
                        // check if the target element is valid for the template
                        if (!isValidElement(page, getParamNewElement())) {
                            m_report.println(Messages.get().container(
                                Messages.RPT_INVALID_ARGUMENT_1,
                                getParamNewElement()), I_CmsReport.FORMAT_NOTE);
                            continue;
                        }
                    }

                    try {
                        // rename the element from the old value to the new
                        page.renameValue(getParamOldElement(), getParamNewElement(), locale);
                        // write the page with the new content
                        writePageAndReport(page, true);
                    } catch (Throwable t) {
                        LOG.error(t);
                        m_report.println(t);
                        continue;
                    }

                } catch (Throwable t) {
                    LOG.error(t);
                    m_report.println(t);
                } finally {
                    // finally do remove empty elements of the page
                    // the remove operation is executed if the user has checked the specified checkbox and selected a template (NOT ALL)
                    if (removeEmptyElements) {
                        removeInValidElements(page, locale);
                    }
                }
            }
        }
    }

    /**
     * Analyzes xml page and removes any element if this is not valid for the specified template and has no content.<p>
     * 
     * @param page a xml page
     * @param locale the locale
     */
    private void removeInValidElements(CmsXmlPage page, Locale locale) {

        if (page == null) {
            return;
        }

        if (ALL.equals(getParamTemplate())) {
            return;
        }

        // get all elements of this page
        List pageElements = page.getNames(locale);
        if (pageElements != null) {
            Iterator i = pageElements.iterator();
            while (i.hasNext()) {
                String currElement = (String)i.next();
                // remove current element only is invalid and has no content 
                if (!isValidElement(currElement) && isEmptyElement(page, currElement, locale)) {
                    page.removeValue(currElement, locale);
                    try {
                        writePageAndReport(page, false);
                        m_report.println(
                            Messages.get().container(Messages.RPT_REMOVE_INVALID_EMPTY_ELEM_1, currElement),
                            I_CmsReport.FORMAT_NOTE);
                    } catch (CmsException e) {
                        // ignore
                    }
                }
            }
        }
    }

    /**
     * Writes the given xml page by reporting the result.<p>
     * 
     * @param page the xml page
     * @param report if true then some output will be written to the report
     * @throws CmsException if operation failed
     */
    private void writePageAndReport(CmsXmlPage page, boolean report) throws CmsException {

        CmsFile file = page.getFile();
        byte[] content = page.marshal();
        file.setContents(content);
        // check lock            
        CmsLock lock = getCms().getLock(file);
        if (lock.isNullLock() || lock.getUserId().equals(getCms().getRequestContext().currentUser().getId())) {
            // lock the page
            checkLock(getCms().getSitePath(file));
            // write the file with the new content
            getCms().writeFile(file);
            // unlock the page
            getCms().unlockResource(getCms().getSitePath(file));
            if (report) {
                m_report.println(Messages.get().container(
                    Messages.RPT_ELEM_RENAME_2,
                    getParamOldElement(),
                    getParamNewElement()), I_CmsReport.FORMAT_OK);
            }
        }
    }
}

⌨️ 快捷键说明

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