KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > xml > XMLHelper


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2005 Marc Eppelmann
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21 package com.izforge.izpack.util.xml;
22
23 import net.n3.nanoxml.XMLElement;
24
25
26 /**
27  * A Collection of convenient XML-Helper Methods and Constants
28  *
29  * @author marc.eppelmann@gmx.de
30  * @version $Revision: 1.1 $
31  */

32 public class XMLHelper
33 {
34   //~ Static fields/initializers *********************************************************
35

36   /** YES = "YES" */
37   public final static String JavaDoc YES = "YES";
38
39   /** NO = "NO" */
40   public final static String JavaDoc NO = "NO";
41
42   /** TRUE = "TRUE" */
43   public final static String JavaDoc TRUE = "TRUE";
44
45   /** FALSE = "FALSE" */
46   public final static String JavaDoc FALSE = "FALSE";
47
48   /** ON = "ON" */
49   public final static String JavaDoc ON = "ON";
50
51   /** OFF = "OFF" */
52   public final static String JavaDoc OFF = "OFF";
53
54   /** _1 = "1" */
55   public final static String JavaDoc _1 = "1";
56
57   /** _0 = "0" */
58   public final static String JavaDoc _0 = "0";
59
60   //~ Constructors ***********************************************************************
61

62   /**
63    * Creates a new XMLHelper object.
64    */

65   public XMLHelper( )
66   {
67     super( );
68   }
69
70   //~ Methods ****************************************************************************
71

72   /**
73    * Determines if the named attribute in true. True is represented by any of the
74    * following strings and is not case sensitive. <br>
75    *
76    * <ul>
77    * <li>
78    * yes
79    * </li>
80    * <li>
81    * 1
82    * </li>
83    * <li>
84    * true
85    * </li>
86    * <li>
87    * on
88    * </li>
89    * </ul>
90    *
91    * <br> Every other string, including the empty string as well as the non-existence of
92    * the attribute will cuase <code>false</code> to be returned.
93    *
94    * @param element the <code>XMLElement</code> to search for the attribute.
95    * @param name the name of the attribute to test.
96    *
97    * @return <code>true</code> if the attribute value equals one of the pre-defined
98    * strings, <code>false</code> otherwise.
99    */

100
101   /*--------------------------------------------------------------------------*/
102   public static boolean attributeIsTrue( XMLElement element, String JavaDoc name )
103   {
104     String JavaDoc value = element.getAttribute( name, "" ).toUpperCase( );
105
106     if( value.equals( YES ) )
107     {
108       return ( true );
109     }
110     else if( value.equals( TRUE ) )
111     {
112       return ( true );
113     }
114     else if( value.equals( ON ) )
115     {
116       return ( true );
117     }
118     else if( value.equals( _1 ) )
119     {
120       return ( true );
121     }
122
123     return ( false );
124   }
125
126   /**
127    * The Opposit of AttributeIsTrue()
128    *
129    * @param element the element to inspect
130    * @param name the attribute to inspect
131    *
132    * @return returns true if name attribute of the given element contains &quot;false&quot;
133    */

134   public static boolean attributeIsFalse( XMLElement element, String JavaDoc name )
135   {
136     String JavaDoc value = element.getAttribute( name, "" ).toUpperCase( );
137
138     if( value.equals( "NO" ) )
139     {
140       return ( true );
141     }
142     else if( value.equals( "FALSE" ) )
143     {
144       return ( true );
145     }
146     else if( value.equals( "OFF" ) )
147     {
148       return ( true );
149     }
150     else if( value.equals( "0" ) )
151     {
152       return ( true );
153     }
154
155     return ( false );
156   }
157 }
158
Popular Tags