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

📄 cmsresourcetypepage.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }

    /**
    * Moves a resource to the given destination.
    *
    * @param source the complete path of the sourcefile.
    * @param destination the complete path of the destinationfile.
    *
    * @exception CmsException if the user has not the rights to move this resource,
    * or if the file couldn't be moved.
    */
    public void moveResource(CmsObject cms, String source, String destination) throws CmsException{
        CmsFile file = cms.readFile(source);
        String bodyPath = checkBodyPath(cms, file);
        if(bodyPath != null) {
            String hbodyPath = C_CONTENTBODYPATH.substring(0, C_CONTENTBODYPATH.lastIndexOf("/")) + destination;
            checkFolders(cms, destination.substring(0, destination.lastIndexOf("/")));
            cms.doMoveFile(bodyPath, hbodyPath);
            changeContent(cms, source, hbodyPath);
        }
        cms.doMoveFile(source, destination);
    }

    /**
    * Renames the file to the new name.
    *
    * @param oldname the complete path to the file which will be renamed.
    * @param newname the new name of the file.
    *
    * @exception CmsException if the user has not the rights
    * to rename the file, or if the file couldn't be renamed.
    */
    public void renameResource(CmsObject cms, String oldname, String newname) throws CmsException{
        CmsFile file = cms.readFile(oldname);
        String bodyPath = readBodyPath(cms, file);
        int help = C_CONTENTBODYPATH.lastIndexOf("/");
        String hbodyPath=(C_CONTENTBODYPATH.substring(0,help)) + oldname;
        if(hbodyPath.equals(bodyPath)) {
            cms.doRenameFile(bodyPath, newname);
            help=bodyPath.lastIndexOf("/") + 1;
            hbodyPath = bodyPath.substring(0,help) + newname;
            changeContent(cms, oldname, hbodyPath);
        }

        cms.doRenameFile(oldname,newname);
    }

    /**
     * Restores a file in the current project with a version in the backup
     *
     * @param cms The CmsObject
     * @param versionId The version id of the resource
     * @param filename The name of the file to restore
     *
     * @exception CmsException  Throws CmsException if operation was not succesful.
     */
    public void restoreResource(CmsObject cms, int versionId, String filename) throws CmsException{
        if(!cms.accessWrite(filename)){
            throw new CmsException(filename, CmsException.C_NO_ACCESS);
        }
        CmsFile file = cms.readFile(filename);
        cms.doRestoreResource(versionId, filename);
        String bodyPath = checkBodyPath(cms, (CmsFile)file);
        if (bodyPath != null){
            try{
                cms.doRestoreResource(versionId, bodyPath);
            } catch(CmsException e){
                // do not throw an exception when there is no body for this version
                // maybe only the control file was changed
                if(e.getType() == CmsException.C_NOT_FOUND){
                    if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                        A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO,"[CmsResourceTypePage] version "+versionId+" of "+bodyPath+" not found!");
                    }
                } else {
                    throw e;
                }
            }
        }
    }

    /**
    * Undo changes in a resource.
    * <br>
    *
    * @param resource the complete path to the resource to be restored.
    *
    * @exception CmsException if the user has not the rights
    * to write this resource.
    */
    public void undoChanges(CmsObject cms, String resource) throws CmsException{
        if(!cms.accessWrite(resource)){
            throw new CmsException(resource, CmsException.C_NO_ACCESS);
        }
        CmsFile file = cms.readFile(resource);
        cms.doUndoChanges(resource);
        String bodyPath = checkBodyPath(cms, (CmsFile)file);
        if (bodyPath != null){
            cms.doUndoChanges(bodyPath);
        }
    }

    /**
    * Unlocks a resource.
    * <br>
    * A user can unlock a resource, so other users may lock this file.
    *
    * @param resource the complete path to the resource to be unlocked.
    *
    * @exception CmsException if the user has not the rights
    * to unlock this resource.
    */
    public void unlockResource(CmsObject cms, String resource) throws CmsException{
        // First read the page file.
        CmsFile pageFile = cms.readFile(resource);

        CmsUser pageLocker = null;
        CmsUser bodyLocker = null;

        // Check any locks on th page file
        pageLocker = getLockedBy(cms, resource);
        CmsUser currentUser = cms.getRequestContext().currentUser();

        CmsResource bodyFile = null;
        String bodyPath = null;
        // Try to fetch the body file.
        try {
            bodyPath = readBodyPath(cms, pageFile);
            bodyFile = cms.readFileHeader(bodyPath);
        } catch(Exception e) {
            bodyPath = null;
            bodyFile = null;
        }

        cms.doUnlockResource(resource);

        if(bodyFile != null) {
            // Everything with the page file is ok. We have write access. XML is valid.
            // Body file could be determined and fetched.
            // Now check further body file details (is it locked already, WHO has locked it, etc.)
            bodyLocker = getLockedBy(cms, bodyPath);
            // Unlock the body, if neccessary
            //if((pageLocker == null || pageLocker.equals(currentUser)) && (bodyLocker != null)) {
                cms.doUnlockResource(bodyPath);
            //}
        }

        // Unlock the page file, if neccessary
        //if(pageLocker != null || bodyLocker == null) {
        //cms.doUnlockResource(resource);
        //}
    }

    /**
     * method to check get the real body path from the content file
     *
     * @param cms The CmsObject, to access the XML read file.
     * @param file File in which the body path is stored. This should really
     *      be a CmsFile object an not a file header. This won't be checked for
     *      performance reasons.
     */
    private String readBodyPath(CmsObject cms, CmsFile file)
        throws CmsException{
        CmsXmlControlFile hXml=new CmsXmlControlFile(cms, file);
        String body = "";
        try{
            body = hXml.getElementTemplate("body");
        } catch (CmsException exc){
            // could not read body
        }
        return body;
    }

    /**
     * method to check get the real body path from the content file
     *
     * @param cms The CmsObject, to access the XML read file.
     * @param file File in which the body path is stored.
     */
    private String checkBodyPath(CmsObject cms, CmsFile file) throws CmsException {
        String result =(C_CONTENTBODYPATH.substring(0, C_CONTENTBODYPATH.lastIndexOf("/")))+(file.getAbsolutePath());
        if (!result.equals(readBodyPath(cms, (CmsFile)file))){
            result = null;
        }
        return result;
    }

    private CmsUser getLockedBy(CmsObject cms, String filename) {
        CmsUser result = null;
        try {
            result = cms.lockedBy(filename);
            if(result.getId() == -1) {
                result = null;
            }
        } catch(Exception e) {
            result = null;
        }
        return result;
    }

      /**
       * This method changes the path of the body file in the xml conten file
       * if file type name is page
       *
       * @param cms The CmsObject
       * @param file The XML content file
       * @param bodypath the new XML content entry
       * @exception Exception if something goes wrong.
       */
      private void changeContent(CmsObject cms, String filename, String bodypath)
          throws CmsException {
          CmsFile file=cms.readFile(filename);
          CmsXmlControlFile hXml=new CmsXmlControlFile(cms, file);
          hXml.setElementTemplate("body", bodypath);
          hXml.write();
      }

    /**
       * This method checks if all nescessary folders are exisitng in the content body
       * folder and creates the missing ones. <br>
       * All page contents files are stored in the content body folder in a mirrored directory
       * structure of the OpenCms filesystem. Therefor it is nescessary to create the
       * missing folders when a new page document is createg.
       * @param cms The CmsObject
       * @param path The path in the CmsFilesystem where the new page should be created.
       * @exception CmsException if something goes wrong.
       */
     private void checkFolders(CmsObject cms, String path)
          throws CmsException {

          String completePath=C_CONTENTBODYPATH;
          StringTokenizer t=new StringTokenizer(path,"/");
          String correspFolder = "/";
          // check if all folders are there
          while (t.hasMoreTokens()) {
              String foldername=t.nextToken();
              correspFolder = correspFolder+foldername+"/";
               try {
                // try to read the folder. if this fails, an exception is thrown

                cms.readFolder(completePath+foldername+"/");
              } catch (CmsException e) {
                  // the folder could not be read, so create it.
                  String orgFolder=completePath+foldername+"/";
                  orgFolder=orgFolder.substring(C_CONTENTBODYPATH.length()-1);
                  CmsFolder newfolder=cms.doCreateFolder(completePath,foldername);
                  CmsFolder folder=cms.readFolder(orgFolder);
                  cms.doLockResource(newfolder.getAbsolutePath(),false);
                  cms.doChgrp(newfolder.getAbsolutePath(),cms.readGroup(folder).getName());
                  cms.doChmod(newfolder.getAbsolutePath(),folder.getAccessFlags());
                  cms.doChown(newfolder.getAbsolutePath(),cms.readOwner(folder).getName());
                  try{
                    CmsFolder correspondingFolder = cms.readFolder(correspFolder);
                    if(!correspondingFolder.isLocked()){
                        cms.doUnlockResource(newfolder.getAbsolutePath());
                    }
                  } catch (CmsException ex){
                    // unable to unlock folder if parent folder is locked
                  }
              }
              completePath+=foldername+"/";
          }
     }

    /**
     * Set the access flags of the copied resource to the default values.
     * @param cms The CmsObject.
     * @param filename The name of the file.
     * @exception Throws CmsException if something goes wrong.
     */
    private void setDefaultFlags(CmsObject cms, String filename)
        throws CmsException {

        Hashtable startSettings=null;
        Integer accessFlags=null;
        startSettings=(Hashtable)cms.getRequestContext().currentUser().getAdditionalInfo(C_ADDITIONAL_INFO_STARTSETTINGS);
        if (startSettings != null) {
            accessFlags=(Integer)startSettings.get(C_START_ACCESSFLAGS);
        }
        if (accessFlags == null) {
            accessFlags = new Integer(C_ACCESS_DEFAULT_FLAGS);
        }
        chmod(cms, filename, accessFlags.intValue(), false);
    }

    /**
     * Changes the project-id of the resource to the new project
     * for publishing the resource directly
     *
     * @param newProjectId The Id of the new project
     * @param resourcename The name of the resource to change
     */
    public void changeLockedInProject(CmsObject cms, int newProjectId, String resourcename)
        throws CmsException{
        CmsFile file = cms.readFile(resourcename, true);
        cms.doChangeLockedInProject(newProjectId, resourcename);
        String bodyPath = checkBodyPath(cms, (CmsFile)file);
        if (bodyPath != null){
            cms.doChangeLockedInProject(newProjectId, bodyPath);
        }

        // The page file contains XML.
        // So there could be some data in the parser's cache.
        // Clear it!
        String currentProject = cms.getRequestContext().currentProject().getName();
        CmsXmlControlFile.clearFileCache(currentProject + ":" + resourcename);
    }
}

⌨️ 快捷键说明

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