/** 生成patterns的xml文件 */ public void addruleset() throws Exception { try { // 生成的文件名 String fn = request.getParameter("fn"); if (fn == null || fn.equals("")) { error("Please Input FileName!"); return; } fn = (String) application.getAttribute("Ruleset") + fn + ".xml"; String all = request.getParameter("all"); String cond = request.getParameter("cond"); if (all == "1") cond = ""; // 写入xml头部 OutputStream out = new FileOutputStream(fn); OutputStreamWriter fw = new OutputStreamWriter(out, "UTF-8"); fw.write( "<?xml version=\"1.0\"?> <ruleset name=\"jcpa pmd rules\" xmlns=\"http://pmd.sourceforge.net/ruleset/2.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd\" xsi:noNamespaceSchemaLocation=\"http://pmd.sourceforge.net/ruleset_2_0_0.xsd\">"); fw.write("<description>jcpa pmd ruleset</description>"); // 分页取出数据写入xml final int ONE_PAGE_COUNT = 100; PatternDao dao = new PatternDaoImpl(); long Count = dao.count(cond); // 数据总条数 long pages = ToolUtil.getSplitCount(Count, ONE_PAGE_COUNT); // 总页数 for (int i = 0; i < pages; i++) { List<Pattern> list = dao.list(i, ONE_PAGE_COUNT, cond); Iterator<Pattern> it = list.iterator(); while (it.hasNext()) { Pattern p = it.next(); fw.write( "<rule name=\"" + p.getName() + "\" language=\"java\" since=\"5.0\" scope=\"" + p.getScope() + "\" message=\"\" class=\"net.sourceforge.pmd.lang.rule.XPathRule\" externalInfoUrl=\"pattern.jsp?id=" + p.getId() + "\">"); fw.write("<description>" + p.getWarning() + "</description>"); fw.write("<priority>" + p.getPriority() + "</priority>"); fw.write( "<properties><property name=\"xpath\"><value><![CDATA[" + p.getExpression() + "]]></value></property></properties>"); fw.write("<example><![CDATA[" + p.getExample() + "]]></example>"); fw.write("</rule>"); } } // 写入xml尾部 fw.write("</ruleset>"); fw.flush(); fw.close(); } catch (Exception e) { error("xml file bulid error:" + e.getMessage()); return; } success("ok"); }
/** 获得rulesets */ public void rulesets() throws Exception { Json j = new Json(1); JsonObjectNode data = j.createData(); JsonArrayNode rules = new JsonArrayNode("rules"); data.addChild(rules); List<String> files = ToolUtil.getFileFromPath((String) application.getAttribute("Ruleset"), "xml"); Iterator<String> it = files.iterator(); while (it.hasNext()) { rules.addItem(new JsonLeafNode("", it.next())); } echo(j.toString()); }
/** 列出第p页的pattern */ public void page() throws Exception { int ONE_PAGE_COUNT = ToolUtil.strToPositiveInt(request.getParameter("rp"), 25); ; // 一页pattern的个数 int page = ToolUtil.strToPositiveInt(request.getParameter("page"), 1); // 页码数 try { PatternDao dao = new PatternDaoImpl(); List<Pattern> list = dao.list(page, ONE_PAGE_COUNT, ""); long Count = dao.count(""); JsonObjectNode j = new JsonObjectNode(""); j.addChild(new JsonLeafNode("page", String.valueOf(page))); j.addChild(new JsonLeafNode("total", String.valueOf(Count))); JsonArrayNode rows = new JsonArrayNode("rows"); Iterator<Pattern> it = list.iterator(); while (it.hasNext()) { rows.addItem(it.next().getTBJsonNode()); } j.addChild(rows); echo(j.toString()); } catch (Exception e) { error("Data Access Fail:" + e.getMessage()); } }
/** 查看某个ruleset文件 */ public void viewRuleset() throws Exception { try { // 文件名 String fn = request.getParameter("file"); fn = (String) application.getAttribute("Ruleset") + fn; // 读取文件内容 String txt = ToolUtil.getFileConetent(fn, "UTF-8"); // 设置HTTP头: response.setContentType("text/xml"); // 写入内容 out.write(txt); out.flush(); } catch (Exception e) { echo(e.getMessage()); } }
/** 下载某个ruleset */ public void downRuleset() throws Exception { try { // 文件名 String file = request.getParameter("file"); String fn = (String) application.getAttribute("Ruleset") + file; // 读取文件内容 String txt = ToolUtil.getFileConetent(fn, "UTF-8"); // 设置HTTP头: response.reset(); response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition", "attachment;" + "filename=\"" + file + "\""); // 写入内容 out.write(txt); out.flush(); } catch (Exception e) { echo(e.getMessage()); } }