View Javadoc

1   package org.dbe.studio.tools.accounting.metering.wizards;
2   
3   import java.io.BufferedReader;
4   import java.io.BufferedWriter;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.FileNotFoundException;
8   import java.io.FileOutputStream;
9   import java.io.FileReader;
10  import java.io.FileWriter;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.InputStreamReader;
14  import java.io.OutputStream;
15  import java.net.URI;
16  import java.util.Enumeration;
17  import java.util.Properties;
18  
19  import org.apache.log4j.Logger;
20  
21  public class PropertiesFile extends File {
22  
23  	private Properties properties;
24  
25  	private static final String COMMENT_CHAR = "#";
26  	
27  	private static Logger logger = Logger
28  	.getLogger(PropertiesFile.class.getName());
29  
30  	public PropertiesFile(String arg0) {
31  		
32  		super(arg0);
33  		logger.info("creating properties file - construct");
34  		properties = new Properties();
35  		try {
36  			InputStream in = new FileInputStream(this);
37  			properties.load(in);
38  			in.close();
39  		} catch (IOException e) {
40  			// TODO Auto-generated catch block
41  			e.printStackTrace();
42  		}
43  
44  		// TODO Auto-generated constructor stub
45  	}
46  
47  	public PropertiesFile(String arg0, String arg1) {
48  		super(arg0, arg1);
49  		// TODO Auto-generated constructor stub
50  	}
51  
52  	public PropertiesFile(File arg0, String arg1) {
53  		super(arg0, arg1);
54  		// TODO Auto-generated constructor stub
55  	}
56  
57  	public PropertiesFile(URI arg0) {
58  		super(arg0);
59  		// TODO Auto-generated constructor stub
60  	}
61  
62  	
63  
64  	public int compareTo(Object arg0) {
65  		// TODO Auto-generated method stub
66  		return 0;
67  	}
68  
69  	public String getProperty(String key) {
70  
71  		return properties.getProperty(key);
72  
73  	}
74  
75  	public void setProperty(String key, String value) {
76  		logger.info("setting properties key");
77  		
78  		Properties tempProperty = new Properties();
79  		String propertyString = key + "=" + value;
80  		tempProperty.setProperty(key, value);
81  		// BufferedWriter bw = new BufferedWriter();
82  
83  		try {
84  			// OutputStream os = new FileOutputStream(this);
85  			
86  			if (!isProperty(key)) {
87  				logger.info("appending to file");
88  				BufferedWriter bw = new BufferedWriter(new FileWriter(this,
89  						true));
90  				bw.newLine();
91  				bw.write(propertyString);
92  				bw.close();
93  			} else {
94  				logger.info("creating new file");
95  				// Create temp file.
96  				File temp = File.createTempFile("tempProps", ".suffix");
97  				// Delete temp file when program exits.
98  				temp.deleteOnExit();
99  				// Write to temp file
100 				BufferedWriter out = new BufferedWriter(new FileWriter(temp,
101 						true));
102 
103 				FileReader fr = new FileReader(this);
104 				BufferedReader in = new BufferedReader(new FileReader(this));
105 				String str = "";
106 				String firstChar = "";
107 				String tempKey = "";
108 				
109 				while (str != null) {
110 					
111 					str = in.readLine();
112 					if (str == null) break;
113 					if (str.trim().startsWith(key)){
114 						out.write(propertyString);
115 						out.newLine();
116 						continue;}
117 					
118 					
119 					out.write(str);
120 					out.newLine();
121 					//str = in.readLine();
122 				}
123 				out.close();
124 				in.close();
125 				InputStream in1 = new FileInputStream(temp);
126 		        OutputStream out1 = new FileOutputStream(this);
127 		    
128 		        // Transfer bytes from in to out
129 		        byte[] buf = new byte[1024];
130 		        int len;
131 		        while ((len = in1.read(buf)) > 0) {
132 		            out1.write(buf, 0, len);
133 		        }
134 		        in1.close();
135 		        out1.close();
136 			}
137 
138 		} catch (FileNotFoundException e) {
139 			// TODO Auto-generated catch block
140 			e.printStackTrace();
141 		} catch (IOException e) {
142 			// TODO Auto-generated catch block
143 			e.printStackTrace();
144 		}catch (Exception e){
145 			e.printStackTrace();
146 		}
147 
148 	}
149 
150 	public boolean isProperty(String key) {
151 
152 		
153 		return properties.containsKey(key);
154 
155 	}
156 
157 	public void setPropertys(Properties filterProperties) {
158 		logger.info("adding properties");
159 		
160 		String key = null;
161 		String value = null;
162 		Enumeration keys = filterProperties.keys();
163 		while (keys.hasMoreElements()){
164 			key = keys.nextElement().toString();
165 			value = filterProperties.getProperty(key);
166 			setProperty(key,value);
167 		}
168 		
169 	}
170 
171 }