页面解析

package com.atom.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TranFile {

	// public String readFile(String path) {
	// try {
	// File file = new File(path);
	// FileReader reader = null;
	// if (file.exists()) {
	// reader = new FileReader(file);
	// reader.
	//				
	// }
	// } catch (FileNotFoundException e) {
	// e.printStackTrace();
	// }

	public static Map<String, List> map = new HashMap<String, List>();

	public String readHtmlSFTL(String html) {
		String a[] = html.split("<SFTL:");
		List checkList = new ArrayList();
		for (int i = 0; i < a.length; i++) {
			checkList = new ArrayList();
			String s = a[i];
			// System.out.println(s);
			if (s.indexOf(">") >= 0) {
				s = s.substring(0, s.indexOf(">", 1));
			} else
				continue;
			html = html.replaceAll("<SFTL:" + s, "-");
			// System.out.println("=====++"+s);
			if (!StringUtil.isEmpty(a[i])) {
				String[] a2 = a[i].split(" ");
				// checkList = new ArrayList();
				String pName = "";
				for (int j = 0; j < a2.length; j++) {
					String s2 = a2[j];
					if (j == 0) {
						pName = "SFTL:" + s2;
						pName = pName.trim();
					}
					// if(a2[0].equals("Sections"))
					// System.out.println(s+"==s2"+s2);
					html = html.replaceAll("</" + pName + ">", "");
					if (!StringUtil.isEmpty(s2)) {
						if (s2.indexOf("=") > 0) {
							String[] s3 = s2.split("=");

							if (null != map.get(pName)) {
								checkList = map.get(pName);
								if (null != checkList && checkList.size() >= 0) {

									if (!checkList.contains(s3[0].trim())) {
										checkList.add(s3[0].trim());
										if (pName.equals("SFTL:Sections"))
											System.out.println(s3[0]);
									}
								} else {
									checkList = new ArrayList();
									checkList.add(s3[0].trim());

								}
							} else {
								// checkList = new ArrayList();
								checkList.add(s3[0].trim());

							}
						}

					}
				}
				map.put(pName, checkList);
			}

		}
		// System.out.println("====="+html);
		html = html.replaceAll("</form>", "");
		return html;
	}

	public String readHtmlSFTD(String html) {
		if (html.indexOf("<SFTD:") == -1)
			return "";
		String a[] = html.split("<SFTD:");
		List checkList = new ArrayList();
		for (int i = 0; i < a.length; i++) {
			checkList = new ArrayList();
			String s = a[i];
			// System.out.println(s);
			if (s.indexOf(">") >= 0) {
				s = s.substring(0, s.indexOf(">", 1));
			} else
				continue;
			html = html.replaceAll("<SFTD:" + s, "-");
			// System.out.println("=====++"+s);
			if (!StringUtil.isEmpty(a[i])) {
				String[] a2 = a[i].split(" ");
				// checkList = new ArrayList();
				String pName = "";
				for (int j = 0; j < a2.length; j++) {
					String s2 = a2[j];
					if (j == 0) {
						pName = "SFTD:" + s2;
						pName = pName.trim();
					}
					// if(a2[0].equals("Sections"))
					// System.out.println(s+"==s2"+s2);
					html = html.replaceAll("</" + pName + ">", "");
					if (!StringUtil.isEmpty(s2)) {
						if (s2.indexOf("=") > 0) {
							String[] s3 = s2.split("=");

							if (null != map.get(pName)) {
								checkList = map.get(pName);
								if (null != checkList && checkList.size() >= 0) {

									if (!checkList.contains(s3[0].trim())) {
										checkList.add(s3[0].trim());
									}
								} else {
									checkList = new ArrayList();
									checkList.add(s3[0].trim());

								}
							} else {
								// checkList = new ArrayList();
								checkList.add(s3[0].trim());

							}
						}

					}
				}
				map.put(pName, checkList);
			}

		}
		// System.out.println("====="+html);
		html = html.replaceAll("</form>", "");
		return html;
	}

	public String readFile(File file) {
		String data = "";
		try {
			// File file = new File(path);
			if (file.exists()) {
				FileInputStream fs = new FileInputStream(file);
				InputStreamReader is = new InputStreamReader(fs);
				BufferedReader br = new BufferedReader(is);
				String str = "";
				while ((str = br.readLine()) != null) {
					data += str;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

	

	private static void readTld() {
		TranFile f = new TranFile();
		String s = "";
		File file = new File("E:\\workspace\\project2009-2\\asitom\\doc\\ats\\");
		File[] fall = file.listFiles();
		for (int i = 0; i < fall.length; i++) {
			File fi = fall[i];
			if (fi.getName().indexOf("List") == -1
					&& !fi.getName().equals("Welcome")) {
				s = f.readFile(fi);
				s = s.replaceAll("'", "");
				s = s.replaceAll("\"", "'");
				s = s.replaceAll("[\\s]", " ");
				s = s.replaceAll("  ", " ");
				// s =s.replaceAll("[<div]*[>]", "");
				// s = s.replaceAll("", "");
				s = s.replaceAll("\\\\", "");
				s = s.replaceAll("<p>", "");
				s = s.replaceAll("</p>", "");
				s = s.replaceAll("<hr>", "");
				s = s.replaceAll("</hr>", "");

				// s = s.replaceAll("RegularExpression", "Reg");
				s = s.replaceAll("(')[^=]*(')", "''");
				if (s.indexOf("<SFTL:") >= 0) {
					s = s.substring(s.indexOf("<SFTL:", 1), s.length());
				} else
					continue;
				s = s.replaceAll("(<SFTL)[^>]*(>)", "");
				s = s.replaceAll("(</SFTL:)[^>]*(>)", "");
				// s = s.replaceAll("(<SFTD)[^>]*(>)", "");
				s = s.replaceAll("(</SFTD)[^>]*(>)", "");
				s = s.replaceAll("(<form)[^>]*(>)", "");
				s = s.replaceAll("</form>", "");
				s = s.replaceAll("(<div)[^>]*(>)", "");
				s = s.replaceAll("</div>", "");
				s = s.replaceAll("(<h2)[^>]*(>)", "");
				s = s.replaceAll("</h2>", "");
				s = s.replaceAll("(<a)[^>]*(>)", "");
				s = s.replaceAll("</a>", "");
				s = s.replaceAll("(<table)[^>]*(>)", "");
				s = s.replaceAll("</table>", "");
				s = s.replaceAll("(<tr)[^>]*(>)", "");
				s = s.replaceAll("</tr>", "");
				s = s.replaceAll("(<td)[^>]*(>)", "");
				s = s.replaceAll("</td>", "");
				s = s.replaceAll("(<img)[^>]*(>)", "");
				s = s.replaceAll("</img>", "");
				s = s.replaceAll("(<span)[^>]*(>)", "");
				s = s.replaceAll("</span>", "");
				s = s.replaceAll("(<li)[^>]*(>)", "");
				s = s.replaceAll("</li>", "");

				System.out.println("====" + s);
				// s = s.replaceAll("<%@", "");
				// s = s.replaceAll("%>", "");
				// String html = f.readHtmlSFTL(s);
				String html = f.readHtmlSFTD(s);
				// html = html.replaceAll("->", "");
				// html = html.replaceAll("<form id='Form1' method='post' >",
				// "");
				// //System.out.println("--------"+html);
				// f.readHtmlSFTD(html);
			}
		}

		// Set set = map.keySet();
		// String out = "";
		// for (Iterator iterator = set.iterator(); iterator.hasNext();) {
		// String obj = (String) iterator.next();
		// out = "";
		// out = obj.toString() + "=";
		// List<String> list = map.get(obj.toString());
		// for (String sb : list) {
		// out += "+" + sb;
		// }
		// System.out.println(out);
		// }
		// f.writeCap();
		f.writeNewFile();
	}

	private void writeCap() {
		Set set = map.keySet();
		String out = "";
		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			String obj = (String) iterator.next();
			List<String> list = map.get(obj.toString());
			for (String sb : list) {
				if (!StringUtil.isEmpty(sb)) {
					if (sb.charAt(0) >= 65 && sb.charAt(0) <= 90
							&& sb.charAt(1) >= 97 && sb.charAt(1) <= 122)
						out += "   " + sb;
				}

			}
			System.out.println(out);
		}

	}

	
	private void readJSP(){
		TranFile f = new TranFile();
		String s = "";
		File file = new File("E:\\workspace\\project2009-2\\asitom\\doc\\ats\\");
		File[] fall = file.listFiles();
		for (int i = 0; i < fall.length; i++) {
			File fi = fall[i];
			s = f.readFile(fi);
			
			s = s.replaceAll("<SFTL:Page ", "<SFFF:Page ");
			s = s.replaceAll("</SFTL:Page>", "</SFFF:Page>");
			s = s.replaceAll("(<SFTL:)[^>]*(>)", "");
			s = s.replaceAll("(</SFTL:)[^>]*(>)", "");
			s = s.replaceAll("<SFFF:Page ", "<SFTL:Page ");
			s = s.replaceAll("</SFFF:Page>", "</SFTL:Page>");
			s = s.replaceAll(">", ">\n");
			s = s.replaceAll("defaultvalue", "defaultValue");
			f.writeNewJsp("", fi.getName(), s);
			}
	}
	
	private void writeNewJsp(String folder, String name, String s) {
		try {
			String path = "D:\\asitom\\" + name;
			File file = new File(path);
			if (!file.exists()){
				file.createNewFile();
				PrintWriter pt = new PrintWriter(file);
				pt.write(s);
				pt.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void writeNewFile() {
		Set set = map.keySet();
		try {
			String out = "";
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				String obj = (String) iterator.next();
				out = "";
				out = obj.toString();
				out = out.substring(out.indexOf(":") + 1, out.length());
				File file = new File(
						"E:/workspace/project2009-2/asitom/doc/tagtd/" + out
								+ "Tag.java");
				if (!file.exists())
					file.createNewFile();
				PrintWriter pt = new PrintWriter(file);
				pt.write("package j2ee.tag;\n");
				pt.write("import java.io.IOException;\n");
				pt.write("import javax.servlet.http.HttpServletRequest;\n");
				pt.write("import javax.servlet.jsp.JspException;\n");
				pt.write("import javax.servlet.jsp.JspTagException;\n");
				pt.write("import javax.servlet.jsp.JspWriter;\n");
				pt.write("import javax.servlet.jsp.tagext.BodyContent;\n");
				pt.write("import javax.servlet.jsp.tagext.BodyTagSupport;\n");
				pt.write("public class " + out
						+ "Tag extends BodyTagSupport{\n");
				pt
						.write("public int doStartTag() throws JspTagException{\nreturn EVAL_PAGE;\n}\n");
				pt
						.write("public int doEndTag() throws JspTagException{\nreturn EVAL_PAGE;\n}\n");
				List<String> list = map.get(obj.toString());
				for (String sb : list) {
					if (!StringUtil.isEmpty(sb)) {
						if (sb.charAt(0) >= 65 && sb.charAt(0) <= 90
								&& sb.charAt(1) >= 97 && sb.charAt(1) <= 122) {
							char tmp = sb.charAt(0);
							tmp = (char) ((int) tmp + 32);
							sb = tmp + sb.substring(1, sb.length());
						}
						pt.write("private String " + sb + ";\n");
					}
				}
				pt.write("}");
				pt.close();
				// System.out.println(out);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public static void main(String[] args) {
		TranFile f = new TranFile();
		f.readJSP();
		
		

	}
	
	public static void main2(String[] args) {
//		System.out.println((int) 'a');
//		System.out.println((int) 'z');
//		System.out.println((int) 'A');
//		System.out.println((int) 'Z');

		String ss = "<SFTL:PageFrame  id=\"PageFrame\">" +
				"   <SFTL:PanelControl  id=\"FavoritesFilterPanel\" name=\"FavoritesFilterPanel\" permissionBit=\"0\" hasMinimize=\"true\" hasPrint=\"false\" hasReset=\"false\" isMinimized=\"true\" tip=\"最大化/最小化\" instruction=\"Click a Favorite's arrow to display its data in the Summary view. Click a Favorite's name to view it in the Choices section.\" isHidden=\"true\">" +
						"<SFTL:BarFooter  id=\"TaskPaneFooter\" name=\"TaskPaneFooter\"></SFTL:PageFrame>";
		ss = ss.replaceAll("<SFTL:Page ", "<SFFF:Page ");
		ss = ss.replaceAll("</SFTL:Page>", "</SFFF:Page>");
		System.out.println(ss);
		ss = ss.replaceAll("(<SFTL:)[^>]*(>)", "");
		ss = ss.replaceAll("(</SFTL:)[^>]*(>)", "");
		ss = ss.replaceAll("<SFFF:Page ", "<SFTL:Page ");
		ss = ss.replaceAll("</SFFF:Page>", "</SFTL:Page>");
		ss = ss.replaceAll(">", ">\n");
	//	ss = ss.replaceAll("(<SFTL)[^>]*(>)", "--");
		System.out.println(ss);
	}
}
 
原文地址:https://www.cnblogs.com/qq1988627/p/6606880.html