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 public final class SourcesQuery extends AbstractResourceQuery<FileSource> {
032
033 private Integer from;
034 private Integer length;
035
036 public static final SourcesQuery get(String resourceKey) {
037 return new SourcesQuery(resourceKey);
038 }
039
040 private SourcesQuery(String resourceKey) {
041 super(resourceKey);
042 }
043
044 public SourcesQuery setFrom(Integer from) {
045 this.from = from;
046 return this;
047 }
048
049 public SourcesQuery setLength(Integer length) {
050 this.length = length;
051 return this;
052 }
053
054 @Override
055 public String toString() {
056 String url = Utils.getServerApiUrl() + "/sources?resource=" + getResourceKey() + "&";
057 if (length > 0) {
058 url += "from=" + from + "&to=" + (from + length) + "&";
059 }
060 return url;
061 }
062
063 @Override
064 public void execute(QueryCallBack<FileSource> callback) {
065 JsonUtils.requestJson(this.toString(), new JSONHandlerDispatcher<FileSource>(callback) {
066 @Override
067 public FileSource parseResponse(JavaScriptObject obj) {
068 return parseLines(obj);
069 }
070 });
071 }
072
073 private FileSource parseLines(JavaScriptObject obj) {
074 Map<Integer, String> sourceLines = new TreeMap<Integer, String>();
075 FileSource src = new FileSource(sourceLines);
076 JSONArray jsonArray = new JSONArray(obj);
077 if (jsonArray.size() == 0) return src;
078 JSONObject sources = jsonArray.get(0).isObject();
079 if (sources.size() == 0) return src;
080 int maxSize = new Double(Math.pow(2, 16)).intValue();
081 int currentLine = from == 0 ? 1 : from;
082 while (currentLine < maxSize) {
083 JSONValue line = sources.get(Integer.toString(currentLine));
084 if (line == null) {
085 break;
086 }
087 sourceLines.put(currentLine++, line.isString().stringValue());
088 }
089 return src;
090 }
091
092 }