1 22 package org.jboss.metadata; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.NodeList ; 29 import org.w3c.dom.Node ; 30 31 import org.jboss.deployment.DeploymentException; 32 import org.jboss.logging.Logger; 33 import org.jboss.util.StringPropertyReplacer; 34 35 41 public abstract class MetaData 42 implements Cloneable , XmlLoadable 43 { 44 46 protected static Logger log = Logger.getLogger(MetaData.class); 47 48 50 public static final byte TX_NOT_SUPPORTED = 0; 51 public static final byte TX_REQUIRED = 1; 52 public static final byte TX_SUPPORTS = 2; 53 public static final byte TX_REQUIRES_NEW = 3; 54 public static final byte TX_MANDATORY = 4; 55 public static final byte TX_NEVER = 5; 56 public static final byte TX_UNKNOWN = 6; 57 58 60 62 70 public static Iterator getChildrenByTagName(Element element, 71 String tagName) 72 { 73 if (element == null) return null; 74 77 NodeList children = element.getChildNodes(); 78 ArrayList goodChildren = new ArrayList (); 79 for (int i=0; i<children.getLength(); i++) { 80 Node currentChild = children.item(i); 81 if (currentChild.getNodeType() == Node.ELEMENT_NODE && 82 ((Element )currentChild).getTagName().equals(tagName)) { 83 goodChildren.add(currentChild); 84 } 85 } 86 return goodChildren.iterator(); 87 } 88 89 100 public static Element getUniqueChild(Element element, String tagName) 101 throws DeploymentException 102 { 103 Iterator goodChildren = getChildrenByTagName(element, tagName); 104 105 if (goodChildren != null && goodChildren.hasNext()) { 106 Element child = (Element )goodChildren.next(); 107 if (goodChildren.hasNext()) { 108 throw new DeploymentException 109 ("expected only one " + tagName + " tag"); 110 } 111 return child; 112 } else { 113 throw new DeploymentException 114 ("expected one " + tagName + " tag"); 115 } 116 } 117 118 127 public static Element getOptionalChild(Element element, String tagName) 128 throws DeploymentException 129 { 130 return getOptionalChild(element, tagName, null); 131 } 132 133 144 public static Element getOptionalChild(Element element, 145 String tagName, 146 Element defaultElement) 147 throws DeploymentException 148 { 149 Iterator goodChildren = getChildrenByTagName(element, tagName); 150 151 if (goodChildren != null && goodChildren.hasNext()) { 152 Element child = (Element )goodChildren.next(); 153 if (goodChildren.hasNext()) { 154 throw new DeploymentException 155 ("expected only one " + tagName + " tag"); 156 } 157 return child; 158 } else { 159 return defaultElement; 160 } 161 } 162 163 170 public static String getElementAttribute(final Element element, final String attrName) 171 { 172 return getElementAttribute(element, attrName, true); 173 } 174 175 183 public static String getElementAttribute(final Element element, final String attrName, boolean replace) 184 { 185 if (element == null) 186 return null; 187 188 if (attrName == null || element.hasAttribute(attrName) == false) 189 return null; 190 191 String result = element.getAttribute(attrName); 192 if (replace) 193 return StringPropertyReplacer.replaceProperties(result.trim()); 194 else 195 return result.trim(); 196 } 197 198 204 public static String getElementContent(final Element element) 205 { 206 return getElementContent(element, null); 207 } 208 209 216 public static String getElementContent(Element element, String defaultStr) 217 { 218 return getElementContent(element, defaultStr, true); 219 } 220 221 229 public static String getElementContent(Element element, String defaultStr, boolean replace) 230 { 231 if (element == null) 232 return defaultStr; 233 234 NodeList children = element.getChildNodes(); 235 String result = ""; 236 for (int i = 0; i < children.getLength(); i++) 237 { 238 if (children.item(i).getNodeType() == Node.TEXT_NODE || 239 children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) 240 { 241 result += children.item(i).getNodeValue(); 242 } 243 else if( children.item(i).getNodeType() == Node.COMMENT_NODE ) 244 { 245 } 247 else 248 { 249 result += children.item(i).getFirstChild(); 250 } 251 } 252 if (replace) 253 return StringPropertyReplacer.replaceProperties(result.trim()); 254 else 255 return result.trim(); 256 } 257 258 public static String getFirstElementContent(Element element, String defaultStr) 259 { 260 return getFirstElementContent(element, defaultStr, true); 261 } 262 263 public static String getFirstElementContent(Element element, String defaultStr, boolean replace) 264 { 265 if (element == null) 266 return defaultStr; 267 268 NodeList children = element.getChildNodes(); 269 String result = ""; 270 for (int i = 0; i < children.getLength(); i++) 271 { 272 if (children.item(i).getNodeType() == Node.TEXT_NODE || 273 children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) 274 { 275 String val = children.item(i).getNodeValue(); 276 result += val; 277 } 278 else if( children.item(i).getNodeType() == Node.COMMENT_NODE ) 279 { 280 } 282 288 } 289 if (replace) 290 return StringPropertyReplacer.replaceProperties(result.trim()); 291 else 292 return result.trim(); 293 } 294 295 302 public static String getUniqueChildContent(Element element, 303 String tagName) 304 throws DeploymentException 305 { 306 return getElementContent(getUniqueChild(element, tagName)); 307 } 308 309 316 public static String getOptionalChildContent(Element element, 317 String tagName) 318 throws DeploymentException 319 { 320 return getElementContent(getOptionalChild(element, tagName)); 321 } 322 323 330 public static String getOptionalChildContent(Element element, String tagName, String defaultValue) 331 throws DeploymentException 332 { 333 return getElementContent(getOptionalChild(element, tagName), defaultValue); 334 } 335 336 public static boolean getOptionalChildBooleanContent(Element element, String name) 337 throws DeploymentException 338 { 339 Element child = getOptionalChild(element, name); 340 if(child != null) 341 { 342 String value = getElementContent(child).toLowerCase(); 343 return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); 344 } 345 346 return false; 347 } 348 349 public static boolean getOptionalChildBooleanContent(Element element, 350 String name, boolean defaultValue) 351 throws DeploymentException 352 { 353 Element child = getOptionalChild(element, name); 354 boolean flag = defaultValue; 355 if(child != null) 356 { 357 String value = getElementContent(child).toLowerCase(); 358 flag = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); 359 } 360 361 return flag; 362 } 363 364 366 368 370 public Object clone() 371 { 372 Object clone = null; 373 try 374 { 375 clone = super.clone(); 376 } 377 catch(CloneNotSupportedException ignore) 378 { 379 } 380 return clone; 381 } 382 383 390 public void importXml(Element element) throws DeploymentException { 391 String rootTag = element.getOwnerDocument(). 392 getDocumentElement().getTagName(); 393 394 if (rootTag.equals("jboss")) { 395 importJbossXml(element); 397 } else if (rootTag.equals("ejb-jar")) { 398 importEjbJarXml(element); 400 } else { 401 throw new DeploymentException("Unrecognized root tag : "+ rootTag); 402 } 403 } 404 405 412 public void importEjbJarXml(Element element) throws DeploymentException { 413 } 415 416 423 public void importJbossXml(Element element) throws DeploymentException { 424 } 426 427 429 431 436 protected boolean jdk13Enabled() { 437 String javaVersion = System.getProperty("java.vm.version"); 439 return javaVersion.compareTo("1.3") >= 0; 440 } 441 442 444 } 446 449 | Popular Tags |