1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.util;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import org.simantics.databoard.type.BooleanType;
18 import org.simantics.databoard.type.ByteType;
19 import org.simantics.databoard.type.Datatype;
20 import org.simantics.databoard.type.DoubleType;
21 import org.simantics.databoard.type.FloatType;
22 import org.simantics.databoard.type.IntegerType;
23 import org.simantics.databoard.type.LongType;
24 import org.simantics.databoard.type.StringType;
27 * @author Tuukka Lehtonen
29 public final class PrimitiveValueParser {
31 static final Pattern truePattern = Pattern.compile("\\s*1|[tT]([rR]([uU]([eE])?)?)?\\s*");
32 static final Pattern falsePattern = Pattern.compile("\\s*0|[fF]([aA]([lL]([sS]([eE])?)?)?)?\\s*");
33 static final String fpRegex =
34 "^\\s*[\\x00-\\x20]*" +
39 "\\p{Digit}+([\\.,]\\p{Digit}*)?|" +
42 ")([eE][+-]?\\p{Digit}+)?\\s*$";
43 static final Pattern p = Pattern.compile(fpRegex);
44 static final Pattern commaPat = Pattern.compile(",");
46 public interface IValidator {
47 String isValid(String value);
50 public static final IValidator NON_MODIFIABLE_VALIDATOR = new IValidator() {
52 public String isValid(String value) {
53 return "Value not modifiable";
57 public static abstract class Validator implements IValidator {
58 abstract void tryValidate(String v) throws Exception;
61 public String isValid(String value) {
65 } catch (Exception e) {
66 return e.getMessage();
71 public static class DoubleArrayValidator extends Validator {
73 public void tryValidate(String v) throws Exception {
77 public static class DoubleValidator extends Validator {
79 public void tryValidate(String v) throws Exception {
80 Double.parseDouble(v.trim());
83 public static class FloatArrayValidator extends Validator {
85 public void tryValidate(String v) throws Exception {
89 public static class FloatValidator extends Validator {
91 public void tryValidate(String v) throws Exception {
92 Float.parseFloat(v.trim());
95 public static class ByteArrayValidator extends Validator {
97 public void tryValidate(String v) throws Exception {
101 public static class ByteValidator extends Validator {
103 public void tryValidate(String v) throws Exception {
107 public static class BooleanArrayValidator extends Validator {
109 void tryValidate(String v) throws Exception {
110 parseBooleanArray(v);
113 public static class BooleanValidator extends Validator {
115 void tryValidate(String v) throws Exception {
116 parseBoolean(v.trim());
119 public static class IntegerArrayValidator extends Validator {
121 public void tryValidate(String v) throws Exception {
125 public static class IntegerValidator extends Validator {
127 public void tryValidate(String v) throws Exception {
128 Integer.parseInt(v.trim());
131 public static class LongArrayValidator extends Validator {
133 public void tryValidate(String v) throws Exception {
137 public static class LongValidator extends Validator {
139 public void tryValidate(String v) throws Exception {
140 Long.parseLong(v.trim());
144 public static Object parse(String value, Class<?> clazz) throws IllegalArgumentException {
145 if(Double.class.equals(clazz)) return parseDouble(value);
146 if(Float.class.equals(clazz)) return parseFloat(value);
147 if(Integer.class.equals(clazz)) return parseInt(value);
148 if(Long.class.equals(clazz)) return parseLong(value);
149 if(Boolean.class.equals(clazz)) return parseBoolean(value);
150 if(Byte.class.equals(clazz)) return parseByte(value);
151 if(String.class.equals(clazz)) return value;
152 throw new IllegalArgumentException("class=" + clazz + ", value='" + value + "'");
155 public static Object parse(String value, Datatype datatype) throws IllegalArgumentException {
156 if(datatype instanceof DoubleType) return parseDouble(value);
157 if(datatype instanceof FloatType) return parseFloat(value);
158 if(datatype instanceof IntegerType) return parseInt(value);
159 if(datatype instanceof LongType) return parseLong(value);
160 if(datatype instanceof BooleanType) return parseBoolean(value);
161 if(datatype instanceof ByteType) return parseByte(value);
162 if(datatype instanceof StringType) return value;
163 throw new IllegalArgumentException("datatype=" + datatype + ", value='" + value + "'");
166 public static double parseDouble(String value) throws IllegalArgumentException {
167 Matcher m = p.matcher(value);
169 throw new IllegalArgumentException(value + " is not a double number");
170 String result = m.group();
172 // "Usaify", then parse to allow both , and . as decimal separators.
173 return Double.parseDouble(result.replace(',', '.').trim());
175 throw new IllegalArgumentException();
178 public static double[] parseDoubleArray(String value) throws IllegalArgumentException {
179 String[] parts = commaPat.split(value);
180 double[] ret = new double[parts.length];
181 for(int i=0;i<parts.length;++i)
182 ret[i] = parseDouble(parts[i].trim());
186 public static float parseFloat(String value) throws IllegalArgumentException {
187 Matcher m = p.matcher(value);
189 throw new IllegalArgumentException(value + " is not a float number");
190 String result = m.group();
192 // "USAify", then parse to allow both , and . as decimal separators.
193 return Float.parseFloat(result.replace(',', '.').trim());
195 throw new IllegalArgumentException();
198 public static float[] parseFloatArray(String value) throws IllegalArgumentException {
199 String[] parts = commaPat.split(value);
200 float[] ret = new float[parts.length];
201 for(int i=0;i<parts.length;++i)
202 ret[i] = parseFloat(parts[i].trim());
206 public static boolean parseBoolean(String value) throws IllegalArgumentException {
207 if (truePattern.matcher(value.trim()).matches())
209 if (falsePattern.matcher(value.trim()).matches())
211 throw new IllegalArgumentException("not a boolean: " + value);
214 public static boolean[] parseBooleanArray(String value) {
215 String[] parts = commaPat.split(value);
216 boolean[] ret = new boolean[parts.length];
217 for(int i=0;i<parts.length;++i)
218 ret[i] = parseBoolean(parts[i].trim());
222 public static byte parseByte(String value) throws IllegalArgumentException {
224 return Byte.parseByte(value);
225 } catch (NumberFormatException e) {
226 throw new IllegalArgumentException(e);
230 public static byte[] parseByteArray(String value) {
231 String[] parts = commaPat.split(value);
232 byte[] ret = new byte[parts.length];
233 for(int i=0;i<parts.length;++i)
234 ret[i] = parseByte(parts[i]);
238 public static int parseInt(String value) throws IllegalArgumentException {
240 return Integer.parseInt(value.trim());
241 } catch (NumberFormatException e) {
242 throw new IllegalArgumentException(e);
246 public static int[] parseIntArray(String value) {
247 String[] parts = commaPat.split(value);
248 int[] ret = new int[parts.length];
249 for(int i=0;i<parts.length;++i)
250 ret[i] = parseInt(parts[i].trim());
254 public static long parseLong(String value) throws IllegalArgumentException {
256 return Long.parseLong(value.trim());
257 } catch (NumberFormatException e) {
258 throw new IllegalArgumentException(e);
262 public static long[] parseLongArray(String value) {
263 String[] parts = commaPat.split(value);
264 long[] ret = new long[parts.length];
265 for(int i=0;i<parts.length;++i)
266 ret[i] = parseLong(parts[i].trim());