1 23 package org.enhydra.util; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 36 import org.apache.xml.serialize.OutputFormat; 37 import org.apache.xml.serialize.XMLSerializer; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Node ; 40 import org.w3c.dom.NodeList ; 41 import org.xml.sax.EntityResolver ; 42 import org.xml.sax.InputSource ; 43 44 import com.lutris.util.Config; 45 import com.lutris.util.ConfigException; 46 import com.lutris.util.KeywordValueException; 47 import com.lutris.util.KeywordValueTable; 48 import com.lutris.util.ParseException; 49 50 58 59 public class XMLConfigFile extends AbsConfigFile{ 60 61 64 private Document doc = null; 65 66 69 private Node xmlDoc = null; 70 71 74 private String version; 75 76 79 private String encoding; 80 81 84 public XMLConfigFile () { 85 super(); 86 version = ""; 87 encoding = ""; 88 } 89 90 95 99 100 107 public XMLConfigFile (File file) throws ConfigException, IOException { 108 super(file); 109 version = ""; 110 encoding = ""; 111 this.readXmlDeclaration(); 113 String name = file.getName(); 114 EntityResolver er = new PublicIdResolver(); 117 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 118 factory.setValidating(false); 119 factory.setIgnoringElementContentWhitespace(false); 120 try { 121 DocumentBuilder builder = factory.newDocumentBuilder(); 122 builder.setEntityResolver(er); 123 doc = builder.parse(file); 124 } 125 catch (Exception ex){ 126 throw new ConfigException("Error in reading configuration parameters from file."); 128 129 } 130 131 NodeList nl = doc.getChildNodes(); 132 boolean found = false; 133 int i = 0; 134 135 while (i<nl.getLength() && !found){ 137 xmlDoc = nl.item(i); 138 if (xmlDoc.getNodeName().equalsIgnoreCase("web-app") && xmlDoc.getNodeType() == Document.ELEMENT_NODE) 139 found = true; 140 i++; 141 } 142 153 } 154 155 160 public XMLConfigFile (KeywordValueTable kvt) throws ConfigException { 161 super(kvt); 162 version = ""; 163 encoding = ""; 164 } 165 166 169 protected void readJndi() throws ConfigException { 170 try { 171 if (jndiAdapt == null){ 172 try { 173 jndiAdapt = new JNDIAdapter(this.file.getAbsolutePath(), "org.enhydra.spi.webxml.WebXmlInitialContextFactory"); 174 } 175 catch(Exception e){ 176 jndiAdapt = new JNDIAdapter(); 177 } 178 } 179 String [] leafKeys = jndiAdapt.leafKeys(); 180 if (leafKeys != null) { 181 int leafKeysLength = leafKeys.length; 182 String newKey = null; 183 String stringValue; 184 String [] stringValues; 185 for (int i=0; i<leafKeysLength; i++){ 186 String leafKey = leafKeys[i]; 187 if (leafKey != null) { 188 if (jndiAdapt.isArray(leafKey)){ 189 newKey = jndiAdapt.removeArrayMark(leafKey); 190 if (newKey !=null) { 191 stringValues = jndiAdapt.getStrings(newKey); 192 newKey = jndiAdapt.makeConfigString(newKey); 193 config.set(newKey, stringValues); 194 if (!order.contains(newKey)) 195 order.addElement(newKey); 196 comments.put(newKey, ""); 197 } 198 } else { 200 Object ovalue = jndiAdapt.get(leafKey); 201 newKey = jndiAdapt.makeConfigString(leafKey); 202 203 if (ovalue instanceof java.lang.String ){ 205 stringValue = (String )ovalue; 206 if (stringValue.startsWith("jndi:")){ 207 stringValue = stringValue.substring(5); 208 Object resource = jndiAdapt.getResource(stringValue); 209 210 if (resource != null) { 211 config.set(newKey, resource); 212 jndiParameterNames.put(newKey,stringValue); 213 } 214 else { 215 config.set(newKey, "jndi:"+stringValue); 216 } 217 } else { 219 config.set(newKey, stringValue); 220 } 221 if (!order.contains(newKey)) 222 order.addElement(newKey); 223 comments.put(newKey, ""); 224 } else { 226 } 227 } } } } } catch (Exception e){ 233 throw new ConfigException("Error in reading JNDI configuration parameters."); 236 } 237 } 238 239 246 private void readXmlDeclaration() throws IOException { 247 String xmlHeader = ""; 248 int length; 249 FileInputStream fi = new FileInputStream (file); 250 byte[] bChar = new byte[1]; 251 int ok = fi.read(bChar); 252 String tempString = ""; 253 if (ok != -1) { 254 String ch = new String (bChar); 255 if (ch.equalsIgnoreCase("<")) { 256 tempString = tempString + ch; 257 ok = fi.read(bChar); 258 while (ok != -1 && !new String (bChar).equalsIgnoreCase(">")) { 259 tempString = tempString + new String (bChar); 260 ok = fi.read(bChar); 261 } 262 if (ok != -1){ 263 tempString = tempString + new String (bChar); 264 if (tempString.length() >= 5) { 265 if (tempString.substring(0, 5).equalsIgnoreCase("<?xml")) { 266 xmlHeader = tempString; 267 } 268 } 269 } 270 } 271 } 272 if (xmlHeader != null){ 273 int index, startQuote, endQuote; 274 index = xmlHeader.indexOf("version"); 275 if (index != -1){ 276 startQuote = xmlHeader.indexOf("\"",(index+7)); 277 if (startQuote != -1) { 278 endQuote = xmlHeader.indexOf("\"", startQuote + 1); 279 if ((endQuote != -1) && (startQuote+1 < endQuote)) { 280 version = xmlHeader.substring(startQuote + 1, endQuote); 281 } 282 } 283 } 284 index = xmlHeader.indexOf("encoding"); 285 if (index != -1){ 286 startQuote = xmlHeader.indexOf("\"",(index+8)); 287 if (startQuote != -1) { 288 endQuote = xmlHeader.indexOf("\"", startQuote + 1); 289 if ((endQuote != -1) && (startQuote+1 < endQuote)) { 290 encoding = xmlHeader.substring(startQuote + 1, endQuote); 291 } 292 } 293 } 294 } 295 fi.close(); 296 } 297 298 306 415 416 424 425 private void parseTree() throws ParseException, KeywordValueException { 426 Node web = xmlDoc; 427 Node envParam = null; 428 Node param = null; 429 Node paramValue = null; 430 Node deleteNode = null; 431 432 if ((web != null) && (web.getNodeName().equals("web-app"))) { 433 envParam = web.getFirstChild(); 434 while (envParam != null) { 435 if (envParam.getNodeName().equals("env-entry")){ 436 param = envParam.getFirstChild(); 437 String paramNameString = null; 438 String paramValueString = null; 439 String paramTypeString = null; 440 String commentString = null; 441 442 while (param != null) { 443 if (param.getNodeName().equals("env-entry-name")){ 444 paramValue = param.getFirstChild(); 445 try { 446 paramNameString = paramValue.getNodeValue(); 447 } 448 catch (Exception ex ){ 449 throw new KeywordValueException("Error in parsing <env-entry-name> tag."); 450 } 451 } 452 else { 453 if (param.getNodeName().equals("env-entry-value")){ 454 paramValue = param.getFirstChild(); 455 if (paramValue == null){ 456 paramValueString = new String (""); 457 param.appendChild(doc.createTextNode(paramValueString)); 458 paramValue = param.getFirstChild(); 459 } 460 461 else { 462 try { 463 paramValueString = paramValue.getNodeValue(); 464 } 465 catch (Exception ex) { 466 throw new KeywordValueException( 467 "Error in parsing <env-entry-value> tag."); 468 } 469 } 470 } 471 else{ 472 if (param.getNodeName().equals("env-entry-type")){ 473 paramValue = param.getFirstChild(); 474 if (paramValue != null) { 475 paramTypeString = paramValue.getNodeValue(); 476 } 477 } 482 } 483 } 484 485 param = param.getNextSibling(); 486 if (deleteNode != null){ 487 envParam.removeChild((Node )(deleteNode)); 488 deleteNode = null; 489 } 490 } if ((paramNameString == null) || (paramValueString == null) || (paramTypeString == null)) { 492 throw new KeywordValueException( 493 "Error in parsing configuration parameters."); 494 } 495 commentString = ""; 497 paramNameString = paramNameString.trim(); 498 499 int len = paramNameString.length(); 500 if ((len>2) && (paramNameString.substring(len-2).equals("[]"))){ 501 paramNameString = paramNameString.substring(0, len-2); 502 StringTokenizer tok = new StringTokenizer (paramValueString, 503 new String (",")); 504 String [] stringArray = new String [tok.countTokens()]; 505 int i = 0; 506 while (tok.hasMoreTokens()) { 507 stringArray[i] = tok.nextToken().trim(); 508 i++; 509 } 510 addEntry(JNDIAdapter.makeConfigString(paramNameString), stringArray, commentString); 511 if (jndiAdapt != null) { 512 String jndiName = JNDIAdapter.makeContextString(paramNameString); 513 try { 514 jndiAdapt.set(jndiName+"[]", paramValueString); 515 } 516 catch(Exception ex){} 517 } 518 } 519 else { 520 addEntry(JNDIAdapter.makeConfigString(paramNameString), paramValueString, commentString); 521 if (jndiAdapt != null) { 522 String jndiName = JNDIAdapter.makeContextString(paramNameString); 523 try { 524 jndiAdapt.set(paramNameString, paramValueString); 525 } 526 catch(Exception ex){} 527 528 } 529 } 530 } envParam = envParam.getNextSibling(); 532 } } } 535 536 537 542 public void write(OutputStream outputStream) { 543 try { 544 547 this.updateEnvEntryTagInTree(); 549 } 550 catch (Exception ex){ 551 System.out.println("Error in writting file."); 552 } 554 try { 555 FileOutputStream os = new FileOutputStream (file); 556 OutputFormat of = new OutputFormat(); 557 of.setIndenting(true); 558 of.setIndent(2); 559 of.setLineWidth(79); 562 if (!version.equals("")) { 563 of.setVersion(version); 564 } 565 if (!encoding.equals("")) { 566 of.setEncoding(encoding); 567 } 568 XMLSerializer out = new XMLSerializer(os, of); 569 out.serialize(doc); 570 os.close(); 571 } 572 catch (Exception ex){ 573 System.out.println("Error in writing file."); 574 } 576 } 577 578 583 735 736 737 741 private void updateEnvEntryTagInTree() throws KeywordValueException { 742 Node web = xmlDoc; 744 Node envEntryParam = null; 745 Node placeToAdd = null; 746 Node param = null; 747 Node paramValue = null; 748 Vector allParams = (Vector )order.clone(); 749 Node deleteNode = null; 750 if ((web != null) && (web.getNodeName().equals("web-app"))) { 751 envEntryParam = web.getFirstChild(); 752 while (envEntryParam != null) { 753 if (envEntryParam.getNodeName().equals("env-entry")){ 754 param = envEntryParam.getFirstChild(); 756 String paramNameString = null; 757 String paramType = null; 758 String commentString = null; 759 Node paramValueNode = null; 760 Node paramNodeForNullValue = null; 761 Node commentNode = null; 762 Node descriptionNode = null; 763 String arrayIndicator = ""; 764 int paramLength =0; 765 while (param != null) { 766 if (param.getNodeName().equals("env-entry-name")){ 767 paramValue = param.getFirstChild(); 768 paramNameString = paramValue.getNodeValue(); 769 paramLength = paramNameString.length(); 770 if ((paramLength>2)&&(paramNameString.substring(paramLength-2).equals("[]"))){ 771 paramNameString = paramNameString.substring(0, paramLength-2); 772 arrayIndicator = "[]"; 773 } 774 } 775 else { 776 if (param.getNodeName().equals("env-entry-value")){ 777 paramNodeForNullValue = param; 778 paramValue = param.getFirstChild(); 779 paramValueNode = paramValue; 780 } 781 else{ 782 if (param.getNodeName().equals("env-entry-type")){ 783 paramValue = param.getFirstChild(); 784 paramType = paramValue.getNodeValue(); 785 } 786 else { 787 if (param.getNodeName().equals("description")){ 788 descriptionNode = param; 790 paramValue = param.getFirstChild(); 791 commentNode = paramValue; 792 if (paramValue != null){ 793 commentString = paramValue.getNodeValue(); 794 } 795 else { 796 commentString = ""; 797 } 798 } 799 } 800 } 801 } 802 param = param.getNextSibling(); 803 } if (allParams.contains(JNDIAdapter.makeConfigString(paramNameString))){ 805 boolean doComment = true; 806 Object newValue; 807 String newValueString; 808 String newComment; 809 newValue = config.get(JNDIAdapter.makeConfigString(paramNameString)); 810 newComment = (String )comments.get(JNDIAdapter.makeConfigString(paramNameString)); 811 if (arrayIndicator.equals("")) { if ((!newValue.getClass().isArray()) && (paramType.trim().equals("java.lang.String"))) { 813 if (jndiParameterNames.containsKey(JNDIAdapter.makeConfigString(paramNameString))){ 819 newValueString = "jndi:" + (String )jndiParameterNames.get(JNDIAdapter.makeConfigString(paramNameString)); 820 } 822 else { 823 newValueString = newValue.toString(); 824 } 825 826 if (paramValueNode == null) { 827 paramNodeForNullValue.appendChild(doc.createTextNode(newValueString)); 828 } 829 else 830 paramValueNode.setNodeValue(newValueString); 831 allParams.remove(jndiAdapt.makeConfigString(paramNameString)); 832 } 833 else { deleteNode = envEntryParam; 835 doComment = false; 836 } 837 } 838 else { if (!newValue.getClass().isArray()){ deleteNode = envEntryParam; 841 doComment = false; 842 } 843 else { 844 if (paramType.trim().equals("java.lang.String")) { String [] s = (String []) newValue; 846 int len = s.length; 847 if (len > 0) { 848 newValueString = s[0].trim(); 849 if (s.length > 1){ 850 for (int i = 1; i < s.length; i++) { 851 newValueString = newValueString.concat(", "); 852 newValueString = newValueString.concat(s[i].trim()); 853 } 854 } 855 } 856 else 857 newValueString = new String (""); 858 paramValueNode.setNodeValue(newValueString); 859 allParams.remove(JNDIAdapter.makeConfigString(paramNameString)); 860 } 861 else { 862 deleteNode = envEntryParam; 863 doComment = false; 864 } 865 } 866 } 867 if (doComment) { 869 if (commentNode != null) { 870 if (!newComment.equals("")) 871 commentNode.setNodeValue(newComment); 872 else 873 envEntryParam.removeChild(descriptionNode); 874 } 875 else { 876 if ( (newComment != null) && (!newComment.equals(""))) { 877 if (descriptionNode != null) { 878 descriptionNode.appendChild(doc.createTextNode( (String ) 879 newComment)); 880 } 881 else { 882 descriptionNode = doc.createElement("description"); 883 descriptionNode.appendChild(doc.createTextNode( ( (String ) 884 newComment))); 885 envEntryParam.appendChild(descriptionNode); 886 } 887 } 888 } 889 } 890 } 892 else { 893 deleteNode = envEntryParam; 894 } 895 896 } envEntryParam = envEntryParam.getNextSibling(); 898 if (deleteNode != null){ 899 web.removeChild((Node )deleteNode); 900 deleteNode = null; 901 } 902 } } 905 Node newNode; 911 for (int i=0; i<allParams.size(); i++) { 912 newNode = createNewEnvEntry((String )allParams.elementAt(i)); 917 web.insertBefore(newNode, placeToAdd); 919 } 920 } 922 923 930 977 978 984 private Node createNewEnvEntry(String key) throws KeywordValueException { 985 String contextKeyForm = JNDIAdapter.makeContextString(key); 987 Node newEnvEntry = null; 988 Node newNode = null; 989 Object value = null; 990 String valStr = null; 991 String [] strings = null; 992 try { 993 newEnvEntry = doc.createElement("env-entry"); 994 995 if (!comments.get(key).equals("")) { 996 newNode = doc.createElement("description"); 997 newNode.appendChild(doc.createTextNode((String )comments.get(key))); 998 newEnvEntry.appendChild(newNode); 999 } 1000 1001 newNode = doc.createElement("env-entry-name"); 1002 value = config.get(key); 1003 if (value.getClass().isArray()) { 1004 valStr = contextKeyForm.concat("[]"); 1006 } 1007 else { 1008 valStr = contextKeyForm; 1010 } 1011 newNode.appendChild(doc.createTextNode(valStr)); 1012 newEnvEntry.appendChild(newNode); 1013 1014 valStr = null; 1015 newNode = doc.createElement("env-entry-value"); 1016 if (value.getClass().isArray()) { 1017 strings = (String [])value; 1018 valStr = strings[0]; 1019 int i = 1; 1020 while (i < strings.length){ 1021 valStr = valStr.concat(", "); 1022 valStr = valStr.concat(strings[i]); 1023 i++; 1024 } 1025 newNode.appendChild(doc.createTextNode(valStr)); 1026 } 1027 else { 1028 newNode.appendChild(doc.createTextNode(value.toString())); 1029 } 1030 newEnvEntry.appendChild(newNode); 1031 1032 newNode = doc.createElement("env-entry-type"); 1033 newNode.appendChild(doc.createTextNode("java.lang.String")); 1034 newEnvEntry.appendChild(newNode); 1035 } 1036 catch (Exception ex){ 1037 System.out.println("error in createNewEnvEntry method"); 1038 } 1039 return newEnvEntry; 1040 } 1041 1042 1043 1051 1089 1090 1091 1097 1098 private Node findPlaceInTree () throws KeywordValueException { 1099 Node web = xmlDoc; 1101 Node child = null; 1102 1103 if ((web != null) && (web.getNodeName().equals("web-app"))) { 1104 child = web.getFirstChild(); 1105 while ( (child != null) && !(child.getNodeName().equals("ejb-ref") || child.getNodeName().equals("ejb-local-ref"))) { 1106 child = child.getNextSibling(); 1107 } if (child != null) { 1109 Node prev = child.getPreviousSibling(); 1110 while ( (prev != null) && (prev.getNodeType() != Node.ELEMENT_NODE)) { 1111 child = prev; 1112 prev = prev.getPreviousSibling(); 1113 } 1114 } 1115 } 1116 return child; 1117 } 1118 1119 public class PublicIdResolver implements EntityResolver { 1120 public PublicIdResolver () { 1121 super(); 1122 } 1123 1124 public InputSource resolveEntity(String publicId, String systemId) { 1125 String Resource23 = "/org/enhydra/util/dtd/webXml2_3.dtd"; 1126 String Resource24 = "/org/enhydra/util/dtd/webXml2_4.dtd"; 1127 try { 1128 if (publicId != null) { 1129 if (publicId.equals("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN")) { 1131 return new InputSource (this.getClass().getResource(Resource23).toString()); 1132 } 1133 else { 1134 if (publicId.equals("-//Sun Microsystems, Inc.//DTD Web Application 2.4//EN")) { 1135 return new InputSource (this.getClass().getResource(Resource24).toString()); 1136 } 1137 else { 1138 return new InputSource (this.getClass().getResource(Resource23).toString()); 1139 } 1140 1141 } 1142 } 1143 } 1144 catch (Exception e){ 1145 System.out.println("Resolver Entity Error."); 1146 } 1148 return null; 1150 } 1151 } 1152 1153 1154} | Popular Tags |