Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ce2mapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import freemarker.core.ParseException; import freemarker.template.Configuration; import freemarker.template.MalformedTemplateNameException; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Alx */ public class HTMLbuilder { final static Charset ENCODING = StandardCharsets.UTF_8; private Configuration cfg; private Map<String, Object> iData; private ArrayList<Element> resultList; private ArrayList<Element> resultList2D; private ArrayList<XObj> resultListIndex; private HashMap<String, Object> iDataIndex; public void build(ArrayList<Element> _resultList) { this.resultList = new ArrayList(); this.resultList = _resultList; try { createJSON(); //IN ??... XMLSYNCagrigator agr = new XMLSYNCagrigator(resultList2D); resultListIndex = agr.agregate(); // try { freemarkerConfig(); //javascript for diagramm prepareData(); freemarkerMagic(); //index.html prepareDataIndex(); freemarkerMagicIndex(); // createJSON(); } catch (ParseException ex) { Logger.getLogger(HTMLbuilder.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(HTMLbuilder.class.getName()).log(Level.SEVERE, null, ex); } } catch (IOException ex) { Logger.getLogger(HTMLbuilder.class.getName()).log(Level.SEVERE, null, ex); } } private void freemarkerConfig() { String tempPath = System.getProperty("user.dir") + File.separator + "template" + File.separator; cfg = new Configuration(Configuration.VERSION_2_3_22); try { cfg.setDirectoryForTemplateLoading(new File(tempPath)); } catch (IOException ex) { Logger.getLogger(HTMLbuilder.class.getName()).log(Level.SEVERE, null, ex); } cfg.setDefaultEncoding("UTF-8"); } /* javascript create and save */ private void freemarkerMagic() throws MalformedTemplateNameException, ParseException, IOException { Template temp = cfg.getTemplate("atcrew31.ftl"); /// index.ftl !!!! //System.out.println(HMc.dirPathStr); Path path = Paths.get(System.getProperty("user.dir") + File.separator + "HTML_SET" + File.separator + "js" + File.separator + "at3000.js"); /// index.html !!!! BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(path.toString()), "UTF-8"));///////// ? UTF8 try { temp.process(iData, out); } catch (TemplateException ex) { Logger.getLogger(HTMLbuilder.class.getName()).log(Level.SEVERE, null, ex); } //out.write(res); out.flush(); } /* prepare data for js */ private void prepareData() { iData = new HashMap<String, Object>(); String _date = CE2Mapper._currentDate; String _userNom = CE2Mapper._userNom; iData.put("root", createGraphData()); iData.put("curDate", _date); iData.put("userNom", _userNom); } /* create index.html and save */ private void freemarkerMagicIndex() throws MalformedTemplateNameException, ParseException, IOException { Template temp = cfg.getTemplate("index01.ftl"); /// index.ftl !!!! //System.out.println(HMc.dirPathStr); Path path = Paths .get(System.getProperty("user.dir") + File.separator + "HTML_SET" + File.separator + "index.html"); /// index.html !!!! BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(path.toString()), "UTF-8"));///////// ? UTF8 try { temp.process(iDataIndex, out); } catch (TemplateException ex) { Logger.getLogger(HTMLbuilder.class.getName()).log(Level.SEVERE, null, ex); } //out.write(res); out.flush(); } /* prepare data for index.html */ private void prepareDataIndex() { iDataIndex = new HashMap<String, Object>(); String _login = CE2Mapper._login; String _base = CE2Mapper._base; String _name = CE2Mapper._userName; String _date = CE2Mapper._currentDate; String _table = CE2Mapper._table; iDataIndex.put("root", resultListIndex); iDataIndex.put("userLogin", _login); iDataIndex.put("userName", _name); iDataIndex.put("base", _base); iDataIndex.put("curDate", _date); iDataIndex.put("curTable", _table); iDataIndex.put("grTable", CE2Mapper.grList); iDataIndex.put("ViewCommList", CE2Mapper.vcsList); iDataIndex.put("SpecStrList", CE2Mapper.ssList); } /* JSON create and save in to file */ private void createJSON() throws IOException { //Gson gson = new Gson(); prepareListForJSON(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(jsonList); String path = System.getProperty("user.dir") + File.separator + "HTML_SET" + File.separator + "index01.json"; FileWriter writer = new FileWriter(path);//"d:\\file.json"); writer.write(json); writer.close(); } private ArrayList<JSONObj> jsonList; private void prepareListForJSON() { jsonList = new ArrayList(); resultList2D = new ArrayList(); parseElementList(this.resultList); } private void parseElementList(ArrayList<Element> arr) { for (Element el : arr) { JSONObj j = new JSONObj(); j.setType(el.getType()); String _id = el.getId(); j.setId(_id); String _p = el.getParent_id(); j.setParent(_p); String _text = el.getName(); j.setText(_text); JSONdata data = new JSONdata(); data.setScode(el.getSelectCode()); data.setOcode(el.getHTMLTable()); j.setData(data); jsonList.add(j); resultList2D.add(el); if (el.getChild().size() > 0) { parseElementList(el.getChild()); } } } private GraphRootElement createGraphData() { GraphRootElement root = new GraphRootElement(); root.setId(jsonList.get(0).getId()); root.setName(jsonList.get(0).getText()); root.setCoordinate("600,0"); int w = 1200; int h = 900; int wres = w / 7; int hres = 100; ArrayList<LinkObject> links = new ArrayList(); ArrayList<GraphElementObject> elements = new ArrayList(); for (int i = 1; i < jsonList.size(); ++i) { JSONObj jso = jsonList.get(i); LinkObject link = new LinkObject(); link.setId(jso.getId()); link.setParent(jso.getParent()); GraphElementObject el = new GraphElementObject(); el.setId(jso.getId()); el.setName(jso.getText()); String type = "erd.Entity"; if (jso.getType().equals("nav")) { type = "erd.Normal"; } if (jso.getType().equals("param") || jso.getType().equals("job")) { type = "erd.Relationship"; } el.setType(type); wres = wres + w / 8; if (wres >= w) { wres = 0; hres = hres + 150; } String resstr = Integer.toString(wres) + "," + Integer.toString(hres); el.setCoordinat(resstr); links.add(link); elements.add(el); } root.setLilnks(links); root.setElements(elements); return root; } }