Creates a folder and its parent folders if they don't exist.<p>

@param cms the CMS context to use
@param rootPath the folder root path

@throws CmsException if something goes wrong
767214
public static void createFolder(CmsObject cms, String rootPath) throws CmsException {

        CmsObject rootCms = OpenCms.initCmsObject(cms);
        rootCms.getRequestContext().setSiteRoot("");
        List<String> parents = new ArrayList<String>();
        String currentPath = rootPath;
        while (currentPath != null) {
            if (rootCms.existsResource(currentPath)) {
                break;
            }
            parents.add(currentPath);
            currentPath = CmsResource.getParentFolder(currentPath);
        }
        parents = Lists.reverse(parents);
        for (String parent : parents) {
            try {
                rootCms.createResource(
                    parent,
                    OpenCms.getResourceManager().getResourceType(CmsResourceTypeFolder.getStaticTypeName()));
                try {
                    rootCms.unlockResource(parent);
                } catch (CmsException e) {
                    // may happen if parent folder is locked also
                    if (LOG.isInfoEnabled()) {
                        LOG.info(e);
                    }
                }
            } catch (CmsVfsResourceAlreadyExistsException e) {
                // nop
            }
        }
    }
1003208
public static String getParentFolderTokens(String rootPath) {

        if (CmsStringUtil.isEmpty(rootPath)) {
            return "/";
        }
        StringBuffer result = new StringBuffer(128);
        String folderName = CmsResource.getFolderPath(rootPath);
        for (int i = 0; i < folderName.length(); i++) {
            char c = folderName.charAt(i);
            if (c == '/') {
                if (result.length() > 0) {
                    result.append(' ');
                }
                result.append(folderName.substring(0, i + 1));
            }
        }
        return result.toString();
    }
925319
public CmsInheritedContainerState getInheritedContainerState(CmsObject cms, String rootPath, String name)
    throws CmsException {

        String oldSiteRoot = cms.getRequestContext().getSiteRoot();
        try {
            cms.getRequestContext().setSiteRoot("");
            CmsResource resource = cms.readResource(rootPath);
            return getInheritedContainerState(cms, resource, name);
        } finally {
            cms.getRequestContext().setSiteRoot(oldSiteRoot);
        }
    }
