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

📄 linksubstitution.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            path = "/";
        }
        try{
            if(converter.hasErrors(content)){
                String errors = converter.showErrors(content);
                throw new CmsException(errors);
            }
            converter.setConverterConfString(m_converterConfiguration);
            // get parameter to create the url object of the edited file
            String servletPrefix = cms.getRequestContext().getRequest().getServletUrl();
            String prot = ((HttpServletRequest)cms.getRequestContext().getRequest().getOriginalRequest()).getScheme();
            String host = ((HttpServletRequest)cms.getRequestContext().getRequest().getOriginalRequest()).getServerName();
            int port = ((HttpServletRequest)cms.getRequestContext().getRequest().getOriginalRequest()).getServerPort();
            URL urltool = new URL(prot, host, port, servletPrefix + path);
            converter.setServletPrefix(servletPrefix, relativeRoot);
            converter.setOriginalUrl(urltool);
            retValue = converter.convertHTML(content);
        }catch ( Exception e ){
            throw new CmsException("["+this.getClass().getName()+"] can't convert the editor content:"+e.toString());
        }
        return retValue;
    }
    /**
     * Replaces the link according to the rules and registers it to the
     * requestcontex if we are in export modus.
     * @param cms. The cms object.
     * @param link. The link to process.
     * @return String The substituded link.
     */
    public static String getLinkSubstitution(CmsObject cms, String link){
        if(link == null || "".equals(link)){
            return "";
        }
        if(!link.startsWith("/")){
            // this is a relative link, lets make an absolute out of it
            link = Utils.mergeAbsolutePath(cms.getRequestContext().getRequest().getRequestedResource(), link);
        }
        // first ask if this is the export
        int modus = cms.getMode();
        if(modus == CmsObject.C_MODUS_EXPORT){
            // we have to register this link to the request context
            cms.getRequestContext().addLink(link);
            // and we have to process the startrule
            String startRule = OpenCms.getStaticExportProperties().getStartRule();
            if(startRule != null && !"".equals(startRule)){
                try{
                    link = c_perlUtil.substitute(startRule, link);
                }catch(Exception e){
                    if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                        A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_CRITICAL, "[LinkSubstitution.getLinkSubstitution()/1] problems with startrule:\""+startRule+"\" (" + e + "). ");
                    }
                }
            }
        }
        // check if we are in a https page, then we have to set the http
        // protocol ahead the "not-https-links" in this page
        boolean httpsMode = false;
        if(modus == CmsObject.C_MODUS_ONLINE){
            // https pages are always online
            try {
                // HACK: Original request might be unavailable here. 
                // If you start the export in a Thread (what is done most of the time now)
                // the original request might be gone here and a NullPointer Exception will raised.
                String scheme = ((HttpServletRequest)cms.getRequestContext().getRequest().getOriginalRequest()).getScheme();
                if("https".equalsIgnoreCase(scheme)){
                    httpsMode = true;
                }
            } catch (Exception e) {
                httpsMode = false;
            }
        }
        boolean needsScheme = true;
        if(httpsMode){
            needsScheme = CmsStaticExport.needsScheme(link);
        }
        String[] rules = CmsStaticExportProperties.getLinkRules(modus);
        if(rules == null || rules.length == 0){
            return link;
        }
        String retValue = link;
        for(int i=0; i<rules.length; i++){
            try{
                boolean nextRule = true;
                if("*dynamicRules*".equals(rules[i])){
                    // here we go trough our dynamic rules
                    Vector booleanReplace = new Vector();
                    retValue = CmsStaticExport.handleDynamicRules(cms, link, modus, booleanReplace);
                    Boolean goOn =(Boolean)booleanReplace.firstElement();
                    if(goOn.booleanValue()){
                        link = retValue;
                    }else{
                        nextRule = false;
                    }
                }else{
                    StringBuffer result = new StringBuffer();
                    int matches = c_perlUtil.substitute(result, rules[i], link);
                    if(matches != 0){
                        retValue = result.toString();
                        nextRule = false;
                    }
                }
                if(!nextRule){
                    // found the match
                    if(httpsMode && !retValue.startsWith("http")){
                        if(needsScheme){
                            retValue = CmsObject.getStaticExportProperties().getUrlPrefixArray()[3] + retValue;
                        }
                    }else{
                        if(CmsObject.getStaticExportProperties().relativLinksInExport() && modus == CmsObject.C_MODUS_EXPORT
                                && (retValue != null) && retValue.startsWith(CmsObject.getStaticExportProperties().getUrlPrefixArray()[0])){
                            // we want the path relative
                            retValue = getRelativePath(cms.getRequestContext().getRequest().getRequestedResource(), retValue.substring((CmsObject.getStaticExportProperties().getUrlPrefixArray()[0]).length()));
                        }
                    }
                    return retValue;
                }
            }catch(MalformedPerl5PatternException e){
                if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                    A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_CRITICAL, "[LinkSubstitution.getLinkSubstitution()/2] problems with rule:\""+rules[i]+"\" (" + e + "). ");
                }
            }
        }
        if(httpsMode && !retValue.startsWith("http")){
            if(needsScheme){
                retValue = CmsObject.getStaticExportProperties().getUrlPrefixArray()[3] + retValue;
            }
        }else{
            if(CmsObject.getStaticExportProperties().relativLinksInExport() && modus == CmsObject.C_MODUS_EXPORT && (retValue != null) && retValue.startsWith(CmsObject.getStaticExportProperties().getUrlPrefixArray()[0])){
                // we want the path relative
                retValue = getRelativePath(cms.getRequestContext().getRequest().getRequestedResource(), retValue.substring((CmsObject.getStaticExportProperties().getUrlPrefixArray()[0]).length()));
            }
        }
        return retValue;
    }

    /**
     * This methood calculates the relative path to a resource in OpenCms
     * depending on the page where it is used.
     * i.e.: baseFile  = "/folder1/folder2/index.html"
     *       linkTarget= "/folder1/pics/pic.gif"
     *       returns: "../pics/pic.gif"
     *
     * @param baseFile: the name(incl. path) of the page containing the link
     * @param linkTarget: the name(incl. path) of the resource to link to.
     *
     * @return the relative path to the target resource.
     */
    public static String getRelativePath(String baseFile, String linkTarget){

        // use tokenizer for better performance
        java.util.StringTokenizer cur = new java.util.StringTokenizer(baseFile, "/");
        java.util.StringTokenizer tar = new java.util.StringTokenizer(linkTarget,"/");

        // get the minimum of the number of tokens for both paths
        int maxAllowed = cur.countTokens();
        if(maxAllowed > tar.countTokens()){
            maxAllowed = tar.countTokens();
        }
        // serch for the part of the path they have in common.
        String currentToken = cur.nextToken();
        String targetToken = tar.nextToken();
        int counter = 1;
        while(currentToken.equals(targetToken) && counter < maxAllowed){
            currentToken = cur.nextToken();
            targetToken = tar.nextToken();
            counter++;
        }
        StringBuffer result = new StringBuffer();

        // link to the shared root path
        counter = cur .countTokens();
        for(int i=0; i < counter; i++){
            result.append("../");
        }

        // finaly add the link to the target from the shared root path
        result.append(targetToken);
        while(tar.hasMoreTokens()){
            result.append("/" +tar.nextToken());
        }
        return result.toString();
    }
}

⌨️ 快捷键说明

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