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

📄 miscutilities.java

📁 Java写的文本编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * MiscUtilities.java - Various miscallaneous utility functions * Copyright (C) 1999, 2000, 2001 Slava Pestov * Portions copyright (C) 2000 Richard S. Hall * Portions copyright (C) 2001 Dirk Moebius * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */package org.gjt.sp.jedit;import javax.swing.JMenuItem;import java.io.*;import java.util.Vector;import java.util.StringTokenizer;import org.gjt.sp.util.Log;/** * Class with several useful miscellaneous functions.<p> * * It provides methods for converting file names to class names, for * constructing path names, and for various indentation calculations. * A quicksort implementation is also available. * * @author Slava Pestov * @version $Id: MiscUtilities.java,v 1.2 2001/09/05 01:25:05 spestov Exp $ */public class MiscUtilities{	/**	 * Converts a file name to a class name. All slash characters are	 * replaced with periods and the trailing '.class' is removed.	 * @param name The file name	 */	public static String fileToClass(String name)	{		char[] clsName = name.toCharArray();		for(int i = clsName.length - 6; i >= 0; i--)			if(clsName[i] == '/')				clsName[i] = '.';		return new String(clsName,0,clsName.length - 6);	}	/**	 * Converts a class name to a file name. All periods are replaced	 * with slashes and the '.class' extension is added.	 * @param name The class name	 */	public static String classToFile(String name)	{		return name.replace('.','/').concat(".class");	}	/**	 * Constructs an absolute path name from a directory and another	 * path name.	 * @param parent The directory	 * @param path The path name	 */	public static String constructPath(String parent, String path)	{		if(new File(path).isAbsolute())			return canonPath(path);		if(parent == null)			parent = System.getProperty("user.dir");		// have to handle these cases specially on windows.		if(File.separatorChar == '\\')		{			if(path.length() == 2 && path.charAt(1) == ':')				return path;			if(path.startsWith("/") || path.startsWith("\\"))				parent = parent.substring(0,2);		}		if(parent.endsWith(File.separator) || path.endsWith("/"))			return canonPath(parent + path);		else			return canonPath(parent + File.separator + path);	}	/**	 * Constructs an absolute path name from three path components.	 * @param parent The parent directory	 * @param path1 The first path	 * @param path2 The second path	 */	public static String constructPath(String parent,		String path1, String path2)	{		return constructPath(constructPath(parent,path1),path2);	}	/**	 * Like constructPath(), except <code>path</code> will be	 * appended to <code>parent</code> even if it is absolute.	 * @param path	 * @param parent	 */	public static String concatPath(String parent, String path)	{		// Make all child paths relative.		if (path.startsWith(File.separator))			path = path.substring(1);		else if ((path.length() >= 3) && (path.charAt(1) == ':'))			path = path.replace(':', File.separatorChar);		if (parent == null)			parent = System.getProperty("user.dir");		if (parent.endsWith(File.separator))			return parent + path;		else			return parent + File.separator + path;	}		/**	 * Returns the extension of the specified filename, or an empty	 * string if there is none.	 * @param name The file name	 */	public static String getFileExtension(String name)	{		int index = name.indexOf('.');		if(index == -1)			return "";		else			return name.substring(index);	}	/**	 * For use with local files only - returns the last component	 * of the specified path.	 * @param path The path name	 */	public static String getFileName(String path)	{		int count = Math.max(0,path.length() - 2);		int index1 = path.lastIndexOf(File.separatorChar,count);		int index2 = path.lastIndexOf('/',count);		return path.substring(Math.max(index1,index2) + 1);	}	/**	 * @deprecated Call getParentOfPath() instead	 */	public static String getFileParent(String path)	{		return getParentOfPath(path);	}	/**	 * For use with local files only - returns the parent of the	 * specified path.	 * @param path The path name	 * @since jEdit 2.6pre5	 */	public static String getParentOfPath(String path)	{		// ignore last character of path to properly handle		// paths like /foo/bar/		int count = Math.max(0,path.length() - 2);		int index = path.lastIndexOf(File.separatorChar,count);		if(index == -1)			index = path.lastIndexOf('/',count);		if(index == -1)		{			// this ensures that getFileParent("protocol:"), for			// example, is "protocol:" and not "".			index = path.lastIndexOf(':');		}		return path.substring(0,index + 1);	}	/**	 * @deprecated Call getProtocolOfURL() instead	 */	public static String getFileProtocol(String url)	{		return getProtocolOfURL(url);	}	/**	 * Returns the protocol specified by a URL.	 * @param url The URL	 * @since jEdit 2.6pre5	 */	public static String getProtocolOfURL(String url)	{		return url.substring(0,url.indexOf(':'));	}	/**	 * Checks if the specified string is a URL.	 * @param str The string to check	 * @return True if the string is a URL, false otherwise	 */	public static boolean isURL(String str)	{		int fsIndex = Math.max(str.indexOf(File.separatorChar),			str.indexOf('/'));		if(fsIndex == 0) // /etc/passwd			return false;		else if(fsIndex == 2) // C:\AUTOEXEC.BAT			return false;		int cIndex = str.indexOf(':');		if(cIndex <= 1) // D:\WINDOWS			return false;		else if(fsIndex != -1 && cIndex > fsIndex) // /tmp/RTF::read.pm			return false;		return true;	}	/**	 * Returns the number of leading white space characters in the	 * specified string.	 * @param str The string	 */	public static int getLeadingWhiteSpace(String str)	{		int whitespace = 0;loop:		for(;whitespace < str.length();)		{			switch(str.charAt(whitespace))			{			case ' ': case '\t':				whitespace++;				break;			default:				break loop;			}		}		return whitespace;	}	/**	 * Returns the number of trailing whitespace characters in the	 * specified string.	 * @param str The string	 * @since jEdit 2.5pre5	 */	public static int getTrailingWhiteSpace(String str)	{		int whitespace = 0;loop:		for(int i = str.length() - 1; i >= 0; i--)		{			switch(str.charAt(i))			{			case ' ': case '\t':				whitespace++;				break;			default:				break loop;			}		}		return whitespace;	}	/**	 * Returns the width of the leading white space in the specified	 * string.	 * @param str The string	 * @param tabSize The tab size	 */	public static int getLeadingWhiteSpaceWidth(String str, int tabSize)	{		int whitespace = 0;loop:		for(int i = 0; i < str.length(); i++)		{			switch(str.charAt(i))			{			case ' ':				whitespace++;				break;			case '\t':				whitespace += (tabSize - whitespace % tabSize);				break;			default:				break loop;			}		}		return whitespace;	}	/**	 * Creates a string of white space with the specified length.	 * @param len The length	 * @param tabSize The tab size, or 0 if tabs are not to be used	 */	public static String createWhiteSpace(int len, int tabSize)	{		StringBuffer buf = new StringBuffer();		if(tabSize == 0)		{			while(len-- > 0)				buf.append(' ');		}		else				{			int count = len / tabSize;			while(count-- > 0)				buf.append('\t');			count = len % tabSize;			while(count-- > 0)				buf.append(' ');		}		return buf.toString();	}	/**	 * Converts a Unix-style glob to a regular expression.	 * ? becomes ., * becomes .*, {aa,bb} becomes (aa|bb).	 * @param glob The glob pattern	 */	public static String globToRE(String glob)	{		StringBuffer buf = new StringBuffer();		boolean backslash = false;		boolean insideGroup = false;		for(int i = 0; i < glob.length(); i++)		{			char c = glob.charAt(i);			if(backslash)			{				buf.append('\\');				buf.append(c);				backslash = false;				continue;			}			switch(c)			{			case '\\':				backslash = true;				break;			case '?':				buf.append('.');				break;			case '.':				buf.append("\\.");				break;			case '*':				buf.append(".*");				break;			case '{':				buf.append('(');				insideGroup = true;				break;			case ',':				if(insideGroup)					buf.append('|');				else					buf.append(',');				break;			case '}':				buf.append(')');				insideGroup = false;				break;			default:				buf.append(c);			}		}		return buf.toString();	}	/**	 * Converts "\n" and "\t" escapes in the specified string to	 * newlines and tabs.	 * @param str The string	 * @since jEdit 2.3pre1	 */	public static String escapesToChars(String str)	{		StringBuffer buf = new StringBuffer();		for(int i = 0; i < str.length(); i++)		{			char c = str.charAt(i);			switch(c)			{			case '\\':				if(i == str.length() - 1)				{					buf.append('\\');					break;				}				c = str.charAt(++i);				switch(c)				{				case 'n':					buf.append('\n');					break;				case 't':					buf.append('\t');					break;				default:					buf.append(c);					break;				}				break;			default:				buf.append(c);			}		}		return buf.toString();	}	/**	 * Escapes newlines, tabs, backslashes, quotes in the specified	 * string.	 * @param str The string	 * @since jEdit 2.3pre1	 */	public static String charsToEscapes(String str)	{		return charsToEscapes(str,false);	}	/**

⌨️ 快捷键说明

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