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 org.jdom2.Content; import org.jdom2.Element; import org.jdom2.output.XMLOutputter; /** * <h3></h3> * <p></p> * <p>Author: predtechenskaya (predtechenskaya@i-teco.ru)</p> * <p>Date: 03.04.13</p> */ public abstract class Config { protected Element element; protected Scope scope; public Scope getScope() { return scope; } public String getPath() { return path; } protected String path; public void init(Element element, String path) { this.element = element; this.path = path.replace("\\", "/").substring(path.lastIndexOf("webapp") + 6); initScope(); }; public String getXMLContent() { return new XMLOutputter().outputString(element).trim(); } public static String parseIdFromPath(String path) { String[] couples = path.split(":"); if (couples.length == 2) return couples[0]; return ""; } public static String generateBaseUniqueId(String id, String path) { return new StringBuilder().append(id).append(":").append(path).toString(); } public String getDescription() { return getPreviousComment(element); } /** * ? element. * ? ? , null. * * @param element * @return */ protected static String getPreviousComment(Element element) { //? ? Element parent = element.getParentElement(); //? ? int index = parent.indexOf(element); // "" ?, ?- for (int i = index - 1; i > 0; i--) { //? Content prev = parent.getContent(i); //? ? ? - ?, , ? /?? if (prev.getCType().equals(Content.CType.Text)) continue; //? ? - , ?, else if (prev.getCType().equals(Content.CType.Comment)) return prev.getValue(); // ?, ? , ? ? ?. . else return ""; } // ? . return ""; } public String getElementSource(String name) { Element child = element.getChild(name); if (child != null) return new XMLOutputter().outputString(child).trim(); return ""; } protected void initScope() { scope = new Scope(element); } }