KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > convert > XMLFile


1 /*
2  * Created on Dec 27, 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package org.enhydra.convert;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import org.enhydra.convert.xml.*;
12 import java.util.Vector JavaDoc;
13 import com.lutris.util.KeywordValueException;
14 import com.lutris.util.Config;
15
16 import java.io.InputStream JavaDoc;
17
18
19 /**
20  * @author Slobodan Vujasinovic
21  *
22  * Window - Preferences - Java - Code Style - Code Templates
23  */

24 public class XMLFile {
25     private WebApp webapp;
26     private boolean description;
27     private String JavaDoc encoding;
28     
29     public XMLFile (){
30         description = true;
31         encoding = "ISO-8859-1";
32         InputStream JavaDoc is = this.getClass().getResourceAsStream("/org/enhydra/convert/xml/template/web.xml");
33         webapp = new WebAppImpl();
34         read(is);
35     }
36     
37     public XMLFile (File JavaDoc templateFile){
38         description = true;
39         webapp = new WebAppImpl();
40         read(templateFile);
41     }
42     
43     public XMLFile (InputStream JavaDoc is){
44         description = true;
45         webapp = new WebAppImpl();
46         read(is);
47     }
48     
49     public boolean read(File JavaDoc inFile){
50         try {
51             if (inFile.exists())
52                 webapp = WebAppUnmarshaller.unmarshal(inFile);
53             return true;
54         } catch (IOException JavaDoc ex){
55             System.out.println("Problem with reading file have occured!");
56             return false;
57         }
58     }
59     
60     public boolean read(InputStream JavaDoc is){
61         try {
62             if (is!=null)
63                 webapp = WebAppUnmarshaller.unmarshal(is);
64             return true;
65         } catch (IOException JavaDoc ex){
66             System.out.println("Problem with reading InputStream have occured!");
67             return false;
68         }
69     }
70     
71     public boolean write(File JavaDoc outFile){
72         try {
73             if (!outFile.exists()){
74                 outFile.createNewFile();
75             }
76             webapp.setOutputEncoding(encoding);
77             webapp.marshal(outFile);
78             return true;
79         } catch (IOException JavaDoc ex){
80             System.out.println("Couldn't create file");
81             return false;
82         }
83     }
84     
85     public boolean sinhronize(ConfigFile configFile){
86         Config config = configFile.getConfig();
87         
88         String JavaDoc keyName=null;
89         String JavaDoc keyValue=null;
90         String JavaDoc keyType=null;
91         String JavaDoc keyDescription=null;
92         
93         boolean isArray=false;
94         
95         //add existing EnvEntries from Config object
96
String JavaDoc [] keys = config.leafKeys();
97         
98         Vector JavaDoc order = configFile.getOrder();
99         if (keys.length==order.size()){
100             for (int i = 0; i < order.size(); i++) {
101                 
102                 keyName=null;
103                 keyValue=null;
104                 keyType=null;
105                 keyDescription=null;
106                 
107                 isArray=false;
108                     
109                 keyName = (String JavaDoc) order.elementAt(i);
110
111                 try {
112                     Object JavaDoc tempValue = config.get(keyName);
113                     if(tempValue instanceof String JavaDoc) {
114                         keyValue = new String JavaDoc((String JavaDoc)tempValue);
115                     }else if (tempValue.getClass().isArray()){
116                         isArray=true;
117                         String JavaDoc [] keyValues = null;
118                         Object JavaDoc[] oa = (Object JavaDoc[])tempValue;
119                         if ((oa.length > 0) && (oa[0] instanceof java.lang.String JavaDoc))
120                           keyValues = (String JavaDoc[])tempValue;
121                         else {
122                           keyValues = new String JavaDoc[oa.length];
123                           for (int k = 0; k < oa.length; k++)
124                               keyValues[k] = oa[k].toString();
125                         }
126                         if (keyValues.length>0){
127                             keyValue = keyValues[0];
128                             for (int j = 1; j < keyValues.length; j++){
129                                 keyValue = keyValue + ", " + keyValues[j];
130                                 
131                             }
132                         } else {
133                             keyValue = "";
134                         }
135                     }
136                     keyType = "java.lang.String";
137                     if (description) {
138                         keyDescription = makeDescription(configFile.getComment(keyName));
139                     }else {
140                         keyDescription = "";
141                     }
142                     keyName = makeXMLString(keyName);
143                     if (isArray){
144                         keyName = keyName + "[]";
145                     }
146                 } catch(KeywordValueException ex){
147                     System.out.println("Experienced problems with "+keyName+" parameter definition!");
148                 }
149                 
150                 Description desc = new DescriptionImpl();
151                 desc.setValue(keyDescription);
152                 
153                 EnvEntryName en = new EnvEntryNameImpl();
154                 en.setValue(keyName);
155                 
156                 EnvEntryValue ev = new EnvEntryValueImpl();
157                 ev.setValue(keyValue);
158                 
159                 EnvEntryType et = new EnvEntryTypeImpl();
160                 et.setValue(keyType);
161                 
162                 EnvEntry ee = new EnvEntryImpl();
163                 
164                 ee.setDescription(desc);
165                 ee.setEnvEntryName(en);
166                 ee.setEnvEntryValue(ev);
167                 ee.setEnvEntryType(et);
168                 
169                 webapp.addEnvEntry(ee);
170             }
171             return true;
172         } else {
173             return false;
174         }
175     }
176     
177     private static String JavaDoc makeXMLString (String JavaDoc oldString) {
178           if (oldString == null)
179             return null;
180           String JavaDoc newString = null;
181           newString = oldString.replace('.', '/');
182           return newString;
183     }
184     
185     private static String JavaDoc makeDescription (String JavaDoc oldString) {
186
187         int i =-1;
188           
189         if (oldString == null)
190             return null;
191           
192         String JavaDoc newString = null;
193         oldString = oldString.trim();
194         oldString = oldString.replaceAll("#","");
195         oldString = oldString.replaceAll("--","++");
196         
197         newString = oldString.trim();
198         
199         return newString;
200     }
201     
202     public void setDescription(boolean setting){
203         description = setting;
204     }
205     
206     public boolean getDescription(){
207         return description;
208     }
209     
210     public void setEncoding(String JavaDoc setting){
211         encoding = setting;
212     }
213     
214     public String JavaDoc getEncoding(){
215         return encoding;
216     }
217 }
218
Popular Tags