430467
private CmsModule createModuleFolders(CmsObject cms, CmsModule module) throws CmsException {

        String modulePath = CmsWorkplace.VFS_PATH_MODULES + module.getName() + "/";
        List<CmsExportPoint> exportPoints = module.getExportPoints();
        List<String> resources = module.getResources();

        // set the createModuleFolder flag if any other flag is set
        if (module.isCreateClassesFolder()
            || module.isCreateElementsFolder()
            || module.isCreateI18NFolder()
            || module.isCreateLibFolder()
            || module.isCreateResourcesFolder()
            || module.isCreateSchemasFolder()
            || module.isCreateTemplateFolder()
            || module.isCreateFormattersFolder()) {
            module.setCreateModuleFolder(true);
        }

        Set<String> exportPointPaths = new HashSet<String>();
        for (CmsExportPoint exportPoint : exportPoints) {
            exportPointPaths.add(exportPoint.getUri());
        }

        // check if we have to create the module folder

        I_CmsResourceType folderType = OpenCms.getResourceManager().getResourceType(
            CmsResourceTypeFolder.getStaticTypeName());
        I_CmsResourceType configType = OpenCms.getResourceManager().getResourceType(CmsADEManager.MODULE_CONFIG_TYPE);

        if (module.isCreateModuleFolder()) {
            CmsResource resource = cms.createResource(modulePath, folderType);
            CmsResource configResource = cms.createResource(modulePath + CONFIG_FILE, configType);
            try {
                cms.unlockResource(resource);
                cms.unlockResource(configResource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
            // add the module folder to the resource list
            resources.add(modulePath);
            module.setResources(resources);
        }

        // check if we have to create the template folder
        if (module.isCreateTemplateFolder()) {
            String path = modulePath + PATH_TEMPLATES;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
        }

        if (module.isCreateI18NFolder()) {
            String path = modulePath + PATH_i18n;
            CmsResource resource = cms.createResource(path, folderType);
            CmsResource bundleResource = cms.createResource(
                path + module.getName() + SUFFIX_BUNDLE_FILE + "_" + CmsLocaleManager.getDefaultLocale(),
                OpenCms.getResourceManager().getResourceType(CmsVfsBundleManager.TYPE_PROPERTIES_BUNDLE),
                null,
                null);
            cms.writeResource(bundleResource);
            try {
                cms.unlockResource(resource);
                cms.unlockResource(bundleResource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
        }

        // check if we have to create the elements folder
        if (module.isCreateElementsFolder()) {
            String path = modulePath + PATH_ELEMENTS;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
        }

        if (module.isCreateFormattersFolder()) {
            String path = modulePath + PATH_FORMATTERS;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
        }

        // check if we have to create the schemas folder
        if (module.isCreateSchemasFolder()) {
            String path = modulePath + PATH_SCHEMAS;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
        }

        // check if we have to create the resources folder
        if (module.isCreateResourcesFolder()) {
            String path = modulePath + PATH_RESOURCES;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
        }

        // check if we have to create the lib folder
        if (module.isCreateLibFolder()) {
            String path = modulePath + PATH_LIB;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
            if (!exportPointPaths.contains(path)) {
                CmsExportPoint exp = new CmsExportPoint(path, "WEB-INF/lib/");
                exportPoints.add(exp);
            }
            module.setExportPoints(exportPoints);
        }

        // check if we have to create the classes folder
        if (module.isCreateClassesFolder()) {
            String path = modulePath + PATH_CLASSES;
            CmsResource resource = cms.createResource(path, folderType);
            try {
                cms.unlockResource(resource);
            } catch (CmsLockException locke) {
                LOG.warn("Unbale to unlock resource", locke);
            }
            if (!exportPointPaths.contains(path)) {
                CmsExportPoint exp = new CmsExportPoint(path, "WEB-INF/classes/");
                exportPoints.add(exp);
                module.setExportPoints(exportPoints);
            }

            // now create all subfolders for the package structure
            StringTokenizer tok = new StringTokenizer(m_module.getName(), ".");
            while (tok.hasMoreTokens()) {
                String folder = tok.nextToken();
                path += folder + "/";
                CmsResource resource2 = cms.createResource(path, folderType);
                try {
                    cms.unlockResource(resource2);
                } catch (CmsLockException locke) {
                    LOG.warn("Unbale to unlock resource", locke);
                }
            }
        }
        return module;
    }
1310544
protected List<CmsResource> getSitemapSubEntryResources(String rootPath) throws CmsException {

        CmsObject rootCms = OpenCms.initCmsObject(getCmsObject());
        List<CmsResource> result = new ArrayList<CmsResource>();
        rootCms.getRequestContext().setSiteRoot("");
        CmsJspNavBuilder navBuilder = new CmsJspNavBuilder(rootCms);
        for (CmsJspNavElement navElement : navBuilder.getNavigationForFolder(
            rootPath,
            Visibility.all,
            CmsResourceFilter.ONLY_VISIBLE)) {
            if ((navElement != null) && navElement.isInNavigation()) {
                result.add(navElement.getResource());
            }
        }
        return result;
    }
339230
protected static void checkIsFolder(CmsResource resource) throws CmsException {

        if (!isFolder(resource)) {
            throw new CmsIllegalArgumentException(
                Messages.get().container(
                    org.opencms.file.Messages.ERR_REWRITE_LINKS_ROOT_NOT_FOLDER_1,
                    resource.getRootPath()));
        }
    }
305078
private void importResources(File folder, String importPath) throws Exception {

        String[] diskFiles = folder.list();
        File currentFile;

        for (int i = 0; i < diskFiles.length; i++) {
            currentFile = new File(folder, diskFiles[i]);

            if (currentFile.isDirectory()) {
                // create directory in cms
                m_importedResources.add(
                    m_cms.createResource(importPath + currentFile.getName(), CmsResourceTypeFolder.RESOURCE_TYPE_ID));
                importResources(currentFile, importPath + currentFile.getName() + "/");
            } else {
                // import file into cms
                int type = OpenCms.getResourceManager().getDefaultTypeForName(currentFile.getName()).getTypeId();
                byte[] content = CmsFileUtil.readFile(currentFile);
                // create the file
                try {
                    m_importedResources.add(
                        m_cms.createResource(importPath + currentFile.getName(), type, content, null));
                } catch (CmsSecurityException e) {
                    // in case of not enough permissions, try to create a plain text file
                    int plainId = OpenCms.getResourceManager().getResourceType(
                        CmsResourceTypePlain.getStaticTypeName()).getTypeId();
                    m_importedResources.add(
                        m_cms.createResource(importPath + currentFile.getName(), plainId, content, null));
                }
                content = null;
            }
        }
    }
378524
public CmsGallerySearchResult searchByPath(String path, Locale locale) throws CmsException {

        I_CmsSearchDocument sDoc = m_index.getDocument(CmsSearchField.FIELD_PATH, path);
        CmsGallerySearchResult result = null;
        if ((sDoc != null) && (sDoc.getDocument() != null)) {
            result = new CmsGallerySearchResult(sDoc, m_cms, 100, locale);
        } else {
            CmsResource res = m_cms.readResource(path, CmsResourceFilter.IGNORE_EXPIRATION);
            result = new CmsGallerySearchResult(m_cms, res);
        }
        return result;
    }
237556
public void setParentResource(CmsResource parentResource) {

        if (parentResource.isFile()) {
            m_parentPath = CmsResource.getFolderPath(parentResource.getRootPath());
        } else {
            m_parentPath = parentResource.getRootPath();
        }
    }
556112
public CmsResource getFolder(CmsObject cms) throws CmsException {

        if (m_folder != null) {
            return m_folder;
        }
        try {
            CmsResource folder = cms.readResource(m_path);
            return folder;
        } catch (CmsVfsResourceNotFoundException e) {
            return null;
        }
    }
482716
public void load(CmsObject cms, CmsResource resource) throws CmsException {

        CmsFile file = cms.readFile(resource);
        CmsObject rootCms = OpenCms.initCmsObject(cms);
        rootCms.getRequestContext().setSiteRoot("");
        CmsXmlContent content = CmsXmlContentFactory.unmarshal(cms, file);
        Locale en = new Locale("en");
        for (I_CmsXmlContentValue value : content.getValues(N_INCLUDE, en)) {
            String include = value.getStringValue(rootCms);
            if (!CmsStringUtil.isEmpty(include)) {
                m_includes.add(include);
            }
        }
        for (I_CmsXmlContentValue value : content.getValues(N_EXCLUDE, en)) {
            String exclude = value.getStringValue(rootCms);
            if (!CmsStringUtil.isEmpty(exclude)) {
                m_excludes.add(exclude);
            }
        }
        I_CmsXmlContentValue robotsValue = content.getValue(N_MODE, en);
        m_mode = robotsValue.getStringValue(rootCms);

        I_CmsXmlContentValue robotsTxtTextValue = content.getValue(N_ROBOTS_TXT_TEXT, en);
        if (robotsTxtTextValue != null) {
            m_robotsTxtText = robotsTxtTextValue.getStringValue(rootCms);
        }

        I_CmsXmlContentValue computeContPageDates = content.getValue(N_COMPUTE_CONTAINER_PAGE_DATES, en);
        if (computeContPageDates != null) {
            m_computeContainerPageDates = Boolean.parseBoolean(computeContPageDates.getStringValue(rootCms));
        }

        I_CmsXmlContentValue generatorClassValue = content.getValue(N_GENERATOR_CLASS, en);
        if (generatorClassValue != null) {
            m_generatorClassName = generatorClassValue.getStringValue(rootCms);
        }

        I_CmsXmlContentValue cacheValue = content.getValue(N_CACHE, en);
        if (cacheValue != null) {
            m_useCache = Boolean.parseBoolean(cacheValue.getStringValue(rootCms));
        }

        I_CmsXmlContentValue serverUrlValue = content.getValue(N_SERVER_URL, en);
        if (serverUrlValue != null) {
            m_serverUrl = serverUrlValue.getStringValue(rootCms);
        }
    }
191003
public void substituteLinkForRootPath(final String rootPath, final I_CmsSimpleCallback<String> callback) {

        CmsRpcAction<String> action = new CmsRpcAction<String>() {

            @Override
            public void execute() {

                getVfsService().substituteLinkForRootPath(getSiteRoot(), rootPath, this);
            }

            @Override
            protected void onResponse(String result) {

                callback.execute(result);
            }
        };
        action.execute();
    }
567078
public List<String> getCategoryRepositories(CmsObject cms, String referencePath) {

        List<String> ret = new ArrayList<String>();
        if (referencePath == null) {
            ret.add(CmsCategoryService.CENTRALIZED_REPOSITORY);
            return ret;
        }
        String path = referencePath;
        if (!CmsResource.isFolder(path)) {
            path = CmsResource.getParentFolder(path);
        }
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(path)) {
            path = "/";
        }
        String categoryBase = getRepositoryBaseFolderName(cms);
        do {
            String repositoryPath = internalCategoryRootPath(path, categoryBase);
            if (cms.existsResource(repositoryPath)) {
                ret.add(repositoryPath);
            }
            path = CmsResource.getParentFolder(path);
        } while (path != null);
        ret.add(CmsCategoryService.CENTRALIZED_REPOSITORY);
        // the order is important in case of conflicts
        Collections.reverse(ret);
        return ret;
    }
1229402
private List<CmsResource> getContents(String folderPath, String type) throws CmsException {

        CmsPathMap<CmsResource> pathMap = getPathMapForType(type);
        return pathMap.getChildValues(folderPath);
    }
1210526
private void loadAncestors(CmsResource resource) throws CmsException {

        CmsResource currentResource = resource;
        while ((currentResource != null) && m_cms.existsResource(currentResource.getStructureId(), m_filter)) {
            if (!m_knownResources.add(currentResource)) {
                break;
            }

            if (CmsStringUtil.comparePaths(currentResource.getRootPath(), m_commonRoot)) {
                break;
            }
            CmsResource parent = m_cms.readParentFolder(currentResource.getStructureId());
            if (parent != null) {
                m_mustLoadChildren.add(parent);

            }
            currentResource = parent;
        }
    }
710813
private void initializeFileData(String baseSitePath) throws CmsException {

        m_resultMap.clear();
        m_siteRootLink = OpenCms.getLinkManager().getOnlineLink(m_siteGuestCms, "/");
        m_siteRootLink = CmsFileUtil.removeTrailingSeparator(m_siteRootLink);
        m_detailPageInfos = OpenCms.getADEManager().getAllDetailPages(m_guestCms);
        for (CmsDetailPageInfo detailPageInfo : m_detailPageInfos) {
            String type = detailPageInfo.getType();
            String path = detailPageInfo.getUri();
            path = CmsFileUtil.removeTrailingSeparator(path);
            m_detailTypesByPage.put(path, type);
        }
        List<CmsAlias> siteAliases = OpenCms.getAliasManager().getAliasesForSite(
            m_siteGuestCms,
            m_siteGuestCms.getRequestContext().getSiteRoot());
        for (CmsAlias alias : siteAliases) {
            if (isAliasBelowBaseFolder(alias) && (alias.getMode() == CmsAliasMode.page)) {
                CmsUUID aliasId = alias.getStructureId();
                m_pageAliasesBelowBaseFolderByStructureId.put(aliasId, alias);
            }
        }

    }
945013
public CmsPermissionInfo getPermissionInfo(CmsObject cms, CmsResource resource, String contextPath)
    throws CmsException {

        boolean hasView = cms.hasPermissions(
            resource,
            CmsPermissionSet.ACCESS_VIEW,
            false,
            CmsResourceFilter.ALL.addRequireVisible());
        boolean hasWrite = false;
        if (hasView) {
            try {
                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource.getTypeId());
                CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(
                    type.getTypeName());
                hasView = (settings == null)
                    || settings.getAccess().getPermissions(cms, resource).requiresViewPermission();
                if (hasView
                    && CmsResourceTypeXmlContent.isXmlContent(resource)
                    && !CmsResourceTypeXmlContainerPage.isContainerPage(resource)) {
                    if (contextPath == null) {
                        contextPath = resource.getRootPath();
                    }
                    CmsResourceTypeConfig localConfigData = lookupConfiguration(cms, contextPath).getResourceType(
                        type.getTypeName());
                    if (localConfigData != null) {
                        Map<CmsUUID, CmsElementView> elmenetViews = getElementViews(cms);
                        hasView = elmenetViews.containsKey(localConfigData.getElementView())
                            && elmenetViews.get(localConfigData.getElementView()).hasPermission(cms, resource);
                    }
                }
                // the user may only have write permissions if he is allowed to view the resource
                hasWrite = hasView
                    && cms.hasPermissions(
                        resource,
                        CmsPermissionSet.ACCESS_WRITE,
                        false,
                        CmsResourceFilter.IGNORE_EXPIRATION)
                    && ((settings == null)
                        || settings.getAccess().getPermissions(cms, resource).requiresWritePermission());
            } catch (CmsLoaderException e) {
                LOG.warn(e.getLocalizedMessage(), e);
                hasWrite = false;
            }
        }

        String noEdit = new CmsResourceUtil(cms, resource).getNoEditReason(
            OpenCms.getWorkplaceManager().getWorkplaceLocale(cms),
            true);
        return new CmsPermissionInfo(hasView, hasWrite, noEdit);
    }