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

📄 cmsxmlpage.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/page/CmsXmlPage.java,v $
 * Date   : $Date: 2006/07/20 13:51:41 $
 * Version: $Revision: 1.34 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.xml.page;

import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.CmsLog;
import org.opencms.main.CmsRuntimeException;
import org.opencms.staticexport.CmsLinkProcessor;
import org.opencms.staticexport.CmsLinkTable;
import org.opencms.xml.A_CmsXmlDocument;
import org.opencms.xml.CmsXmlContentDefinition;
import org.opencms.xml.CmsXmlEntityResolver;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;
import org.opencms.xml.content.CmsXmlContentErrorHandler;
import org.opencms.xml.types.CmsXmlHtmlValue;
import org.opencms.xml.types.I_CmsXmlContentValue;
import org.opencms.xml.types.I_CmsXmlSchemaType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.InputSource;

/**
 * Implementation of a page object used to access and manage xml data.<p>
 * 
 * This implementation consists of several named elements optionally available for 
 * various languages. The data of each element is accessible via its name and language. 
 * 
 * The content of each element is stored as CDATA, links within the 
 * content are processed and are seperately accessible as entries of a CmsLinkTable.
 * 
 * @author Carsten Weinholz 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.34 $ 
 * 
 * @since 6.0.0 
 */
public class CmsXmlPage extends A_CmsXmlDocument {

    /** Name of the name attribute of the elements node. */
    public static final String ATTRIBUTE_ENABLED = "enabled";

    /** Name of the internal attribute of the link node. */
    public static final String ATTRIBUTE_INTERNAL = "internal";

    /** Name of the language attribute of the elements node. */
    public static final String ATTRIBUTE_LANGUAGE = "language";

    /** Name of the name attribute of the elements node. */
    public static final String ATTRIBUTE_NAME = "name";

    /** Name of the type attribute of the elements node. */
    public static final String ATTRIBUTE_TYPE = "type";

    /** Name of the anchor node. */
    public static final String NODE_ANCHOR = "anchor";

    /** Name of the element node. */
    public static final String NODE_CONTENT = "content";

    /** Name of the elements node. */
    public static final String NODE_ELEMENTS = "elements";

    /** Name of the link node. */
    public static final String NODE_LINK = "link";

    /** Name of the links node. */
    public static final String NODE_LINKS = "links";

    /** Name of the page node. */
    public static final String NODE_PAGE = "page";

    /** Name of the page node. */
    public static final String NODE_PAGES = "pages";

    /** Name of the query node. */
    public static final String NODE_QUERY = "query";

    /** Name of the target node. */
    public static final String NODE_TARGET = "target";

    /** Property to check if relative links are allowed. */
    public static final String PROPERTY_ALLOW_RELATIVE = "allowRelativeLinks";

