KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > MetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30
31 import org.jboss.deployment.DeploymentException;
32 import org.jboss.logging.Logger;
33 import org.jboss.util.StringPropertyReplacer;
34
35 /**
36  * An abstract base class for metadata containers.
37  *
38  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
39  * @version $Revision: 37459 $
40  */

41 public abstract class MetaData
42    implements Cloneable JavaDoc, XmlLoadable
43 {
44    // Constants -----------------------------------------------------
45

46    protected static Logger log = Logger.getLogger(MetaData.class);
47    
48    // These do not really belong here
49

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    // Attributes ----------------------------------------------------
59

60    // Static --------------------------------------------------------
61

62    /**
63     * Returns an iterator over the children of the given element with
64     * the given tag name.
65     *
66     * @param element The parent element
67     * @param tagName The name of the desired child
68     * @return An interator of children or null if element is null.
69     */

70    public static Iterator JavaDoc getChildrenByTagName(Element JavaDoc element,
71                                                String JavaDoc tagName)
72    {
73       if (element == null) return null;
74       // getElementsByTagName gives the corresponding elements in the whole
75
// descendance. We want only children
76

77       NodeList JavaDoc children = element.getChildNodes();
78       ArrayList JavaDoc goodChildren = new ArrayList JavaDoc();
79       for (int i=0; i<children.getLength(); i++) {
80          Node JavaDoc currentChild = children.item(i);
81          if (currentChild.getNodeType() == Node.ELEMENT_NODE &&
82              ((Element JavaDoc)currentChild).getTagName().equals(tagName)) {
83             goodChildren.add(currentChild);
84          }
85       }
86       return goodChildren.iterator();
87    }
88
89    /**
90     * Gets the child of the specified element having the specified unique
91     * name. If there are more than one children elements with the same name
92     * and exception is thrown.
93     *
94     * @param element The parent element
95     * @param tagName The name of the desired child
96     * @return The named child.
97     *
98     * @throws DeploymentException Child was not found or was not unique.
99     */

100    public static Element JavaDoc getUniqueChild(Element JavaDoc element, String JavaDoc tagName)
101       throws DeploymentException
102    {
103       Iterator JavaDoc goodChildren = getChildrenByTagName(element, tagName);
104
105       if (goodChildren != null && goodChildren.hasNext()) {
106          Element JavaDoc child = (Element JavaDoc)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    /**
119     * Gets the child of the specified element having the
120     * specified name. If the child with this name doesn't exist
121     * then null is returned instead.
122     *
123     * @param element the parent element
124     * @param tagName the name of the desired child
125     * @return either the named child or null
126     */

127    public static Element JavaDoc getOptionalChild(Element JavaDoc element, String JavaDoc tagName)
128       throws DeploymentException
129    {
130       return getOptionalChild(element, tagName, null);
131    }
132
133    /**
134     * Gets the child of the specified element having the
135     * specified name. If the child with this name doesn't exist
136     * then the supplied default element is returned instead.
137     *
138     * @param element the parent element
139     * @param tagName the name of the desired child
140     * @param defaultElement the element to return if the child
141     * doesn't exist
142     * @return either the named child or the supplied default
143     */

144    public static Element JavaDoc getOptionalChild(Element JavaDoc element,
145                                           String JavaDoc tagName,
146                                           Element JavaDoc defaultElement)
147       throws DeploymentException
148    {
149       Iterator JavaDoc goodChildren = getChildrenByTagName(element, tagName);
150
151       if (goodChildren != null && goodChildren.hasNext()) {
152          Element JavaDoc child = (Element JavaDoc)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    /**
164     * Get an attribute value of the given element.
165     *
166     * @param element The element to get the attribute value for.
167     * @param attrName The attribute name
168     * @return The attribute value or null.
169     */

170    public static String JavaDoc getElementAttribute(final Element JavaDoc element, final String JavaDoc attrName)
171    {
172       return getElementAttribute(element, attrName, true);
173    }
174
175    /**
176     * Get an attribute value of the given element.
177     *
178     * @param element The element to get the attribute value for.
179     * @param attrName The attribute name
180     * @param replace Whether to replace system properties
181     * @return The attribute value or null.
182     */

183    public static String JavaDoc getElementAttribute(final Element JavaDoc element, final String JavaDoc 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 JavaDoc result = element.getAttribute(attrName);
192       if (replace)
193          return StringPropertyReplacer.replaceProperties(result.trim());
194       else
195          return result.trim();
196    }
197
198    /**
199     * Get the content of the given element.
200     *
201     * @param element The element to get the content for.
202     * @return The content of the element or null.
203     */

204    public static String JavaDoc getElementContent(final Element JavaDoc element)
205    {
206       return getElementContent(element, null);
207    }
208
209    /**
210     * Get the content of the given element.
211     *
212     * @param element The element to get the content for.
213     * @param defaultStr The default to return when there is no content.
214     * @return The content of the element or the default.
215     */

216    public static String JavaDoc getElementContent(Element JavaDoc element, String JavaDoc defaultStr)
217    {
218       return getElementContent(element, defaultStr, true);
219    }
220
221    /**
222     * Get the content of the given element.
223     *
224     * @param element The element to get the content for.
225     * @param defaultStr The default to return when there is no content.
226     * @param replace Whether to replace system properties
227     * @return The content of the element or the default.
228     */

229    public static String JavaDoc getElementContent(Element JavaDoc element, String JavaDoc defaultStr, boolean replace)
230    {
231       if (element == null)
232          return defaultStr;
233
234       NodeList JavaDoc children = element.getChildNodes();
235       String JavaDoc 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             // Ignore comment nodes
246
}
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 JavaDoc getFirstElementContent(Element JavaDoc element, String JavaDoc defaultStr)
259    {
260       return getFirstElementContent(element, defaultStr, true);
261    }
262
263    public static String JavaDoc getFirstElementContent(Element JavaDoc element, String JavaDoc defaultStr, boolean replace)
264    {
265       if (element == null)
266          return defaultStr;
267
268       NodeList JavaDoc children = element.getChildNodes();
269       String JavaDoc 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 JavaDoc val = children.item(i).getNodeValue();
276             result += val;
277          }
278          else if( children.item(i).getNodeType() == Node.COMMENT_NODE )
279          {
280             // Ignore comment nodes
281
}
282          /* For some reason, the original version gets the text of the first child
283          else
284          {
285             result += children.item(i).getFirstChild();
286          }
287          */

288       }
289       if (replace)
290          return StringPropertyReplacer.replaceProperties(result.trim());
291       else
292          return result.trim();
293    }
294
295    /**
296     * Macro to get the content of a unique child element.
297     *
298     * @param element The parent element.
299     * @param tagName The name of the desired child.
300     * @return The element content or null.
301     */

302    public static String JavaDoc getUniqueChildContent(Element JavaDoc element,
303                                               String JavaDoc tagName)
304       throws DeploymentException
305    {
306       return getElementContent(getUniqueChild(element, tagName));
307    }
308
309    /**
310     * Macro to get the content of an optional child element.
311     *
312     * @param element The parent element.
313     * @param tagName The name of the desired child.
314     * @return The element content or null.
315     */

316    public static String JavaDoc getOptionalChildContent(Element JavaDoc element,
317                                                 String JavaDoc tagName)
318       throws DeploymentException
319    {
320       return getElementContent(getOptionalChild(element, tagName));
321    }
322
323    /**
324     * Macro to get the content of an optional child element with default value.
325     *
326     * @param element The parent element.
327     * @param tagName The name of the desired child.
328     * @return The element content or null.
329     */

330    public static String JavaDoc getOptionalChildContent(Element JavaDoc element, String JavaDoc tagName, String JavaDoc defaultValue)
331            throws DeploymentException
332    {
333       return getElementContent(getOptionalChild(element, tagName), defaultValue);
334    }
335
336    public static boolean getOptionalChildBooleanContent(Element JavaDoc element, String JavaDoc name)
337       throws DeploymentException
338    {
339       Element JavaDoc child = getOptionalChild(element, name);
340       if(child != null)
341       {
342          String JavaDoc 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 JavaDoc element,
350       String JavaDoc name, boolean defaultValue)
351       throws DeploymentException
352    {
353       Element JavaDoc child = getOptionalChild(element, name);
354       boolean flag = defaultValue;
355       if(child != null)
356       {
357          String JavaDoc value = getElementContent(child).toLowerCase();
358          flag = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
359       }
360
361       return flag;
362    }
363
364    // Constructors --------------------------------------------------
365

366    // Public --------------------------------------------------------
367

368    /** Create a field wise copy of the object.
369    */

370    public Object JavaDoc clone()
371    {
372       Object JavaDoc clone = null;
373       try
374       {
375          clone = super.clone();
376       }
377       catch(CloneNotSupportedException JavaDoc ignore)
378       {
379       }
380       return clone;
381    }
382
383    /**
384     * Imports either the jboss or ejb-jar from the given element.
385     *
386     * @param element The element to import.
387     *
388     * @throws DeploymentException Unrecognized root tag.
389     */

390    public void importXml(Element JavaDoc element) throws DeploymentException {
391       String JavaDoc rootTag = element.getOwnerDocument().
392          getDocumentElement().getTagName();
393
394       if (rootTag.equals("jboss")) {
395          // import jboss.xml
396
importJbossXml(element);
397       } else if (rootTag.equals("ejb-jar")) {
398          // import ejb-jar.xml
399
importEjbJarXml(element);
400       } else {
401          throw new DeploymentException("Unrecognized root tag : "+ rootTag);
402       }
403    }
404
405    /**
406     * Non-operation.
407     *
408     * @param element
409     *
410     * @throws DeploymentException
411     */

412    public void importEjbJarXml(Element JavaDoc element) throws DeploymentException {
413       // empty
414
}
415
416    /**
417     * Non-operation.
418     *
419     * @param element
420     *
421     * @throws DeploymentException
422     */

423    public void importJbossXml(Element JavaDoc element) throws DeploymentException {
424       // empty
425
}
426
427    // Package protected ---------------------------------------------
428

429    // Protected -----------------------------------------------------
430

431    /**
432     * Check if we are running in a JDK v1.3 virtual machine or better.
433     *
434     * @return True if the virtual machine is v1.3 or better.
435     */

436    protected boolean jdk13Enabled() {
437       // should use "java.version" ?
438
String JavaDoc javaVersion = System.getProperty("java.vm.version");
439       return javaVersion.compareTo("1.3") >= 0;
440    }
441
442    // Private -------------------------------------------------------
443

444    // Inner classes -------------------------------------------------
445
}
446 /*
447 vim:ts=3:sw=3:et
448 */

449
Popular Tags