View Javadoc

1   /*
2    * File: ContentHandlerUtils.java
3    * Created: 15-dub-06 8:40:12 
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program 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
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  package net.sf.webmancer.util.xml.sax;
22  
23  import javax.xml.namespace.QName;
24  
25  import net.sf.webmancer.util.ContractChecker;
26  
27  import org.xml.sax.Attributes;
28  import org.xml.sax.Locator;
29  
30  /**
31   * This class provides various utility methods used while creating SAX parsers. SAX parsing is parsing of XML files
32   * using Simple API for XML.
33   * 
34   * @author Michal Burda
35   */
36  public class ContentHandlerUtils {
37  
38      /**
39       * Prevent instantiation
40       */
41      private ContentHandlerUtils() {
42          super();
43      }
44  
45      /**
46       * Search in <code>atts</code> for an attribute with name given by <code>attributeQName</code> and return its
47       * value or <code>null</code> if no such attribute is found. All parameters must not be <code>null</code>.
48       * 
49       * @param atts
50       *            The list of attributes being searched in
51       * @param attributeQName
52       *            The QName of the searched attribute
53       * @return The attribute value or <code>null</code> if no such attribute is found
54       */
55      public static String getAttributesValue(final Attributes atts, final QName attributeQName) {
56          ContractChecker.mustNotBeNull(atts, "atts");
57          ContractChecker.mustNotBeNull(attributeQName, "attributeQName");
58          return atts.getValue(attributeQName.getNamespaceURI(), attributeQName.getLocalPart());
59      }
60  
61      /**
62       * Throws {@link SAXSemanticException} if the given <code>condition</code> is not satisfied.
63       * 
64       * @param condition
65       *            The condition that has to be satisfied or the exception is thrown
66       * @param message
67       *            The error message
68       * @param locator
69       *            The locator of the error
70       * @throws SAXSemanticException
71       *             Thrown if the given condition is not satisfied
72       */
73      public static void expected(boolean condition, final String message, final Locator locator) throws SAXSemanticException {
74          if (!condition) {
75              throw new SAXSemanticException(message, locator);
76          }
77      }
78  
79      /**
80       * Search in <code>atts</code> for an attribute with name given by <code>attributeQName</code> and return its
81       * value as int. If no such attribute is found or its value is not parsable string, the {@link SAXSemanticException}
82       * is thrown.
83       * 
84       * @param atts
85       *            The list of attributes being searched in
86       * @param attributeQName
87       *            The QName of the searched attribute
88       * @param locator
89       *            The locator that would be used in exception creation
90       * @return The attribute value as int
91       * @throws SAXSemanticException
92       *             Thrown if no such attribute is found or its value is not parsable string
93       */
94      public static int getAttributesValueAsInt(final Attributes atts, final QName attributeQName, final Locator locator)
95              throws SAXSemanticException {
96          try {
97              return Integer.parseInt(getAttributesValue(atts, attributeQName));
98          } catch (NumberFormatException e) {
99              throw new SAXSemanticException("Integer value expected", locator, e);
100         }
101     }
102 
103     /**
104      * Search in <code>atts</code> for an attribute with name given by <code>attributeQName</code> and return its
105      * value as long. If no such attribute is found or its value is not parsable string, the
106      * {@link SAXSemanticException} is thrown.
107      * 
108      * @param atts
109      *            The list of attributes being searched in
110      * @param attributeQName
111      *            The QName of the searched attribute
112      * @param locator
113      *            The locator that would be used in exception creation
114      * @return The attribute value as long
115      * @throws SAXSemanticException
116      *             Thrown if no such attribute is found or its value is not parsable string
117      */
118     public static long getAttributesValueAsLong(final Attributes atts, final QName attributeQName, final Locator locator)
119             throws SAXSemanticException {
120         try {
121             return Long.parseLong(getAttributesValue(atts, attributeQName));
122         } catch (NumberFormatException e) {
123             throw new SAXSemanticException("Long value expected", locator, e);
124         }
125     }
126 
127     /**
128      * Search in <code>atts</code> for an attribute with name given by <code>attributeQName</code> and return its
129      * value as float. If no such attribute is found or its value is not parsable string, the
130      * {@link SAXSemanticException} is thrown.
131      * 
132      * @param atts
133      *            The list of attributes being searched in
134      * @param attributeQName
135      *            The QName of the searched attribute
136      * @param locator
137      *            The locator that would be used in exception creation
138      * @return The attribute value as float
139      * @throws SAXSemanticException
140      *             Thrown if no such attribute is found or its value is not parsable string
141      */
142     public static float getAttributesValueAsFloat(final Attributes atts, final QName attributeQName, final Locator locator)
143             throws SAXSemanticException {
144         try {
145             return Float.parseFloat(getAttributesValue(atts, attributeQName));
146         } catch (NumberFormatException e) {
147             throw new SAXSemanticException("Float value expected", locator, e);
148         }
149     }
150 
151     /**
152      * Search in <code>atts</code> for an attribute with name given by <code>attributeQName</code> and return its
153      * value as double. If no such attribute is found or its value is not parsable string, the
154      * {@link SAXSemanticException} is thrown.
155      * 
156      * @param atts
157      *            The list of attributes being searched in
158      * @param attributeQName
159      *            The QName of the searched attribute
160      * @param locator
161      *            The locator that would be used in exception creation
162      * @return The attribute value as double
163      * @throws SAXSemanticException
164      *             Thrown if no such attribute is found or its value is not parsable string
165      */
166     public static double getAttributesValueAsDouble(final Attributes atts, final QName attributeQName, final Locator locator)
167             throws SAXSemanticException {
168         try {
169             return Integer.parseInt(getAttributesValue(atts, attributeQName));
170         } catch (NumberFormatException e) {
171             throw new SAXSemanticException("Double value expected", locator, e);
172         }
173     }
174 }