Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/robrohan/tools/ConfigFile.java


1    /*
2    * Treebeard: an xml xslt transfomer
3    * Copyright (C) 2002 Rob Rohan
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the
6    * Free Software Foundation; either version 2 of the License, or (at your
7    * option) any later version.
8    * 
9    * This program is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; without even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
12   * General Public License for more details.
13   * 
14   * You should have received a copy of the GNU General Public License along 
15   * with this program; if not, write to the Free Software Foundation, Inc.,
16   * 675 Mass Ave, Cambridge, MA 02139, USA.
17   *
18   * Email: me@robrohan.com
19   * 
20   * ConfigFile.java
21   *
22   * Created on May 2, 2002, 10:22 PM
23   */
24   
25  package com.robrohan.tools;
26  
27  /**
28   * This is used to read simple xml config documents and
29   * either just get the DOM or allow access to specific
30   * values.
31   * @author  rob 
32   */
33  public class ConfigFile {
34      
35      /** Which dom factory to use */    
36      public String DOMFactory = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl";
37      
38      private String filename;
39      private org.w3c.dom.Document document=null;
40      
41      /** Creates a new instance of ConfigFile */
42      public ConfigFile() {;}
43      
44      /** Creates a new config file object using the passed file name
45       * @param path URI to a xml file to be used as the config
46       * file.
47       * @throws Exception io and others, covers all
48       * @throws FactoryConfigurationError if the dom string is wrong or some other xml factory error
49       */    
50      public ConfigFile(String path) throws Exception, javax.xml.parsers.FactoryConfigurationError{
51          this.filename=path;
52          openFile();
53      }
54      
55      /** Sets the config file URI to a new file
56       * @param path URI to the xml file to be used as the config
57       * file
58       */    
59      public void setFileName(String path){
60          this.filename=path;
61      }
62      
63      /** Returns the current file URI
64       * @return currrent file used in this object
65       */    
66      public String getFileName(){
67          return this.filename;
68      }
69      
70      /** Opens the file that was set using setFile or on object creation
71       * @throws Exception io et all
72       * @throws FactoryConfigurationError if the dom string is wrong or some other xml factory error
73       */    
74      public void openFile() throws Exception, javax.xml.parsers.FactoryConfigurationError{
75          
76          System.setProperty("javax.xml.parsers.DocumentBuilderFactory", DOMFactory);
77          javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
78          javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();          
79          document = builder.parse(new java.io.File(this.filename));
80    
81      }
82      
83      /** Saves the config file if possible
84       */    
85      public void saveFile(){
86          
87      }
88      
89      /** Returns the number of times "key" is found in the config
90       * file
91       * @return The number of times key is found in config
92       * file
93       * @param key The key you want to know how many times is in
94       * the file
95       */    
96      public int getLength(String key){
97          return document.getElementsByTagName(key).getLength();
98      }
99      
100     /** Gets a value from the file
101      * @param key The key sought. for example using
102      * <CODE>
103      * ...
104      * &lt;thing&gt;my info&lt;/thing&gt;
105      * ...
106      * </CODE>
107      * it would be "thing"
108      * @param iteration which one you want if the key appears multi
109      * times in the file. Most often this will be
110      * set to 0.
111      * @return The value of the key
112      */    
113     public String getValue(String key, int iteration){
114         return document.getElementsByTagName(key).item(iteration)
115             .getFirstChild().getNodeValue();
116     }
117     
118     /** Changes the value of a key
119      * @param key The key name to change
120      * @param iteration if multi times, which one. Most often set to
121      * 0
122      * @param newValue What the key should equal when this is done
123      * with it.
124      */    
125     public void setValue(String key, int iteration, String newValue){
126         document.getElementsByTagName(key).item(iteration)
127             .getFirstChild().setNodeValue(newValue);
128     }
129     
130     /** Get the raw DOM
131      * @return the xml config file in a DOM object
132      */    
133     public org.w3c.dom.Document getDOM(){
134         return this.document;
135     }
136     
137 }