Package misc

Source Code of misc.Settings

package misc;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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.Node;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class Settings {
  private Document document = null;
  private Node rootNode = null;
  private String settingFile = null;
//  private Node lastPathNode = null;
//  private Node languageNode = null;
//  private Node lookAndFeelNode = null;
//  private Node undoLevelNode = null;
 
  //  ------------------------------------------------
  public boolean loadFromFile(String settingFile)
  {
    this.settingFile = settingFile;
    if(!readFromXml(settingFile))
      return false;
    rootNode = this.getXmlChildNode(document,"settings");
    if(rootNode == document)
      return false;
    return true;
  }
  public void save() {
      try{
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        Properties properties = new Properties();
        properties.put("method", "xml");
        properties.put("encoding", "UTF-8");
        properties.put("indent", "yes");
        transformer.setOutputProperties(properties);

        Source input = new DOMSource(document);
        File outputFile = new File(settingFile);
        FileOutputStream outputStream = new FileOutputStream(outputFile);

        if(outputStream != null) {
          Result output = new StreamResult(outputStream);
          transformer.transform(input, output);
        }

        outputStream.flush();
        outputStream.close();
      }
      catch (FileNotFoundException e) {
        e.printStackTrace();
      }
      catch (FactoryConfigurationError e) {
        //erreur de configuration de fabrique
        e.printStackTrace();
      }
      catch (TransformerException e) {
        //erreur lors de la transformation
        e.printStackTrace();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
 
  //  ------------------------------------------------
  //accessors
 
  public String getLastPath()
  {
    if(rootNode == null)
      return "";
    Node lastPathNode = this.getXmlChildNode(rootNode,"lastPath");
    if(lastPathNode == rootNode)
      return "";
    return lastPathNode.getTextContent();
  }
 
  public void setLastPath(String value)
  {
    if(rootNode == null)
      return;
    Node lastPathNode = this.getXmlChildNode(rootNode,"lastPath");
    if(lastPathNode == rootNode)
      return;
    lastPathNode.setTextContent(value);
  }
 
  public String getLanguage()
  {
    if(rootNode == null)
      return "";
    Node languageNode = this.getXmlChildNode(rootNode,"language");
    if(languageNode == rootNode)
      return "";
    return languageNode.getTextContent();
  }
 
  public String getCountry()
  {
    if(rootNode == null)
      return "";
    Node countryNode = this.getXmlChildNode(rootNode,"country");
    if(countryNode == rootNode)
      return "";
    return countryNode.getTextContent();
  }
  public String getLookAndFeel()
  {
    if(rootNode == null)
      return "";
    Node node = this.getXmlChildNode(rootNode,"lookAndFeel");
    if(node == rootNode)
      return "";
    return node.getTextContent();
  }
  public int getUndoLevel()
  {
    if(rootNode == null)
      return 10;
    Node node = this.getXmlChildNode(rootNode,"undoLevel");
    if(node == rootNode)
      return 10;
    return Integer.parseInt(node.getTextContent());
  }
 
  //------------------------------------------------
 
  private Node getXmlChildNode(Node father,String name)
  {
    if(father == null)
      return null;
    Node child= father.getFirstChild();
    do
    {
      if(child.getLocalName() == name)
        return child;
    }while((child = child.getNextSibling()) != null);
    return father;
 
 
  //------------------------------------------------
 
  private boolean readFromXml(String xmlPath)
  {
      try
      {
        DOMParser parser = new DOMParser();
          parser.parse( xmlPath);
          this.document = parser.getDocument();         
      }  
      catch (IOException ie) {
            System.err.println("impossible de charge le fichier de configuration :" + xmlPath);
            return false;
         
      }  
      catch (SAXException se) {
        System.err.println("impossible de charge le fichier de configuration :" + xmlPath);
            return false;
         
      }
      return true;
  }
}
TOP

Related Classes of misc.Settings

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.