001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2005 Marc Eppelmann
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package com.izforge.izpack.util.xml;
022:
023: import net.n3.nanoxml.XMLElement;
024:
025: /**
026: * A Collection of convenient XML-Helper Methods and Constants
027: *
028: * @author marc.eppelmann@gmx.de
029: * @version $Revision: 1.1 $
030: */
031: public class XMLHelper {
032: //~ Static fields/initializers *********************************************************
033:
034: /** YES = "YES" */
035: public final static String YES = "YES";
036:
037: /** NO = "NO" */
038: public final static String NO = "NO";
039:
040: /** TRUE = "TRUE" */
041: public final static String TRUE = "TRUE";
042:
043: /** FALSE = "FALSE" */
044: public final static String FALSE = "FALSE";
045:
046: /** ON = "ON" */
047: public final static String ON = "ON";
048:
049: /** OFF = "OFF" */
050: public final static String OFF = "OFF";
051:
052: /** _1 = "1" */
053: public final static String _1 = "1";
054:
055: /** _0 = "0" */
056: public final static String _0 = "0";
057:
058: //~ Constructors ***********************************************************************
059:
060: /**
061: * Creates a new XMLHelper object.
062: */
063: public XMLHelper() {
064: super ();
065: }
066:
067: //~ Methods ****************************************************************************
068:
069: /**
070: * Determines if the named attribute in true. True is represented by any of the
071: * following strings and is not case sensitive. <br>
072: *
073: * <ul>
074: * <li>
075: * yes
076: * </li>
077: * <li>
078: * 1
079: * </li>
080: * <li>
081: * true
082: * </li>
083: * <li>
084: * on
085: * </li>
086: * </ul>
087: *
088: * <br> Every other string, including the empty string as well as the non-existence of
089: * the attribute will cuase <code>false</code> to be returned.
090: *
091: * @param element the <code>XMLElement</code> to search for the attribute.
092: * @param name the name of the attribute to test.
093: *
094: * @return <code>true</code> if the attribute value equals one of the pre-defined
095: * strings, <code>false</code> otherwise.
096: */
097:
098: /*--------------------------------------------------------------------------*/
099: public static boolean attributeIsTrue(XMLElement element,
100: String name) {
101: String value = element.getAttribute(name, "").toUpperCase();
102:
103: if (value.equals(YES)) {
104: return (true);
105: } else if (value.equals(TRUE)) {
106: return (true);
107: } else if (value.equals(ON)) {
108: return (true);
109: } else if (value.equals(_1)) {
110: return (true);
111: }
112:
113: return (false);
114: }
115:
116: /**
117: * The Opposit of AttributeIsTrue()
118: *
119: * @param element the element to inspect
120: * @param name the attribute to inspect
121: *
122: * @return returns true if name attribute of the given element contains "false"
123: */
124: public static boolean attributeIsFalse(XMLElement element,
125: String name) {
126: String value = element.getAttribute(name, "").toUpperCase();
127:
128: if (value.equals("NO")) {
129: return (true);
130: } else if (value.equals("FALSE")) {
131: return (true);
132: } else if (value.equals("OFF")) {
133: return (true);
134: } else if (value.equals("0")) {
135: return (true);
136: }
137:
138: return (false);
139: }
140: }
|