fixed string appends
[lwes-java.git] / src / org / lwes / serializer / StringParser.java
blob75c7acb1e321885849f60261d81dc736860c3e08
1 package org.lwes.serializer;
3 import org.lwes.EventSystemException;
4 import org.lwes.TypeID;
5 import org.lwes.util.IPAddress;
6 import org.lwes.util.Log;
7 import org.lwes.util.NumberCodec;
9 import java.util.regex.Pattern;
11 /**
12 * This contains low level type serialization used by the rest of the system.
14 * @author Anthony Molinaro
15 * @author Michael P. Lum
17 public class StringParser {
19 public static Object fromStringBYTE(String string)
20 throws EventSystemException {
21 Object toReturn = null;
23 return toReturn;
26 public static Object fromStringBOOLEAN(String string)
27 throws EventSystemException {
28 Log.trace("Parsing boolean");
29 Object toReturn = Boolean.valueOf(string);
30 Log.trace("Got '" + toReturn + "'");
31 return toReturn;
34 public static Object fromStringUINT16(String string)
35 throws EventSystemException {
36 Object toReturn = null;
38 Log.trace("Parsing uint16");
39 if (Pattern.matches(TypeID.HEX_SHORT_REGEX, string)) {
40 if (string.startsWith("0x"))
41 string = string.substring(2);
43 /* pad with zeros since NumberCodec.decodeInt expects a length of 8 */
44 string = "0000" + string;
45 byte[] bytes = NumberCodec.hexStringToByteArray(string);
46 toReturn = NumberCodec.decodeInt(bytes, 0, bytes.length);
47 } else {
48 try {
49 toReturn = Integer.valueOf(string);
50 } catch (NumberFormatException nfe) {
51 throw new EventSystemException(nfe);
53 int intValue = (Integer) toReturn;
54 if (intValue < 0 || intValue > 65535) {
55 throw new EventSystemException("Unsigned Short must be in the "
56 + "range [0-65535] ");
59 Log.trace("received '" + toReturn + "'");
61 return toReturn;
64 public static Object fromStringINT16(String string)
65 throws EventSystemException {
66 Object toReturn = null;
68 Log.trace("Parsing int16");
69 if (Pattern.matches(TypeID.HEX_SHORT_REGEX, string)) {
70 if (string.startsWith("0x"))
71 string = string.substring(2);
73 byte[] bytes = NumberCodec.hexStringToByteArray(string);
74 toReturn = NumberCodec.decodeShort(bytes, 0, bytes.length);
75 } else {
76 try {
77 toReturn = Short.valueOf(string);
78 } catch (NumberFormatException nfe) {
79 throw new EventSystemException("Probably not a short, "
80 + "got exception " + nfe);
82 short shortValue = (Short) toReturn;
83 if (shortValue < -32768 || shortValue > 32767) {
84 throw new EventSystemException("Signed Short must be in the "
85 + "range [-32768 - 32767] ");
88 Log.trace("received '" + toReturn + "'");
90 return toReturn;
93 public static Object fromStringUINT32(String string)
94 throws EventSystemException {
95 Object toReturn = null;
97 Log.trace("Parsing uint32");
98 if (Pattern.matches(TypeID.HEX_INT_REGEX, string)) {
99 if (string.startsWith("0x"))
100 string = string.substring(2);
102 /* pad with zeros since NumberCodec.decodeLong expects a length of 8 */
103 string = "00000000" + string;
104 byte[] bytes = NumberCodec.hexStringToByteArray(string);
105 toReturn = NumberCodec.decodeLong(bytes, 0, bytes.length);
106 } else {
107 try {
108 toReturn = Long.valueOf(string);
109 } catch (NumberFormatException nfe) {
110 throw new EventSystemException(nfe);
112 long longValue = (Long) toReturn;
113 if (longValue < 0
114 || longValue > ((long) Integer.MAX_VALUE - ((long) Integer.MIN_VALUE))) {
115 throw new EventSystemException("Unsigned Int must be in the "
116 + "range [0-"
117 + ((long) Integer.MAX_VALUE - (long) Integer.MIN_VALUE)
118 + "] ");
121 Log.trace("received '" + toReturn + "'");
123 return toReturn;
126 public static Object fromStringINT32(String string)
127 throws EventSystemException {
128 Object toReturn = null;
130 Log.trace("Parsing int32");
131 if (Pattern.matches(TypeID.HEX_INT_REGEX, string)) {
132 if (string.startsWith("0x"))
133 string = string.substring(2);
135 byte[] bytes = NumberCodec.hexStringToByteArray(string);
136 toReturn = NumberCodec.decodeInt(bytes, 0, bytes.length);
137 } else {
138 try {
139 toReturn = Integer.valueOf(string);
140 } catch (NumberFormatException nfe) {
141 throw new EventSystemException(nfe);
144 Log.trace("received '" + toReturn + "'");
146 return toReturn;
149 public static Object fromStringUINT64(String string)
150 throws EventSystemException {
151 Object toReturn = null;
153 Log.trace("Parsing uint64");
154 if (Pattern.matches(TypeID.HEX_LONG_REGEX, string)) {
155 if (string.startsWith("0x"))
156 string = string.substring(2);
158 byte[] bytes = NumberCodec.hexStringToByteArray(string);
159 toReturn = NumberCodec.decodeLong(bytes, 0, bytes.length);
160 } else {
161 try {
162 toReturn = Long.valueOf(string);
163 } catch (NumberFormatException nfe) {
164 throw new EventSystemException("Got Exception " + nfe);
167 Log.trace("received '" + toReturn + "'");
169 return toReturn;
172 public static Object fromStringINT64(String string)
173 throws EventSystemException {
174 Object toReturn = null;
176 Log.trace("Parsing int64");
177 if (Pattern.matches(TypeID.HEX_LONG_REGEX, string)) {
178 if (string.startsWith("0x"))
179 string = string.substring(2);
181 byte[] bytes = NumberCodec.hexStringToByteArray(string);
182 toReturn = NumberCodec.decodeLong(bytes, 0, bytes.length);
183 } else {
184 try {
185 toReturn = Long.valueOf(string);
186 } catch (NumberFormatException nfe) {
187 throw new EventSystemException(nfe);
190 Log.trace("received '" + toReturn + "'");
192 return toReturn;
195 public static Object fromStringSTRING(String string)
196 throws EventSystemException {
198 Log.trace("Parsing string '" + string + "'");
199 return string;
202 public static Object fromStringIPADDR(String string)
203 throws EventSystemException {
204 Object toReturn = null;
206 Log.trace("Parsing IPAddress");
208 if (Pattern.matches(TypeID.IP_ADDR_REGEX, string)) {
209 toReturn = new IPAddress(string);
210 if (((IPAddress) toReturn).toInt() == 0) {
211 throw new EventSystemException("Possible Bad IP Address "
212 + string);
214 } else {
215 throw new EventSystemException("Invalid IP Address");
217 Log.trace("received '" + toReturn + "'");
219 return toReturn;