当前位置:   article > 正文

取得eclipse 的src路径 和webroot路径包括 相对路径_eclipse src路径

eclipse src路径
  1. package com.generate;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.w3c.dom.*;
  6. import javax.xml.parsers.*;
  7. import com.sunwayworld.Constants;
  8. import com.sunwayworld.common.util.StringUtil;
  9. import com.sunwayworld.srm30.common.util.SafeString;
  10. import org.apache.log4j.Logger;
  11. import org.apache.velocity.Template;
  12. import org.apache.velocity.context.Context;
  13. import org.apache.velocity.app.Velocity;
  14. import org.apache.velocity.app.VelocityEngine;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.io.Writer;
  21. import org.apache.velocity.VelocityContext;
  22. import java.util.Properties;
  23. import java.io.*;
  24. import java.net.URL;
  25. enum CreateType{
  26. //简单无关联的表,比如系统单表
  27. SimpleSingleTable
  28. }
  29. public class GenerateMaster
  30. { //jsp文件绝对路径
  31. public static Logger llog=Logger.getLogger(GenerateMaster.class);
  32. //生成类型
  33. String jsprealpath =null;
  34. String javarealpath=null;
  35. String javatemplatesPath="Templates\\SimpleSingleTable\\java\\";
  36. String jsptemplatesPath ="Templates\\jsp\\";
  37. String javaoutpath="com\\sunwayworld\\suppsyb\\";
  38. String jspoutpath="suppsyb";
  39. private Map m=null;
  40. private CreateType createtype=null;
  41. private String tablename=null;
  42. private String titlename=null;
  43. private GetTableCols tableinformation;
  44. // String javatemplatesPath = "D:\\crccworkspace\\coal_code\\template\\MasterDetail\\java\\";
  45. // String jsptemplatesPath="D:\\crccworkspace\\coal_code\\template\\MasterDetail\\jsp\\";
  46. // String javaoutpath="D:\\crccworkspace\\coal_code\\src\\com\\sunwayworld\\ccsup\\";
  47. // String jspoutpath="D:\\Oracle\\Middleware\\user_projects\\domains\\crcctedit_domain\\autodeploy\\DefaultWebApp\\ccsup\\";
  48. private GenerateMaster(){
  49. }
  50. public GenerateMaster(String tablename,String titlename,CreateType c)throws Exception{
  51. System.out.println( getSourceRealPath());
  52. createtype=c;
  53. this.tablename =tablename;
  54. this.titlename =titlename;
  55. this.tableinformation=new GetTableCols();
  56. }
  57. public static void main(String[] args){
  58. // 获取工程路径
  59. try {
  60. GenerateMaster m= new GenerateMaster("bwbmbm", "评分记录",CreateType.SimpleSingleTable);
  61. m.createNewFile();
  62. llog.debug("文件生成完毕");
  63. } catch (Exception e) {
  64. llog.debug("文件生成失败");
  65. e.printStackTrace();
  66. }
  67. }
  68. public void createNewFile()throws Exception{
  69. this.createNewFile(tablename, titlename);
  70. }
  71. //检查"表格"是否符合要求
  72. public boolean checkUerTable(){
  73. List l=tableinformation.getAllColumnList(tablename);
  74. switch(createtype){
  75. case SimpleSingleTable:
  76. //简单的
  77. TableColumnInfo columninfo=new TableColumnInfo();
  78. columninfo.setLowercolumnname("xgrmc");
  79. l.contains(columninfo);
  80. break;
  81. default: break;
  82. }
  83. return true;
  84. }
  85. /**
  86. * 系统自动生成模块文件
  87. * @param templatesPath
  88. * @param outpath
  89. * @param tablename
  90. * @param filetype
  91. * @param charset
  92. * @return
  93. */
  94. private boolean createNewFile(String tablename,String titlename){
  95. boolean b = true;
  96. try{
  97. String fileoutpath="";
  98. String filename ="";
  99. //---初始化生成器
  100. VelocityEngine ve = new VelocityEngine();
  101. VelocityEngine jspve = new VelocityEngine();
  102. VelocityEngine jspcommonve = new VelocityEngine();
  103. Properties propertie = new Properties();
  104. propertie.put("input.encoding", "UTF-8");//--设置输入编码
  105. propertie.put("output.encoding", "UTF-8");//--设置输出编码
  106. File f=new File(".");
  107. //包路径
  108. String PACKAGENAME=javaoutpath;
  109. //获取工程路径
  110. String projectpath=f.getCanonicalPath();
  111. javatemplatesPath =projectpath+"\\"+javatemplatesPath;
  112. jsptemplatesPath =projectpath+"\\"+jsptemplatesPath;
  113. javaoutpath =javarealpath+"\\"+javaoutpath;
  114. jspoutpath =jsprealpath+"\\"+jspoutpath;
  115. //--初始化替换文件及属性
  116. // HashMap map = this.getReplacePropertys(tablename,titlename,PACKAGENAME);
  117. try{
  118. VelocityContext context = this.generate(tablename,titlename,PACKAGENAME);
  119. //---生成类文件-------------------------------------------
  120. propertie.put("file.resource.loader.path", javatemplatesPath);//--模板所在文件夹
  121. ve.init(propertie);
  122. Template template = null;
  123. tablename = tablename.substring(0,1).toLowerCase()+tablename.substring(1,tablename.length());
  124. tablename = tablename.replace("_", "");
  125. String firstuppertablename = tablename.substring(0,1).toUpperCase()+tablename.substring(1,tablename.length());
  126. //生成action
  127. template = ve.getTemplate("Action.vm");
  128. fileoutpath=javaoutpath+"\\"+tablename.toLowerCase()+"\\action\\";
  129. filename= firstuppertablename+"Action.java";
  130. writeFile(context,template,fileoutpath,filename);
  131. //生成bean
  132. template = ve.getTemplate("Bean.vm");
  133. fileoutpath=javaoutpath+"\\"+tablename.toLowerCase()+"\\bean\\";
  134. filename= firstuppertablename+"Bean.java";
  135. writeFile(context,template,fileoutpath,filename);
  136. //生成beansql
  137. template = ve.getTemplate("BeanSql.vm");
  138. fileoutpath=javaoutpath+"\\"+tablename.toLowerCase()+"\\beansql\\";
  139. filename= firstuppertablename+"BeanSql.java";
  140. writeFile(context,template,fileoutpath,filename);
  141. //生成info
  142. template = ve.getTemplate("Info.vm");
  143. fileoutpath=javaoutpath+"\\"+tablename.toLowerCase()+"\\valueobject\\";
  144. filename= firstuppertablename+"Info.java";
  145. writeFile(context,template,fileoutpath,filename);
  146. /*
  147. //---生成共通的JSP文件-------------------------------------------
  148. propertie.put("file.resource.loader.path", jsptemplatesPath+"\\MasterDetail\\jsp");//--模板所在文件夹
  149. jspcommonve.init(propertie);
  150. //--编辑文件列表
  151. template = jspcommonve.getTemplate("createlist.vm");
  152. fileoutpath=jspoutpath+"\\"+tablename.toLowerCase()+"\\";
  153. filename= "createlist.jsp";
  154. writeFile(context,template,fileoutpath,filename);
  155. //--审核页面
  156. template = jspcommonve.getTemplate("approvelist.vm");
  157. fileoutpath=jspoutpath+"\\"+tablename.toLowerCase()+"\\";
  158. filename= "approvelist.jsp";
  159. writeFile(context,template,fileoutpath,filename);
  160. //--回退页面
  161. template = jspcommonve.getTemplate("retrievelist.vm");
  162. fileoutpath=jspoutpath+"\\"+tablename.toLowerCase()+"\\";
  163. filename= "retrievelist.jsp";
  164. writeFile(context,template,fileoutpath,filename);
  165. //--查询页面
  166. template = jspcommonve.getTemplate("querylist.vm");
  167. fileoutpath=jspoutpath+"\\"+tablename.toLowerCase()+"\\";
  168. filename= "querylist.jsp";
  169. writeFile(context,template,fileoutpath,filename);
  170. //---生成独立的JSP文件-------------------------------------------
  171. propertie.put("file.resource.loader.path", jsptemplatesPath+"\\Master\\jsp");//--模板所在文件夹
  172. jspve.init(propertie);
  173. //--详细信息页面
  174. template = jspve.getTemplate("detail.vm");
  175. fileoutpath=jspoutpath+"\\"+tablename.toLowerCase()+"\\";
  176. filename= "detail.jsp";
  177. writeFile(context,template,fileoutpath,filename);
  178. //--生成struts-config文件
  179. template = jspve.getTemplate("struts.vm");
  180. fileoutpath=jspoutpath+"\\"+tablename.toLowerCase()+"\\";
  181. filename= "struts-config.xml";
  182. writeFile(context,template,fileoutpath,filename);*/
  183. }catch (Exception e){
  184. e.printStackTrace();
  185. }
  186. }catch(Exception e){
  187. e.printStackTrace();
  188. b=false;
  189. }
  190. return b;
  191. }
  192. public HashMap getReplacePropertys(String tablename,String titlename,String PACKAGEPATH)throws Exception{
  193. HashMap map = new HashMap();
  194. //--初始化主表信息
  195. String UPPERTABLENAME = tablename.toUpperCase();//--大写的表名
  196. String LOWERTABLENAME = tablename.toLowerCase();//--小写的表名
  197. String FIRSTUPPERTABLENAME = tablename.substring(0,1).toUpperCase()+tablename.substring(1,tablename.length());//--首字母大写的表名
  198. String CHECKTABLENAME = tablename.replace("_", "");
  199. String CHECKUPPERTABLENAME = CHECKTABLENAME.toUpperCase();//--去下划线后大写的表名
  200. String CHECKLOWERTABLENAME = CHECKTABLENAME.toLowerCase();//--去下划线后小写的表名
  201. String CHECKFIRSTUPPERTABLENAME = CHECKTABLENAME.substring(0,1).toUpperCase()+CHECKTABLENAME.substring(1,CHECKTABLENAME.length());//--去下划线后首字母大写的表名
  202. //--初始化明细表信息
  203. //--获取主表主键
  204. String TBALEPK = new GetTableCols().getTablePrimaryKey(UPPERTABLENAME);
  205. if(TBALEPK.equals("")){
  206. throw new Exception("此表格没有标明主键");
  207. }
  208. TBALEPK = TBALEPK.toLowerCase();
  209. TBALEPK = TBALEPK.substring(0,1).toUpperCase()+TBALEPK.substring(1,TBALEPK.length()).toLowerCase();//--首字母大写
  210. //--创建主表主键序列 及明细表主键序列
  211. // new GetTableCols().createTablePKisBm(UPPERTABLENAME);
  212. new GetTableCols().createTablePKisSeq(UPPERTABLENAME);
  213. //---初始化替换参数
  214. // String packagename = javaoutpath.substring(javaoutpath.indexOf("\\supp")+5).replace("\\", "");
  215. //llog.debug("packagename="+packagename);
  216. map.put("actionPath", CHECKLOWERTABLENAME);// 类的包名
  217. // if(!"".equals(SafeString.safeString(packagename))){
  218. // CHECKLOWERTABLENAME=packagename+"."+CHECKLOWERTABLENAME;
  219. CHECKLOWERTABLENAME=PACKAGEPATH.replaceAll("\\\\", ".").replaceAll("/",".")+CHECKLOWERTABLENAME;
  220. // }
  221. map.put("packageName", CHECKLOWERTABLENAME);// 类的包名
  222. map.put("className", CHECKFIRSTUPPERTABLENAME);// java类名
  223. map.put("lowerTableName", LOWERTABLENAME);// 主表名-小写
  224. map.put("upperTableName", UPPERTABLENAME);// 主表名-大写
  225. map.put("lowerPk", TBALEPK.toLowerCase());// 主键字段-小写
  226. map.put("firstUpperPk", TBALEPK);// 主键字段-首字母大写
  227. map.put("upperPk", TBALEPK.toUpperCase());// 主键字段-大写
  228. map.put("title", titlename);// 标题
  229. return map;
  230. }
  231. /**
  232. * 统一参数替换
  233. * @param map
  234. * @return
  235. */
  236. public VelocityContext generate(String tablename,String titlename,String PACKAGEPATH)
  237. throws Exception {
  238. if(m==null){
  239. m= this.getReplacePropertys(tablename,titlename,PACKAGEPATH) ;
  240. }
  241. Map map=m;
  242. String templatesPath = (String)map.get("templatesPath");// 模板路径
  243. VelocityContext context =null;
  244. try{
  245. context = new VelocityContext();
  246. //获取基础字段
  247. String packageName = (String)map.get("packageName");// 类的包名
  248. String actionPath = (String)map.get("actionPath");// action路径
  249. String className = (String)map.get("className");// java类名
  250. String upperTableName = (String)map.get("upperTableName");// 主表名-大写
  251. String lowerTableName = (String)map.get("lowerTableName");// 主表名-小写
  252. String firstUpperPk = (String)map.get("firstUpperPk");// 主键字段-首字母大写
  253. String upperPk = (String)map.get("upperPk");// 主键字段-大写
  254. String lowerPk = (String)map.get("lowerPk");// 主键字段-小写
  255. String title = (String) map.get("title") ;//标题名称
  256. //获取表结构
  257. GetTableCols cols = new GetTableCols();
  258. List alltablelist = cols.getAllColumnList(upperTableName);//--主表所有字段
  259. List tablelistnocommnopk = cols.getColumnListNoCommNoPK(upperTableName);//--主表的字段(除基础字段外和主键)
  260. List tablelistnocomm = cols.getColumnListNoComm(upperTableName);//--主表的字段(除基础字段外)
  261. List tablelistnopk = cols.getColumnListNoPK(upperTableName);//--主表字段(除PK)
  262. List tablelistnopkfk= cols.getColumnListNoPkFk(upperTableName);//--主表字段(除PK和FK)
  263. //---------------基础的参数替换----------------
  264. context.put("PACKAGENAME", packageName); // 类的包名
  265. context.put("ACTIONPATH", actionPath); // action path=
  266. context.put("CLASSNAME", className); // java类名
  267. context.put("UPPERTABLENAME", upperTableName);// 主表名-大写
  268. context.put("LOWERTABLENAME", lowerTableName);//主表全小写
  269. context.put("FIRSTUPPERPK", firstUpperPk);// 主键字段-首字母大写
  270. context.put("UPPERPK", upperPk);// 主键字段-大写
  271. context.put("LOWERPK", lowerPk);// 主键字段-小写
  272. context.put("TITLE", title);//标题名称
  273. //---------------数据库表信息替换---------------------
  274. context.put("ALLTABLELIST", alltablelist);
  275. context.put("TABLELISTNOCOMM", tablelistnocomm);
  276. context.put("TABLELISTNOCOMMNOPK", tablelistnocommnopk);
  277. context.put("TABLELISTNOPKFK", tablelistnopkfk);
  278. context.put("TABLELISTNOPK", tablelistnopk);
  279. }catch(Exception e){
  280. e.printStackTrace();
  281. }
  282. return context;
  283. }
  284. /**
  285. * 写文件
  286. * @param context
  287. * @param template
  288. * @param filepath
  289. * @param filename
  290. */
  291. public void writeFile(VelocityContext context,Template template,String filepath,String filename){
  292. try{
  293. //输出路径
  294. String fileRealPath =filepath;
  295. System.out.println("filePath=" + fileRealPath);
  296. java.io.File fileTemp = new java.io.File(fileRealPath);
  297. //目录不存在,则创建
  298. if (!fileTemp.exists())
  299. {
  300. System.out.println("make dir ");
  301. fileTemp.mkdirs();
  302. }
  303. String newFileName = fileRealPath + filename;
  304. System.out.println("newFileName: "+newFileName);
  305. java.io.File fileDst = new java.io.File(newFileName);
  306. //输出
  307. // if(fileDst.exists()){
  308. // System.out.println("文件:"+filename+"创建失败,当前文件已经存在!");
  309. // }else{
  310. Writer writer = new PrintWriter(new FileOutputStream(fileDst));
  311. template.merge(context, writer);
  312. writer.flush();
  313. // }
  314. }catch(Exception e){
  315. e.printStackTrace();
  316. }
  317. }
  318. /**
  319. * 得到类的路径,例如E:/workspace/JavaGUI/bin/com/util
  320. * @return
  321. * @throws java.lang.Exception
  322. */
  323. public String getClassPath() throws Exception {
  324. try {
  325. String strClassName = getClass().getName();
  326. String strPackageName = "";
  327. if (getClass().getPackage() != null) {
  328. strPackageName = getClass().getPackage().getName();
  329. }
  330. String strClassFileName = "";
  331. if (!"".equals(strPackageName)) {
  332. strClassFileName = strClassName.substring(strPackageName.length() + 1,
  333. strClassName.length());
  334. } else {
  335. strClassFileName = strClassName;
  336. }
  337. URL url = null;
  338. url = getClass().getResource(strClassFileName + ".class");
  339. String strURL = url.toString();
  340. strURL = strURL.substring(strURL.indexOf('/') + 1, strURL
  341. .lastIndexOf('/'));
  342. //返回当前类的路径,并且处理路径中的空格,因为在路径中出现的空格如果不处理的话,
  343. //在访问时就会从空格处断开,那么也就取不到完整的信息了,这个问题在web开发中尤其要注意
  344. return strURL.replaceAll("%20", " ");
  345. } catch (Exception ex) {
  346. ex.printStackTrace();
  347. throw ex;
  348. }
  349. }
  350. /***/
  351. public String getSourceRealPath()throws Exception{
  352. String srcprefix="";
  353. String Webrootprefix="";
  354. File fclasspath=new File(".classpath");
  355. File fproject=new File(".project");
  356. //很明显该类是一个单例,先获取产生DocumentBuilder工厂
  357. //的工厂,在通过这个工厂产生一个DocumentBuilder,
  358. //DocumentBuilder就是用来产生Document的
  359. Document docclasspath=null;
  360. Document docproject =null;
  361. DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  362. DocumentBuilder db=dbf.newDocumentBuilder();
  363. //这个Document就是一个XML文件在内存中的镜像
  364. docclasspath=db.parse(fclasspath);
  365. NodeList nodeListclasspath= docclasspath.getElementsByTagName("classpathentry");
  366. for(int i=0;i<nodeListclasspath.getLength();i++){
  367. NamedNodeMap attributes= nodeListclasspath.item(i).getAttributes();
  368. Map m=new HashMap();
  369. for(int j=0;j<attributes.getLength();j++){
  370. Node attribute=attributes.item(j);
  371. System.out.println("dbstore的属性名为:"+attribute.getNodeName()+" 相对应的属性值为:"+attribute.getNodeValue());
  372. m.put(attribute.getNodeName(), attribute.getNodeValue());
  373. }
  374. System.out.println(m.containsKey("kind")&&m.get("kind").equals("src"));
  375. if(m.containsKey("kind")&&m.get("kind").equals("src")){
  376. srcprefix=m.get("path")+"";
  377. }
  378. if(m.containsKey("kind")&&m.get("kind").equals("output")){
  379. Webrootprefix=m.get("path")+"";
  380. Webrootprefix=Webrootprefix.substring(0,Webrootprefix.indexOf("/WEB-INF/classes"));
  381. // System.out.println(Webrootprefix);
  382. }
  383. m.clear();
  384. }
  385. if(!srcprefix.equals("")&&!Webrootprefix.equals("")){
  386. docproject =db.parse(fproject);
  387. NodeList nodeListproject= docproject.getElementsByTagName("linkedResources");
  388. if(nodeListproject.getLength()==0){
  389. // System.getProperty("user.dir");
  390. javarealpath=System.getProperty("user.dir")+
  391. System.getProperty("file.separator")+srcprefix;
  392. jsprealpath=System.getProperty("user.dir")+
  393. System.getProperty("file.separator")+Webrootprefix;
  394. // System.out.println("文件就在workspace下面");
  395. }else{
  396. // System.out.println("存在外链"+nodeListproject.getLength());
  397. Node n =nodeListproject.item(0);
  398. // System.out.println(n.getNodeName());
  399. NodeList childnodelist=n.getChildNodes();
  400. // System.out.println(childnodelist.getLength());
  401. for(int i=0;i<childnodelist.getLength();i++){
  402. Node childn= childnodelist.item(i);
  403. if(!childn.getNodeName().equals("link"))continue;
  404. // System.out.println(childn.getNodeName());
  405. Map m =new HashMap();
  406. for(int j=0;j<childn.getChildNodes().getLength();j++){
  407. if(childn.getChildNodes().item(j).getNodeName().equals("#text")){
  408. continue;
  409. }
  410. // System.out.println(childn.getChildNodes().item(j).getNodeName()
  411. // +childn.getChildNodes().item(j).getTextContent());
  412. m.put(childn.getChildNodes().item(j).getNodeName(),
  413. childn.getChildNodes().item(j).getTextContent()
  414. );
  415. };
  416. if( m.containsKey("name")&&m.get("name").equals(srcprefix)
  417. ){
  418. javarealpath= m.get("location")+"";
  419. }
  420. if( m.containsKey("name")&&m.get("name").equals(Webrootprefix)
  421. ){
  422. jsprealpath= m.get("location")+"";
  423. }
  424. m.clear();
  425. }
  426. }
  427. }
  428. return " jsprealpath ="+jsprealpath +"---"+
  429. " javarealpath="+javarealpath;
  430. }
  431. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/583127
推荐阅读
相关标签
  

闽ICP备14008679号