You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonBuilderBase.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright 2011, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.client.builders;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. import com.google.gwt.core.client.JsArray;
  19. import com.google.gwt.query.client.IsProperties;
  20. import com.google.gwt.query.client.Properties;
  21. import com.google.gwt.query.client.js.JsCache;
  22. import com.google.gwt.query.client.js.JsObjectArray;
  23. import com.google.gwt.query.client.js.JsUtils;
  24. import com.google.gwt.user.client.Window;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. /**
  28. * Common class for all JsonBuilder implementations.
  29. *
  30. * @param <J>
  31. */
  32. public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements JsonBuilder {
  33. protected Properties p = Properties.create();
  34. protected String[] fieldNames = new String[] {};
  35. @SuppressWarnings("unchecked")
  36. @Override
  37. public J parse(String json) {
  38. return load(JsUtils.parseJSON(json));
  39. }
  40. @SuppressWarnings("unchecked")
  41. @Override
  42. public J parse(String json, boolean fix) {
  43. return fix ? parse(Properties.wrapPropertiesString(json)) : parse(json);
  44. }
  45. @SuppressWarnings("unchecked")
  46. @Override
  47. public J strip() {
  48. List<String> names = Arrays.asList(getFieldNames());
  49. for (String jsonName : p.getFieldNames()) {
  50. // TODO: figure out a way so as we can generate some marks in generated class in
  51. // order to call getters to return JsonBuilder object given an an attribute name
  52. if (!names.contains(jsonName)) {
  53. p.<JsCache>cast().delete(jsonName);
  54. }
  55. }
  56. return (J)this;
  57. }
  58. @SuppressWarnings("unchecked")
  59. @Override
  60. public J load(Object prp) {
  61. assert prp == null || prp instanceof JavaScriptObject || prp instanceof String;
  62. if (prp != null && prp instanceof String) {
  63. return parse((String) prp);
  64. }
  65. if (prp != null) {
  66. p = (Properties) prp;
  67. }
  68. return (J) this;
  69. }
  70. protected <T> void setArrayBase(String n, T[] r) {
  71. if (r.length > 0 && r[0] instanceof JsonBuilder) {
  72. JsArray<JavaScriptObject> a = JavaScriptObject.createArray().cast();
  73. for (T o : r) {
  74. a.push(((JsonBuilder) o).<Properties> getDataImpl());
  75. }
  76. p.set(n, a);
  77. } else {
  78. JsObjectArray<Object> a = JsObjectArray.create();
  79. a.add(r);
  80. p.set(n, a);
  81. }
  82. }
  83. @SuppressWarnings("unchecked")
  84. protected <T> T[] getArrayBase(String n, T[] r, Class<T> clazz) {
  85. JsObjectArray<?> a = p.getArray(n).cast();
  86. int l = r.length;
  87. for (int i = 0; i < l; i++) {
  88. Object w = a.get(i);
  89. Class<?> c = w.getClass();
  90. do {
  91. if (c.equals(clazz)) {
  92. r[i] = (T) w;
  93. break;
  94. }
  95. c = c.getSuperclass();
  96. } while (c != null);
  97. }
  98. return r;
  99. }
  100. protected Properties getPropertiesBase(String n) {
  101. if (p.getJavaScriptObject(n) == null) {
  102. p.set(n, Properties.create());
  103. }
  104. return p.getJavaScriptObject(n);
  105. }
  106. public String toString() {
  107. return p.tostring();
  108. }
  109. @Override
  110. public String toJson() {
  111. return p.tostring();
  112. }
  113. public String toJsonWithName() {
  114. return "{\"" + getJsonName() + "\":" + p.tostring() + "}";
  115. }
  116. @SuppressWarnings("unchecked")
  117. @Override
  118. public Properties getProperties() {
  119. return p;
  120. }
  121. @Override
  122. public String toQueryString() {
  123. return p.toQueryString();
  124. }
  125. @SuppressWarnings("unchecked")
  126. @Override
  127. public Properties getDataImpl() {
  128. return p;
  129. }
  130. public <T> T get(Object key) {
  131. return p.get(key);
  132. }
  133. @SuppressWarnings("unchecked")
  134. public <T extends IsProperties> T set(Object key, Object val) {
  135. if (val instanceof IsProperties) {
  136. p.set(key, ((IsProperties) val).getDataImpl());
  137. } else {
  138. p.set(key, val);
  139. }
  140. return (T) this;
  141. }
  142. public <T extends JsonBuilder> T as(Class<T> clz) {
  143. return p.as(clz);
  144. }
  145. public final String[] getFieldNames() {
  146. return fieldNames;
  147. }
  148. }