Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B28CC10BD4 for ; Wed, 19 Jun 2013 18:31:37 +0000 (UTC) Received: (qmail 41013 invoked by uid 500); 19 Jun 2013 18:31:36 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 40970 invoked by uid 500); 19 Jun 2013 18:31:36 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 39905 invoked by uid 99); 19 Jun 2013 18:31:34 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 19 Jun 2013 18:31:34 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id C2BEC24B23; Wed, 19 Jun 2013 18:31:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lorinbeer@apache.org To: commits@cordova.apache.org Date: Wed, 19 Jun 2013 18:31:58 -0000 Message-Id: In-Reply-To: <56d82bd5941543b5b935722b63cffa79@git.apache.org> References: <56d82bd5941543b5b935722b63cffa79@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [26/37] [CB-3401] renamed blackberry root folder to bbos http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b66b2ef1/bbos/framework/ext/src/org/apache/cordova/json4j/internal/JSON4JStringReader.java ---------------------------------------------------------------------- diff --git a/bbos/framework/ext/src/org/apache/cordova/json4j/internal/JSON4JStringReader.java b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/JSON4JStringReader.java new file mode 100644 index 0000000..5c3583a --- /dev/null +++ b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/JSON4JStringReader.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cordova.json4j.internal; + +import java.io.IOException; +import java.io.Reader; + +public class JSON4JStringReader extends Reader { + + private char[] _buf = null; + private int _mark = 0; + private int maxLength = 0; + + public JSON4JStringReader(String str) { + _buf = str.toCharArray(); + maxLength = str.length(); + _mark = 0; + } + + public void close() throws IOException { + return; + } + + public int read(char[] cbuf, int off, int len) throws IOException { + if (_mark == (maxLength)) + return -1; + + int read = 0; + for (int x=0; x= _buffer.length) { + // Resize the array first. + _buffer = JSON4JStringWriter.resizeArray(_buffer); + } + for (int x=0; x < len; x++) { + _buffer[_mark] = cbuf[off+x]; + _mark++; + } + } + + public String toString() { + return new String(_buffer, 0, _mark); + } +} http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b66b2ef1/bbos/framework/ext/src/org/apache/cordova/json4j/internal/NumberUtil.java ---------------------------------------------------------------------- diff --git a/bbos/framework/ext/src/org/apache/cordova/json4j/internal/NumberUtil.java b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/NumberUtil.java new file mode 100644 index 0000000..d63d4db --- /dev/null +++ b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/NumberUtil.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cordova.json4j.internal; + +public class NumberUtil { + + public static boolean isNumber(Class clazz) { + if ( (clazz == Integer.class) || (clazz == Long.class) || (clazz == Double.class) || (clazz == Short.class) || (clazz == Float.class)) { + return true; + } + return false; + } + + public static double getDouble(Object val) { + if (val instanceof Double) { + return ((Double)val).doubleValue(); + } + else if (val instanceof Long) { + return ((Long)val).doubleValue(); + } + else if (val instanceof Short) { + Integer i = new Integer( ((Short)val).shortValue() ); + return i.doubleValue(); + } + else if (val instanceof Float) { + return ((Float)val).doubleValue(); + } + else if (val instanceof Integer) { + return ((Integer)val).doubleValue(); + } + throw new IllegalArgumentException("Not a number"); + } + + public static short getShort(Object val) { + if (val instanceof Double) { + return ((Double)val).shortValue(); + } + else if (val instanceof Long) { + Double dg = new Double(((Long)val).longValue()); + return dg.shortValue(); + } + else if (val instanceof Short) { + return ((Short)val).shortValue(); + } + else if (val instanceof Float) { + return ((Float)val).shortValue(); + } else if (val instanceof Integer) { + return ((Integer)val).shortValue(); + } + throw new IllegalArgumentException("Not a number"); + } + + public static int getInt(Object val) { + if (val instanceof Double) { + return ((Double)val).intValue(); + } + else if (val instanceof Long) { + Double dg = new Double(((Long)val).longValue()); + return dg.intValue(); + } + else if (val instanceof Short) { + Double dg = new Double(((Short)val).shortValue()); + return dg.intValue(); + } + else if (val instanceof Float) { + return ((Float)val).intValue(); + } + else if (val instanceof Integer) { + return ((Integer)val).intValue(); + } + throw new IllegalArgumentException("Not a number"); + } + + public static long getLong(Object val) { + if (val instanceof Double) { + return ((Double)val).longValue(); + } + else if (val instanceof Long) { + return ((Long)val).longValue(); + } + else if (val instanceof Short) { + Long lg = new Long(((Short)val).shortValue()); + return lg.longValue(); + } + else if (val instanceof Float) { + return ((Float)val).longValue(); + } + else if (val instanceof Integer) { + return ((Integer)val).longValue(); + } + throw new IllegalArgumentException("Not a number"); + } +} http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b66b2ef1/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Parser.java ---------------------------------------------------------------------- diff --git a/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Parser.java b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Parser.java new file mode 100644 index 0000000..39ddf98 --- /dev/null +++ b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Parser.java @@ -0,0 +1,338 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cordova.json4j.internal; + +import java.io.IOException; +import java.io.Reader; + +import org.apache.cordova.json4j.JSONArray; +import org.apache.cordova.json4j.JSONException; +import org.apache.cordova.json4j.JSONObject; + +/** + * Private parser class which handles doing the parsing of the JSON string into tokens. + */ +public class Parser { + + private Tokenizer tokenizer; + private Token lastToken; + + /** + * Contructor + * @param reader The Reader to use when reading in the JSON stream/string. + * + * @throws JSONException Thrown if an error occurs in tokenizing the JSON string. + */ + public Parser(Reader reader) throws JSONException { + super(); + try { + this.tokenizer = new Tokenizer(reader, false); + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during input read."); + jex.setCause(iox); + throw jex; + } + } + + /** + * Contructor + * @param reader The Reader to use when reading in the JSON stream/string. + * @param strict Boolean indicating if the parser should parse in strict mode, meaning unqoted strings and comments are not allowed. + * + * @throws JSONException Thrown if an error occurs in tokenizing the JSON string. + */ + public Parser(Reader reader, boolean strict) throws JSONException { + super(); + try { + this.tokenizer = new Tokenizer(reader, strict); + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during input read."); + jex.setCause(iox); + throw jex; + } + } + + /** + * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within. + * Same as calling parse(false); + * + * @throws JSONException Thrown if an IO error occurd during parse of the JSON object(s). + */ + public JSONObject parse() throws JSONException { + return parse(false, (JSONObject)null); + } + + /** + * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within. + * Same as calling parse(false); + * + * @throws JSONException Thrown if an IO error occurd during parse of the JSON object(s). + */ + public JSONObject parse(JSONObject jObj) throws JSONException { + return parse(false, jObj); + } + + /** + * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within. + * @param ordered Flag to denote if the parse should contruct a JSON object which maintains serialization order of the attributes. + * + * @throws JSONException Thrown if an IO error occurd during parse of the JSON object(s). + */ + public JSONObject parse(boolean ordered) throws JSONException { + try { + lastToken = tokenizer.next(); + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during input read."); + jex.setCause(iox); + throw jex; + } + return parseObject(ordered, null); + } + + /** + * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within. + * @param ordered Flag to denote if the parse should contruct a JSON object which maintains serialization order of the attributes. + * @param jObj The JSONObjetc to fill out from the parsing. If null, create a new one. + * + * @throws JSONException Thrown if an IO error occurd during parse of the JSON object(s). + */ + public JSONObject parse(boolean ordered, JSONObject jObj) throws JSONException { + try { + lastToken = tokenizer.next(); + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during input read."); + jex.setCause(iox); + throw jex; + } + return parseObject(ordered, jObj); + } + + /** + * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within. + * @param jObj The JSONArray to fill out from the parsing. If null, create a new one. + * + * @throws JSONException Thrown if an IO error occurd during parse of the JSON object(s). + */ + public JSONArray parse(JSONArray jObj) throws JSONException { + return parse(false, jObj); + } + + /** + * Method to initiate the parse of the toplevel JSON object, which will in turn parse all child JSON objects contained within. + * @param ordered Flag to denote if the parse should contruct for all JSON objects encounted, a JSON object which maintains serialization order of the attributes. + * @param jObj The JSONArray to fill out from the parsing. If null, create a new one. + * + * @throws JSONException Thrown if an IO error occurd during parse of the JSON object(s). + */ + public JSONArray parse(boolean ordered, JSONArray jObj) throws JSONException { + try { + lastToken = tokenizer.next(); + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during input read."); + jex.setCause(iox); + throw jex; + } + return parseArray(ordered, jObj); + } + + /** + * Method to parse a JSON object out of the current JSON string position. + * @return JSONObject Returns the parsed out JSON object. + * + * @throws JSONException Thrown if an IO error occurs during parse, such as a malformed JSON object. + */ + public JSONObject parseObject() throws JSONException { + return parseObject(false, null); + } + + /** + * Method to parse a JSON object out of the current JSON string position. + * @param ordered Flag to denote if the parse should contruct a JSON object which maintains serialization order of the attributes. + * @return JSONObject Returns the parsed out JSON object. + * + * @throws JSONException Thrown if an IO error occurs during parse, such as a malformed JSON object. + */ + public JSONObject parseObject(boolean ordered, JSONObject rootObject) throws JSONException { + + try { + JSONObject result = null; + if (rootObject != null) { + result = rootObject; + } else { + if (!ordered) { + result = new JSONObject(); + } else { + //MSN NO ORDERED + result = new JSONObject(); + } + } + + if (lastToken != Token.TokenBraceL) throw new JSONException("Expecting '{' " + tokenizer.onLineCol() + " instead, obtained token: '" + lastToken + "'"); + lastToken = tokenizer.next(); + + while (true) { + if (lastToken == Token.TokenEOF) throw new JSONException("Unterminated object " + tokenizer.onLineCol()); + + if (lastToken == Token.TokenBraceR) { + lastToken = tokenizer.next(); + break; + } + + if (!lastToken.isString()) throw new JSONException("Expecting string key " + tokenizer.onLineCol()); + String key = lastToken.getString(); + + lastToken = tokenizer.next(); + if (lastToken != Token.TokenColon) throw new JSONException("Expecting colon " + tokenizer.onLineCol()); + + lastToken = tokenizer.next(); + Object val = parseValue(ordered); + + result.put(key, val); + + if (lastToken == Token.TokenComma) { + lastToken = tokenizer.next(); + } + + else if (lastToken != Token.TokenBraceR) { + throw new JSONException("expecting either ',' or '}' " + tokenizer.onLineCol()); + } + } + return result; + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during object input read."); + jex.setCause(iox); + throw jex; + } + } + + /** + * Method to parse out a JSON array from a JSON string + * Same as calling parseArray(false, null) + * + * @throws JSONException Thrown if a parse error occurs, such as a malformed JSON array. + */ + public JSONArray parseArray() throws JSONException { + return parseArray(false, null); + } + + /** + * Method to parse out a JSON array from a JSON string + * @param ordered Flag to denote if the parse should contruct JSON objects which maintain serialization order of the attributes for all JSONOjects in the array. + * *param array An array instance to populate instead of creating a new one. + * + * @throws JSONException Thrown if a parse error occurs, such as a malformed JSON array. + */ + public JSONArray parseArray(boolean ordered, JSONArray array) throws JSONException { + JSONArray result = null; + if(array != null){ + result = array; + } else { + result = new JSONArray(); + } + + try { + if (lastToken != Token.TokenBrackL) throw new JSONException("Expecting '[' " + tokenizer.onLineCol()); + lastToken = tokenizer.next(); + while (true) { + if (lastToken == Token.TokenEOF) throw new JSONException("Unterminated array " + tokenizer.onLineCol()); + + /** + * End of the array. + */ + if (lastToken == Token.TokenBrackR) { + lastToken = tokenizer.next(); + break; + } + + Object val = parseValue(ordered); + result.add(val); + + if (lastToken == Token.TokenComma) { + lastToken = tokenizer.next(); + } else if (lastToken != Token.TokenBrackR) { + throw new JSONException("expecting either ',' or ']' " + tokenizer.onLineCol()); + } + } + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during array input read."); + jex.setCause(iox); + throw jex; + } + return result; + } + + /** + * Method to parse the current JSON property value from the last token. + * @return The java object type that represents the JSON value. + * + * @throws JSONException Thrown if an IO error (read incomplete token) occurs. + */ + public Object parseValue() throws JSONException { + return parseValue(false); + } + + /** + * Method to parse the current JSON property value from the last token. + * @return The java object type that represents the JSON value. + * @param ordered Flag to denote if the parse should contruct JSON objects and arrays which maintain serialization order of the attributes. + * + * @throws JSONException Thrown if an IO error (read incomplete token) occurs. + */ + public Object parseValue(boolean ordered) throws JSONException { + if (lastToken == Token.TokenEOF) throw new JSONException("Expecting property value " + tokenizer.onLineCol()); + + try { + if (lastToken.isNumber()) { + Object result = lastToken.getNumber(); + lastToken = tokenizer.next(); + return result; + } + + if (lastToken.isString()) { + Object result = lastToken.getString(); + lastToken = tokenizer.next(); + return result; + } + + if (lastToken == Token.TokenFalse) { + lastToken = tokenizer.next(); + return Boolean.FALSE; + } + + if (lastToken == Token.TokenTrue) { + lastToken = tokenizer.next(); + return Boolean.TRUE; + } + + if (lastToken == Token.TokenNull) { + lastToken = tokenizer.next(); + return JSONObject.NULL; + } + + if (lastToken == Token.TokenBrackL) return parseArray(ordered, null); + if (lastToken == Token.TokenBraceL) return parseObject(ordered, null); + + } catch (IOException iox) { + JSONException jex = new JSONException("Error occurred during value input read."); + jex.setCause(iox); + throw jex; + } + throw new JSONException("Invalid token " + tokenizer.onLineCol()); + } +} http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b66b2ef1/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Serializer.java ---------------------------------------------------------------------- diff --git a/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Serializer.java b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Serializer.java new file mode 100644 index 0000000..50f34d3 --- /dev/null +++ b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/Serializer.java @@ -0,0 +1,353 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cordova.json4j.internal; + +import java.io.IOException; +import java.io.Writer; +import java.util.Enumeration; +import java.util.Hashtable; + +import org.apache.cordova.json4j.JSONArray; +import org.apache.cordova.json4j.JSONObject; +import org.apache.cordova.json4j.JSONString; + +/** + * Class to handle serialization of a JSON object to a JSON string. + */ +public class Serializer { + + /** + * The writer to use when writing this JSON object. + */ + private Writer writer; + + /** + * Create a serializer on the specified output stream writer. + */ + public Serializer(Writer writer) { + super(); + this.writer = writer; + } + + /** + * Method to flush the current writer. + * @throws IOException Thrown if an error occurs during writer flush. + */ + public void flush() throws IOException { + writer.flush(); + } + + /** + * Method to close the current writer. + * @throws IOException Thrown if an error occurs during writer close. + */ + public void close() throws IOException { + writer.close(); + } + + /** + * Method to write a raw string to the writer. + * @param s The String to write. + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeRawString(String s) throws IOException { + writer.write(s); + return this; + } + + /** + * Method to write the text string 'null' to the output stream (null JSON object). + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeNull() throws IOException { + writeRawString("null"); + return this; + } + + /** + * Method to write a number to the current writer. + * @param value The number to write to the JSON output string. + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeNumber(Object value) throws IOException { + if (null == value) return writeNull(); + + if (value instanceof Float) { + if (((Float)value).isNaN()) return writeNull(); + if (Float.NEGATIVE_INFINITY == ((Float)value).floatValue()) return writeNull(); + if (Float.POSITIVE_INFINITY == ((Float)value).floatValue()) return writeNull(); + } + + if (value instanceof Double) { + if (((Double)value).isNaN()) return writeNull(); + if (Double.NEGATIVE_INFINITY == ((Double)value).doubleValue()) return writeNull(); + if (Double.POSITIVE_INFINITY == ((Double)value).doubleValue()) return writeNull(); + } + + writeRawString(value.toString()); + + return this; + } + + /** + * Method to write a boolean value to the output stream. + * @param value The Boolean object to write out as a JSON boolean. + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeBoolean(Boolean value) throws IOException { + if (null == value) return writeNull(); + + writeRawString(value.toString()); + + return this; + } + + /** + * Method to generate a string with a particular width. Alignment is done using zeroes if it does not meet the width requirements. + * @param s The string to write + * @param len The minimum length it should be, and to align with zeroes if length is smaller. + * @return A string properly aligned/correct width. + */ + private static String rightAlignedZero(String s, int len) { + if (len == s.length()) return s; + + StringBuffer sb = new StringBuffer(s); + + while (sb.length() < len) { + sb.insert(0, '0'); + } + + return sb.toString(); + } + + /** + * Method to write a String out to the writer, encoding special characters and unicode characters properly. + * @param value The string to write out. + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeString(String value) throws IOException { + if (null == value) return writeNull(); + + writer.write('"'); + + char[] chars = value.toCharArray(); + + for (int i=0; i= 32) && (c <= 126)) { + writer.write(c); + } else { + writer.write("\\u"); + writer.write(rightAlignedZero(Integer.toHexString(c),4)); + } + } + } + + writer.write('"'); + + return this; + } + + /** + * Method to write out a generic JSON type. + * @param object The JSON compatible object to serialize. + * @throws IOException Thrown if an error occurs during write, or if a nonJSON compatible Java object is passed.. + */ + private Serializer write(Object object) throws IOException { + if (null == object) return writeNull(); + + // Serialize the various types! + Class clazz = object.getClass(); + if (NumberUtil.isNumber(clazz)) return writeNumber(object); + if (Boolean.class.isAssignableFrom(clazz)) return writeBoolean((Boolean) object); + if (JSONObject.class.isAssignableFrom(clazz)) return writeObject((JSONObject) object); + if (JSONArray.class.isAssignableFrom(clazz)) return writeArray((JSONArray) object); + if (JSONString.class.isAssignableFrom(clazz)) return writeRawString(((JSONString) object).toJSONString()); + if (String.class.isAssignableFrom(clazz)) return writeString((String) object); + + throw new IOException("Attempting to serialize unserializable object: '" + object + "'"); + } + + /** + * Method to write a complete JSON object to the stream. + * @param object The JSON object to write out. + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeObject(JSONObject object) throws IOException { + if (null == object) return writeNull(); + + // write header + writeRawString("{"); + indentPush(); + + Enumeration iter = getPropertyNames(object); + + while ( iter.hasMoreElements() ) { + Object key = iter.nextElement(); + if (!(key instanceof String)) throw new IOException("attempting to serialize object with an invalid property name: '" + key + "'" ); + + Object value = object.get(key); + if (!JSONObject.isValidObject(value)) throw new IOException("attempting to serialize object with an invalid property value: '" + value + "'"); + + newLine(); + indent(); + writeString((String)key); + writeRawString(":"); + space(); + write(value); + + if (iter.hasMoreElements()) writeRawString(","); + } + + // write trailer + indentPop(); + newLine(); + indent(); + writeRawString("}"); + + return this; + } + + /** + * Method to write a JSON array out to the stream. + * @param value The JSON array to write out. + * @throws IOException Thrown if an error occurs during write. + */ + public Serializer writeArray(JSONArray value) throws IOException { + if (null == value) return writeNull(); + + // write header + writeRawString("["); + indentPush(); + + for (Enumeration iter=value.elements(); iter.hasMoreElements(); ) { + Object element = iter.nextElement(); + if (!JSONObject.isValidObject(element)) throw new IOException("attempting to serialize array with an invalid element: '" + value + "'"); + + newLine(); + indent(); + write(element); + + if (iter.hasMoreElements()) writeRawString(","); + } + + // write trailer + indentPop(); + newLine(); + indent(); + writeRawString("]"); + + return this; + } + + //--------------------------------------------------------------- + // pretty printing overridables + //--------------------------------------------------------------- + + /** + * Method to write a space to the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void space() throws IOException { + } + + /** + * Method to write a newline to the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void newLine() throws IOException { + } + + /** + * Method to write an indent to the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void indent() throws IOException { + } + + /** + * Method to increase the indent depth of the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void indentPush() { + } + + /** + * Method to reduce the indent depth of the output writer. + */ + public void indentPop() { + } + + /** + * Method to get a list of all the property names stored in a map. + */ + public Enumeration getPropertyNames(Hashtable map) { + return map.keys(); + } + + /** + * Method to write a String out to the writer, encoding special characters and unicode characters properly. + * @param value The string to write out. + */ + public static String quote(String value) { + if (value == null || value.length() == 0) { + return "\"\""; + } + + StringBuffer buf = new StringBuffer(); + char[] chars = value.toCharArray(); + + buf.append('"'); + for (int i=0; i= 32) && (c <= 126)) { + buf.append(c); + } else { + buf.append("\\u"); + buf.append(rightAlignedZero(Integer.toHexString(c),4)); + } + } + } + buf.append('"'); + + return buf.toString(); + } +} http://git-wip-us.apache.org/repos/asf/cordova-blackberry/blob/b66b2ef1/bbos/framework/ext/src/org/apache/cordova/json4j/internal/SerializerVerbose.java ---------------------------------------------------------------------- diff --git a/bbos/framework/ext/src/org/apache/cordova/json4j/internal/SerializerVerbose.java b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/SerializerVerbose.java new file mode 100644 index 0000000..5132591 --- /dev/null +++ b/bbos/framework/ext/src/org/apache/cordova/json4j/internal/SerializerVerbose.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cordova.json4j.internal; + +import java.io.IOException; +import java.io.Writer; + +/** + * Internaql class for handling the serialization of JSON objects in a verbose + * format, meaning newlines and indention. + */ +public class SerializerVerbose extends Serializer { + + /** + * Internal tracker keeping indent position. + */ + private int indent = 0; + + /** + * The indent string to use when serializing. + */ + private String indentStr = "\t"; + + /** + * Constructor. + */ + public SerializerVerbose(Writer writer) { + super(writer); + } + + /** + * Constructor. + * @param Writer The writer to serialize JSON to. + * @param indentSpaces: How many spaces to indent by (0 to 8). + * The default indent is the TAB character. + */ + public SerializerVerbose(Writer writer, int indentSpaces) { + super(writer); + if(indentSpaces > 0 && indentSpaces < 8){ + this.indentStr = ""; + for(int i = 0; i < indentSpaces; i++){ + this.indentStr += " "; + } + } + } + + /** + * Method to write a space to the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void space() throws IOException { + writeRawString(" "); + } + + /** + * Method to write a newline to the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void newLine() throws IOException { + writeRawString("\n"); + } + + /** + * Method to write an indent to the output writer. + * @throws IOException Thrown if an error occurs during write. + */ + public void indent() throws IOException { + for (int i=0; i= Integer.MIN_VALUE)) { +// return new Integer(value.intValue()); +// } +// else { +// return value; +// } +// } + + if (string.equals("0")) { + return new Integer(0); +// } else if (string.startsWith("0") && string.length() > 1) { +// Long value = Long.valueOf(sign + string.substring(1),8); +// if (value.longValue() <= Integer.MAX_VALUE && (value.longValue() >= Integer.MIN_VALUE)) { +// return new Integer(value.intValue()); +// } +// else { +// return value; +// } + } + + /** + * We have to check for the exponential and treat appropriately + * Exponentials should be treated as Doubles. + */ + if (string.indexOf("e") != -1 || string.indexOf("E") != -1) { + return Double.valueOf(sign + string); + } else { + Long value = new Long(Long.parseLong(sign+ string, 10)); + //Long value = Long.valueOf(sign + string); + if (value.longValue() <= Integer.MAX_VALUE && (value.longValue() >= Integer.MIN_VALUE)) { + return new Integer(Integer.parseInt(sign + string, 10)); + } + else { + return value; + } + } + // else return new Integer(0); //MSN + } catch (NumberFormatException e) { + IOException iox = new IOException("Invalid number literal " + onLineCol(l,c)); + //MSNiox.setCause(e); + throw iox; + } + } + + /** + * Method to indicate if the character read is a HEX digit or not. + * @param c The character to check for being a HEX digit. + */ + private boolean isHexDigit(int c) { + switch (c) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + return true; + } + + return false; + } + + /** + * Method to indicate if the character read is an OCTAL digit or not. + * @param c The character to check for being a OCTAL digit. + */ + private boolean isOctalDigit(int c) { + switch (c) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + return true; + } + + return false; + } + + /** + * Method to indicate if the character read is a digit or not. + * @param c The character to check for being a digit. + */ + private boolean isDigitChar(int c) { + switch (c) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '.': + case 'e': + case 'E': + case 'x': + case 'X': + case '+': + case '-': + return true; + } + + return false; + } + + /** + * Method to read a partular character string. + * only really need to handle 'null', 'true', and 'false' + */ + private String readIdentifier() throws IOException { + StringBuffer sb = new StringBuffer(); + + if (this.strict) { + while ((-1 != lastChar) && ( (Character.isUpperCase((char)lastChar)) || (Character.isLowerCase((char)lastChar)) ) ){ + sb.append((char)lastChar); + readChar(); + } + } + else { + while ((-1 != lastChar) && isValidUnquotedChar((char)lastChar)) { + sb.append((char)lastChar); + readChar(); + } + } + + return sb.toString(); + } + + /** + * Method to read the next character from the string, keeping track of line/column position. + * + * @throws IOEXception Thrown when underlying reader throws an error. + */ + private void readChar() throws IOException { + if ('\n' == lastChar) { + this.colNo = 0; + this.lineNo++; + } + lastChar = reader.read(); + if (-1 == lastChar) return ; + colNo++; + } + + /** + * Method to generate a String indicationg the current line and column position in the JSON string. + */ + private String onLineCol(int line, int col) { + return "on line " + line + ", column " + col; + } + + /** + * Method to generate a String indicationg the current line and column position in the JSON string. + */ + public String onLineCol() { + return onLineCol(lineNo,colNo); + } + + /** + * High speed test for whitespace! Faster than the java one (from some testing). + * @return if the indicated character is whitespace. + */ + public boolean isWhitespace(char c) { + switch (c) { + case 9: //'unicode: 0009 + case 10: //'unicode: 000A' + case 11: //'unicode: 000B' + case 12: //'unicode: 000C' + case 13: //'unicode: 000D' + case 28: //'unicode: 001C' + case 29: //'unicode: 001D' + case 30: //'unicode: 001E' + case 31: //'unicode: 001F' + case ' ': // Space + //case Character.SPACE_SEPARATOR: + //case Character.LINE_SEPARATOR: + //MSN case Character.PARAGRAPH_SEPARATOR: + return true; + } + return false; + } + + /** + * For non strict mode, check if char is valid when not quoted. + * @param c + * @return if character is valid unquoted character. + */ + public boolean isValidUnquotedChar(char c) { + + if ( (Character.isDigit(c)) || (Character.isLowerCase(c)) || (Character.isUpperCase(c)) ) { + return true; + } + + switch (c) { + case '@': + case '-': + case '.': + case '$': + case '+': + case '!': + case '_': + return true; + } + return false; + } + +}