Java tutorial
/* * (C) Copyright 1997 i-Teco, CJSK. All Rights reserved. * i-Teco PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * ? 1997 i-Teco, ?. * ? ??? * ? i-Teco. */ package ru.iteco.xmldoc; import freemarker.template.Configuration; import freemarker.template.ObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; import org.apache.commons.lang.StringUtils; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.input.SAXBuilder; import org.jdom2.xpath.XPathExpression; import org.jdom2.xpath.XPathFactory; import java.io.*; import java.util.*; /** * <h3> HTML- XML-?</h3> * <p> * ?? generic - ??, ? T ?. * ? , ? - run(), ??? ? ?? HTML. * </p> * <p>Author: predtechenskaya (predtechenskaya@i-teco.ru)</p> * <p>Date: 09.04.13</p> */ public abstract class AbstractGenerator<T extends ConfigEx> { //: ID - ?? (?/) - ? ID protected Map<String, List<T>> items = new TreeMap<String, List<T>>(); // protected Set<Modifier> modifiers = new LinkedHashSet<Modifier>(); //? protected Set<T> extendeds = new LinkedHashSet<T>(); // notdefined, ? ID protected Map<String, Set<Notdefined>> notdefineds = new LinkedHashMap<String, Set<Notdefined>>(); //? ? ? protected Scope scopes = new Scope(); /** * el * * @param el XML ? * @param path XML (? ? - ? , ? webtop) */ protected void process(Element el, String path) { // Config confEl = ConfigEx.getElement(el, path); // if (confEl instanceof Modifier) { //? ? ?- modifiers.add((Modifier) confEl); return; } // notdefined - ? if (confEl instanceof Notdefined) { //? ? ?- String id = ((Notdefined) confEl).getId(); if (!notdefineds.containsKey(id)) notdefineds.put(id, new LinkedHashSet<Notdefined>()); notdefineds.get(id).add((Notdefined) confEl); return; } // ?? ? if (confEl instanceof ConfigEx) { //? T item = (T) confEl; if (!items.containsKey(item.getId())) items.put(item.getId(), new LinkedList<T>()); items.get(item.getId()).add(item); //? ? ?, ?? if (item.getParent() != null) extendeds.add(item); //? scope ? scopes.add(item.getScope()); return; } } /** * ?- <br /> * ? ? ? , ? ?. */ protected void endProcess() { // for (Modifier modifier : modifiers) { T item = getByPath(modifier.getModify()); //?, if (item != null) //? item.addModifier(modifier); } // ? for (T child : extendeds) { T parent = getByPath(child.getParent()); //? ? if (parent != null) { // ? parent.addChild(child); // - ? ? child.setParentConfig(parent); } } } /** * T ? ( webtop) * * @param path webtop- (id:path) * @return null, ? */ protected T getByPath(String path) { //ID ? String id = Config.parseIdFromPath(path); //? ? ? ID List<T> lst = items.get(id); //? , if (lst == null) return null; // for (T item : lst) { //? if (item.getUniqueId().equals(path)) return item; } ; return null; } /** * , ??? extends. * ?, ? ? * * @param path * @return ? */ protected String getActionPath(String path) { return path.substring(path.lastIndexOf("webapp") + 6); } /** * , ?, ? ? ? ? * * * @param xmlfile * @throws Exception */ protected void processFile(File xmlfile) throws Exception { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(xmlfile); Element root = doc.getRootElement(); XPathExpression itemExpr = XPathFactory.instance().compile("scope/" + getElementName()); List<Element> items = itemExpr.evaluate(root); for (Element i : items) { System.out.println(i.getAttributeValue("id")); process(i, xmlfile.getPath()); counter++; } } //, ? private int counter = 0; /** * ?? ? ?. ? processFile ? ? xml * * @param dirs ?? / ? * @throws Exception */ public void processRecursive(File[] dirs) throws Exception { for (File dir : dirs) { if (dir.isDirectory()) { processRecursive(dir.listFiles()); } else if (dir.isFile()) { String path = dir.getPath(); if (isXmlFile(path)) { processFile(dir); } } } } /** * ?, ???? XML ?? ?? * * @param path * @return true ? XML false */ protected boolean isXmlFile(String path) { int i = path.lastIndexOf('.'); if (i > 0) return path.substring(i + 1).equals("xml"); return false; } /** * ? freemarker. * @return */ protected abstract Map<Object, Object> getBaseTemplateMap(); /** * ? ?: action|component * @return */ protected abstract String getElementName(); /** * ? ? ? * * @throws Exception */ public void run() throws Exception { // ? ? Properties props = new Properties(); props.load(new InputStreamReader(new FileInputStream(CONFIG_FILE), "UTF-8")); conf = (Map<Object, Object>) props; // freemarker, ? ??, ? ? setTplHelper(new TemplateHelper(conf)); //? ? String[] paths = StringUtils.split((String) conf.get("SEARCH_FOLDERS"), ","); if (paths.length == 0) throw new Exception( "? ? . ?: SEARCH_FOLDERS"); // File[] source_dirs = new File[paths.length]; for (int i = 0; i < paths.length; i++) source_dirs[i] = new File(paths[i]); //? ? processRecursive(source_dirs); //?- endProcess(); // HTML TemplateHelper tplHelper = getTplHelper(); // HTML tplHelper.processBase(getBaseTemplateMap()); //HTML ? for (String k : items.keySet()) { for (T item : items.get(k)) { tplHelper.processItem(item); } } } // ? ? protected String CONFIG_FILE = "./config.properties"; // protected Map<Object, Object> conf = new LinkedHashMap<Object, Object>(); protected TemplateHelper getTplHelper() { return tplHelper; } protected void setTplHelper(TemplateHelper tplHelper) { this.tplHelper = tplHelper; } protected TemplateHelper tplHelper; class TemplateHelper { // ? protected String TPL_FOLDER; // ? HTML protected String OUT_FOLDER; //? ? ? HTML protected String TPL_BASE; //? ? ? protected String TPL_ITEM; //? ( OUT_FOLDER), ?? HTML ? ? protected String ITEMS_FOLDER_NAME; //? HTML- protected String RESULT_FILENAME; public TemplateHelper(Map<Object, Object> params) throws Exception { if (!params.containsKey("TPL_FOLDER")) throw new Exception("? ? "); if (!params.containsKey("OUT_FOLDER")) throw new Exception( "? "); TPL_FOLDER = (String) params.get("TPL_FOLDER"); OUT_FOLDER = (String) params.get("OUT_FOLDER"); if (getElementName().equals("action")) { if (!params.containsKey("ACTIONS_TREE_TEMPLATE")) throw new Exception("? ?"); if (!params.containsKey("ACTION_TEMPLATE")) throw new Exception("? ?? ?"); if (!params.containsKey("OUT_ACTIONS_FILENAME")) throw new Exception("? ? html- "); if (!params.containsKey("OUT_ACTIONS_FOLDER")) throw new Exception("? ? ? ?? ?"); TPL_BASE = (String) params.get("ACTIONS_TREE_TEMPLATE"); TPL_ITEM = (String) params.get("ACTION_TEMPLATE"); RESULT_FILENAME = (String) params.get("OUT_ACTIONS_FILENAME"); ITEMS_FOLDER_NAME = (String) params.get("OUT_ACTIONS_FOLDER"); } else if (getElementName().equals("component")) { if (!params.containsKey("COMPONENTS_TREE_TEMPLATE")) throw new Exception("? "); if (!params.containsKey("COMPONENT_TEMPLATE")) throw new Exception("? ?? "); if (!params.containsKey("OUT_COMPONENTS_FILENAME")) throw new Exception("? ? html- "); if (!params.containsKey("OUT_COMPONENTS_FOLDER")) throw new Exception( "? ? ? ?? "); TPL_BASE = (String) params.get("COMPONENTS_TREE_TEMPLATE"); TPL_ITEM = (String) params.get("COMPONENT_TEMPLATE"); RESULT_FILENAME = (String) params.get("OUT_COMPONENTS_FILENAME"); ITEMS_FOLDER_NAME = (String) params.get("OUT_COMPONENTS_FOLDER"); } else { throw new Exception("?? " + getElementName()); } this.params = params; } protected Map<Object, Object> params; public void processBase(Map<Object, Object> data) throws IOException, TemplateException { Writer out = new FileWriter(OUT_FOLDER + RESULT_FILENAME); //? ? data.put("generator", readVersion()); //? data.put("project", params); getBaseTemplate().process(data, out); } public void processItem(T item) throws IOException, TemplateException { Writer out_a = new FileWriter(OUT_FOLDER + ITEMS_FOLDER_NAME + "/" + item.getLink() + ".html"); getActionTemplate().process(item.getTemplateDataModel(), out_a); } // freemarker protected Configuration templateConfig; /** * freemarker * * @return * @throws java.io.IOException */ protected Configuration getTemplateConfiguration() throws IOException { if (templateConfig == null) { templateConfig = new Configuration(); templateConfig.setOutputEncoding("UTF-8"); templateConfig.setDirectoryForTemplateLoading(new File(TPL_FOLDER)); templateConfig.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); } return templateConfig; } // freemarker ? ? protected Template htmlActionTpl; /** * freemarker ? * * @return * @throws java.io.IOException */ protected Template getActionTemplate() throws IOException { if (htmlActionTpl == null) htmlActionTpl = getTemplateConfiguration().getTemplate(TPL_ITEM); return htmlActionTpl; } // freemarker ? ? HTML protected Template htmlBaseTpl; /** * freemarker ? HTML * * @return * @throws java.io.IOException */ protected Template getBaseTemplate() throws IOException { if (htmlBaseTpl == null) htmlBaseTpl = getTemplateConfiguration().getTemplate(TPL_BASE); return htmlBaseTpl; } // .properties ? ? protected String VERSION_FILE = "./version.properties"; /** * ?? * * @return ?? ? freemarker * @throws java.io.IOException */ protected Map<Object, Object> readVersion() throws IOException { Properties props = new Properties(); props.load(new InputStreamReader(new FileInputStream(VERSION_FILE), "UTF-8")); return (Map<Object, Object>) props; } } }