    /** The DTD address of the OpenCms xmlpage. */
    public static final String XMLPAGE_XSD_SYSTEM_ID = CmsConfigurationManager.DEFAULT_DTD_PREFIX + "xmlpage.xsd";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsXmlPage.class);

    /** The XML page content definition is static. */
    private static CmsXmlContentDefinition m_xmlPageContentDefinition;

    /** Name of the element node. */
    private static final String NODE_ELEMENT = "element";

    /** Indicates if relative Links are allowed. */
    private boolean m_allowRelativeLinks;

    /**
     * Creates a new CmsXmlPage based on the provided document and encoding.<p>
     * 
     * The encoding is used for marshalling the XML document later.<p>
     * 
     * @param document the document to create the CmsXmlPage from
     * @param encoding the encoding of the xml page
     */
    public CmsXmlPage(Document document, String encoding) {

        initDocument(document, encoding, getContentDefinition());
    }

    /**
     * Creates an empty XML page in the provided locale using 
     * the provided encoding.<p>
     * 
     * The page is initialized according to the minimal neccessary xml structure.
     * The encoding is used for marshalling the XML document later.<p>
     * 
     * @param locale the initial locale of the XML page
     * @param encoding the encoding of the XML page
     */
    public CmsXmlPage(Locale locale, String encoding) {

        initDocument(CmsXmlPageFactory.createDocument(locale), encoding, getContentDefinition());
    }

    /**
     * @see org.opencms.xml.I_CmsXmlDocument#addLocale(org.opencms.file.CmsObject, java.util.Locale)
     */
    public void addLocale(CmsObject cms, Locale locale) throws CmsXmlException {

        if (hasLocale(locale)) {
            throw new CmsXmlException(Messages.get().container(Messages.ERR_XML_PAGE_LOCALE_EXISTS_1, locale));
        }
        // add element node for Locale
        getContentDefinition().createLocale(cms, this, m_document.getRootElement(), locale);
        // re-initialize the bookmarks
        initDocument(m_document, m_encoding, getContentDefinition());
    }

    /**
     * Adds a new, empty value with the given name and locale
     * to this XML document.<p>
     * 
     * @param name the name of the value
     * @param locale the locale of the value
     *  
     * @throws CmsIllegalArgumentException if the name contains an index ("[&lt;number&gt;]") or the value for the 
     *         given locale already exists in the xmlpage.
     *     
     */
    public void addValue(String name, Locale locale) throws CmsIllegalArgumentException {

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

        if (hasValue(name, locale)) {
            throw new CmsIllegalArgumentException(Messages.get().container(
                Messages.ERR_XML_PAGE_LANG_ELEM_EXISTS_2,
                name,
                locale));
        }

        Element pages = m_document.getRootElement();
        String localeStr = locale.toString();
        Element page = null;

        // search if a page for the selected language is already available
        for (Iterator i = pages.elementIterator(NODE_PAGE); i.hasNext();) {
            Element nextPage = (Element)i.next();
            String language = nextPage.attributeValue(ATTRIBUTE_LANGUAGE);
            if (localeStr.equals(language)) {
                // a page for the selected language was found
                page = nextPage;
                break;
            }
        }

        // create the new element
        Element element;
        if (page != null) {
            // page for selected language already available
            element = page.addElement(NODE_ELEMENT).addAttribute(ATTRIBUTE_NAME, name);
        } else {
            // no page for the selected language was found
            element = pages.addElement(NODE_PAGE).addAttribute(ATTRIBUTE_LANGUAGE, localeStr);
            element = element.addElement(NODE_ELEMENT).addAttribute(ATTRIBUTE_NAME, name);
        }

        // add empty nodes for link table and content to the element
        element.addElement(NODE_LINKS);
        element.addElement(NODE_CONTENT);

        CmsXmlHtmlValue value = new CmsXmlHtmlValue(this, element, locale);

        // bookmark the element
        addBookmark(CmsXmlUtils.createXpathElement(name, 1), locale, true, value);
    }

    /**
     * Returns if relative links are accepted (and left unprocessed).<p>
     * 
     * @return true if relative links are allowed
     */
    public boolean getAllowRelativeLinks() {

        return m_allowRelativeLinks;
    }

    /**
     * @see org.opencms.xml.I_CmsXmlDocument#getContentDefinition()
     */
    public CmsXmlContentDefinition getContentDefinition() throws CmsRuntimeException {

        if (m_xmlPageContentDefinition == null) {
            // since XML page schema is cached anyway we don't need an CmsObject instance
            CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);
            InputSource source;
            try {
                source = resolver.resolveEntity(null, XMLPAGE_XSD_SYSTEM_ID);
                // store content definition in static variable
                m_xmlPageContentDefinition = CmsXmlContentDefinition.unmarshal(source, XMLPAGE_XSD_SYSTEM_ID, resolver);
            } catch (CmsXmlException e) {
                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_XML_PAGE_UNMARSHAL_CONTENDDEF_0), e);
            }
        }
        return m_xmlPageContentDefinition;
    }

    /**
     * @see org.opencms.xml.A_CmsXmlDocument#getLinkProcessor(org.opencms.file.CmsObject, org.opencms.staticexport.CmsLinkTable)
     */
    public CmsLinkProcessor getLinkProcessor(CmsObject cms, CmsLinkTable linkTable) {

        // initialize link processor
        String relativeRoot = null;
        if ((!m_allowRelativeLinks) && (m_file != null)) {
            relativeRoot = CmsResource.getParentFolder(cms.getSitePath(m_file));
        }
        return new CmsLinkProcessor(cms, linkTable, getEncoding(), relativeRoot);
    }

⌨️ 快捷键说明

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