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

📄 fileutil.java

📁 低版本的tomcat 对于有些老版本的应用还真的需要老版的中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	String locRes = null;

	if (null != loc)
	    locRes = getLocalizedFile (base, path, loc);

	if (null != locRes)
	    return locRes;

	if (null != fbloc)
	    locRes= getLocalizedFile (base, path, fbloc);

	if (null != locRes)
	    return locRes;

	return safePath (base, path);
    }

    /**  utility method to return the name of the localized file whose
     *   name best-matches the Locale passed as an argument using the
     *   'file-based' lookup mechanism.
     *
     *  @param base the directory that is the Document Base for the context
     *			in which the 'path' passed is expected to be located
     *  @param path the pathname for the file that is being searched for its
     *			best-matched Localized version
     *  @param loc the requested Locale to match
     *
     *  @return the name of the file that best-matched the Locale requested
     */
    public static String getLocalizedFile (String base,
					   String path,
					   Locale loc) {
	
	return getLocalizedResource (base, path, loc, true);
    }

    /**  utility method to get the best-match for getting the Localized
     *   version of the file whose 'path' is passed as an argument, using
     *   the 'loc' Locale, and performing 'safePath'checks
     *   The algorithm used matches 'file-based' lookup mechanism.
     *
     *  The method performs a resource lookup in a manner similar to the
     *  one specified by java.util.ResourceBundle.
     *
     *  In the case of 'typed' files (files whose name is [file].[ftype])
     *  search for localized versions of the file are looked for:
     *
     *   file + "_" + language1 + "_" + country1 + "_" + variant1 + "." + ftype
     *   file + "_" + language1 + "_" + country1 + "." + ftype
     *   file + "_" + language1 + "." + ftype
     *   file + "." + ftype
     *
     *  Where language1, country1, variant1 are associated with the Locale
     *  passed as an argument. 
     *
     *  For example, if the preferred Locale is <CODE>es_AR_POSIX</CODE>
     *  pathname is <CODE>/foo/bar/index.html</CODE>, then a search for
     *  the following localized versions of that file will be done, in order:
     *<UL>
     *<LI>/foo/bar/index_es_AR_POSIX.html</LI>
     *<LI>/foo/bar/index_es_AR.html</LI>
     *<LI>/foo/bar/index_es_AR.html</LI>
     *<LI>/foo/bar/index_es.html</LI>
     *</UL>
     *
     *  @param base the document base for the path passed
     *  @param rPath the path to the file relative to the base
     *  @param loc the Locale preferred
     *  @param safechek perform safePath checks
     *
     *  @return the name of the file that best-matched the Locale requested
     *		null, if no localization matching could be done.
     */
    public static String getLocalizedResource (String base, String rPath,
					       Locale loc, boolean safechek) {

	if (null == rPath)
	    return null;

	String path = (safechek ? safePath (base, rPath) : rPath);

	return getLocalizedResource (path, loc);
    }

    /**  utility method to get the best-match for getting the Localized
     *   version of the file whose 'path' is passed as an argument, using
     *   the 'loc' Locale, and performing 'safePath'checks
     *
     *  The method performs a resource lookup in a manner similar to the
     *  one specified by java.util.ResourceBundle.
     * 
     *  See above explanation of lookup order.
     *
     *  @param path the path to the file 
     *  @param loc Locale with which to match.
     *
     *  @return the name of the file that best-matched the Locale requested
     *		null if no Locale match existed. Invokers of the method
     *  	need to test whether null is returned, and then decide
     *		what to do (whether to default to the original path
     *		or not).
     */
    public static String getLocalizedResource (String path, Locale loc) {

	if (null == path)
	    return null;

	String wPath = null;
	String pPath = null;    // parent path
	String gpPath = null;   // grand-parent path

	String rLang = loc.getLanguage();
	String rCoun = loc.getCountry();
	String rVar  = loc.getVariant();

	int    pathLen   = path.length();
	int    lastParen = path.lastIndexOf (File.separator);
	int    lastDot   = path.lastIndexOf ('.');

	//  evaluate the following conditions
	//
	//  a. path passed includes the File.separator
	//	  the path specified is a 'dotted' file if:
	//		lastParen < lastDot
	//
	//  b. path passed has not File.separator
	//	  the path specified is a 'dotted' file if:
	//		-1 < lastDot
	//
	//  c. path is not a dotted file if:
	//		-1 == lastDot
	//
	//
	//  'pathBase' is anything to the left of the 'lastDot' position
	//  (if 'lastDot' is -1, 'pathBase' is 'path')
	//
	String pathBase = null;
	String dotTail  = null;

	if (lastParen < lastDot && lastDot > 0)
	{
	    pathBase = path.substring (0, lastDot);
	    dotTail = path.substring (lastDot);
	}
	else
	    pathBase = path;
	    
	File file = null;

	gpPath = pathBase + '_' + rLang;

	//  first, try the language+country+variant match
	//
	if (null != rCoun && ! ("".equals (rCoun)) ) {

	    pPath = gpPath + '_' + rCoun;
	    if (null != rVar && ! ("".equals (rVar)) ) {

		wPath = pPath + '_' + rVar;

		//  append the dotTail if necessary
		//
		if (null != dotTail)
		    wPath += dotTail;

		// perform existence test
		//
		if ((new File(wPath).exists()))
		    return wPath;
	    }

	    //  append the dotTail if necessary
	    //
	    if (null != dotTail)
		pPath += dotTail;

	    if ((new File(pPath)).exists())
		return pPath;
	}

	//  append the dotTail if necessary
	//
	if (null != dotTail)
	    gpPath += dotTail;

	if ((new File(gpPath)).exists())
	    return gpPath;

	return null;
    }

    /**  utility method to return the name of the localized file whose
     *   name best-matches the Locale passed as an argument using the
     *   'docbase-based' lookup mechanism.
     *
     *  @param base the directory that is the Document Base for the context
     *			in which the 'path' passed is expected to be located
     *  @param path the pathname for the file that is being searched for its
     *			best-matched Localized version
     *  @param loc the requested Locale to match
     *  @param fbloc the requested fall-back Locale to match
     *
     *  @return the name of the file that best-matched the Locale requested
     */
    public static String getDocBaseLocalizedFile (String base,
					   String path,
					   Locale loc,
					   Locale fbloc) {
	return getDocBaseLocalizedResource (base, path, loc, fbloc);
    }

    /**  utility method to get the best-match for getting the Localized
     *   version of the file whose 'path' is passed as an argument, using
     *   the 'loc' Locale, and performing 'safePath'checks
     *   The algorithm used matches 'docBase-based' lookup mechanism.
     *
     *  In the case of 'typed' files (files whose name is [file].[ftype])
     *  search for localized versions of the file are looked for:
     *
     *   docbase + "/" + language1 + "_" + country1 + "_" + variant1 + filepath 
     *   docbase + "/" + language1 + "_" + country1 + filepath 
     *   docbase + "/" + language1 + filepath 
     *
     *  Where language1, country1, variant1 are associated with the Locale
     *  passed as an argument. 
     *
     *  For example, if the preferred Locale is <CODE>es_AR_POSIX</CODE>
     *  the docBase is '/' and  pathname is <CODE>/foo/bar/index.html</CODE>,
     *  then a search for the following localized versions of that file will
     *  be done, in order:
     *<UL>
     *<LI>/es_AR_POSIX/foo/bar/index.html</LI>
     *<LI>/es_AR/foo/bar/index.html</LI>
     *<LI>/es/foo/bar/index.html</LI>
     *<LI>/foo/bar/index_es.html</LI>
     *</UL>
     *
     *  @param base the directory that is the Document Base for the context
     *			in which the 'path' passed is expected to be located
     *  @param path the pathname for the file that is being searched for its
     *			best-matched Localized version
     *  @param loc the requested Locale to match
     *  @param fbloc the fallback Locale if the requested one not found
     *
     *  @return the name of the file that best-matched the Locale requested
     */
    public static String getDocBaseLocalizedResource (String base,
					              String path,
					              Locale loc,
					              Locale fbloc) {
	
	String locRes = null;

	//  test first for safePath
	//
	locRes = safePath (base, path);

	//  if it was not safe as requested, it will not be safe after
	//  localization lookup. Localization name transformations do not
	//  affect the safety of the file.
	//
	if (null == locRes)
	    return null;

	if (null != loc)
	    locRes = getDocBaseLocalizedPath (base, path, loc);

	if (null != locRes)
	    return locRes;

	if (null != fbloc)
	    locRes= getDocBaseLocalizedPath (base, path, fbloc);

	return locRes;
    }

    /**  internal method to check the existence of a file
     *
     *  @param base the docbase where the file is
     *  @param info the pathinfo component under the docbase
     *
     *  @return a TestedFile object for that base+info
     */
    private static TestedFile checkFile (String base, String info)
    {
	return new TestedFile (base, info);
    }

    /**  method to perform docBase localization
     *
     *  The method performs a resource lookup in a manner similar to the
     *  one performed by JavaHelp.
     * 
     *  See above explanation of lookup order.
     *
     *  @param dBase the document base for the context of the document
     *  @param path the path to the document
     *  @param loc the Locale from the request
     *
     *  @return the String for the localized path
     */
    private static String getDocBaseLocalizedPath (String dBase, String path,
						   Locale loc)
    {
	String rLang = loc.getLanguage();
	String rCoun = loc.getCountry();
	String rVar  = loc.getVariant();
	String ldBase = dBase.endsWith(File.separator)
			    		? dBase
			    		: (dBase + File.separatorChar);

	TestedFile tf = null;

	//  first, try with the provided language+country+variant
	//
	String base = null;

	if (null != rVar && ! ("".equals (rVar)) )
	{
	    base = ldBase + rLang + '_' + rCoun + '_' + rVar;

	    if ((tf = checkFile(base, path)).getFile().exists())
		return tf.getPath();
	}

	//  then, try with the provided language+country
	//
	if (null != rCoun && ! ("".equals (rCoun)) )
	{
	    base = ldBase + rLang + '_' + rCoun;

	    if ((tf = checkFile(base, path)).getFile().exists())
		return tf.getPath();
	}

	//  now, try with the provided language only
	//
	base = ldBase + rLang;

	if ((tf = checkFile(base, path)).getFile().exists())
	    return tf.getPath();

	return checkFile(dBase, path).getPath();
    }

    static String concatPath( String s1, String s2 )
    {
        if( s1.endsWith( File.separator ) ) {
            if( s2.startsWith( File.separator ))
                return s1 + s2.substring(1);
            else
                return s1 + s2;
        } else {
            if( s2.startsWith(File.separator))
                return s1 + s2;
            else
                return s1 + File.separatorChar + s2;
        }
    }

    /**  inner class to perform file tests
     */
    public static class TestedFile {

	File	file = null;
	String	path = null;

	/**  class to return the combination of File and pathname
	 *
	 *  @param base the directory name for the document base 
	 *  @param path the pathname for the file under the document base
	 */
	public TestedFile (String base, String path)
	{
	    if ((path == null) || (path.length() < 1))
		this.path = base;
	    else
		this.path = concatPath (base, path);

	    file = new File (this.path);
	}

	/**  method to obtain the path for the file being tested
	 *
	 *  @return a String with the path for the file associated
	 */
	public String getPath ()
	{
	    return path;
	}

	/**  method to obtain the File for the path being tested
	 *
	 *  @return a File corresponding to the path passed.
	 */
	public File getFile ()
	{
	    return file;
	}
    }
}

⌨️ 快捷键说明

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