001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.web.gwt.client.webservices;
021
022 import com.google.gwt.core.client.JavaScriptObject;
023 import com.google.gwt.json.client.JSONArray;
024 import com.google.gwt.json.client.JSONObject;
025 import com.google.gwt.json.client.JSONValue;
026 import org.sonar.api.web.gwt.client.Utils;
027
028 import java.util.Map;
029 import java.util.TreeMap;
030
031 /**
032 * @deprecated since 2.5, use {@link org.sonar.wsclient.services.SourceQuery} instead
033 */
034 @Deprecated
035 public final class SourcesQuery extends AbstractResourceQuery<FileSource> {
036
037 private Integer from;
038 private Integer length;
039 private boolean color;
040
041 public static SourcesQuery get(String resourceKey) {
042 return new SourcesQuery(resourceKey);
043 }
044
045 private SourcesQuery(String resourceKey) {
046 super(resourceKey);
047 }
048
049 public SourcesQuery setFrom(Integer from) {
050 this.from = from;
051 return this;
052 }
053
054 public SourcesQuery setLength(Integer length) {
055 this.length = length;
056 return this;
057 }
058
059 public SourcesQuery setColor(boolean color) {
060 this.color = color;
061 return this;
062 }
063
064 @Override
065 public String toString() {
066 String url = Utils.getServerApiUrl() + "/sources?resource=" + getResourceKey() + "&";
067 if (length > 0) {
068 url += "from=" + from + "&to=" + (from + length) + "&";
069 }
070 if (color) {
071 url += "color=true&";
072 }
073 return url;
074 }
075
076 @Override
077 public void execute(QueryCallBack<FileSource> callback) {
078 JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<FileSource>(callback) {
079 @Override
080 public FileSource parseResponse(JavaScriptObject obj) {
081 return parseLines(obj);
082 }
083 });
084 }
085
086 private FileSource parseLines(JavaScriptObject obj) {
087 Map<Integer, String> sourceLines = new TreeMap<Integer, String>();
088 FileSource src = new FileSource(sourceLines);
089 JSONArray jsonArray = new JSONArray(obj);
090 if (jsonArray.size() == 0)
091 return src;
092 JSONObject sources = jsonArray.get(0).isObject();
093 if (sources.size() == 0)
094 return src;
095 int maxSize = new Double(Math.pow(2, 16)).intValue();
096 int currentLine = from == 0 ? 1 : from;
097 while (currentLine < maxSize) {
098 JSONValue line = sources.get(Integer.toString(currentLine));
099 if (line == null) {
100 break;
101 }
102 sourceLines.put(currentLine++, line.isString().stringValue());
103 }
104 return src;
105 }
106
107 }