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

📄 cmslink.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/relations/CmsLink.java,v $
 * Date   : $Date: 2007-08-31 16:08:14 $
 * Version: $Revision: 1.6 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 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.relations;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.wrapper.CmsObjectWrapper;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.staticexport.CmsLinkProcessor;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.util.CmsUriSplitter;

import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;

import org.dom4j.Attribute;
import org.dom4j.Element;

/**
 * A single link entry in the link table.<p>
 * 
 * @author Carsten Weinholz
 * @author Michael Moossen 
 * 
 * @version $Revision: 1.6 $ 
 * 
 * @since 6.0.0 
 */
public class CmsLink {

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

    /** 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";

    /** Default link name. */
    public static final String DEFAULT_NAME = "ref";

    /** Default link type. */
    public static final CmsRelationType DEFAULT_TYPE = CmsRelationType.XML_WEAK;

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

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

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

    /** Name of the UUID node. */
    public static final String NODE_UUID = "uuid";

    /** Constant for the NULL link. */
    public static final CmsLink NULL_LINK = new CmsLink();

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

    /** The anchor of the URI, if any. */
    private String m_anchor;

    /** The XML element reference. */
    private Element m_element;

    /** Indicates if the link is an internal link within the OpenCms VFS. */
    private boolean m_internal;

    /** The internal name of the link. */
    private String m_name;

    /** The parameters of the query , if any. */
    private Map m_parameters;

    /** The query, if any. */
    private String m_query;

    /** The site root of the (internal) link. */
    private String m_siteRoot;

    /** The structure id of the linked resource. */
    private CmsUUID m_structureId;

    /** The link target (destination). */
    private String m_target;

    /** The type of the link. */
    private CmsRelationType m_type;

    /** The raw uri. */
    private String m_uri;

    /**
     * Reconstructs a link object from the given XML node.<p>
     * 
     * @param element the XML node containing the link information
     */
    public CmsLink(Element element) {

        m_element = element;
        Attribute attrName = element.attribute(ATTRIBUTE_NAME);
        if (attrName != null) {
            m_name = attrName.getValue();
        } else {
            m_name = DEFAULT_NAME;
        }
        Attribute attrType = element.attribute(ATTRIBUTE_TYPE);
        if (attrType != null) {
            m_type = CmsRelationType.valueOfXml(attrType.getValue());
        } else {
            m_type = DEFAULT_TYPE;
        }
        Attribute attrInternal = element.attribute(ATTRIBUTE_INTERNAL);
        if (attrInternal != null) {
            m_internal = Boolean.valueOf(attrInternal.getValue()).booleanValue();
        } else {
            m_internal = true;
        }

        Element uuid = element.element(NODE_UUID);
        Element target = element.element(NODE_TARGET);
        Element anchor = element.element(NODE_ANCHOR);
        Element query = element.element(NODE_QUERY);

        m_structureId = (uuid != null) ? new CmsUUID(uuid.getText()) : null;
        m_target = (target != null) ? target.getText() : null;
        m_anchor = (anchor != null) ? anchor.getText() : null;
        setQuery((query != null) ? query.getText() : null);

        // update the uri from the components
        setUri();
    }

    /**
     * Creates a new link object without a reference to the xml page link element.<p>
     * 
     * @param name the internal name of this link
     * @param type the type of this link
     * @param structureId the structure id of the link
     * @param uri the link uri
     * @param internal indicates if the link is internal within OpenCms 
     */
    public CmsLink(String name, CmsRelationType type, CmsUUID structureId, String uri, boolean internal) {

        m_element = null;
        m_name = name;
        m_type = type;
        m_internal = internal;
        m_structureId = structureId;
        m_uri = uri;
        // update component members from the uri
        setComponents();
    }

    /**
     * Creates a new link object without a reference to the xml page link element.<p>
     * 
     * @param name the internal name of this link
     * @param type the type of this link
     * @param uri the link uri
     * @param internal indicates if the link is internal within OpenCms 
     */
    public CmsLink(String name, CmsRelationType type, String uri, boolean internal) {

        this(name, type, null, uri, internal);
    }

    /**
     *  Empty constructor for NULL constant.<p>
     */
    private CmsLink() {

        // empty constructor for NULL constant
    }

    /**
     * Checks and updates the structure id or the path of the target.<p>  
     * 
     * @param cms the cms context
     */
    public void checkConsistency(CmsObject cms) {

        if (!m_internal || (cms == null)) {
            return;
        }
        try {
            if (m_structureId == null) {
                // try by path
                throw new CmsException(Messages.get().container(Messages.LOG_BROKEN_LINK_NO_ID_0));
            }
            // first look for the resource with the given structure id
            CmsResource res = cms.readResource(m_structureId, CmsResourceFilter.ALL);
            if (!res.getRootPath().equals(m_target)) {
                // update path if needed
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(
                        Messages.LOG_BROKEN_LINK_UPDATED_BY_ID_3,
                        m_structureId,
                        m_target,
                        res.getRootPath()));
                }
                // set the new target
                m_target = res.getRootPath();
                setUri();
                // update xml node
                CmsLinkUpdateUtil.updateXml(this, m_element, true);
            }
        } catch (CmsException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_BROKEN_LINK_BY_ID_2, m_target, m_structureId), e);
            }
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_target)) {
                // no correction is possible
                return;
            }
            // go on with the resource with the given path
            String siteRoot = cms.getRequestContext().getSiteRoot();
            try {
                cms.getRequestContext().setSiteRoot("");
                // now look for the resource with the given path
                CmsResource res = cms.readResource(m_target, CmsResourceFilter.ALL);
                if (!res.getStructureId().equals(m_structureId)) {
                    // update structure id if needed
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(Messages.get().getBundle().key(
                            Messages.LOG_BROKEN_LINK_UPDATED_BY_NAME_3,
                            m_target,
                            m_structureId,
                            res.getStructureId()));
                    }
                    m_target = res.getRootPath(); // could change by a translation rule
                    m_structureId = res.getStructureId();
                    CmsLinkUpdateUtil.updateXml(this, m_element, true);
                }
            } catch (CmsException e1) {
                // no correction was possible
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_BROKEN_LINK_BY_NAME_1, m_target), e1);
                }
                m_structureId = null;
            } finally {
                cms.getRequestContext().setSiteRoot(siteRoot);
            }
        }
    }

    /**
     * A link is considered equal if the link target and the link type is equal.<p>
     *  
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {

        if (obj == this) {
            return true;
        }
        if (obj instanceof CmsLink) {
            CmsLink other = (CmsLink)obj;
            return (m_type == other.m_type) && CmsStringUtil.isEqual(m_target, other.m_target);
        }
        return false;
    }

    /**
     * Returns the anchor of this link.<p>
     * 
     * @return the anchor or null if undefined
     */
    public String getAnchor() {

        return m_anchor;
    }

    /**
     * Returns the xml node element representing this link object.<p>
     * 
     * @return the xml node element representing this link object
     */
    public Element getElement() {

        return m_element;
    }

    /**
     * Returns the processed link.<p>
     * 
     * @param cms the current OpenCms user context, can be <code>null</code>
     * 
     * @return the processed link
     */
    public String getLink(CmsObject cms) {

        if (m_internal) {

⌨️ 快捷键说明

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