Package xml.config

Source Code of xml.config.XMLConfigManager

package xml.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import xml.config.exceptions.EmptyXMLConfigException;
import xml.config.exceptions.NotWellFormedXMLConfigFileException;
import xml.config.exceptions.UnexpectedException;


public class XMLConfigManager {
  // ** ATTRIBUTES ** //
  // **************** //
  private XMLConfig xmlConfig;
   
  // ** CONSTRUCTORS ** //
  // ****************** //
  public XMLConfigManager(){
    List<String> tagNames = new ArrayList<String>();
    Hashtable<String,String> table = new Hashtable<String,String>();
    xmlConfig = new XMLConfig(tagNames, table);
  }
 
  XMLConfigManager(List<String> tagNames, Hashtable<String, String> table){
    xmlConfig = new XMLConfig(tagNames, table);
  }
 
  // ** GETTERS n SETTERS ** //
  // *********************** //
  public XMLConfig getXmlConfig() {
    return xmlConfig;
  }

  public void setXmlConfig(XMLConfig xmlConfig) {
    this.xmlConfig = xmlConfig;
  }
 
  // ** METHODS ** //
  // ************* //
  /**
   * Generates a XML file which contains the XMLConfig data.
   * @param file The Object File pointing to where the XML file is
   * going to be generated.
   * @throws UnexpectedException This should never occur.
   * @see UnexpectedException
   * @throws EmptyXMLConfigException
   * @see EmptyXMLConfigException
   */
  public void toFile(File file) throws UnexpectedException, EmptyXMLConfigException{
    try{
      // GENERATE
      Iterator<String> keys = xmlConfig.getTagNames().iterator();
     
      if(!keys.hasNext())
        throw new EmptyXMLConfigException();
     
      Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
      Element rootElement = doc.createElement("XMLConfig");
      doc.appendChild(rootElement);

      while(keys.hasNext()){
        String tagName = keys.next();
        String tagValue = xmlConfig.get(tagName);
       
        Element el = doc.createElement(tagName);
        el.setTextContent(tagValue);
       
        rootElement.appendChild(el);
      }
     
      // EXPORT
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      DOMSource source = new DOMSource(doc);
      StreamResult result = new StreamResult(new FileOutputStream(file));
     
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
     
      transformer.transform(source, result);
     
    }catch(ParserConfigurationException pcex){
      throw new UnexpectedException();
    }catch(TransformerConfigurationException tcex){
      throw new UnexpectedException();
    }catch(FileNotFoundException fnfex){
      throw new UnexpectedException();
    }catch(TransformerException tex){
      throw new UnexpectedException();
    }
  }
 
  // ** STATIC METHODS ** //
  // ******************** //
  public static XMLConfigManager fromFile(File file) throws IOException, NotWellFormedXMLConfigFileException{
    Document doc;
    try{
       doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
    }catch(ParserConfigurationException pcex){
      throw new NotWellFormedXMLConfigFileException();
    }catch(SAXException saxex){
      throw new NotWellFormedXMLConfigFileException();
    }
   
    Node rootNode = doc.getFirstChild();
    NodeList children = rootNode.getChildNodes();
   
    List<String> tagNames = new ArrayList<String>(0);
    Hashtable<String,String> table = new Hashtable<String,String>();
   
    for(int i = 0 ; i < children.getLength() ; i++){
      Node n = children.item(i);
      if(n.getNodeType()==Node.ELEMENT_NODE){
        String key = n.getNodeName();
        String value = n.getTextContent();
        tagNames.add(key);
        table.put(key,value);
      }
    }
   
    return new XMLConfigManager(tagNames, table);
  }
}
TOP

Related Classes of xml.config.XMLConfigManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.