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

📄 cmsxmlnav.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        int size = resources.size();
        String navLink[] = new String[size];
        String navText[] = new String[size];
        float navPos[] = new float[size];
        // extract the navigation from the given resources, i.e. it will be showed
        // if the elements of that resources must be showed in navigation, i.e.
        // wheather navigation Text und navigation position are defined for an element
        int max=extractNav(cms, resources, navLink, navText, navPos);
        // check wheather there is some elements
        if (max>0) {
            result.append(template.getProcessedDataValue("navstart", this, userObject));
            for (int i=0; i<max; i++) {
                // set the templates
                count[0]++;
                template.setData("navtext", navText[i]);
                template.setData("navcount", new Integer(count[0]).toString());
                // this part is to set the level starting from specified level given as tagcontent
                // there it must be make a difference between extracted level and the given level
                int extractedLevel=extractLevel(cms, navLink[i]);
                template.setData("navlevel", new Integer(extractedLevel).toString());
                /*int rightLevel=extractedLevel;
                if (level!=0) {
                    rightLevel=(extractedLevel-level);
                    if (rightLevel>=0) {
                        rightLevel++;
                    }
                }
                template.setData("navlevel", new Integer(rightLevel).toString());*/
                String link="";
                // Check whether the link is a folder
                if (navLink[i].endsWith("/")) {
                    // read the property of folder to find the link file.
                    String navIndex=cms.readProperty(navLink[i], C_PROPERTY_NAVINDEX);
                    // if there is not defined a property then take C_NAVINDEX constant
                    if (navIndex==null || (navIndex!=null && navIndex.equals(""))) {
                        navIndex=C_NAVINDEX;
                    }
                    // check whether the file exist, if not then the link is current uri
                    try {
                        cms.readFile(navLink[i] + navIndex);
                        link=navLink[i] + navIndex;
                        template.setData("navlink", OpenCms.getLinkManager().substituteLink(cms, navLink[i] + navIndex));
                    } catch (CmsException e) {
                        link=requestedUri;
                        template.setData("navlink", OpenCms.getLinkManager().substituteLink(cms, requestedUri));
                    }
                } else {
                    // check whether the file exist, if not then the link is current uri
                    try {
                        cms.readFile(navLink[i]);
                        link=navLink[i];
                        template.setData("navlink", OpenCms.getLinkManager().substituteLink(cms, navLink[i]));
                    } catch (CmsException e) {
                        link=requestedUri;
                        template.setData("navlink", OpenCms.getLinkManager().substituteLink(cms, requestedUri));
                    }
                }
                // Check if nav is current nav
                if (link.equals(requestedUri)) {
                    result.append(template.getProcessedDataValue("navcurrent", this, userObject));
                } else {
                    result.append(template.getProcessedDataValue("naventry", this, userObject));
                }
                // redurce the depth and test if it is now zero or is the depth
                // given as tagcontent or not.
                // if the depth is not given in tagcontent then the depth variable must be ignored
                // the user don't want to give depth otherwise the depth must be considered
                // because it must not be exceeded the limit that user has defined.
                depth--;
                if (depthIsNull || depth>=0) {
                    if (navLink[i].endsWith("/")) {
                        List all=cms.getSubFolders(navLink[i]);
                        List files=cms.getFilesInFolder(navLink[i]);
                        // register this folder for changes
                        Vector vfsDeps = new Vector();
                        vfsDeps.add(cms.readFolder(navLink[i]));
                        registerVariantDeps(cms, template.getAbsoluteFilename(), null, null,
                                        (Hashtable)userObject, vfsDeps, null, null);
                        ((ArrayList)all).ensureCapacity(all.size() + files.size());
                        Iterator e = files.iterator();
                        while (e.hasNext()) {
                            all.add(e.next());
                        }
                        result.append(buildNavTree(cms, template, userObject, all, requestedUri, currentFolder, servletPath, level, depth, depthIsNull, count));
                    }
                }
            }
            result.append(template.getProcessedDataValue("navend", this, userObject));
        }
        return result.toString();
    }
    /**
     * Builds the link to folder determined by level.
     *
     * @param cms CmsObject Object for accessing system resources
     * @param level The level of folder
     * @param exact this parameter determines wheater exact level of folder must be exctracted
     * @return String that contains the path of folder determind by level
     * @throws CmsException if something goes wrong
     */
    protected String extractFolder(CmsObject cms, int level, String exact)
        throws CmsException {

        // get uri and requested uri
        String navIndex=C_NAVINDEX;
        try {
            navIndex=cms.readProperty(CmsResource.getFolderPath(cms.getRequestContext().getUri()), C_PROPERTY_NAVINDEX);
            navIndex=((navIndex==null || (navIndex!=null && navIndex.equals("")))?C_NAVINDEX:navIndex);
        } catch (Exception err) {
            navIndex=C_NAVINDEX;
        }
        String uri=CmsResource.getFolderPath(cms.getRequestContext().getUri())+navIndex;
        String requestedUri=cms.getRequestContext().getUri();
        // get count of folder
        String currentFolder="/";
        StringTokenizer st = new StringTokenizer(CmsResource.getFolderPath(cms.getRequestContext().getUri()), "/");
        int count=st.countTokens()+1;
        // if the level is negative then take the folder starting from
        // current folder otherwise take the folder starting from root
        if (level<0) {
            level=(-1)*level;
            level=count-level;
        }
        // increment count to get real level
        if (exact.equals("true") && (level<=0 || level>count || (level==count && requestedUri.equals(uri)))) {
            return "";
        }
        // ulrich.rueth@gmx.de: this seems to have been the reason for the navigation
        // not displaying correctly in top-level index.html files!
        //if (level==count && requestedUri.equals(uri)) {
        //    level--;
        //}
        while (st.hasMoreTokens()) {
            if (level>1) {
                currentFolder=currentFolder+st.nextToken()+"/";
                level--;
            } else {
                break;
            }
        }
        return currentFolder;
    }
    /**
     * Extract the level of navigation.<p>
     *
     * @param cms Object for accessing system resources
     * @param folder the folder to extract the level
     * @return int that contains the level
     * @throws CmsException if something goes wrong
     */
    protected int extractLevel(CmsObject cms, String folder)
        throws CmsException {
        StringTokenizer st = new StringTokenizer(folder, "/");
        int count=st.countTokens();
        count=(count==0?1:count);
        return count;
    }
    /**
     * Extracts the navbar.<p>
     *
     * @param cms CmsObject Object for accessing system resources
     * @param resources a vector that contains the elements of navigation
     * @param navLink an array of navigation's link
     * @param navText an array of navigation's Text
     * @param navPos an array of position of navbar
     * @return The maximum number of navbars in navigation
     * @throws CmsException if something goes wrong
     */
    protected int extractNav(CmsObject cms, List resources, String[] navLink, String[] navText, float[] navPos)
        throws CmsException {

        String requestedUri = cms.getRequestContext().getUri();

        int size = resources.size();
        int max = 0;

        // First scan all subfolders of the root folder
        // for any navigation metainformations and store
        // the maximum position found
        for (int i=0; i<size; i++) {
            CmsResource currentResource = (CmsResource)resources.get(i);
            String path = cms.getSitePath(currentResource);
            String pos = cms.readProperty(path, CmsPropertyDefinition.PROPERTY_NAVPOS);
            String text = cms.readProperty(path, CmsPropertyDefinition.PROPERTY_NAVTEXT);
            // Only list folders in the nav bar if they are not deleted!
            if (currentResource.getState() != CmsResource.STATE_DELETED) {
                // don't list the temporary folders in the nav bar!
                if (pos != null && text != null && (!"".equals(pos)) && (!"".equals(text))
                     && ((!currentResource.getName().startsWith(CmsWorkplace.TEMP_FILE_PREFIX)) || path.equals(requestedUri))) {
                    navLink[max] = path;
                    navText[max] = text;
                    navPos[max] = new Float(pos).floatValue();
                    max++;
                }
            }
        }

        // Sort the navigation
        sortNav(max, navLink, navText, navPos);

        return max;
    }
    /**
     * Gets the current folder.<p>
     *
     * @param cms CmsObject Object for accessing system resources
     * @param tagcontent Unused in this special case of a user method. Can be ignored
     * @param doc Reference to the A_CmsXmlContent object of the initiating XLM document
     * @param userObject Hashtable with parameters
     * @return byte[] with the content of this subelement
     * @throws CmsException if something goes wrong
     */
    public Object getFolderCurrent(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject)
            throws CmsException {

        String currentFolder=CmsResource.getFolderPath(cms.getRequestContext().getUri());
        currentFolder=CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl() + currentFolder;
        return currentFolder.getBytes();
    }
    /**
     * gets the parent folder.
     *
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent Unused in this special case of a user method. Can be ignored.
     * @param doc Reference to the A_CmsXmlContent object of the initiating XLM document.
     * @param userObject Hashtable with parameters.
     * @return byte[] with the content of this subelement.
     * @throws CmsException if something goes wrong
     */
    public Object getFolderParent(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject)
            throws CmsException {

        int level=1;
        // tagcontent determines the folder starting from parent folder.
        // if tagcontent is null, zero or negative, then the navigation of current
        // folder must be showed.
        String exact="false";
        // tagcontent determines the folder starting from root folder.
        // if tagcontent is null, then the navigation of root folder must be showed.
        if (!tagcontent.equals("")) {
            try {
                if (tagcontent.indexOf(",")!=-1) {
                    level=Integer.parseInt(tagcontent.substring(0, tagcontent.indexOf(",")));
                    exact=tagcontent.substring(tagcontent.indexOf(",")+1).toLowerCase();
                } else {
                    level=Integer.parseInt(tagcontent);
                }
            } catch (NumberFormatException e) {
                level=1;
                exact=tagcontent.toLowerCase();
                if (!exact.equals("true")) {
                    exact="false";
                }
            }
        }
        String currentFolder=extractFolder(cms, ((-1)*level), exact);
        if (currentFolder.equals(""))
            return "".getBytes();
        String parentFolder=CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl() + currentFolder;
        return parentFolder.getBytes();
    }
    /**
     * gets the root folder.
     *
     * @param cms CmsObject Object for accessing system resources
     * @param tagcontent Unused in this special case of a user method. Can be ignored
     * @param doc Reference to the A_CmsXmlContent object of the initiating XLM document
     * @param userObject Hashtable with parameters
     * @return byte[] with the content of this subelement
     * @throws CmsException if something goes wrong
     */
    public Object getFolderRoot(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject)
            throws CmsException {

        int level=1;
        String exact="false";

⌨️ 快捷键说明

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