1 18 19 package org.apache.tools.ant.taskdefs.optional.ejb; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.Hashtable ; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.Task; 30 import org.xml.sax.AttributeList ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 34 42 public class DescriptorHandler extends org.xml.sax.HandlerBase { 43 private static final int DEFAULT_HASH_TABLE_SIZE = 10; 44 private static final int STATE_LOOKING_EJBJAR = 1; 45 private static final int STATE_IN_EJBJAR = 2; 46 private static final int STATE_IN_BEANS = 3; 47 private static final int STATE_IN_SESSION = 4; 48 private static final int STATE_IN_ENTITY = 5; 49 private static final int STATE_IN_MESSAGE = 6; 50 51 private Task owningTask; 52 53 private String publicId = null; 54 55 59 private static final String EJB_REF = "ejb-ref"; 60 private static final String EJB_LOCAL_REF = "ejb-local-ref"; 61 private static final String HOME_INTERFACE = "home"; 62 private static final String REMOTE_INTERFACE = "remote"; 63 private static final String LOCAL_HOME_INTERFACE = "local-home"; 64 private static final String LOCAL_INTERFACE = "local"; 65 private static final String BEAN_CLASS = "ejb-class"; 66 private static final String PK_CLASS = "prim-key-class"; 67 private static final String EJB_NAME = "ejb-name"; 68 private static final String EJB_JAR = "ejb-jar"; 69 private static final String ENTERPRISE_BEANS = "enterprise-beans"; 70 private static final String ENTITY_BEAN = "entity"; 71 private static final String SESSION_BEAN = "session"; 72 private static final String MESSAGE_BEAN = "message-driven"; 73 74 77 private int parseState = STATE_LOOKING_EJBJAR; 78 79 85 protected String currentElement = null; 86 87 90 protected String currentText = null; 91 92 97 protected Hashtable ejbFiles = null; 98 99 102 protected String ejbName = null; 103 104 private Hashtable fileDTDs = new Hashtable (); 105 106 private Hashtable resourceDTDs = new Hashtable (); 107 108 private boolean inEJBRef = false; 109 110 private Hashtable urlDTDs = new Hashtable (); 111 113 117 private File srcDir; 118 119 124 public DescriptorHandler(Task task, File srcDir) { 125 this.owningTask = task; 126 this.srcDir = srcDir; 127 } 128 129 136 public void registerDTD(String publicId, String location) { 137 if (location == null) { 138 return; 139 } 140 141 File fileDTD = new File (location); 142 if (!fileDTD.exists()) { 143 fileDTD = owningTask.getProject().resolveFile(location); 145 } 146 147 if (fileDTD.exists()) { 148 if (publicId != null) { 149 fileDTDs.put(publicId, fileDTD); 150 owningTask.log("Mapped publicId " + publicId + " to file " 151 + fileDTD, Project.MSG_VERBOSE); 152 } 153 return; 154 } 155 156 if (getClass().getResource(location) != null) { 157 if (publicId != null) { 158 resourceDTDs.put(publicId, location); 159 owningTask.log("Mapped publicId " + publicId + " to resource " 160 + location, Project.MSG_VERBOSE); 161 } 162 } 163 164 try { 165 if (publicId != null) { 166 URL urldtd = new URL (location); 167 urlDTDs.put(publicId, urldtd); 168 } 169 } catch (java.net.MalformedURLException e) { 170 } 172 173 } 174 175 185 public InputSource resolveEntity(String publicId, String systemId) 186 throws SAXException { 187 this.publicId = publicId; 188 189 File dtdFile = (File ) fileDTDs.get(publicId); 190 if (dtdFile != null) { 191 try { 192 owningTask.log("Resolved " + publicId + " to local file " 193 + dtdFile, Project.MSG_VERBOSE); 194 return new InputSource (new FileInputStream (dtdFile)); 195 } catch (FileNotFoundException ex) { 196 } 198 } 199 200 String dtdResourceName = (String ) resourceDTDs.get(publicId); 201 if (dtdResourceName != null) { 202 InputStream is = this.getClass().getResourceAsStream(dtdResourceName); 203 if (is != null) { 204 owningTask.log("Resolved " + publicId + " to local resource " 205 + dtdResourceName, Project.MSG_VERBOSE); 206 return new InputSource (is); 207 } 208 } 209 210 URL dtdUrl = (URL ) urlDTDs.get(publicId); 211 if (dtdUrl != null) { 212 try { 213 InputStream is = dtdUrl.openStream(); 214 owningTask.log("Resolved " + publicId + " to url " 215 + dtdUrl, Project.MSG_VERBOSE); 216 return new InputSource (is); 217 } catch (IOException ioe) { 218 } 220 } 221 222 owningTask.log("Could not resolve ( publicId: " + publicId 223 + ", systemId: " + systemId + ") to a local entity", Project.MSG_INFO); 224 225 return null; 226 } 227 228 232 public Hashtable getFiles() { 233 return (ejbFiles == null) ? new Hashtable () : ejbFiles; 234 } 235 236 240 public String getPublicId() { 241 return publicId; 242 } 243 244 248 public String getEjbName() { 249 return ejbName; 250 } 251 252 257 public void startDocument() throws SAXException { 258 this.ejbFiles = new Hashtable (DEFAULT_HASH_TABLE_SIZE, 1); 259 this.currentElement = null; 260 inEJBRef = false; 261 } 262 263 264 272 public void startElement(String name, AttributeList attrs) 273 throws SAXException { 274 this.currentElement = name; 275 currentText = ""; 276 if (name.equals(EJB_REF) || name.equals(EJB_LOCAL_REF)) { 277 inEJBRef = true; 278 } else if (parseState == STATE_LOOKING_EJBJAR && name.equals(EJB_JAR)) { 279 parseState = STATE_IN_EJBJAR; 280 } else if (parseState == STATE_IN_EJBJAR && name.equals(ENTERPRISE_BEANS)) { 281 parseState = STATE_IN_BEANS; 282 } else if (parseState == STATE_IN_BEANS && name.equals(SESSION_BEAN)) { 283 parseState = STATE_IN_SESSION; 284 } else if (parseState == STATE_IN_BEANS && name.equals(ENTITY_BEAN)) { 285 parseState = STATE_IN_ENTITY; 286 } else if (parseState == STATE_IN_BEANS && name.equals(MESSAGE_BEAN)) { 287 parseState = STATE_IN_MESSAGE; 288 } 289 } 290 291 292 302 public void endElement(String name) throws SAXException { 303 processElement(); 304 currentText = ""; 305 this.currentElement = ""; 306 if (name.equals(EJB_REF) || name.equals(EJB_LOCAL_REF)) { 307 inEJBRef = false; 308 } else if (parseState == STATE_IN_ENTITY && name.equals(ENTITY_BEAN)) { 309 parseState = STATE_IN_BEANS; 310 } else if (parseState == STATE_IN_SESSION && name.equals(SESSION_BEAN)) { 311 parseState = STATE_IN_BEANS; 312 } else if (parseState == STATE_IN_MESSAGE && name.equals(MESSAGE_BEAN)) { 313 parseState = STATE_IN_BEANS; 314 } else if (parseState == STATE_IN_BEANS && name.equals(ENTERPRISE_BEANS)) { 315 parseState = STATE_IN_EJBJAR; 316 } else if (parseState == STATE_IN_EJBJAR && name.equals(EJB_JAR)) { 317 parseState = STATE_LOOKING_EJBJAR; 318 } 319 } 320 321 337 public void characters(char[] ch, int start, int length) 338 throws SAXException { 339 340 currentText += new String (ch, start, length); 341 } 342 343 344 350 protected void processElement() { 351 if (inEJBRef 352 || (parseState != STATE_IN_ENTITY 353 && parseState != STATE_IN_SESSION 354 && parseState != STATE_IN_MESSAGE)) { 355 return; 356 } 357 358 if (currentElement.equals(HOME_INTERFACE) 359 || currentElement.equals(REMOTE_INTERFACE) 360 || currentElement.equals(LOCAL_INTERFACE) 361 || currentElement.equals(LOCAL_HOME_INTERFACE) 362 || currentElement.equals(BEAN_CLASS) 363 || currentElement.equals(PK_CLASS)) { 364 365 File classFile = null; 367 String className = currentText.trim(); 368 369 if (!className.startsWith("java.") 372 && !className.startsWith("javax.")) { 373 className = className.replace('.', File.separatorChar); 376 className += ".class"; 377 classFile = new File (srcDir, className); 378 ejbFiles.put(className, classFile); 379 } 380 } 381 382 if (currentElement.equals(EJB_NAME)) { 384 if (ejbName == null) { 385 ejbName = currentText.trim(); 386 } 387 } 388 } 389 } 390 | Popular Tags |