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

📄 project.java

📁 A static analysis tool to find bugs in Java programs
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				Attributes mainAttrs = manifest.getMainAttributes();				String classPath = mainAttrs.getValue("Class-Path");				if (classPath != null) {					String[] fileList = classPath.split("\\s+");					for (String jarFile : fileList) {						URL referencedURL = workList.createRelativeURL(jarFileURL, jarFile);						if (workList.add(new WorkListItem(referencedURL))) {							implicitClasspath.add(referencedURL.toString());							if (DEBUG) System.out.println("Implicit jar: " + referencedURL.toString());						}					}				}			} finally {				if (in != null) {					in.close();				}			}		} catch (IOException ignore) {			// Ignore		}	}	private static final String OPTIONS_KEY = "[Options]";	private static final String JAR_FILES_KEY = "[Jar files]";	private static final String SRC_DIRS_KEY = "[Source dirs]";	private static final String AUX_CLASSPATH_ENTRIES_KEY = "[Aux classpath entries]";	// Option keys	public static final String RELATIVE_PATHS = "relative_paths";	/**	 * Save the project to an output file.	 *	 * @param outputFile       name of output file	 * @param useRelativePaths true if the project should be written	 *                         using only relative paths	 * @param relativeBase     if useRelativePaths is true,	 *                         this file is taken as the base directory in terms of which	 *                         all files should be made relative	 * @throws IOException if an error occurs while writing	 */	@Deprecated	public void write(String outputFile, boolean useRelativePaths, String relativeBase)			throws IOException {		PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));		try {			writer.println(JAR_FILES_KEY);			for (String jarFile : fileList) {				if (useRelativePaths)					jarFile = convertToRelative(jarFile, relativeBase);				writer.println(jarFile);			}			writer.println(SRC_DIRS_KEY);			for (String srcDir : srcDirList) {				if (useRelativePaths)					srcDir = convertToRelative(srcDir, relativeBase);				writer.println(srcDir);			}			writer.println(AUX_CLASSPATH_ENTRIES_KEY);			for (String auxClasspathEntry : auxClasspathEntryList) {				if (useRelativePaths)					auxClasspathEntry = convertToRelative(auxClasspathEntry, relativeBase);				writer.println(auxClasspathEntry);			}			if (useRelativePaths) {				writer.println(OPTIONS_KEY);				writer.println(RELATIVE_PATHS + "=true");			}		} finally {			writer.close();		}		// Project successfully saved		isModified = false;	}	public static Project readXML(File f) throws IOException, DocumentException, SAXException {		Project project = new Project();		InputStream in = new BufferedInputStream(new FileInputStream(f));		String tag = Util.getXMLType(in);		SAXBugCollectionHandler handler;		if (tag.equals("Project")) {			handler = new SAXBugCollectionHandler(project, f);		} else if (tag.equals("BugCollection")) {			SortedBugCollection bugs = new SortedBugCollection();			handler = new SAXBugCollectionHandler(bugs, project, f);		} else throw new IOException("Can't load a project from a " + tag + " file");		try {			XMLReader xr = null;			if (true) try { 				xr = XMLReaderFactory.createXMLReader();			} catch (SAXException e) {				AnalysisContext.logError("Couldn't create XMLReaderFactory", e);   			}			if (xr == null) {				xr = new org.dom4j.io.aelfred.SAXDriver();			}			xr.setContentHandler(handler);			xr.setErrorHandler(handler);			Reader reader = Util.getReader(in);			xr.parse(new InputSource(reader));		}		finally {			in.close();		}		// Presumably, project is now up-to-date		project.setModified(false);		return project;	}	public  void writeXML(File f) throws IOException {		OutputStream out  = new FileOutputStream(f);		XMLOutput xmlOutput = new OutputStreamXMLOutput(out);		try {			writeXML(xmlOutput);		} finally {			xmlOutput.finish();		}	}	/**	 * Read the project from an input file.	 * This method should only be used on an empty Project	 * (created with the default constructor).	 *	 * @param inputFile name of the input file to read the project from	 * @throws IOException if an error occurs while reading	 */	public void read(String inputFile) throws IOException {		if (isModified)			throw new IllegalStateException("Reading into a modified Project!");		// Make the input file absolute, if necessary		File file = new File(inputFile);		if (!file.isAbsolute())			inputFile = file.getAbsolutePath();		// Store the project filename		setProjectFileName(inputFile);		BufferedReader reader = null;		try {			reader = new BufferedReader(Util.getFileReader(inputFile));			String line;			line = getLine(reader);			if (line == null || !line.equals(JAR_FILES_KEY))				throw new IOException("Bad format: missing jar files key");			while ((line = getLine(reader)) != null && !line.equals(SRC_DIRS_KEY)) {				addToListInternal(fileList, line);			}			if (line == null)				throw new IOException("Bad format: missing source dirs key");			while ((line = getLine(reader)) != null && !line.equals(AUX_CLASSPATH_ENTRIES_KEY)) {				addToListInternal(srcDirList, line);			}			// The list of aux classpath entries is optional			if (line != null) {				while ((line = getLine(reader)) != null) {					if (line.equals(OPTIONS_KEY))						break;					addToListInternal(auxClasspathEntryList, line);				}			}			// The Options section is also optional			if (line != null && line.equals(OPTIONS_KEY)) {				while ((line = getLine(reader)) != null && !line.equals(JAR_FILES_KEY))					parseOption(line);			}			// If this project has the relative paths option set,			// resolve all internal relative paths into absolute			// paths, using the absolute path of the project			// file as a base directory.			if (getOption(RELATIVE_PATHS)) {				makeListAbsoluteProject(fileList);				makeListAbsoluteProject(srcDirList);				makeListAbsoluteProject(auxClasspathEntryList);			}			// Clear the modification flag set by the various "add" methods.			isModified = false;		} finally {			if (reader != null)				reader.close();		}	}			/**	 * Read a line from a BufferedReader, ignoring blank lines	 * and comments.	 */	private static String getLine(BufferedReader reader) throws IOException {		String line;		while ((line = reader.readLine()) != null) {			line = line.trim();			if (!line.equals("") && !line.startsWith("#"))				break;		}		return line;	}	public String projectNameFromProjectFileName() {		String name = projectFileName;		int lastSep = name.lastIndexOf(File.separatorChar);		if (lastSep >= 0)			name = name.substring(lastSep + 1);		int dot = name.lastIndexOf('.');		if (dot >= 0)			name = name.substring(0, dot);		return name;	}	/**	 * Convert to a string in a nice (displayable) format.	 */	@Override	public String toString() {		String name = projectFileName;		int lastSep = name.lastIndexOf(File.separatorChar);		if (lastSep >= 0)			name = name.substring(lastSep + 1);		//int dot = name.lastIndexOf('.');		//Don't hide every suffix--some are informative and/or disambiguative.		int dot = (name.endsWith(".fb") ? name.length()-3 : -1);		if (dot >= 0)			name = name.substring(0, dot);		return name;	}	/**	 * Transform a user-entered filename into a proper filename,	 * by adding the ".fb" file extension if it isn't already present.	 */	public static String transformFilename(String fileName) {		if (!fileName.endsWith(".fb"))			fileName = fileName + ".fb";		return fileName;	}	static final String JAR_ELEMENT_NAME = "Jar";	static final String AUX_CLASSPATH_ENTRY_ELEMENT_NAME = "AuxClasspathEntry";	static final String SRC_DIR_ELEMENT_NAME = "SrcDir";	static final String FILENAME_ATTRIBUTE_NAME = "filename";	static final String PROJECTNAME_ATTRIBUTE_NAME = "projectName";	public void writeXML(XMLOutput xmlOutput) throws IOException {		XMLAttributeList attributeList = new XMLAttributeList().addAttribute(FILENAME_ATTRIBUTE_NAME, getProjectFileName());		if (getProjectName() != null)			attributeList = attributeList.addAttribute(PROJECTNAME_ATTRIBUTE_NAME, getProjectName());		xmlOutput.openTag(				BugCollection.PROJECT_ELEMENT_NAME,				attributeList				);		XMLOutputUtil.writeElementList(xmlOutput, JAR_ELEMENT_NAME, fileList);		XMLOutputUtil.writeElementList(xmlOutput, AUX_CLASSPATH_ENTRY_ELEMENT_NAME, auxClasspathEntryList);		XMLOutputUtil.writeElementList(xmlOutput, SRC_DIR_ELEMENT_NAME, srcDirList);		if (suppressionFilter != null && !suppressionFilter.isEmpty()) {			xmlOutput.openTag("SuppressionFilter");			suppressionFilter.writeBodyAsXML(xmlOutput);			xmlOutput.closeTag("SuppressionFilter");		}		xmlOutput.closeTag(BugCollection.PROJECT_ELEMENT_NAME);	}	/**	 * Parse one line in the [Options] section.	 *	 * @param option one line in the [Options] section	 */	private void parseOption(String option) throws IOException {		int equalPos = option.indexOf("=");		if (equalPos < 0)			throw new IOException("Bad format: invalid option format");		String name = option.substring(0, equalPos);		String value = option.substring(equalPos + 1);		optionsMap.put(name, Boolean.valueOf(value));	}	/**	 * Hack for whether files are case insensitive.	 * For now, we'll assume that Windows is the only	 * case insensitive OS.  (OpenVMS users,	 * feel free to submit a patch :-)	 */	private static final boolean FILE_IGNORE_CASE =			SystemProperties.getProperty("os.name", "unknown").startsWith("Windows");	/**	 * Converts a full path to a relative path if possible	 *	 * @param srcFile path to convert	 * @return the converted filename	 */	private String convertToRelative(String srcFile, String base) {		String slash = SystemProperties.getProperty("file.separator");		if (FILE_IGNORE_CASE) {			srcFile = srcFile.toLowerCase();			base = base.toLowerCase();		}		if (base.equals(srcFile))			return ".";		if (!base.endsWith(slash))			base = base + slash;		if (base.length() <= srcFile.length()) {			String root = srcFile.substring(0, base.length());			if (root.equals(base)) {				// Strip off the base directory, make relative				return "." + SystemProperties.getProperty("file.separator") + srcFile.substring(base.length());			}		}		//See if we can build a relative path above the base using .. notation		int slashPos = srcFile.indexOf(slash);		int branchPoint;		if (slashPos >= 0) {			String subPath = srcFile.substring(0, slashPos);			if ((subPath.length() == 0) || base.startsWith(subPath)) {				branchPoint = slashPos + 1;				slashPos = srcFile.indexOf(slash, branchPoint);				while (slashPos >= 0) {					subPath = srcFile.substring(0, slashPos);					if (base.startsWith(subPath))						branchPoint = slashPos + 1;					else						break;					slashPos = srcFile.indexOf(slash, branchPoint);				}				int slashCount = 0;				slashPos = base.indexOf(slash, branchPoint);				while (slashPos >= 0) {					slashCount++;					slashPos = base.indexOf(slash, slashPos + 1);				}				StringBuffer path = new StringBuffer();				String upDir = ".." + slash;				for (int i = 0; i < slashCount; i++)					path.append(upDir);				path.append(srcFile.substring(branchPoint));				return path.toString();			}		}		return srcFile;	}	/**	 * Converts a relative path to an absolute path if possible.	 *	 * @param fileName path to convert	 * @return the converted filename	 */	private String convertToAbsolute(String fileName) throws IOException {		// At present relative paths are only calculated if the fileName is		// below the project file. This need not be the case, and we could use ..		// syntax to move up the tree. (To Be Added)		File file = new File(fileName);		if (!file.isAbsolute()) {			// Only try to make the relative path absolute			// if the project file is absolute.			File projectFile = new File(projectFileName);			if (projectFile.isAbsolute()) {				// Get base directory (parent of the project file)				String base = new File(projectFileName).getParent();				// Make the file absolute in terms of the parent directory				fileName = new File(base, fileName).getCanonicalPath();			}		}		return fileName;	}	/**	 * Make the given filename absolute relative to the	 * current working directory.	 */	private  String makeAbsoluteCWD(String fileName) {		boolean hasProtocol = (URLClassPath.getURLProtocol(fileName) != null);		if (hasProtocol) return fileName;		if (new File(fileName).isAbsolute()) return fileName;		return new File(currentWorkingDirectory, fileName).getAbsolutePath();	}	/**	 * Add a value to given list, making the Project modified	 * if the value is not already present in the list.	 *	 * @param list  the list	 * @param value the value to be added	 * @return true if the value was not already present in the list,	 *         false otherwise	 */	private boolean addToListInternal(List<String> list, String value) {		if (!list.contains(value)) {			list.add(value);			isModified = true;			return true;		} else			return false;	}	/**	 * Make the given list of pathnames absolute relative	 * to the absolute path of the project file.	 */	private void makeListAbsoluteProject(List<String> list) throws IOException {		List<String> replace = new LinkedList<String>();		for (String fileName : list) {			fileName = convertToAbsolute(fileName);			replace.add(fileName);		}		list.clear();		list.addAll(replace);	}	/**	 * @param timestamp The timestamp to set.	 */	public void setTimestamp(long timestamp) {		this.timestamp = timestamp;	}	public void addTimestamp(long timestamp) {		if (this.timestamp < timestamp)			this.timestamp = timestamp;	}	/**	 * @return Returns the timestamp.	 */	public long getTimestamp() {		return timestamp;	}	/**	 * @param projectName The projectName to set.	 */	public void setProjectName(String projectName) {		this.projectName = projectName;	}	/**	 * @return Returns the projectName.	 */	public String getProjectName() {		return projectName;	}	/**     * @param suppressionFilter The suppressionFilter to set.     */    public void setSuppressionFilter(Filter suppressionFilter) {	    this.suppressionFilter = suppressionFilter;    }	/**     * @return Returns the suppressionFilter.     */    public Filter getSuppressionFilter() {    	if (suppressionFilter == null)    		suppressionFilter = new Filter();	    return suppressionFilter;    }}// vim:ts=4

⌨️ 快捷键说明

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