001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.json;
021:
022: import java.util.Calendar;
023: import java.util.regex.Matcher;
024: import java.util.regex.Pattern;
025:
026: import de.schlund.pfixcore.webservice.json.parser.ParseException;
027:
028: /**
029: * @author mleidig@schlund.de
030: */
031: public class ParserUtils {
032:
033: final static char[] ESC_CHARS = { 'b', 'f', 'n', 'r', 't', '\\',
034: '"' };
035: final static char[] ESC_MAP = { '\b', '\f', '\n', '\r', '\t', '\\',
036: '"' };
037:
038: final static Pattern DATE_PATTERN = Pattern
039: .compile("new Date\\((0|[1-9][0-9]*)\\)");
040: final static Pattern DATE_UTC_PATTERN = Pattern
041: .compile("new Date\\(Date.UTC\\(((0|[1-9]([0-9])*)(,(0|[1-9]([0-9])*))*)\\)\\)");
042: final static Pattern COMMA_SPLIT_PATTERN = Pattern.compile(",");
043:
044: final static int[] CALENDAR_FIELDS = { Calendar.YEAR,
045: Calendar.MONTH, Calendar.DATE, Calendar.HOUR_OF_DAY,
046: Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND };
047:
048: public static String jsonToJava(String str) throws ParseException {
049: StringBuilder sb = new StringBuilder();
050: int strLen = str.length() - 1;
051: for (int i = 1; i < strLen; i++) {
052: char ch = str.charAt(i);
053: if (ch == '\\') {
054: if (i < strLen) {
055: i++;
056: ch = str.charAt(i);
057: int found = -1;
058: ;
059: for (int k = 0; k < ESC_CHARS.length && found == -1; k++) {
060: if (ch == ESC_CHARS[k])
061: found = k;
062: }
063: if (found > -1)
064: sb.append(ESC_MAP[found]);
065: else {
066: if (ch == 'u') {
067: if ((i + 4) < strLen) {
068: String hexStr = str.substring(i + 1,
069: i + 5);
070: ch = (char) Integer
071: .parseInt(hexStr, 16);
072: i += 4;
073: } else
074: throw new ParseException(
075: "Premature end of JSON string: "
076: + str);
077: }
078: sb.append(ch);
079: }
080: } else
081: throw new ParseException(
082: "Premature end of JSON string: " + str);
083: } else
084: sb.append(ch);
085: }
086: return sb.toString();
087: }
088:
089: public static String jsonEscape(String str) {
090: StringBuilder sb = new StringBuilder();
091: sb.append("\"");
092: for (int i = 0; i < str.length(); i++) {
093: char ch = str.charAt(i);
094: int found = -1;
095: for (int k = 0; k < ESC_MAP.length && found == -1; k++) {
096: if (ch == ESC_MAP[k])
097: found = k;
098: }
099: if (found > -1)
100: sb.append("\\" + ESC_CHARS[found]);
101: else if (ch < '\u0020') {
102: String hexStr = Integer.toHexString(ch);
103: if (hexStr.length() == 1)
104: hexStr = "0" + hexStr;
105: sb.append("\\u00" + hexStr);
106: } else
107: sb.append(ch);
108: }
109: sb.append("\"");
110: return sb.toString();
111: }
112:
113: public static String javaToJson(Object obj) {
114: if (obj == null) {
115: return "null";
116: } else if (obj instanceof String) {
117: return jsonEscape((String) obj);
118: } else if (obj instanceof Boolean) {
119: return obj.toString();
120: } else if (obj instanceof Number) {
121: return obj.toString();
122: } else if (obj instanceof JSONValue) {
123: return ((JSONValue) obj).toJSONString();
124: } else
125: throw new IllegalArgumentException("Type not supported: "
126: + obj.getClass().getName());
127: }
128:
129: public static Number parseNumber(String numStr) {
130: if (numStr.contains(".") || numStr.contains("e")
131: || numStr.contains("E")) {
132: return Double.valueOf(numStr);
133: } else {
134: Number num = null;
135: try {
136: num = Integer.valueOf(numStr);
137: } catch (NumberFormatException x) {
138: num = Long.valueOf(numStr);
139: }
140: return num;
141: }
142: }
143:
144: public static Calendar parseUTCDate(String dateStr) {
145: Matcher mat = DATE_PATTERN.matcher(dateStr);
146: if (mat.matches()) {
147: long time = Long.parseLong(mat.group(1));
148: Calendar cal = Calendar.getInstance();
149: cal.setTimeInMillis(time);
150: return cal;
151: }
152: return null;
153: }
154:
155: public static Calendar parseUTCFuncDate(String dateStr) {
156: Calendar cal = Calendar.getInstance();
157: Matcher mat = DATE_UTC_PATTERN.matcher(dateStr);
158: if (mat.matches()) {
159: String[] vals = COMMA_SPLIT_PATTERN.split(mat.group(1));
160: for (int i = 0; i < vals.length; i++) {
161: int val = Integer.parseInt(vals[i]);
162: cal.set(CALENDAR_FIELDS[i], val);
163: }
164: }
165: return cal;
166: }
167:
168: public static Calendar parseDate(String dateStr) {
169: Calendar cal = parseUTCDate(dateStr);
170: if (cal == null)
171: cal = parseUTCFuncDate(dateStr);
172: return cal;
173: }
174:
175: }
|