Java tutorial
/* * Copyright 2009-2014 MOSPA(Ministry of Security and Public Administration). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package egovframework.rte.fdl.xml; import java.io.CharArrayReader; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Set; import egovframework.rte.fdl.xml.exception.ValidatorException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.transform.JDOMSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.xml.sax.InputSource; /** * XML ? ? ? ?. * XML XML ? ? . * ? ? ? parse() abstract . * * @author ?? * @since 2009.03.17 * @version 1.0 * @see <pre> * == ?(Modification Information) == * * ? ? * --------- --------- ------------------------------- * 2009.03.17 ? * * </pre> */ public abstract class AbstractXMLUtility { /** ? **/ private String fileName = null; /** ? **/ private String schmafileName = null; /** xmlValue **/ private String xmlValue = null; /** ApplicationContext */ private ApplicationContext context; /** XmlConfig class **/ private XmlConfig xmlConfig; /** xml Class */ private String savedPath; /** xml ? */ private String configPath = "classpath*:spring/egovxmlCfg.xml"; /** * AbstractXMLUtility ?? */ public AbstractXMLUtility() { context = new FileSystemXmlApplicationContext(configPath); xmlConfig = (XmlConfig) context.getBean("xmlconfig"); savedPath = xmlConfig.getXmlpath(); } /** * XML ? ? ?? * * @param fileName - xml */ public void setXMLFile(String fileName) { this.fileName = fileName; } /** * XML * * @return XML */ public String getXMLFile() { return fileName; } /** * XML SCHEMA ? ? ?? * * @param schmafileName - ? */ public void setSCHEMAFile(String schmafileName) { this.schmafileName = schmafileName; } /** * XML SCHEMA * * @return XML SCHEMA */ public String getSCHEMAFile() { return schmafileName; } /** * XML String String ? * * @param xmlValue - XML String */ public void setXML(String xmlValue) { this.xmlValue = xmlValue; } /** * XML String * * @return XML String */ public String getXML() { return xmlValue; } /** * ? String? ? InputSource * * @return XML InputStream */ protected InputSource stringToInputSource() { char[] xml = getXML().toCharArray(); CharArrayReader reader = new CharArrayReader(xml); return new InputSource(reader); } /** * DOM, SAX ? XML ? ? ? ? ? * * @param isValid - Validation * @throws ValidatorException */ public abstract boolean parse(boolean isValid) throws ValidatorException; //public abstract NodeList getResults(String xmlFile,String expression) throws UnSupportedException; /** * XML ? ? ? ? ? * * @param errorReport - ? ? * @throws ValidatorException */ protected void makeErrorMessage(Set<?> errorReport) throws ValidatorException { StringBuffer errorMessage = new StringBuffer(); Iterator<?> iterator = errorReport.iterator(); while (iterator.hasNext()) { String tmp = (String) iterator.next(); errorMessage.append(tmp); errorMessage.append("<br/>"); iterator.remove(); } throw new ValidatorException(errorMessage.toString()); } @SuppressWarnings("deprecation") public List<?> getResult(Document doc, String exps) throws JDOMException { org.jdom2.xpath.XPath xPath = org.jdom2.xpath.XPath.newInstance(exps); return xPath.selectNodes(doc); } /** * Element * * @param doc - Document ? * @param addNDName - ? Element * @param list - Element * @param path - ? XML * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void addElement(Document doc, String addNDName, List<?> list, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element root = doc.getRootElement(); addNode(root, addNDName, list); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "addElement.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } } /** * TextElement * * @param doc - Document ? * @param addNDName - ? Elememt * @param list - TextElement * @param path * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void addTextElement(Document doc, String addNDName, List<?> list, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element root = doc.getRootElement(); addTextNode(root, addNDName, list); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "addElement.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } } /** * TextNode * * @param element - ? Element * @param addNDName - ? Element * @param list - TextElement */ public void addTextNode(Element element, String addNDName, List<?> list) { List<?> childList = element.getChildren(); if (childList.size() != 0) { for (int yy = 0; yy < childList.size(); yy++) { Element tmp = (Element) childList.get(yy); if (tmp.getName().equals(addNDName)) { for (int tt = 0; tt < list.size(); tt++) { SharedObject sobj = (SharedObject) list.get(tt); tmp.addContent((String) sobj.getValue()); } } addTextNode(tmp, addNDName, list); } } else { if (element.getName().equals(addNDName)) { for (int tt = 0; tt < list.size(); tt++) { SharedObject sobj = (SharedObject) list.get(tt); element.addContent((String) sobj.getValue()); } } } } /** * TextElement Update * * @param doc - Document ? * @param list - update TextNode * @param path - ? XML * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void updTextElement(Document doc, List<?> list, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element root = doc.getRootElement(); updTextNode(root, list); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "updTEXT.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } } /** * TextNode Update * * @param element - ? Element * @param list - update TextNode */ public void updTextNode(Element element, List<?> list) { List<?> childList = element.getChildren(); if (childList.size() != 0) { for (int yy = 0; yy < childList.size(); yy++) { Element tmp = (Element) childList.get(yy); for (int tt = 0; tt < list.size(); tt++) { SharedObject sobj = (SharedObject) list.get(tt); if (tmp.getValue().equals((String) sobj.getKey())) { tmp.setText((String) sobj.getValue()); break; } } updTextNode(tmp, list); } } else { for (int tt = 0; tt < list.size(); tt++) { SharedObject sobj = (SharedObject) list.get(tt); if (element.getValue().equals((String) sobj.getKey())) { element.setText((String) sobj.getValue()); break; } } } } /** * Node * * @param element - ? Element * @param addNDName - ? * @param list - add TextNode */ public void addNode(Element element, String addNDName, List<?> list) { List<?> childList = element.getChildren(); if (childList.size() != 0) { for (int yy = 0; yy < childList.size(); yy++) { Element tmp = (Element) childList.get(yy); if (tmp.getName().equals(addNDName)) { for (int tt = 0; tt < list.size(); tt++) { SharedObject sobj = (SharedObject) list.get(tt); Element enew = new Element((String) sobj.getKey()); enew.setText((String) sobj.getValue()); tmp.addContent(enew); } } addNode(tmp, addNDName, list); } } else { if (element.getName().equals(addNDName)) { for (int tt = 0; tt < list.size(); tt++) { SharedObject sobj = (SharedObject) list.get(tt); Element enew = new Element((String) sobj.getKey()); enew.setText((String) sobj.getValue()); element.addContent(enew); } } } } /** * * * @param element - ? Element * @param name - */ public void removeNode(Element element, String name) { List<?> list = element.getChildren(); if (list.size() != 0) { for (int i = 0; i < list.size(); i++) { Element tmp = (Element) list.get(i); if (tmp.getName().equals(name)) { tmp.getParentElement().removeChild(name); } else { removeNode(tmp, name); } } } else { // Visit the children // logger.debug("element Name :"+element.getName()); if (element.getName().equals(name)) { element.getParentElement().removeChild(name); } } } /** * Element * * @param doc - document ? * @param nodeName - * @param path - ? XML * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void delElement(Document doc, String nodeName, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element element = doc.getRootElement(); removeNode(element, nodeName); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "delXML.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } } /** * * * @param element - ? Element * @param oldNode - * @param newNodename - */ public void chgNode(Element element, String oldNode, String newNodename) { List<?> list = element.getChildren(); if (list.size() != 0) { for (int i = 0; i < list.size(); i++) { Element tmp = (Element) list.get(i); if (element.getName().equals(oldNode)) { element.setName(newNodename); } else { chgNode(tmp, oldNode, newNodename); } } } else { // Visit the children // logger.debug("element Name:"+element.getName()); if (element.getName().equals(oldNode)) { element.setName(newNodename); } } } /** * Element Update * * @param doc - document ? * @param oldNodename - * @param newNodeName - * @param path - ? XML * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void updElement(Document doc, String oldNodename, String newNodeName, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element root = doc.getRootElement(); chgNode(root, oldNodename, newNodeName); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "updElement.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } } /** * XML ? * * @param doc - document ? * @param newNodeName - Root * @param list - Element * @param path - ? XML * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void createNewXML(Document doc, String newNodeName, List<?> list, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element root = new Element(newNodeName); doc.addContent(root); createXML(root, list); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "newXML.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } } /** * XML ? * * @param root - Root Element * @param list - Element */ public void createXML(Element root, List<?> list) { for (int i = 0; i < list.size(); i++) { SharedObject sobj = (SharedObject) list.get(i); Element elm = new Element(sobj.getKey()); elm.setText((String) sobj.getValue()); root.addContent(elm); } } }