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

📄 cmsxmlpage.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * Returns the link table of an element.<p>
     * 
     * @param name name of the element
     * @param locale locale of the element
     * @return the link table
     */
    public CmsLinkTable getLinkTable(String name, Locale locale) {

        CmsXmlHtmlValue value = (CmsXmlHtmlValue)getValue(name, locale);
        if (value != null) {
            return value.getLinkTable();
        }
        return new CmsLinkTable();
    }

    /**
     * @see org.opencms.xml.A_CmsXmlDocument#getNames(java.util.Locale)
     */
    public List getNames(Locale locale) {

        Object o = m_elementNames.get(locale);
        if (o != null) {
            List result = new ArrayList(8);
            Iterator i = ((Set)o).iterator();
            while (i.hasNext()) {
                String path = (String)i.next();
                result.add(CmsXmlUtils.removeXpathIndex(path));
            }
            return result;
        }
        return Collections.EMPTY_LIST;
    }

    /**
     * Checks if the element of a page object is enabled.<p>
     * 
     * @param name the name of the element
     * @param locale the locale of the element
     * @return true if the element exists and is not disabled
     */
    public boolean isEnabled(String name, Locale locale) {

        CmsXmlHtmlValue value = (CmsXmlHtmlValue)getValue(name, locale);

        if (value != null) {
            Element element = value.getElement();
            Attribute enabled = element.attribute(ATTRIBUTE_ENABLED);
            return (enabled == null || Boolean.valueOf(enabled.getValue()).booleanValue());
        }

        return false;
    }

    /**
     * Removes an existing value with the given name and locale
     * from this XML document.<p>
     * 
     * @param name the name of the value
     * @param locale the locale of the value
     */
    public void removeValue(String name, Locale locale) {

        I_CmsXmlContentValue value = removeBookmark(CmsXmlUtils.createXpath(name, 1), locale);
        if (value != null) {
            Element element = value.getElement();
            element.detach();
        }
    }

    /**
     * Renames the page-element value from the old to the new one.<p>
     * 
     * @param oldValue the old value 
     * @param newValue the new value
     * @param locale the locale
     * 
     * @throws CmsIllegalArgumentException if the name contains an index ("[&lt;number&gt;]"), the new value for the 
     *         given locale already exists in the xmlpage or the the old value does not exist for the locale in the xmlpage.
     * 
     */
    public void renameValue(String oldValue, String newValue, Locale locale) throws CmsIllegalArgumentException {

        CmsXmlHtmlValue oldXmlHtmlValue = (CmsXmlHtmlValue)getValue(oldValue, locale);
        if (oldXmlHtmlValue == null) {
            throw new CmsIllegalArgumentException(Messages.get().container(
                Messages.ERR_XML_PAGE_NO_ELEM_FOR_LANG_2,
                oldValue,
                locale));
        }

        if (hasValue(newValue, locale)) {
            throw new CmsIllegalArgumentException(Messages.get().container(
                Messages.ERR_XML_PAGE_LANG_ELEM_EXISTS_2,
                newValue,
                locale));
        }
        if (newValue.indexOf('[') >= 0) {
            throw new CmsIllegalArgumentException(Messages.get().container(
                Messages.ERR_XML_PAGE_CONTAINS_INDEX_1,
                newValue));
        }

        // get the element 
        Element element = oldXmlHtmlValue.getElement();

        // update value of the element attribute 'NAME'         
        element.addAttribute(ATTRIBUTE_NAME, newValue);

        // re-initialize the document to update the bookmarks
        initDocument(m_document, m_encoding, getContentDefinition());
    }

    /**
     * Sets the enabled flag of an already existing element.<p>
     * 
     * Note: if isEnabled is set to true, the attribute is removed
     * since true is the default
     * 
     * @param name name name of the element
     * @param locale locale of the element
     * @param isEnabled enabled flag for the element
     */
    public void setEnabled(String name, Locale locale, boolean isEnabled) {

        CmsXmlHtmlValue value = (CmsXmlHtmlValue)getValue(name, locale);
        Element element = value.getElement();
        Attribute enabled = element.attribute(ATTRIBUTE_ENABLED);

        if (enabled == null) {
            if (!isEnabled) {
                element.addAttribute(ATTRIBUTE_ENABLED, Boolean.toString(isEnabled));
            }
        } else if (isEnabled) {
            element.remove(enabled);
        } else {
            enabled.setValue(Boolean.toString(isEnabled));
        }
    }

    /**
     * Sets the data of an already existing value.<p>
     * 
     * The data will be enclosed as CDATA within the xml page structure.
     * When setting the element data, the content of this element will be
     * processed automatically.<p>
     * 
     * @param cms the cms object
     * @param name name of the element
     * @param locale locale of the element
     * @param content character data (CDATA) of the element
     * 
     * @throws CmsXmlException if something goes wrong
     */
    public void setStringValue(CmsObject cms, String name, Locale locale, String content) throws CmsXmlException {

        CmsXmlHtmlValue value = (CmsXmlHtmlValue)getValue(name, locale);

        if (value != null) {
            // set the values
            value.setStringValue(cms, content);
        } else {
            throw new CmsXmlException(Messages.get().container(
                Messages.ERR_XML_PAGE_INVALID_ELEM_SELECT_2,
                locale,
                name));
        }
    }

    /**
     * @see org.opencms.xml.I_CmsXmlDocument#validate(org.opencms.file.CmsObject)
     */
    public CmsXmlContentErrorHandler validate(CmsObject cms) {

        // XML pages currently do not support validation
        return new CmsXmlContentErrorHandler();
    }

    /**
     * @see org.opencms.xml.A_CmsXmlDocument#initDocument(org.dom4j.Document, java.lang.String, org.opencms.xml.CmsXmlContentDefinition)
     */
    protected void initDocument(Document document, String encoding, CmsXmlContentDefinition definition) {

        m_encoding = CmsEncoder.lookupEncoding(encoding, encoding);
        m_document = document;
        m_elementLocales = new HashMap();
        m_elementNames = new HashMap();
        m_locales = new HashSet();

        // convert pre 5.3.6 XML page documents
        if (!NODE_PAGES.equals(m_document.getRootElement().getName())) {
            convertOldDocument();
        }

        // initialize the bookmarks
        clearBookmarks();
        Element pages = m_document.getRootElement();
        try {
            for (Iterator i = pages.elementIterator(NODE_PAGE); i.hasNext();) {

                Element page = (Element)i.next();
                Locale locale = CmsLocaleManager.getLocale(page.attributeValue(ATTRIBUTE_LANGUAGE));
                for (Iterator j = page.elementIterator(NODE_ELEMENT); j.hasNext();) {

                    Element element = (Element)j.next();
                    String name = element.attributeValue(ATTRIBUTE_NAME);

                    String elementEnabled = element.attributeValue(ATTRIBUTE_ENABLED);
                    boolean enabled = (elementEnabled == null) ? true : Boolean.valueOf(elementEnabled).booleanValue();

                    // create an element type from the XML node                    
                    CmsXmlHtmlValue value = new CmsXmlHtmlValue(this, element, locale);
                    value.setContentDefinition(definition);

                    // add the element type bookmark
                    addBookmark(CmsXmlUtils.createXpathElement(name, 1), locale, enabled, value);
                }
                addLocale(locale);
            }
        } catch (NullPointerException e) {
            LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_PAGE_INIT_BOOKMARKS_0), e);
        }
    }

    /**
     * Sets the parameter that controls the relative link generation.<p>
     * 
     * @param value the parameter that controls the relative link generation
     */
    protected void setAllowRelativeLinks(boolean value) {

        m_allowRelativeLinks = value;
    }

    /**
     * Sets the file this XML page content is written to.<p> 
     * 
     * @param file the file this XML page content is written to
     */
    protected void setFile(CmsFile file) {

        m_file = file;
    }

    /**
     * Converts the XML structure of the pre 5.5.0 development version of 
     * the XML page to the final 6.0 version.<p>
     */
    private void convertOldDocument() {

        Document newDocument = DocumentHelper.createDocument();
        Element root = newDocument.addElement(NODE_PAGES);
        root.add(I_CmsXmlSchemaType.XSI_NAMESPACE);
        root.addAttribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION, XMLPAGE_XSD_SYSTEM_ID);

        Map pages = new HashMap();

        for (Iterator i = m_document.getRootElement().element(NODE_ELEMENTS).elementIterator(NODE_ELEMENT); i.hasNext();) {

            Element elem = (Element)i.next();
            try {
                String elementName = elem.attributeValue(ATTRIBUTE_NAME);
                String elementLang = elem.attributeValue(ATTRIBUTE_LANGUAGE);
                String elementEnabled = elem.attributeValue(ATTRIBUTE_ENABLED);
                boolean enabled = (elementEnabled == null) ? true : Boolean.valueOf(elementEnabled).booleanValue();

                Element page = (Element)pages.get(elementLang);
                if (page == null) {
                    // no page available for the language, add one
                    page = root.addElement(NODE_PAGE).addAttribute(ATTRIBUTE_LANGUAGE, elementLang);
                    pages.put(elementLang, page);
                }

                Element newElement = page.addElement(NODE_ELEMENT).addAttribute(ATTRIBUTE_NAME, elementName);
                if (!enabled) {
                    newElement.addAttribute(ATTRIBUTE_ENABLED, String.valueOf(enabled));
                }
                Element links = elem.element(NODE_LINKS);
                if (links != null) {
                    newElement.add(links.createCopy());
                }
                Element content = elem.element(NODE_CONTENT);
                if (content != null) {
                    newElement.add(content.createCopy());
                }

            } catch (NullPointerException e) {
                LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_PAGE_CONVERT_CONTENT_0), e);
            }
        }

        // now replace the old with the new document
        m_document = newDocument;
    }
}

⌨️ 快捷键说明

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