1 package org.enhydra.spi.webxml.util; 2 3 import java.io.*; 4 import java.util.*; 5 import org.w3c.dom.*; 6 import org.xml.sax.EntityResolver ; 7 import javax.xml.parsers.*; 8 import javax.naming.*; 9 import org.xml.sax.InputSource ; 10 11 19 20 public class WebXmlUtil { 21 22 25 protected File file = null; 26 27 30 private Context elements = null; 31 32 35 private Document doc = null; 36 37 40 private Node xmlDoc = null; 41 42 45 private String version; 46 47 50 private String encoding; 51 52 55 public WebXmlUtil () { 56 version = ""; 57 encoding = ""; 58 } 59 60 67 public WebXmlUtil(File file) throws XmlParseException, IOException { 68 this.file = file; 69 version = ""; 70 encoding = ""; 71 this.readXmlDeclaration(); 73 String name = file.getName(); 74 EntityResolver er = new PublicIdResolver(); 77 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 78 factory.setValidating(false); 79 factory.setIgnoringElementContentWhitespace(false); 80 try { 81 DocumentBuilder builder = factory.newDocumentBuilder(); 82 builder.setEntityResolver(er); 83 doc = builder.parse(file); 84 } 85 catch (Exception ex) { 86 ex.printStackTrace(); 87 throw new XmlParseException ( 88 "Error in reading configuration parameters from file."); 89 } 90 NodeList nl = doc.getChildNodes(); 91 boolean found = false; 92 int i = 0; 93 while (i < nl.getLength() && !found) { 95 xmlDoc = nl.item(i); 96 if (xmlDoc.getNodeName().equalsIgnoreCase("web-app") && 97 xmlDoc.getNodeType() == Document.ELEMENT_NODE) 98 found = true; 99 i++; 100 } 101 } 102 103 110 private void readXmlDeclaration() throws IOException { 111 String xmlHeader = ""; 112 int length; 113 FileInputStream fi = new FileInputStream(file); 114 byte[] bChar = new byte[1]; 115 int ok = fi.read(bChar); 116 String tempString = ""; 117 if (ok != -1) { 118 String ch = new String (bChar); 119 if (ch.equalsIgnoreCase("<")) { 120 tempString = tempString + ch; 121 ok = fi.read(bChar); 122 while (ok != -1 && !new String (bChar).equalsIgnoreCase(">")) { 123 tempString = tempString + new String (bChar); 124 ok = fi.read(bChar); 125 } 126 if (ok != -1){ 127 tempString = tempString + new String (bChar); 128 if (tempString.length() >= 5) { 129 if (tempString.substring(0, 5).equalsIgnoreCase("<?xml")) { 130 xmlHeader = tempString; 131 } 132 } 133 } 134 } 135 } 136 if (xmlHeader != null){ 137 int index, startQuote, endQuote; 138 index = xmlHeader.indexOf("version"); 139 if (index != -1){ 140 startQuote = xmlHeader.indexOf("\"",(index+7)); 141 if (startQuote != -1) { 142 endQuote = xmlHeader.indexOf("\"", startQuote + 1); 143 if ((endQuote != -1) && (startQuote+1 < endQuote)) { 144 version = xmlHeader.substring(startQuote + 1, endQuote); 145 } 146 } 147 } 148 index = xmlHeader.indexOf("encoding"); 149 if (index != -1){ 150 startQuote = xmlHeader.indexOf("\"",(index+8)); 151 if (startQuote != -1) { 152 endQuote = xmlHeader.indexOf("\"", startQuote + 1); 153 if ((endQuote != -1) && (startQuote+1 < endQuote)) { 154 encoding = xmlHeader.substring(startQuote + 1, endQuote); 155 } 156 } 157 } 158 } 159 fi.close(); 160 } 161 162 171 172 public void parseTree(Context con) throws XmlParseException { 173 this.elements = con; 174 Node web = xmlDoc; 175 Node envParam = null; 176 Node param = null; 177 Node paramValue = null; 178 180 if ((web != null) && (web.getNodeName().equals("web-app"))) { 181 envParam = web.getFirstChild(); 182 while (envParam != null) { 183 if (envParam.getNodeName().equals("env-entry")){ 184 param = envParam.getFirstChild(); 185 String paramNameString = null; 186 String paramValueString = null; 187 String paramTypeString = null; 188 String commentString = null; 189 190 while (param != null) { 191 if (param.getNodeName().equals("env-entry-name")){ 192 paramValue = param.getFirstChild(); 193 try { 194 paramNameString = paramValue.getNodeValue(); 195 } 196 catch (Exception ex ){ 197 throw new XmlParseException("Error in parsing <env-entry-name> tag."); 198 } 199 } 200 else { 201 if (param.getNodeName().equals("env-entry-value")){ 202 paramValue = param.getFirstChild(); 203 if (paramValue == null){ 204 paramValueString = new String (""); 205 param.appendChild(doc.createTextNode(paramValueString)); 206 paramValue = param.getFirstChild(); 207 } 208 209 else { 210 try { 211 paramValueString = paramValue.getNodeValue(); 212 } 213 catch (Exception ex) { 214 throw new XmlParseException( 215 "Error in parsing <env-entry-value> tag."); 216 } 217 } 218 } 219 else{ 220 if (param.getNodeName().equals("env-entry-type")){ 221 paramValue = param.getFirstChild(); 222 if (paramValue != null) { 223 paramTypeString = paramValue.getNodeValue(); 224 } 225 } 230 } 231 } 232 233 param = param.getNextSibling(); 234 240 } if ((paramNameString == null) || (paramValueString == null) || (paramTypeString == null)) { 242 throw new XmlParseException( 243 "Error in parsing configuration parameters."); 244 } 245 commentString = ""; 247 paramNameString = paramNameString.trim(); 248 249 int len = paramNameString.length(); 250 if ((len>2) && (paramNameString.substring(len-2).equals("[]"))){ 251 StringTokenizer tok = new StringTokenizer(paramValueString, 253 new String (",")); 254 String [] stringArray = new String [tok.countTokens()]; 255 int i = 0; 256 while (tok.hasMoreTokens()) { 257 stringArray[i] = tok.nextToken().trim(); 258 i++; 259 } 260 try { 261 elements.rebind(paramNameString, stringArray); 262 } 263 catch (Exception e){ 264 throw new XmlParseException( 265 "Error in rebinding configuration array parameters."); 266 } 267 } 268 else { 269 try { 270 elements.rebind(paramNameString, paramValueString); 271 } 272 catch (Exception e){ 273 throw new XmlParseException( 274 "Error in rebinding configuration parameters."); 275 } 276 } 277 } envParam = envParam.getNextSibling(); 279 } } } 282 283 public class PublicIdResolver implements EntityResolver { 284 public PublicIdResolver () { 285 super(); 286 } 287 288 public InputSource resolveEntity(String publicId, String systemId) { 289 String Resource23 = "/org/enhydra/util/dtd/webXml2_3.dtd"; 290 String Resource24 = "/org/enhydra/util/dtd/webXml2_4.dtd"; 291 try { 292 if (publicId != null) { 293 if (publicId.equals("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN")) { 295 return new InputSource (this.getClass().getResource(Resource23).toString()); 296 } 297 else { 298 if (publicId.equals("-//Sun Microsystems, Inc.//DTD Web Application 2.4//EN")) { 299 return new InputSource (this.getClass().getResource(Resource24).toString()); 300 } 301 else { 302 return new InputSource (this.getClass().getResource(Resource23).toString()); 303 } 304 305 } 306 } 307 } 308 catch (Exception e){ 309 System.out.println("Resolver Entity Error."); 310 e.printStackTrace(); 311 } 312 return null; 314 } 315 } 316 317 } | Popular Tags |