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

📄 tomcatproject.java

📁 一个学习eclipse插件开发的绝好入门程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

		String context = contextBuffer.toString();			
		if (getWebClassPathEntries() != null) {
			context = this.addLoaderToContext(context);
		}
		if(getRedirectLogger()) {
			context = this.addLoggerToContext(context);
		}
		if(!(getExtraInfo().equals(""))) {
			context = this.addExtraInfoToContext(context);
		}
		
		return context;
	
	}

	private String updateContextDefinition(String context) {

		// if reloadable param not set
		int reloadableIndex = context.indexOf("reloadable");
		if(reloadableIndex == -1) {
			context = this.addReloadableToContext(context);
		} else {
			context = this.updateReloadableInContext(context);
		}
				
		// update docBase if set
		int docBaseIndex = context.indexOf("docBase");
		if(docBaseIndex == -1) {
			context = this.addDocBaseToContext(context);
		} else {
			context = this.updateDocBaseInContext(context);
		} 

		// if work param not set
		int workIndex = context.indexOf("workDir");
		if(workIndex == -1) {
			context = this.addWorkToContext(context);
		} else {
			context = this.updateWorkInContext(context);
		}

		// if loader not set
		int loaderIndex = context.indexOf("<Loader");
		if((loaderIndex == -1) && (getWebClassPathEntries() != null)) {
			context = this.addLoaderToContext(context);
		}
		if((loaderIndex != -1) && (getWebClassPathEntries() == null)) {
			context = this.removeLoaderInContext(context);
		}		
		if((loaderIndex != -1) && (getWebClassPathEntries() != null)) {
			context = this.updateLoaderInContext(context);
		}		
	

		// if logger not set
		int loggerIndex = context.indexOf("<Logger");
		if((loggerIndex == -1) && getRedirectLogger()) {
			context = this.addLoggerToContext(context);
		}
		if((loggerIndex != -1) && !getRedirectLogger()) {
			context = this.removeLoggerInContext(context);
		}		
		if((loggerIndex != -1) && getRedirectLogger()) {
			context = this.updateLoggerInContext(context);
		}
		
		// Extra info
		int extraInfoIndex = context.indexOf(extraBeginTag);
		if((extraInfoIndex == -1) && !(getExtraInfo().equals(""))) {
			context = this.addExtraInfoToContext(context);
		}
		if((extraInfoIndex != -1) && getExtraInfo().equals("")) {
			context = this.removeExtraInfoInContext(context);
		}		
		if((extraInfoIndex != -1) && !(getExtraInfo().equals(""))) {
			context = this.updateExtraInfoInContext(context);
		}
		return context;
	}
	
	private void updateContextDefinitionInFile(File xmlFile) throws IOException {
		String xml = FileUtil.readTextFile(xmlFile);	
		int contextTagIndex = this.getContextTagIndex(xml);
		
		// If context doesn't exist do nothing
		if( contextTagIndex == -1) {
			return;
		}			

		// Get context					
		int endContextTagIndex = xml.indexOf(">",contextTagIndex);
		if(xml.charAt(endContextTagIndex-1) != '/') {
			endContextTagIndex = xml.indexOf(this.getContextEndTag(), contextTagIndex) + this.getContextEndTag().length() -1; 	
		}
		String context = xml.substring(contextTagIndex, endContextTagIndex+1);

		StringBuffer out = new StringBuffer(xml.substring(0, contextTagIndex));
		out.append(this.updateContextDefinition(context));
		out.append(xml.substring(endContextTagIndex+1));
		FileUtil.toTextFile(xmlFile, out.toString());	
	}


	private String addDocBaseToContext(String context) {
		int reloadableIndex = context.indexOf(getContextReloadable());
		int firstDoubleQuoteIndex = context.indexOf('"', reloadableIndex) + 1;		
		int docBaseIndex = context.indexOf('"',firstDoubleQuoteIndex) + 1;
		StringBuffer out = new StringBuffer(context.substring(0, docBaseIndex));
		out.append(' ');
		out.append(getContextDocBase());
		out.append(' ');
		out.append(context.substring(docBaseIndex, context.length()));				
		
		return out.toString();			
	}

	private String updateDocBaseInContext(String context) {
		int docBaseIndex = context.indexOf("docBase");
		int startIndex = context.indexOf('"', docBaseIndex);
		int endIndex = context.indexOf('"',startIndex+1);
		StringBuffer out = new StringBuffer(context.substring(0, docBaseIndex));
		out.append(getContextDocBase());
		out.append(context.substring(endIndex+1, context.length()));
		
		return out.toString();			
	}

	private String getContextReloadable(){
		return ("reloadable=" + '"' + getReloadable() + '"');
	}

	private String addReloadableToContext(String context) {
		int pathIndex = context.indexOf(getContextPath());
		int firstDoubleQuoteIndex = context.indexOf('"', pathIndex) + 1;
		int reloadableIndex = context.indexOf('"',firstDoubleQuoteIndex) + 1;
		
		StringBuffer out = new StringBuffer(context.substring(0, reloadableIndex));
		out.append(' ');
		out.append(getContextReloadable());
		out.append(' ');
		out.append(context.substring(reloadableIndex, context.length()));				
		
		return out.toString();			
	}

	private String updateReloadableInContext(String context){
		int reloadableIndex = context.indexOf("reloadable");
		int startIndex = context.indexOf('"',reloadableIndex);
		int endIndex = context.indexOf('"',startIndex+1);
		StringBuffer out = new StringBuffer(context.substring(0,reloadableIndex));
		out.append(getContextReloadable());
		out.append(context.substring(endIndex+1,context.length()));
		
		return out.toString();
	}
	
	private String addWorkToContext(String context) {
		int docBaseIndex = context.indexOf("docBase");
		int firstDoubleQuoteIndex = context.indexOf('"',docBaseIndex) + 1;
		int workIndex = context.indexOf('"', firstDoubleQuoteIndex) + 1;
		StringBuffer out = new StringBuffer(context.substring(0, workIndex));
		out.append(' ');
		out.append(getContextWorkDir());
		out.append(' ');
		out.append(context.substring(workIndex, context.length()));				
		
		return out.toString();			
	}

	private String updateWorkInContext(String context) {
		int workDirIndex = context.indexOf("workDir");
		int startIndex = context.indexOf('"', workDirIndex);
		int endIndex = context.indexOf('"',startIndex+1);
		StringBuffer out = new StringBuffer(context.substring(0, workDirIndex));
		out.append(getContextWorkDir());
		out.append(context.substring(endIndex+1, context.length()));
		
		return out.toString();		
	}

	// Add </Context> instead of  />, and \n if needed
	private String formatContextEndTag(String context) {
		int endContextStartTagIndex = context.indexOf(">");
		StringBuffer newContext = new StringBuffer();		
		if(context.charAt(endContextStartTagIndex-1) == '/') {
			newContext.append(context.substring(0, endContextStartTagIndex-1));
			newContext.append(">");
			newContext.append("\n");
			newContext.append(getContextEndTag());
			newContext.append("\n");
			context = newContext.toString();
			endContextStartTagIndex--;	
		} else {
			int endContextTagIndex = context.indexOf(getContextEndTag());
			if(context.charAt(endContextTagIndex-1) != '\n') {
				newContext.append(context.substring(0,endContextTagIndex));
				newContext.append("\n");
				newContext.append(context.substring(endContextTagIndex));				
			} else {
				return context;
			}
		}
		
		return newContext.toString();
	}
	
	private String addLoaderToContext(String context) {
		context = this.formatContextEndTag(context);
		int endContextStartTagIndex = context.indexOf(">");		
		int loaderIndex = endContextStartTagIndex + 1;
		StringBuffer out = new StringBuffer(context.substring(0, loaderIndex));
		out.append(getContextWebAppClassLoader());
		out.append(context.substring(loaderIndex, context.length()));				
		
		return out.toString();			
	}

	private String updateLoaderInContext(String context) {
		context = this.formatContextEndTag(context);		
		int endContextStartTagIndex = context.indexOf(">");
		int startIndex = context.indexOf("<Loader", endContextStartTagIndex);
		if(context.charAt(startIndex-1) == '\t') startIndex--;
		if(context.charAt(startIndex-1) == '\n') startIndex--;		
		int endIndex = context.indexOf("/>",startIndex+1)+1;
		StringBuffer out = new StringBuffer(context.substring(0, startIndex));
		out.append(getContextWebAppClassLoader());
		out.append(context.substring(endIndex+1, context.length()));
		
		return out.toString();		
	}

	private String removeLoaderInContext(String context) {
		int endContextStartTagIndex = context.indexOf(">");
		int startIndex = context.indexOf("<Loader", endContextStartTagIndex);
		if(context.charAt(startIndex-1) == '\t') startIndex--;
		if(context.charAt(startIndex-1) == '\n') startIndex--;			
		int endIndex = context.indexOf("/>",startIndex+1)+1;
		StringBuffer out = new StringBuffer(context.substring(0, startIndex));
		out.append(context.substring(endIndex+1, context.length()));
		
		return out.toString();		
	}

	private String addLoggerToContext(String context) {
		context = this.formatContextEndTag(context);
		int endContextStartTagIndex = context.indexOf(">");		
		int loggerIndex = endContextStartTagIndex + 1;
		StringBuffer out = new StringBuffer(context.substring(0, loggerIndex));
		out.append(getContextLogger());
		out.append(context.substring(loggerIndex, context.length()));				
		
		return out.toString();			
	}

	private String updateLoggerInContext(String context) {
		int endContextStartTagIndex = context.indexOf(">");
		int startIndex = context.indexOf("<Logger", endContextStartTagIndex);
		if(context.charAt(startIndex-1) == '\t') startIndex--;
		if(context.charAt(startIndex-1) == '\n') startIndex--;			
		int endIndex = context.indexOf("/>",startIndex)+1;
		StringBuffer out = new StringBuffer(context.substring(0, startIndex));
		out.append(getContextLogger());
		out.append(context.substring(endIndex+1, context.length()));
		
		return out.toString();		
	}

	private String removeLoggerInContext(String context) {
		int endContextStartTagIndex = context.indexOf(">");
		int startIndex = context.indexOf("<Logger", endContextStartTagIndex);
		if(context.charAt(startIndex-1) == '\t') startIndex--;
		if(context.charAt(startIndex-1) == '\n') startIndex--;			
		int endIndex = context.indexOf("/>",startIndex)+1;
		StringBuffer out = new StringBuffer(context.substring(0, startIndex));
		out.append(context.substring(endIndex+1, context.length()));
		
		return out.toString();	
	}


	private String addExtraInfoToContext(String context) {
		context = this.formatContextEndTag(context);
		int endContextStartTagIndex = context.indexOf(">");		
		int extraInfoIndex = endContextStartTagIndex + 1;
		StringBuffer out = new StringBuffer(context.substring(0, extraInfoIndex));
		out.append('\n');	
		out.append(extraBeginTag);
		out.append('\n');
		out.append(getExtraInfo());
		out.append('\n');
		out.append(extraEndTag);						
		out.append(context.substring(extraInfoIndex, context.length()));				
		
		return out.toString();			
	}

	private String updateExtraInfoInContext(String context) {
		int endContextStartTagIndex = context.indexOf(">");
		int startIndex = context.indexOf(extraBeginTag, endContextStartTagIndex);
		if(context.charAt(startIndex-1) == '\t') startIndex--;
		if(context.charAt(startIndex-1) == '\n') startIndex--;			
		int extraEndTagStartIndex = context.indexOf(extraEndTag,startIndex);
		StringBuffer out = new StringBuffer(context.substring(0, startIndex));
		out.append('\n');		
		out.append(extraBeginTag);
		out.append('\n');
		out.append(getExtraInfo());
		out.append('\n');
		out.append(context.substring(extraEndTagStartIndex, context.length()));
		
		return out.toString();		
	}

	private String removeExtraInfoInContext(String context) {
		int endContextStartTagIndex = context.indexOf(">");
		int startIndex = context.indexOf(extraBeginTag, endContextStartTagIndex);
		if(context.charAt(startIndex-1) == '\t') startIndex--;
		if(context.charAt(startIndex-1) == '\n') startIndex--;			
		int extraEndTagStartIndex = context.indexOf(extraEndTag,startIndex);
		StringBuffer out = new StringBuffer(context.substring(0, startIndex));
		int endIndex = extraEndTagStartIndex + extraEndTag.length();
		out.append(context.substring(endIndex, context.length()));
		
		return out.toString();	
	}
	
	private String getContextStartTag() {
		return ("<Context");
	}
	
	private String getContextPath() {
		return ("path=" + '"' + getWebPath() + '"');	
	}
	
	private String getContextDocBase() {
		String docBaseLocation = "";
		if(getRootDirFolder() == null) {
			docBaseLocation = project.getLocation().toOSString();
		} else {
			docBaseLocation = this.getRootDirFolder().getLocation().toOSString();
		}
		return ("docBase=" + '"' + docBaseLocation + '"');	
	}
	
	private String getContextWorkDir() {
		String workFolderLocation = this.getWorkFolder().getLocation().toOSString();
		return (TomcatLauncherPlugin.getDefault().getTomcatBootstrap().getContextWorkDir(workFolderLocation));
	}
	
	private String getContextWebAppClassLoader() {
		return "\n\t<Loader className=\"org.apache.catalina.loader.DevLoader\" reloadable=\"true\" debug=\"1\" useSystemClassLoaderAsParent=\"false\" />";
	}
	
	private String getContextLogger() {
		return "\n\t<Logger className=\"org.apache.catalina.logger.SystemOutLogger\" verbosity=\"4\" timestamp=\"true\"/>";
	}


	private String getContextEndTag() {
		return "</Context>";
		
	}

	public void exportToWar() throws IOException {

		File warFile = new File(this.getWarLocation());
		
		File directory = null;
	 	if(getRootDirFolder() == null) {
		 	directory = this.getProject().getLocation().toFile();
	 	} else {	 		
		 	directory = this.getRootDirFolder().getLocation().toFile();
	 	}
	 					
		Zipper zipper = new TomcatProjectZipper(warFile, directory, getExportSource());
		zipper.zip();
	}

	/*
	 * if WEB-INF classes contains Java files add it to source folders
	 * Otherwise Eclipse will delete all those files
	 */
	private boolean classesContainsJavaFiles() {		
		IFolder webinfFolder = this.getWebInfFolder();	
		IFolder classesFolder = webinfFolder.getFolder("classes");
		File f = classesFolder.getLocation().toFile();
				 
    	if (!f.exists()) return false; 
    	if (!f.isDirectory()) return false; 
    
    	return FileUtil.dirContainsFiles(f, "java", true);
	}  
	
	
	/*
	 * A new Tomcat project should be checked by default in source path preference page
	 */
	private void addProjectToSourcePathPref() {
		List projects = TomcatLauncherPlugin.getDefault().getProjectsInSourcePath();
		ProjectListElement ple = new ProjectListElement(getProject());
		if(!projects.contains(ple)) {
			projects.add(ple);
			TomcatLauncherPlugin.getDefault().setProjectsInSourcePath(projects);
		}
	}
}

⌨️ 快捷键说明

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