summaryrefslogtreecommitdiff
path: root/src/plugins/translation/src/com/motorola/studio/android/json
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/translation/src/com/motorola/studio/android/json')
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONArray.java92
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONBoolean.java77
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONNull.java58
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONNumber.java65
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONObject.java95
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONPair.java109
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONString.java94
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONValue.java28
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/JSONValueParser.java74
-rw-r--r--src/plugins/translation/src/com/motorola/studio/android/json/Jason.java84
10 files changed, 776 insertions, 0 deletions
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONArray.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONArray.java
new file mode 100644
index 0000000..91d2491
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONArray.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Stack;
+
+public class JSONArray extends JSONValue
+{
+ private final List<JSONValue> value;
+
+ public JSONArray(List<JSONValue> value)
+ {
+ this.value = value;
+ }
+
+ @Override
+ public List<JSONValue> getValue()
+ {
+ return value;
+ }
+
+ static JSONValue parseValues(List<Character> json)
+ {
+ Stack<Character> stack = new Stack<Character>();
+ List<JSONValue> values = new ArrayList<JSONValue>();
+ boolean parsed = false;
+ while (!parsed)
+ {
+ Character next = json.get(0);
+ if (next == '[')
+ {
+ json.remove(0);
+ stack.push('[');
+ }
+ else if (next == ']')
+ {
+ json.remove(0);
+ if (stack.pop() != '[')
+ {
+ throw new IllegalArgumentException();
+ }
+ else
+ {
+ parsed = true;
+ }
+ }
+ else if (next == ' ' || next == '\r' || next == '\n' || next == ',')
+ {
+ json.remove(0);
+ }
+ else
+ {
+ values.add(JSONValueParser.parse(json));
+ }
+ }
+ return new JSONArray(values);
+ }
+
+ @Override
+ public String toString()
+ {
+ String string = "[";
+ Iterator<JSONValue> objectIterator = value.iterator();
+ while (objectIterator.hasNext())
+ {
+ string += objectIterator.next().toString();
+ if (objectIterator.hasNext())
+ {
+ string += ",";
+ }
+ }
+ string += "]";
+
+ return string;
+ }
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONBoolean.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONBoolean.java
new file mode 100644
index 0000000..4332471
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONBoolean.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public class JSONBoolean extends JSONValue
+{
+ public final boolean value;
+
+ public JSONBoolean(boolean value)
+ {
+ this.value = value;
+ }
+
+ @Override
+ public Object getValue()
+ {
+ return value;
+ }
+
+ static JSONValue parseValues(List<Character> json)
+ {
+ boolean parsed = false;
+ boolean value = false;
+
+ while (!parsed)
+ {
+ Character next = json.get(0);
+
+ if (next == 't' && json.get(1) == 'r' && json.get(2) == 'u' && json.get(3) == 'e')
+ {
+ for (int i = 0; i < 4; i++)
+ {
+ json.remove(0);
+ }
+ parsed = true;
+ value = true;
+ }
+ else if (next == 'f' && json.get(1) == 'a' && json.get(2) == 'l' && json.get(3) == 's'
+ && json.get(4) == 'e')
+ {
+ for (int i = 0; i < 5; i++)
+ {
+ json.remove(0);
+ }
+ parsed = true;
+ }
+ else
+ {
+ throw new IllegalArgumentException();
+ }
+
+ }
+ return new JSONBoolean(value);
+ }
+
+ @Override
+ public String toString()
+ {
+ return Boolean.toString(value);
+ }
+
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONNull.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONNull.java
new file mode 100644
index 0000000..792bce3
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONNull.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public class JSONNull extends JSONValue
+{
+ @Override
+ public Object getValue()
+ {
+ return null;
+ }
+
+ static JSONValue parseValues(List<Character> json)
+ {
+ boolean parsed = false;
+
+ while (!parsed)
+ {
+ Character next = json.get(0);
+
+ if (next == 'n' && json.get(1) == 'u' && json.get(2) == 'l' && json.get(3) == 'l')
+ {
+ for (int i = 0; i < 4; i++)
+ {
+ json.remove(0);
+ }
+ parsed = true;
+ }
+ else
+ {
+ throw new IllegalArgumentException();
+ }
+
+ }
+ return new JSONNull();
+ }
+
+ @Override
+ public String toString()
+ {
+ return "null";
+ }
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONNumber.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONNumber.java
new file mode 100644
index 0000000..d10c9ca
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONNumber.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public class JSONNumber extends JSONValue
+{
+
+ private final int value;
+
+ public JSONNumber(int value)
+ {
+ this.value = value;
+ }
+
+ @Override
+ public Object getValue()
+ {
+ return value;
+ }
+
+ static JSONValue parseValues(List<Character> json)
+ {
+ boolean parsed = false;
+ StringBuilder number = new StringBuilder();
+
+ while (!parsed)
+ {
+ Character next = json.get(0);
+ if (next >= 48 && next <= 57)
+ {
+ json.remove(0);
+ number.append(next);
+ }
+ else
+ {
+ parsed = true;
+ }
+ }
+
+ return new JSONNumber(Integer.parseInt(number.toString()));
+
+ }
+
+ @Override
+ public String toString()
+ {
+ return Integer.toString(value);
+ }
+
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONObject.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONObject.java
new file mode 100644
index 0000000..5b64abe
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONObject.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.Stack;
+
+public class JSONObject extends JSONValue
+{
+ private final Set<JSONPair> value;
+
+ public JSONObject(Set<JSONPair> value)
+ {
+ this.value = value;
+ }
+
+ @Override
+ public Set<JSONPair> getValue()
+ {
+ return value;
+ }
+
+ static JSONValue parseValues(List<Character> json)
+ {
+ Stack<Character> stack = new Stack<Character>();
+ Set<JSONPair> values = new HashSet<JSONPair>();
+ boolean parsed = false;
+ while (!parsed)
+ {
+ Character next = json.get(0);
+
+ if (next == '{')
+ {
+ json.remove(0);
+ stack.push('{');
+ }
+ else if (next == '}')
+ {
+ json.remove(0);
+ if (stack.pop() != '{')
+ {
+ throw new IllegalArgumentException();
+ }
+ else
+ {
+ parsed = true;
+ }
+ }
+ else if (next == ' ' || next == '\r' || next == '\n' || next == ',')
+ {
+ json.remove(0);
+ }
+ else if (next == '"')
+ {
+ values.add(JSONPair.parse(json));
+ }
+ }
+ return new JSONObject(values);
+ }
+
+ @Override
+ public String toString()
+ {
+ String string = "{";
+ Iterator<JSONPair> objectIterator = value.iterator();
+ while (objectIterator.hasNext())
+ {
+ string += objectIterator.next().toString();
+ if (objectIterator.hasNext())
+ {
+ string += ",";
+ }
+ }
+ string += "}";
+
+ return string;
+ }
+
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONPair.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONPair.java
new file mode 100644
index 0000000..2a6424b
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONPair.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public class JSONPair
+{
+ private final String name;
+
+ private final JSONValue value;
+
+ public JSONPair(String name, JSONValue value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public JSONValue getValue()
+ {
+ return value;
+ }
+
+ public static JSONPair parse(List<Character> json)
+ {
+ String name = null;
+ JSONValue value = null;
+ boolean parsed = false;
+ while (!parsed)
+ {
+ Character next = json.get(0);
+ if (next == '"')
+ {
+ name = parseName(json);
+ }
+ else if ((next == ' ') || (next == '\r') || (next == '\n'))
+ {
+ json.remove(0);
+ }
+ else if (next == ':')
+ {
+ json.remove(0);
+ value = JSONValueParser.parse(json);
+ parsed = true;
+ }
+ }
+
+ return new JSONPair(name, value);
+ }
+
+ private static String parseName(List<Character> json)
+ {
+ String name = null;
+ StringBuilder nameBuilder = new StringBuilder();
+ boolean specialChar = false;
+ Character next;
+ json.remove(0);
+ while (name == null)
+ {
+ next = json.remove(0);
+ if ((next == '"'))
+ {
+ if (specialChar)
+ {
+ specialChar = false;
+ }
+ else
+ {
+ name = nameBuilder.toString();
+ }
+ }
+ else if (next == '\\')
+ {
+ specialChar = true;
+ }
+ else
+ {
+ specialChar = false;
+ nameBuilder.append(next);
+ }
+ }
+ return name;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "\"" + name + "\":" + value.toString();
+ }
+
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONString.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONString.java
new file mode 100644
index 0000000..c341bba
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONString.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public class JSONString extends JSONValue
+{
+ private final String value;
+
+ public JSONString(String value)
+ {
+ this.value = value;
+ }
+
+ @Override
+ public String getValue()
+ {
+ return value;
+ }
+
+ public static JSONValue parseValues(List<Character> json)
+ {
+ String value = null;
+ boolean parsed = false;
+ while (!parsed)
+ {
+ Character next = json.get(0);
+ if (next == '"')
+ {
+
+ value = parseName(json);
+ parsed = true;
+ }
+ }
+
+ return new JSONString(value);
+ }
+
+ private static String parseName(List<Character> json)
+ {
+ String name = null;
+ StringBuilder nameBuilder = new StringBuilder();
+ boolean specialChar = false;
+ Character next;
+ json.remove(0);
+ while (name == null)
+ {
+ next = json.remove(0);
+ if (next == '"')
+ {
+ if (specialChar)
+ {
+ specialChar = false;
+ nameBuilder.append(next);
+ }
+ else
+ {
+ name = nameBuilder.toString();
+ }
+ }
+ else if (next == '\\')
+ {
+ specialChar = true;
+ }
+ else
+ {
+ specialChar = false;
+ nameBuilder.append(next);
+ }
+ }
+ return name;
+ }
+
+ @Override
+ public String toString()
+ {
+ return "\"" + value + "\"";
+ }
+
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONValue.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONValue.java
new file mode 100644
index 0000000..4738177
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONValue.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public abstract class JSONValue
+{
+ public abstract Object getValue();
+
+ static JSONValue parse(List<Character> json)
+ {
+ return null;
+ }
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/JSONValueParser.java b/src/plugins/translation/src/com/motorola/studio/android/json/JSONValueParser.java
new file mode 100644
index 0000000..816d90c
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/JSONValueParser.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.List;
+
+public class JSONValueParser
+{
+ private JSONValueParser()
+ {
+ };
+
+ static JSONValue parse(List<Character> json)
+ {
+ JSONValue value = null;
+
+ boolean parsed = false;
+
+ while (!parsed)
+ {
+ Character next = json.get(0);
+
+ if (next == '{')
+ {
+ value = JSONObject.parse(json);
+ parsed = true;
+ }
+ else if (next == '[')
+ {
+ value = JSONArray.parse(json);
+ parsed = true;
+ }
+ else if (next == '"')
+ {
+ value = JSONString.parse(json);
+ parsed = true;
+ }
+ else if (next == 'n')
+ {
+ value = JSONNull.parse(json);
+ parsed = true;
+ }
+ else if (next == 't' || next == 'f')
+ {
+ value = JSONBoolean.parse(json);
+ parsed = true;
+ }
+ else if (next >= 48 && next <= 57)
+ {
+ value = JSONNumber.parse(json);
+ parsed = true;
+ }
+ else if (next == ' ' || next == '\r' || next == '\n')
+ {
+ json.remove(0);
+ }
+
+ }
+ return value;
+ }
+}
diff --git a/src/plugins/translation/src/com/motorola/studio/android/json/Jason.java b/src/plugins/translation/src/com/motorola/studio/android/json/Jason.java
new file mode 100644
index 0000000..0b0b039
--- /dev/null
+++ b/src/plugins/translation/src/com/motorola/studio/android/json/Jason.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed 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 com.motorola.studio.android.json;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * This class is responsible to parse a JSON string into objects
+ * Visit {@link http://www.json.org/} for more information
+ *
+ */
+public class Jason
+{
+ private final Set<JSONObject> objects;
+
+ public Jason(String value)
+ {
+ objects = new HashSet<JSONObject>();
+ stip(value);
+ }
+
+ public Set<JSONObject> getJSON()
+ {
+ return objects;
+ }
+
+ private void stip(String value)
+ {
+ List<Character> json = new ArrayList<Character>();
+ for (char c : value.toCharArray())
+ {
+ json.add(c);
+ }
+
+ while (json.size() > 0)
+ {
+ Character next = json.get(0);
+
+ if (next == '{')
+ {
+ JSONObject object = (JSONObject) JSONObject.parse(json);
+ objects.add(object);
+ }
+ else if ((next == ' ') || (next == '\r') || (next == '\n') || (next == ','))
+ {
+ json.remove(0);
+ }
+ }
+ }
+
+ @Override
+ public String toString()
+ {
+ String string = "";
+ Iterator<JSONObject> objectIterator = objects.iterator();
+ while (objectIterator.hasNext())
+ {
+ string += objectIterator.next().toString();
+ if (objectIterator.hasNext())
+ {
+ string += ",";
+ }
+ }
+ return string;
+ }
+}