File: ParserUtil.java

package info (click to toggle)
libxml-java 1.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 544 kB
  • sloc: java: 4,760; xml: 1,011; makefile: 10
file content (311 lines) | stat: -rw-r--r-- 8,521 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2006 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors.  All rights reserved.
 */

package org.pentaho.reporting.libraries.xmlns.common;

import org.pentaho.reporting.libraries.xmlns.parser.ParseException;
import org.pentaho.reporting.libraries.xmlns.LibXmlBoot;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Basic helper functions to ease up the process of parsing.
 *
 * @author Thomas Morgner
 */
public class ParserUtil
{
  private static final Log logger = LogFactory.getLog(ParserUtil.class);
  private static boolean strictParsing;

  static
  {
    strictParsing = "true".equals(LibXmlBoot.getInstance().getGlobalConfig().getConfigProperty
        ("org.pentaho.reporting.libraries.xmlns.StrictParseMode"));
  }


  /**
   * Private constructors prevent initializations of utility classes.
   */
  private ParserUtil()
  {
  }

  /**
   * Parses the string <code>text</code> into an int. If text is null or does
   * not contain a parsable value, the message given in <code>message</code>
   * is used to throw a SAXException.
   *
   * @param text    the text to parse.
   * @param message the error message if parsing fails.
   * @param locator the SAX locator to print meaningfull error messages.
   * @return the int value.
   * @throws SAXException if there is a problem with the parsing.
   */
  public static int parseInt(final String text,
                             final String message,
                             final Locator locator)
      throws SAXException
  {
    if (text == null)
    {
      throw new SAXException(message);
    }

    try
    {
      return Integer.parseInt(text);
    }
    catch (NumberFormatException nfe)
    {
      throw new ParseException("NumberFormatError: " + message, locator);
    }
  }

  /**
   * Parses the string <code>text</code> into an int. If text is null or does
   * not contain a parsable value, the message given in <code>message</code>
   * is used to throw a SAXException.
   *
   * @param text    the text to parse.
   * @param message the error message if parsing fails.
   * @return the int value.
   * @throws SAXException if there is a problem with the parsing.
   */
  public static int parseInt(final String text, final String message)
      throws SAXException
  {
    if (text == null)
    {
      throw new SAXException(message);
    }

    try
    {
      return Integer.parseInt(text);
    }
    catch (NumberFormatException nfe)
    {
      throw new SAXException("NumberFormatError: " + message);
    }
  }

  /**
   * Parses an integer.
   *
   * @param text       the text to parse.
   * @param defaultVal the default value.
   * @return the integer.
   */
  public static int parseInt(final String text, final int defaultVal)
  {
    if (text == null)
    {
      return defaultVal;
    }

    try
    {
      return Integer.parseInt(text);
    }
    catch (NumberFormatException nfe)
    {
      return defaultVal;
    }
  }

  /**
   * Parses the string <code>text</code> into an float. If text is null or
   * does not contain a parsable value, the message given in
   * <code>message</code> is used to throw a SAXException.
   *
   * @param text    the text to parse.
   * @param message the error message if parsing fails.
   * @param locator the SAX locator to print meaningfull error messages.
   * @return the float value.
   * @throws ParseException if the text is no valid float number.
   */
  public static float parseFloat(final String text,
                                 final String message,
                                 final Locator locator)
      throws ParseException
  {
    if (text == null)
    {
      throw new ParseException(message, locator);
    }
    try
    {
      return Float.parseFloat(text);
    }
    catch (NumberFormatException nfe)
    {
      throw new ParseException("NumberFormatError: " + message, locator);
    }
  }

  /**
   * Parses the string <code>text</code> into an float. If text is null or
   * does not contain a parsable value, the message given in
   * <code>message</code> is used to throw a SAXException.
   *
   * @param text    the text to parse.
   * @param message the error message if parsing fails.
   * @return the float value.
   * @throws SAXException if there is a problem with the parsing.
   */
  public static float parseFloat(final String text, final String message)
      throws SAXException
  {
    if (text == null)
    {
      throw new SAXException(message);
    }
    try
    {
      return Float.parseFloat(text);
    }
    catch (NumberFormatException nfe)
    {
      throw new SAXException("NumberFormatError: " + message);
    }
  }

  /**
   * Parses the string <code>text</code> into an float. If text is null or
   * does not contain a parsable value, the message given in
   * <code>message</code> is used to throw a SAXException.
   *
   * @param text       the text to parse.
   * @param defaultVal the defaultValue returned if parsing fails.
   * @return the float value.
   */
  public static float parseFloat(final String text, final float defaultVal)
  {
    if (text == null)
    {
      return defaultVal;
    }
    try
    {
      return Float.parseFloat(text);
    }
    catch (NumberFormatException nfe)
    {
      return defaultVal;
    }
  }

  /**
   * Parses a boolean. If the string <code>text</code> contains the value of
   * "true", the true value is returned, else false is returned.
   *
   * @param text       the text to parse.
   * @param defaultVal the default value.
   * @return a boolean.
   */
  public static boolean parseBoolean(final String text,
                                     final boolean defaultVal)
  {
    if (text == null)
    {
      return defaultVal;
    }
    if (strictParsing)
    {
      return "true".equals(text);
    }
    else
    {
      if (text.equals("true"))
      {
        return true;
      }
      else if (text.equals("false"))
      {
        return false;
      }

      logger.warn("Invalid value encountered: Expected 'true' or 'false', but got '" + text + "'");
      return "true".equalsIgnoreCase(text);
    }
  }


  /**
   * Translates an boolean string ("true" or "false") into the corresponding Boolean
   * object.
   *
   * @param value   the string that represents the boolean.
   * @param locator the SAX locator to print meaningfull error messages.
   * @return Boolean.TRUE or Boolean.FALSE
   * @throws ParseException if an parse error occured or the string is not
   *                        'true' or 'false'.
   */
  public static Boolean parseBoolean(final String value, final Locator locator)
      throws ParseException
  {
    if (value == null)
    {
      return null;
    }
    if ("true".equals(value))
    {
      return Boolean.TRUE;
    }
    else if ("false".equals(value))
    {
      return Boolean.FALSE;
    }
    if (strictParsing)
    {
      throw new ParseException("Failed to parse: Expected 'true' or 'false'", locator);
    }

    if (locator == null)
    {
      logger.warn("Invalid value encountered for boolean attribute.");
    }
    else
    {
      logger.warn("Invalid value encountered for boolean attribute. [Line: " +
          locator.getLineNumber() + " Column: " + locator.getColumnNumber() + "]");
    }
    return Boolean.FALSE;
  }

  /**
   * Parses a string. If the <code>text</code> is null, defaultval is
   * returned.
   *
   * @param text       the text to parse.
   * @param defaultVal the default value.
   * @return a string.
   */
  public static String parseString(final String text, final String defaultVal)
  {
    if (text == null)
    {
      return defaultVal;
    }
    return text;
  }

}