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

📄 miscutilities.java

📁 Java写的文本编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * Escapes newlines, tabs, backslashes, quotes in the specified	 * string.	 * @param str The string	 * @param history jEdit history files require additional escaping	 * @since jEdit 2.7pre2	 */	public static String charsToEscapes(String str, boolean history)	{		StringBuffer buf = new StringBuffer();		for(int i = 0; i < str.length(); i++)		{			char c = str.charAt(i);			switch(c)			{			case '\n':				buf.append("\\n");				break;			case '\t':				buf.append("\\t");				break;			case '[':				if(history)					buf.append("\\[");				else					buf.append(c);				break;			case ']':				if(history)					buf.append("\\]");				else					buf.append(c);				break;			case '"':				if(history)					buf.append(c);				else					buf.append("\\\"");				break;			case '\'':				if(history)					buf.append(c);				else					buf.append("\\\'");				break;			case '\\':				buf.append("\\\\");				break;			default:				buf.append(c);				break;			}		}		return buf.toString();	}	/**	 * Sorts the specified array.	 * @param obj The array	 * @param compare Compares the objects	 */	public static void quicksort(Object[] obj, Compare compare)	{		if(obj.length == 0)			return;		quicksort(obj,0,obj.length - 1,compare);	}	/**	 * Sorts the specified vector.	 * @param vector The vector	 * @param compare Compares the objects	 */	public static void quicksort(Vector vector, Compare compare)	{		if(vector.size() == 0)			return;		quicksort(vector,0,vector.size() - 1,compare);	}	/**	 * An interface for comparing objects.	 */	public interface Compare	{		int compare(Object obj1, Object obj2);	}	/**	 * Compares strings.	 */	public static class StringCompare implements Compare	{		public int compare(Object obj1, Object obj2)		{			return obj1.toString().compareTo(obj2.toString());		}	}	/**	 * Compares strings ignoring case.	 */	public static class StringICaseCompare implements Compare	{		public int compare(Object obj1, Object obj2)		{			return obj1.toString().toLowerCase()				.compareTo(obj2.toString()				.toLowerCase());		}	}	public static class MenuItemCompare implements Compare	{		public int compare(Object obj1, Object obj2)		{			return ((JMenuItem)obj1).getText().compareTo(				((JMenuItem)obj2).getText());		}	}	/**	 * Compares two version strings formatted like 'xxx.xx.xxx'.	 * The versions string are tokenized at '.' and the tokens	 * are compared with each other one by one.	 * For each substring they are compared as Integers first	 * and if that fails, as Strings. The comparison ends with	 * the first difference.	 * Note, that "1.2.0" &lt; "1.2.0pre1", because "0" &lt; "0pre1".	 * Therefore you should avoid mixing numbers and text.	 * All string comparisons are case sensitive.	 */	public static class VersionCompare implements Compare	{		/**		 * compare two version strings 		 * @param obj1 first version. Should be a String.		 * @param obj2 secons version. Should be a String.		 * @return a negative value, if <code>obj1 < obj2</code>, 		 *         a positive value, if <code>obj1 > obj2</code>,		 *         0, if <code>obj1.equals(obj2)</code>.		 */		public int compare(Object obj1, Object obj2)		{			String v1 = obj1.toString();			String v2 = obj2.toString();			StringTokenizer vt1 = new StringTokenizer(v1,".");			StringTokenizer vt2 = new StringTokenizer(v2,".");			int comp = 0;						while(vt1.hasMoreTokens() && vt2.hasMoreTokens()) {				String vt1tok = vt1.nextToken();				String vt2tok = vt2.nextToken();				try				{					int i1 = Integer.parseInt(vt1tok);					int i2 = Integer.parseInt(vt2tok);					comp = i1 < i2 ? -1 : i1 > i2 ? 1 : 0;				}				catch(NumberFormatException e)				{						comp = vt1tok.compareTo(vt2tok);				}				if(comp != 0)					return comp;			}						return vt1.hasMoreTokens() ? 1 				: vt2.hasMoreTokens() ? -1 : 0;		}	}	/**	 * Helper function to compare two version strings, using the 	 * VersionCompare class.	 * @param version1 the first version string	 * @param version2 the second version string	 * @return a negative value, if <code>version1 &lt; version2</code>, 	 *         a positive value, if <code>version1 &gt; version2</code>,	 *         0, if <code>version1.equals(version2)</code>.	 */	public static int compareVersions(String version1, String version2)	{		VersionCompare comparator = new VersionCompare();		return comparator.compare(version1,version2);	}		/**	 * Converts an internal version number (build) into a	 * `human-readable' form.	 * @param build The build	 */	public static String buildToVersion(String build)	{		if(build.length() != 11)			return "<unknown version: " + build + ">";		// First 2 chars are the major version number		int major = Integer.parseInt(build.substring(0,2));		// Second 2 are the minor number		int minor = Integer.parseInt(build.substring(3,5));		// Then the pre-release status		int beta = Integer.parseInt(build.substring(6,8));		// Finally the bug fix release		int bugfix = Integer.parseInt(build.substring(9,11));		return "" + major + "." + minor			+ (beta != 99 ? "pre" + beta :			(bugfix != 0 ? "." + bugfix : "final"));	}	/**	 * If on JDK 1.2 or higher, make sure that tools.jar is available.	 * This method should be called by plugins requiring the classes	 * in this library.	 * <p>	 * tools.jar is searched for in the following places:	 * <ol>	 *   <li>the classpath that was used when jEdit was started,	 *   <li>jEdit's jars folder in the user's home,	 *   <li>jEdit's system jars folder,	 *   <li><i>java.home</i>/lib/. In this case, tools.jar is added to	 *       jEdit's list of known jars using jEdit.addPluginJAR(),	 *       so that it gets loaded through JARClassLoader.	 * </ol><p>	 *	 * On older JDK's this method does not perform any checks, and returns	 * <code>true</code> (even though there is no tools.jar).	 *	 * @return <code>false</code> if and only if on JDK 1.2 and tools.jar	 *    could not be found. In this case it prints some warnings on Log,	 *    too, about the places where it was searched for.	 * @since jEdit 3.2.2	 */	public static boolean isToolsJarAvailable()	{		String javaVersion = System.getProperty("java.version");		if(compareVersions(javaVersion, "1.2") < 0)			return true;		Log.log(Log.DEBUG, MiscUtilities.class, "JDK 1.2 or higher "			+ "detected, searching for tools.jar...");		Vector paths = new Vector();		// 1. Check whether tools.jar is in the system classpath:		paths.addElement("System classpath: "			+ System.getProperty("java.class.path"));		try		{			// Either class sun.tools.javac.Main or			// com.sun.tools.javac.Main must be there:			try			{				Class.forName("sun.tools.javac.Main");			}			catch(ClassNotFoundException e1)			{				Class.forName("com.sun.tools.javac.Main");			}			Log.log(Log.DEBUG, MiscUtilities.class,				"- is in classpath. Fine.");			return true;		}		catch(ClassNotFoundException e)		{			//Log.log(Log.DEBUG, MiscUtilities.class,			//	"- is not in system classpath.");		}		// 2. Check whether it is in the jEdit user settings jars folder:		String settingsDir = jEdit.getSettingsDirectory();		if(settingsDir != null)		{			String toolsPath = constructPath(settingsDir, "jars",				"tools.jar");			paths.addElement(toolsPath);			if(new File(toolsPath).exists())			{				Log.log(Log.DEBUG, MiscUtilities.class,					"- is in the user's jars folder. Fine.");				// jEdit will load it automatically				return true;			}		}		// 3. Check whether it is in jEdit's system jars folder:		String jEditDir = jEdit.getJEditHome();		String toolsPath = constructPath(jEditDir, "jars", "tools.jar");		paths.addElement(toolsPath);		if(new File(toolsPath).exists())		{			Log.log(Log.DEBUG, MiscUtilities.class,				"- is in jEdit's system jars folder. Fine.");			// jEdit will load it automatically			return true;		}		// 4. Check whether it is in <java.home>/lib:		toolsPath = System.getProperty("java.home");		if(toolsPath.toLowerCase().endsWith(File.separator + "jre"))			toolsPath = toolsPath.substring(0, toolsPath.length() - 4);		toolsPath = constructPath(toolsPath, "lib", "tools.jar");		paths.addElement(toolsPath);		if(!(new File(toolsPath).exists()))		{			Log.log(Log.WARNING, MiscUtilities.class,				"Could not find tools.jar.\n"				+ "I checked the following locations:\n"				+ paths.toString());			return false;		}		// Load it, if not yet done:		EditPlugin.JAR jar = jEdit.getPluginJAR(toolsPath);		if(jar == null)		{			Log.log(Log.DEBUG, MiscUtilities.class,				"- adding " + toolsPath + " to jEdit plugins.");			try			{				jEdit.addPluginJAR(new EditPlugin.JAR(toolsPath,					new JARClassLoader(toolsPath)));			}			catch(IOException ioex)			{				Log.log(Log.ERROR, MiscUtilities.class,					"- I/O error loading " + toolsPath);				Log.log(Log.ERROR, MiscUtilities.class, ioex);				return false;			}		}		else			Log.log(Log.DEBUG, MiscUtilities.class,				"- has been loaded before.");		return true;	}	// private members	private MiscUtilities() {}	private static String canonPath(String path)	{		if(File.separatorChar == '\\')		{			// get rid of mixed paths on Windows			path = path.replace('/','\\');		}		try		{			return new File(path).getCanonicalPath();		}		catch(Exception e)		{			return path;		}	}	private static void quicksort(Object[] obj, int _start, int _end,		Compare compare)	{		int start = _start;		int end = _end;		Object mid = obj[(_start + _end) / 2];		if(_start > _end)			return;		while(start <= end)		{			while((start < _end) && (compare.compare(obj[start],mid) < 0))				start++;			while((end > _start) && (compare.compare(obj[end],mid) > 0))				end--;			if(start <= end)			{				Object o = obj[start];				obj[start] = obj[end];				obj[end] = o;				start++;				end--;			}		}		if(_start < end)			quicksort(obj,_start,end,compare);		if(start < _end)			quicksort(obj,start,_end,compare);	}	private static void quicksort(Vector obj, int _start, int _end,		Compare compare)	{		int start = _start;		int end = _end;		Object mid = obj.elementAt((_start + _end) / 2);		if(_start > _end)			return;		while(start <= end)		{			while((start < _end) && (compare.compare(obj.elementAt(start),mid) < 0))				start++;			while((end > _start) && (compare.compare(obj.elementAt(end),mid) > 0))				end--;			if(start <= end)			{				Object o = obj.elementAt(start);				obj.setElementAt(obj.elementAt(end),start);				obj.setElementAt(o,end);				start++;				end--;			}		}		if(_start < end)			quicksort(obj,_start,end,compare);		if(start < _end)			quicksort(obj,start,_end,compare);	}}

⌨️ 快捷键说明

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