View Javadoc

1   package org.apache.torque.generator.configuration;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.xml.sax.Attributes;
24  import org.xml.sax.SAXException;
25  
26  
27  /**
28   * Helper methods for SAX handlers.
29   */
30  public final class SaxHelper
31  {
32      /**
33       * private constructor for utility class.
34       */
35      private SaxHelper()
36      {
37      }
38  
39      /**
40       * Retrieves an attribute as boolean value.
41       *
42       * @param attributeName the name of the attribute to retrieve, not null.
43       * @param attributes The attributes of the current element.
44       * @param elementDescription the description of the parsed element,
45       *        for producing a user-readable error message. E.g
46       *        "the optionAction ${nameOfTheAction}"
47       *
48       * @return the value of the attribute, or null if the attribute is not set.
49       *
50       * @throws SAXException if the attribute contains content
51       *         other than "true", "1" , "false" or "0".
52       */
53      public static Boolean getBooleanAttribute(
54              String attributeName,
55              Attributes attributes,
56              String elementDescription)
57          throws SAXException
58      {
59          String attributeAsString = attributes.getValue(attributeName);
60          if (attributeAsString == null)
61          {
62              return null;
63          }
64          if ("false".equals(attributeAsString)
65                  || "0".equals(attributeAsString))
66          {
67              return false;
68          }
69          else if ("true".equals(attributeAsString)
70                  || "1".equals(attributeAsString))
71          {
72              return true;
73          }
74          else
75          {
76              throw new SAXException("The attribute "
77                      + attributeName
78                      + "of "
79                      + elementDescription
80                      + " must either be false, 0, true or 1");
81          }
82      }
83  
84      /**
85       * Retrieves the unqualified part of an XML element name name,
86       * regardless whether namespace processing is switched on or off.
87       *
88       * @param localName The local name (without prefix), or the
89       *        empty string if namespace processing is not being
90       *        performed.
91       * @param qName The qualified name (with prefix), or the
92       *        empty string if qualified names are not available.
93       *
94       * @return the unqualified part of the name.
95       */
96      public static String getUnqualifiedName(String localName, String qName)
97      {
98          if (!StringUtils.isEmpty(localName))
99          {
100             return localName;
101         }
102         return qName;
103     }
104 }