/** 更新pattern */ public void update() throws Exception { Pattern p = new Pattern(); try { p.setId(Integer.parseInt(request.getParameter("id"))); p.setName(request.getParameter("name")); p.setExpression(request.getParameter("expression")); p.setWarning(request.getParameter("warning")); p.setCategory(request.getParameter("category")); p.setScope(request.getParameter("scope")); p.setExample(request.getParameter("example")); p.setPriority(Integer.parseInt(request.getParameter("priority"))); } catch (Exception e) { // 参数错误 error("Pattern Update Fail:" + e.getMessage()); return; } PatternDao dao = new PatternDaoImpl(); boolean bResult = false; try { bResult = dao.update(p); } catch (Exception e) { // 产生异常 error("Pattern Update Fail:" + e.getMessage()); return; } if (bResult) { Json j = new Json(1, "Pattern Update Success"); j.setData(p.getTBJsonNode()); echo(j.toString()); } else { error("Pattern Update Fail"); } }
/** 获取指定id的pattern */ public void get() throws Exception { try { int id = Integer.parseInt(request.getParameter("id")); PatternDao dao = new PatternDaoImpl(); Pattern p = dao.get(id); Json j = new Json(1, "ok"); j.setData(p.getObjectNode("data")); echo(j.toString()); } catch (Exception e) { error("Exception:" + e.getMessage()); } }
/** 生成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"); }