PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/java/server/src/org/openqa/selenium/remote/server/handler/internal/ResultConverter.java

https://bitbucket.org/abahdanovich/selenium
Java | 79 lines | 50 code | 11 blank | 18 comment | 6 complexity | 8b01c9212f2288009189cc50d7d7577d MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-1.0, MIT, Apache-2.0, BSD-3-Clause, GPL-2.0
  1. /*
  2. Copyright 2007-2010 Selenium committers
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package org.openqa.selenium.remote.server.handler.internal;
  14. import com.google.common.base.Function;
  15. import com.google.common.collect.ImmutableMap;
  16. import com.google.common.collect.Iterables;
  17. import com.google.common.collect.Lists;
  18. import com.google.common.collect.Maps;
  19. import org.openqa.selenium.WebElement;
  20. import org.openqa.selenium.html5.ResultSet;
  21. import org.openqa.selenium.html5.ResultSetRows;
  22. import org.openqa.selenium.remote.server.KnownElements;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * Converts an object to be sent as JSON according to the wire protocol.
  27. */
  28. public class ResultConverter implements Function<Object, Object> {
  29. private final KnownElements knownElements;
  30. public ResultConverter(KnownElements knownElements) {
  31. this.knownElements = knownElements;
  32. }
  33. public Object apply(Object result) {
  34. if (result instanceof WebElement) {
  35. String elementId = knownElements.add((WebElement) result);
  36. return ImmutableMap.of("ELEMENT", elementId);
  37. }
  38. if (result instanceof List) {
  39. @SuppressWarnings("unchecked")
  40. List<Object> resultAsList = (List<Object>) result;
  41. return Lists.newArrayList(Iterables.transform(resultAsList, this));
  42. }
  43. if (result instanceof Map<?, ?>) {
  44. Map<?, ?> resultAsMap = (Map<?, ?>) result;
  45. Map<Object, Object> converted = Maps.newHashMapWithExpectedSize(resultAsMap.size());
  46. for (Map.Entry<?, ?> entry : resultAsMap.entrySet()) {
  47. converted.put(entry.getKey(), apply(entry.getValue()));
  48. }
  49. return converted;
  50. }
  51. if (result instanceof ResultSet) {
  52. Map<Object, Object> converted = Maps.newHashMap();
  53. converted.put("insertId", ((ResultSet) result).getLastInsertedRowId());
  54. converted.put("rowsAffected", ((ResultSet) result).getNumberOfRowsAffected());
  55. ResultSetRows rsRows = ((ResultSet) result).rows();
  56. List<Map<String, Object>> rows = Lists.newArrayList();
  57. for (int i = 0; i < rsRows.size(); i++) {
  58. rows.add(rsRows.item(i));
  59. }
  60. converted.put("rows", Lists.newArrayList(Iterables.transform(rows, this)));
  61. return converted;
  62. }
  63. return result;
  64. }
  65. }