001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.wsclient.services;
021
022public class SourceQuery extends Query<Source> {
023  public static final String BASE_URL = "/api/sources";
024
025  private String resourceKeyOrId;
026  private int from = 0;
027  private int to = 0;
028  private boolean highlightedSyntax = false;
029
030  public SourceQuery(String resourceKeyOrId) {
031    this.resourceKeyOrId = resourceKeyOrId;
032  }
033
034  public String getResourceKeyOrId() {
035    return resourceKeyOrId;
036  }
037
038  public SourceQuery setResourceKeyOrId(String resourceKeyOrId) {
039    this.resourceKeyOrId = resourceKeyOrId;
040    return this;
041  }
042
043  public int getFrom() {
044    return from;
045  }
046
047  /**
048   * Get only a few lines
049   * 
050   * @param from Index of the first line, starts to 1
051   * @param excludedTo Index of the last line (excluded).
052   */
053  public SourceQuery setFromLineToLine(int from, int excludedTo) {
054    this.from = from;
055    this.to = excludedTo;
056    return this;
057  }
058
059  public SourceQuery setLinesFromLine(int from, int length) {
060    this.from = from;
061    this.to = from + length;
062    return this;
063  }
064
065  public int getTo() {
066    return to;
067  }
068
069  public boolean isHighlightedSyntax() {
070    return highlightedSyntax;
071  }
072
073  public SourceQuery setHighlightedSyntax(boolean b) {
074    this.highlightedSyntax = b;
075    return this;
076  }
077
078  @Override
079  public String getUrl() {
080    StringBuilder url = new StringBuilder(BASE_URL);
081    url.append('?');
082    appendUrlParameter(url, "resource", resourceKeyOrId);
083    if (from > 0 && to > 0) {
084      url.append("from=").append(from).append("&to=").append(to).append("&");
085    }
086    if (highlightedSyntax) {
087      url.append("color=true&");
088    }
089    return url.toString();
090  }
091
092  @Override
093  public Class<Source> getModelClass() {
094    return Source.class;
095  }
096
097  public static SourceQuery create(String resourceKeyOrId) {
098    return new SourceQuery(resourceKeyOrId);
099  }
100
101  public static SourceQuery createWithHighlightedSyntax(String resourceKeyOrId) {
102    return new SourceQuery(resourceKeyOrId).setHighlightedSyntax(true);
103  }
104}