cmsresourcewrapperpropertyfile.java

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

JAVA
502
字号

        return false;
    }

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

        if (!resourcename.endsWith(PROPERTY_DIR)) {
            CmsResource res = getResource(cms, resourcename, filter);
            if (res != null) {

                // Workaround for Dreamweaver:
                // Dreamweaver copies folders through creating folder for folder and file for file.
                // So there is first a call if the property folder already exists and afterwards create
                // it. If the folder already exists, the copy action fails.
                // In the first time after creating a folder, the property dir does not exists until it is
                // created or the time expired.
                if (!existsResource(res)) {
                    return null;
                }

                return CmsResourceWrapperUtils.createPropertyFile(cms, res, getPropertyFileName(res));
            }
        }

        return null;
    }

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

        CmsResource res = getResource(cms, resourcename, filter);
        if (res != null) {

            // Workaround for Dreamweaver:
            // Dreamweaver copies folders through creating folder for folder and file for file.
            // So there is first a call if the property folder already exists and afterwards create
            // it. If the folder already exists, the copy action fails.
            // In the first time after creating a folder, the property dir does not exists until it is
            // created or the time expired.
            if (!existsResource(res)) {
                return null;
            }

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

            // create property file and return the resource for it
            if (!resourcename.endsWith(PROPERTY_DIR)) {
                return CmsResourceWrapperUtils.createPropertyFile(cms, res, getPropertyFileName(res));
            }

            // create a resource for the __property folder
            CmsWrappedResource wrap = new CmsWrappedResource(res);
            wrap.setRootPath(res.getRootPath() + PROPERTY_DIR);
            wrap.setFolder(true);
            return wrap.getResource();
        }

        return null;
    }

    /**
     * @see org.opencms.file.wrapper.A_CmsResourceWrapper#restoreLink(org.opencms.file.CmsObject, java.lang.String)
     */
    public String restoreLink(CmsObject cms, String uri) {

        try {
            CmsResource res = getResource(cms, uri, CmsResourceFilter.DEFAULT);
            if (res != null) {
                return res.getRootPath();
            }
        } catch (CmsException ex) {
            // noop
        }

        return null;
    }

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

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

        return false;
    }

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

        CmsResource res = cms.readResource(resource.getStructureId());
        //        CmsResource res = getResource(
        //            cms,
        //            cms.getRequestContext().removeSiteRoot(resource.getRootPath()),
        //            CmsResourceFilter.ALL);
        if (res != null) {
            CmsResourceWrapperUtils.writePropertyFile(
                cms,
                cms.getRequestContext().removeSiteRoot(res.getRootPath()),
                resource.getContents());
            return resource;
        }

        return null;
    }

    /**
     * Checks if a resource exists depending on the creation date and the temp files saved.<p>
     * 
     * Dreamweaver copies folders through creating folder for folder and file for file. 
     * So there is first a call if the property folder already exists and afterwards create 
     * it. If the folder already exists, the copy action fails.
     * In the first time after creating a folder, the property dir does not exists until it is
     * created or the time expired.<p>
     * 
     * @param res the resource to check if it exists
     * 
     * @return return if the folder exists otherwise false
     */
    private boolean existsResource(CmsResource res) {

        long now = new Date().getTime();
        long created = res.getDateCreated();
        long diff = (now - created) / 1000;

        if (diff <= TIME_DELAY) {

            // check tmp file table
            if (TMP_FILE_TABLE.containsKey(res.getRootPath())) {
                return true;
            }

            return false;
        } else {

            // remove from tmp file table
            TMP_FILE_TABLE.remove(res.getRootPath());
        }

        return true;
    }

    /**
     * Creates the full path to the property file of the given resource.<p>
     * 
     * @param res the resource where to create the path for the property file for
     * 
     * @return the full path to the property file of the resource
     */
    private String getPropertyFileName(CmsResource res) {

        StringBuffer ret = new StringBuffer();

        // path to the parent folder
        String parentFolder = CmsResource.getParentFolder(res.getRootPath());
        ret.append(parentFolder);

        // make sure ends with a slash
        if (!parentFolder.endsWith("/")) {
            ret.append("/");
        }

        // append name of the property folder
        ret.append(PROPERTY_DIR);
        ret.append("/");

        // if resource is a folder add the prefix "__"
        if (res.isFolder()) {
            ret.append(FOLDER_PREFIX);
        }

        // append the name of the resource
        ret.append(res.getName());

        return ret.toString();
    }

    /**
     * Reads the resource for the property file.<p>
     * 
     * @param cms the initialized CmsObject
     * @param resourcename the name of the property resource
     * @param filter the filter to use
     * 
     * @return the resource for the property file or null if not found
     * 
     * @throws CmsException if something goes wrong
     */
    private CmsResource getResource(CmsObject cms, String resourcename, CmsResourceFilter filter) throws CmsException {

        // the path without trailing slash
        String path = CmsResource.getParentFolder(resourcename);
        if (path == null) {
            return null;
        }
        
        // the parent path
        String parent = CmsResource.getParentFolder(path);

        // the name of the resource
        String name = CmsResource.getName(resourcename);
        if (name.endsWith("/")) {
            name = name.substring(0, name.length() - 1);
        }

        // read the resource for the property dir
        if (name.equals(PROPERTY_DIR)) {

            return cms.readResource(path, filter);
        }

        if ((path.endsWith(PROPERTY_DIR + "/")) && (name.endsWith(CmsResourceWrapperUtils.EXTENSION_PROPERTIES))) {
            CmsResource res = null;

            if (name.startsWith(FOLDER_PREFIX)) {
                name = name.substring(2);
            }

            try {
                String resPath = CmsResourceWrapperUtils.removeFileExtension(
                    cms,
                    parent + name,
                    CmsResourceWrapperUtils.EXTENSION_PROPERTIES);

                res = cms.readResource(resPath, filter);
            } catch (CmsException ex) {
                // noop
            }

            return res;
        }

        return null;
    }

}

⌨️ 快捷键说明

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