Source Code Cross Referenced for ParserUtils.java in  » J2EE » Pustefix » de » schlund » pfixcore » webservice » json » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Pustefix » de.schlund.pfixcore.webservice.json 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.