cmsresourcewrapperpropertyfile.java

来自「找了很久才找到到源代码」· Java 代码 · 共 502 行 · 第 1/2 页

JAVA
502
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/file/wrapper/CmsResourceWrapperPropertyFile.java,v $
 * Date   : $Date: 2007-08-13 16:29:50 $
 * Version: $Revision: 1.5 $
 *
 * 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.file.wrapper;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsVfsResourceAlreadyExistsException;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.file.CmsResource.CmsResourceDeleteMode;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalArgumentException;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

/**
 * Adds a folder in every existing folder with the name "__properties" which
 * contains property files for every resource in the existing folder.<p>
 * 
 * Empty folders don't have the property folder visible.<p>
 * 
 * The names of the property files are the same as the resource they belong to
 * with the extension "properties". To keep the correct sorting the names of
 * folders gets additionaly the prefix "__" to keep them at the beginning of the
 * list.<p>
 * 
 * When creating new folders, the property folder gets visible after a time period
 * of 60 seconds. For new resources the property file appears after that period too.
 * In this time period it is possible to create the property folder and the property
 * files manually. The properties in the created property files will be set at the
 * resource they belong to.<p>
 * 
 * @author Peter Bonrad
 * 
 * @version $Revision: 1.5 $
 * 
 * @since 6.5.6
 */
public class CmsResourceWrapperPropertyFile extends A_CmsResourceWrapper {

    /** The prefix for folders to keep correct sorting. */
    private static final String FOLDER_PREFIX = "__";

    /** The name to use for the folder where all property files are listed in. */
    private static final String PROPERTY_DIR = "__properties";

    /** The time in seconds to wait till the properties (and the property folder) are visible. */
    private static final int TIME_DELAY = 60;

    /** Table with the states of the virtual files. */
    private static final Hashtable TMP_FILE_TABLE = new Hashtable();

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#addResourcesToFolder(CmsObject, String, CmsResourceFilter)
     */
    public List addResourcesToFolder(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException {

        String path = resourcename;
        if (!path.endsWith("/")) {
            path += "/";
        }

        if (path.endsWith(PROPERTY_DIR + "/")) {

            String parent = CmsResource.getParentFolder(path);
            List ret = new ArrayList();

            // Iterate through all existing resources
            List resources = cms.getResourcesInFolder(parent, filter);
            Iterator iter = resources.iterator();
            while (iter.hasNext()) {
                CmsResource res = (CmsResource)iter.next();

                // check "existance" of resource
                if (existsResource(res)) {

                    // add the generated property file
                    ret.add(CmsResourceWrapperUtils.createPropertyFile(cms, res, getPropertyFileName(res)));
                }
            }

            return ret;
        } else {

            try {
                CmsResource folder = cms.readResource(resourcename);
                if (folder.isFolder()) {

                    // check if folder is empty
                    if (!cms.getResourcesInFolder(resourcename, CmsResourceFilter.DEFAULT).isEmpty()) {

                        // check "existance" of folder
                        if (existsResource(folder)) {
                            List ret = new ArrayList();

                            CmsWrappedResource wrap = new CmsWrappedResource(folder);
                            wrap.setRootPath(folder.getRootPath() + PROPERTY_DIR + "/");

                            ret.add(wrap.getResource());
                            return ret;
                        }
                    }
                }
            } catch (CmsVfsResourceNotFoundException ex) {
                return null;
            }
        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#createResource(org.opencms.file.CmsObject, java.lang.String, int, byte[], java.util.List)
     */
    public CmsResource createResource(CmsObject cms, String resourcename, int type, byte[] content, List properties)
    throws CmsException, CmsIllegalArgumentException {

        CmsResource res = getResource(cms, resourcename, CmsResourceFilter.DEFAULT);
        if (res != null) {

            // cut off trailing slash
            if (resourcename.endsWith("/")) {
                resourcename = resourcename.substring(0, resourcename.length() - 1);
            }

            // check "existance" of resource
            if (existsResource(res)) {

                throw new CmsVfsResourceAlreadyExistsException(org.opencms.db.generic.Messages.get().container(
                    org.opencms.db.generic.Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
                    resourcename));
            }

            // mark file as created in tmp file table
            TMP_FILE_TABLE.put(res.getRootPath(), new Integer(1));

            // lock the resource because this is the expected behaviour
            cms.lockResource(cms.getRequestContext().removeSiteRoot(res.getRootPath()));

            if (resourcename.endsWith(PROPERTY_DIR)) {

                CmsWrappedResource wrap = new CmsWrappedResource(res);
                wrap.setRootPath(res.getRootPath() + PROPERTY_DIR + "/");
                wrap.setFolder(true);
                return wrap.getResource();
            } else if (resourcename.endsWith(CmsResourceWrapperUtils.EXTENSION_PROPERTIES)) {

                CmsResourceWrapperUtils.writePropertyFile(
                    cms,
                    cms.getRequestContext().removeSiteRoot(res.getRootPath()),
                    content);
                return res;
            }

        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#deleteResource(org.opencms.file.CmsObject, java.lang.String, org.opencms.file.CmsResource.CmsResourceDeleteMode)
     */
    public boolean deleteResource(CmsObject cms, String resourcename, CmsResourceDeleteMode siblingMode)
    throws CmsException {

        CmsResource res = getResource(cms, resourcename, CmsResourceFilter.DEFAULT);
        if (res != null) {
            TMP_FILE_TABLE.remove(res.getRootPath());
            return true;
        }

        return false;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#getLock(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
     */
    public CmsLock getLock(CmsObject cms, CmsResource resource) throws CmsException {

        CmsResource org = cms.readResource(resource.getStructureId());
        //CmsResource org = getResource(cms, resource.getRootPath(), CmsResourceFilter.DEFAULT);
        if (org != null) {

            return cms.getLock(org);
        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.I_CmsResourceWrapper#isWrappedResource(org.opencms.file.CmsObject, org.opencms.file.CmsResource)
     */
    public boolean isWrappedResource(CmsObject cms, CmsResource res) {

        String path = res.getRootPath();
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }

        if (path.endsWith(PROPERTY_DIR)) {
            return true;
        }

        return false;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#lockResource(org.opencms.file.CmsObject, java.lang.String)
     */
    public boolean lockResource(CmsObject cms, String resourcename) throws CmsException {

        CmsResource res = getResource(cms, resourcename, CmsResourceFilter.DEFAULT);
        if (res != null) {
            cms.lockResource(cms.getRequestContext().removeSiteRoot(res.getRootPath()));
            return true;
        }

⌨️ 快捷键说明

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