package org.jboss.metadata;
import java.util.ArrayList;
import java.util.Iterator;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.jboss.deployment.DeploymentException;
import org.jboss.logging.Logger;
import org.jboss.util.StringPropertyReplacer;
public abstract class MetaData
implements Cloneable, XmlLoadable
{
protected static Logger log = Logger.getLogger(MetaData.class);
public static final byte TX_NOT_SUPPORTED = 0;
public static final byte TX_REQUIRED = 1;
public static final byte TX_SUPPORTS = 2;
public static final byte TX_REQUIRES_NEW = 3;
public static final byte TX_MANDATORY = 4;
public static final byte TX_NEVER = 5;
public static final byte TX_UNKNOWN = 6;
public static Iterator getChildrenByTagName(Element element,
String tagName)
{
if (element == null) return null;
NodeList children = element.getChildNodes();
ArrayList goodChildren = new ArrayList();
for (int i=0; i<children.getLength(); i++) {
Node currentChild = children.item(i);
if (currentChild.getNodeType() == Node.ELEMENT_NODE &&
((Element)currentChild).getTagName().equals(tagName)) {
goodChildren.add(currentChild);
}
}
return goodChildren.iterator();
}
public static Element getUniqueChild(Element element, String tagName)
throws DeploymentException
{
Iterator goodChildren = getChildrenByTagName(element, tagName);
if (goodChildren != null && goodChildren.hasNext()) {
Element child = (Element)goodChildren.next();
if (goodChildren.hasNext()) {
throw new DeploymentException
("expected only one " + tagName + " tag");
}
return child;
} else {
throw new DeploymentException
("expected one " + tagName + " tag");
}
}
public static Element getOptionalChild(Element element, String tagName)
throws DeploymentException
{
return getOptionalChild(element, tagName, null);
}
public static Element getOptionalChild(Element element,
String tagName,
Element defaultElement)
throws DeploymentException
{
Iterator goodChildren = getChildrenByTagName(element, tagName);
if (goodChildren != null && goodChildren.hasNext()) {
Element child = (Element)goodChildren.next();
if (goodChildren.hasNext()) {
throw new DeploymentException
("expected only one " + tagName + " tag");
}
return child;
} else {
return defaultElement;
}
}
public static String getElementAttribute(final Element element, final String attrName)
{
if (element == null)
return null;
if (attrName == null || element.hasAttribute(attrName) == false)
return null;
String result = element.getAttribute(attrName);
return StringPropertyReplacer.replaceProperties(result.trim());
}
public static String getElementContent(final Element element)
{
return getElementContent(element, null);
}
public static String getElementContent(Element element, String defaultStr)
{
if (element == null)
return defaultStr;
NodeList children = element.getChildNodes();
String result = "";
for (int i = 0; i < children.getLength(); i++)
{
if (children.item(i).getNodeType() == Node.TEXT_NODE ||
children.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
{
result += children.item(i).getNodeValue();
}
else if( children.item(i).getNodeType() == Node.COMMENT_NODE )
{
}
else
{
result += children.item(i).getFirstChild();
}
}
return StringPropertyReplacer.replaceProperties(result.trim());
}
public static String getFirstElementContent(Element element, String defaultStr)
{
if (element == null)
return defaultStr;
NodeList children = element.getChildNodes();
String result = "";
for (int i = 0; i < children.getLength(); i++)
{
if (children.item(i).getNodeType() == Node.TEXT_NODE ||
children.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
{
String val = children.item(i).getNodeValue();
result += val;
}
else if( children.item(i).getNodeType() == Node.COMMENT_NODE )
{
}
}
return StringPropertyReplacer.replaceProperties(result.trim());
}
public static String getUniqueChildContent(Element element,
String tagName)
throws DeploymentException
{
return getElementContent(getUniqueChild(element, tagName));
}
public static String getOptionalChildContent(Element element,
String tagName)
throws DeploymentException
{
return getElementContent(getOptionalChild(element, tagName));
}
public static String getOptionalChildContent(Element element, String tagName, String defaultValue)
throws DeploymentException
{
return getElementContent(getOptionalChild(element, tagName), defaultValue);
}
public static boolean getOptionalChildBooleanContent(Element element, String name)
throws DeploymentException
{
Element child = getOptionalChild(element, name);
if(child != null)
{
String value = getElementContent(child).toLowerCase();
return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
}
return false;
}
public static boolean getOptionalChildBooleanContent(Element element,
String name, boolean defaultValue)
throws DeploymentException
{
Element child = getOptionalChild(element, name);
boolean flag = defaultValue;
if(child != null)
{
String value = getElementContent(child).toLowerCase();
flag = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
}
return flag;
}
public Object clone()
{
Object clone = null;
try
{
clone = super.clone();
}
catch(CloneNotSupportedException ignore)
{
}
return clone;
}
public void importXml(Element element) throws DeploymentException {
String rootTag = element.getOwnerDocument().
getDocumentElement().getTagName();
if (rootTag.equals("jboss")) {
importJbossXml(element);
} else if (rootTag.equals("ejb-jar")) {
importEjbJarXml(element);
} else {
throw new DeploymentException("Unrecognized root tag : "+ rootTag);
}
}
public void importEjbJarXml(Element element) throws DeploymentException {
}
public void importJbossXml(Element element) throws DeploymentException {
}
protected boolean jdk13Enabled() {
String javaVersion = System.getProperty("java.vm.version");
return javaVersion.compareTo("1.3") >= 0;
}
}