jetspeedtemplateservice.java

来自「jetspeed源代码」· Java 代码 · 共 722 行 · 第 1/2 页

JAVA
722
字号
            screenName = (String)screenCache.get(name);
        }
        else
        {
            String[] names = parseScreenTemplate(name);
            screenName = names[0];
            addToCache( name, names[0], screenCache );
            addToCache( name, names[1], layoutCache );
            addToCache( name, names[2], templateCache );
        }
        return screenName;
    }

    /**
     * Get the default extension given in the properties file.
     *
     * @return A String with the extension.
     */
    public String getDefaultExtension()
    {
        return extension;
    }

    /**
     * This method takes the template parameter and parses it, so that
     * relevant Screen/Layout-template information can be extracted.
     *
     * @param template A String with the template name.
     * @return A String[] where the first element is the Screen name
     *         and the second element is the layout template.
     */
    protected String[] parseScreenTemplate( String template ) throws Exception
    {
        // check if an extension was included.  if not, add the default
        if ( template.indexOf('.') == -1 )
        {
            template = template + "." + getDefaultExtension(); 
        }

        if ( logger.isDebugEnabled() )
        {
            logger.debug("JetspeedTemplateService.parseScreen: template = " + template);
        }
        
        StringTokenizer st = new StringTokenizer(template, "/");
        List tokens = new ArrayList(st.countTokens());
        while(st.hasMoreTokens())
        {
            String token = st.nextToken();
            if (!token.equals(""))
            {
                tokens.add(token);
            }
        }
        if ( logger.isDebugEnabled() )
        {
            logger.debug("JetspeedTemplateService.parseScreen: tokens1: " + tokens);
        }
        String fileName = (String)tokens.get(tokens.size() - 1);
        tokens.remove(tokens.size()-1);
        int dot = fileName.lastIndexOf('.');
        String className = null;
        if (dot>0)
        {
            className = fileName.substring(0, dot);
        }
        else
        {
            className = fileName;
        }
        String firstChar = String.valueOf(className.charAt(0));
        firstChar = firstChar.toUpperCase();
        className = firstChar + className.substring(1);
        if ( logger.isDebugEnabled() )
        {
            logger.debug("JetspeedTemplateService.parseScreen: tokens2: " + tokens);
        }

        // make sure the template exists and determine the correct
        // templateRoot path
        String pathRoot = null;
        String allPaths = "";
        String pathSep = System.getProperty("path.separator");
        for (int i=0; i<templateRoot.length; i++)
        {
            if ( logger.isDebugEnabled() )
            {
                logger.debug("JetspeedTemplateService.parseScreen: templateRoot " + i + " " + templateRoot[i]);
            }

            String templatePath = null;
        
            for (int k=tokens.size(); k>=0;  k--)
            {
                StringBuffer path = new StringBuffer();
                for (int j=0; j<k; j++)
                {
                    path.append("/").append((String)tokens.get(j));
                }
                StringBuffer distinctPath = new StringBuffer(path.toString()).append("/").append(fileName);
                templatePath = distinctPath.toString();
                if ( logger.isDebugEnabled() )
                {
                    logger.debug("JetspeedTemplateService.parseScreen: Path: " + templatePath);
                }
         
                if (new File(templateRoot[i] + "/screens" + templatePath).exists())
                {
                    template = templatePath;
                    if ( logger.isDebugEnabled() )
                    {
                        logger.debug("JetspeedTemplateService.parseScreen: template found: " + template);
                    }
                    break;
                }
                templatePath = null;
            }
            if (templatePath != null) {
                pathRoot = templateRoot[i];
                if ( logger.isDebugEnabled() )
                {
                    logger.debug("JetspeedTemplateService.parseScreen: pathRoot: " + pathRoot);
                }
                break;
            }
            allPaths += pathSep + templateRoot[i];
        }
        if (pathRoot == null)
        {
            throw new Exception("The screen template: " +
                                template +
                                " does not exist in " +
                                allPaths.substring(pathSep.length()) +
                                ", so the TemplateService could not " +
                                "determine associated templates.");
        }

        /*
        String[] paths = new String[tokens.size() + 2];
        String[] pkgs = new String[tokens.size() + 2];
        int arrayIndex = 0;
        for (int i=tokens.size(); i>=0;  i--)
        {
            StringBuffer path = new StringBuffer();
            StringBuffer pkg = new StringBuffer();
            for (int j=0; j<i; j++)
            {
                path.append("/").append((String)tokens.get(j));
                pkg.append((String)tokens.get(j)).append('.');
            }
            if ( i == tokens.size() )
            {
                StringBuffer distinctPath = new StringBuffer(path.toString());
                StringBuffer distinctPkg = new StringBuffer(pkg.toString());
                paths[arrayIndex] = distinctPath.append('/').append(fileName).toString();
                pkgs[arrayIndex] = distinctPkg.append(className).toString();
                arrayIndex++;
            }
            paths[arrayIndex] = path.append(defaultLayoutTemplate).toString();
            pkgs[arrayIndex] = pkg.append("Default").toString();
            arrayIndex++;
        }
        */        

        String[] paths = new String[2 * tokens.size() +2];
        String[] pkgs  = new String[2 * tokens.size() +2];
        int arrayIndex = 0;
        for (int i=tokens.size(); i>=0;  i--)
        {
            StringBuffer path = new StringBuffer();
            StringBuffer pkg = new StringBuffer();
            for (int j=0; j<i; j++)
            {
                path.append("/").append((String)tokens.get(j));
                pkg.append((String)tokens.get(j)).append('.');
            }
            paths[arrayIndex] = path.append("/").append(fileName).toString();
            pkgs[arrayIndex]  = pkg.append("/").append(className).toString();
            arrayIndex++;
        }
        
        for (int i=tokens.size(); i>=0;  i--)
        {
            StringBuffer path = new StringBuffer();
            StringBuffer pkg = new StringBuffer();
            for (int j=0; j<i; j++)
            {
                path.append("/").append((String)tokens.get(j));
                pkg.append((String)tokens.get(j)).append('.');
            }
            paths[arrayIndex] = path.append(defaultLayoutTemplate).toString();
            pkgs[arrayIndex]  = pkg.append("Default").toString();
            arrayIndex++;
        }

        if ( logger.isDebugEnabled() )
        {
            for (int i=0; i<paths.length; i++)
            {
                logger.debug("JetspeedTemplateService.parseScreen: paths[" + i + "] = " + paths[i]);
            }
        }

        String[] holder = new String[3];
        holder[0] = getScreenName(pkgs);
        holder[1] = getLayoutTemplateName(pathRoot, paths);
        holder[2] = template;
        return holder;
    }

    /**
     * Parse the template name out to a package path to locate the
     * Navigation module.  This is different than the Screen/Layout
     * parser in that it only looks for packages.  Note: If caching is
     * enabled, this is only performed once for each unique template.
     *
     * @param String The template name (i.e folder/headernav.wm).
     * @return A String with the name of the Navigation module to use
     * for the template.
     */
    protected String parseNavigationTemplate( String template )
    {
        StringTokenizer st = new StringTokenizer(template, "/");
        List tokens = new ArrayList(st.countTokens());
        while(st.hasMoreTokens())
        {
            String token = st.nextToken();
            if (!token.equals(""))
            {
                tokens.add(token);
            }
        }
        String fileName = (String)tokens.get(tokens.size() - 1);
        tokens.remove(tokens.size() - 1);
        int dot = fileName.lastIndexOf('.');
        String className = null;
        if (dot>0)
        {
            className = fileName.substring(0, dot);
        }
        else
        {
            className = fileName;
        }
        String firstChar = String.valueOf(className.charAt(0));
        firstChar = firstChar.toUpperCase();
        className = firstChar + className.substring(1);

        String[] pkgs = new String[tokens.size() + 2];
        int arrayIndex = 0;
        for (int i=tokens.size(); i>=0;  i--)
        {
            StringBuffer pkg = new StringBuffer();
            for (int j=0; j<i; j++)
            {
                pkg.append((String)tokens.get(j)).append('.');
            }
            if ( i == tokens.size() )
            {
                StringBuffer distinctPkg = new StringBuffer(pkg.toString());
                pkgs[arrayIndex] = distinctPkg.append(className).toString();
                arrayIndex++;
            }
            pkgs[arrayIndex] = pkg.append("Default").toString();
            arrayIndex++;
        }
        return getNavigationName( pkgs);
    }

    /**
     * Extract possible layouts paths.
     *
     * @param possiblePaths A String[] with possible paths to search.
     * @return A String with the name of the layout template.
     */
    private String getLayoutTemplateName(String pathRoot, String[] possiblePaths)
    {
        if ( logger.isDebugEnabled() )
        {
            logger.debug("JetspeedTemplatePage.getLayoutTemplateName: pathRoot " + pathRoot);
        
            for (int i=0; i<possiblePaths.length; i++)
            {
                logger.debug("JetspeedTemplatePage.getLayoutTemplateName: possiblePaths[" + i + "]=" + possiblePaths[i]);
            }
        }
        for (int i=0; i<possiblePaths.length; i++)
        {
            if (new File(pathRoot, "layouts" + possiblePaths[i]).exists())
            {
                if ( logger.isDebugEnabled() )
                {
                    logger.debug("JetspeedTemplatePage.getLayoutTemplateName: " + pathRoot + "/layouts" + possiblePaths[i] + " found.");
                }
                return possiblePaths[i];
            } 
            else 
            {
                if ( logger.isDebugEnabled() )
                {
                    logger.debug("JetspeedTemplatePage.getLayoutTemplateName: " + pathRoot + "/layouts" + possiblePaths[i] + " NOT found.");
                }
            }
        }
        return defaultLayoutTemplate;
    }

    /**
     * Extract a possible Screen from the packages.
     *
     * @param possibleScreens A String[] with possible paths to
     * search.
     * @return A String with the name of the Screen class to use.
     */
    private String getScreenName( String[] possibleScreens)
    {
        for (int i=0; i<possibleScreens.length; i++)
        {
            try
            {
                ScreenLoader.getInstance().getInstance(possibleScreens[i]);
                return possibleScreens[i];
            }
            catch (Exception e) 
            {
                logger.error( "Exception in getScreenName", e );
            }
        }
        return defaultScreen;
    }


    /**
     * Seaches for the Navigation class that may match the
     * name of the Navigation template.
     *
     * @param possibleNavigations A String[] with possible navigation
     * packages.
     * @return A String with the name of the Navigation class to use.
     */
    private String getNavigationName( String[] possibleNavigations)
    {
        for (int i=0; i<possibleNavigations.length; i++)
        {
            try
            {
                NavigationLoader.getInstance().getInstance(possibleNavigations[i]);
                return possibleNavigations[i];
            }
            catch (Exception e) 
            {
                logger.error( "Exception in getNavigationName", e );
            }
        }
        return defaultNavigation;
    }
}



⌨️ 快捷键说明

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