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