1 7 package org.enhydra.convert; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import org.enhydra.convert.xml.*; 12 import java.util.Vector ; 13 import com.lutris.util.KeywordValueException; 14 import com.lutris.util.Config; 15 16 import java.io.InputStream ; 17 18 19 24 public class XMLFile { 25 private WebApp webapp; 26 private boolean description; 27 private String encoding; 28 29 public XMLFile (){ 30 description = true; 31 encoding = "ISO-8859-1"; 32 InputStream is = this.getClass().getResourceAsStream("/org/enhydra/convert/xml/template/web.xml"); 33 webapp = new WebAppImpl(); 34 read(is); 35 } 36 37 public XMLFile (File templateFile){ 38 description = true; 39 webapp = new WebAppImpl(); 40 read(templateFile); 41 } 42 43 public XMLFile (InputStream is){ 44 description = true; 45 webapp = new WebAppImpl(); 46 read(is); 47 } 48 49 public boolean read(File inFile){ 50 try { 51 if (inFile.exists()) 52 webapp = WebAppUnmarshaller.unmarshal(inFile); 53 return true; 54 } catch (IOException ex){ 55 System.out.println("Problem with reading file have occured!"); 56 return false; 57 } 58 } 59 60 public boolean read(InputStream is){ 61 try { 62 if (is!=null) 63 webapp = WebAppUnmarshaller.unmarshal(is); 64 return true; 65 } catch (IOException ex){ 66 System.out.println("Problem with reading InputStream have occured!"); 67 return false; 68 } 69 } 70 71 public boolean write(File 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 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 keyName=null; 89 String keyValue=null; 90 String keyType=null; 91 String keyDescription=null; 92 93 boolean isArray=false; 94 95 String [] keys = config.leafKeys(); 97 98 Vector 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 ) order.elementAt(i); 110 111 try { 112 Object tempValue = config.get(keyName); 113 if(tempValue instanceof String ) { 114 keyValue = new String ((String )tempValue); 115 }else if (tempValue.getClass().isArray()){ 116 isArray=true; 117 String [] keyValues = null; 118 Object [] oa = (Object [])tempValue; 119 if ((oa.length > 0) && (oa[0] instanceof java.lang.String )) 120 keyValues = (String [])tempValue; 121 else { 122 keyValues = new String [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 makeXMLString (String oldString) { 178 if (oldString == null) 179 return null; 180 String newString = null; 181 newString = oldString.replace('.', '/'); 182 return newString; 183 } 184 185 private static String makeDescription (String oldString) { 186 187 int i =-1; 188 189 if (oldString == null) 190 return null; 191 192 String 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 setting){ 211 encoding = setting; 212 } 213 214 public String getEncoding(){ 215 return encoding; 216 } 217 } 218 | Popular Tags |