001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.scan.filesystem.internal;
021
022import org.sonar.api.utils.PathUtils;
023
024import javax.annotation.Nullable;
025import java.io.File;
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * FOR UNIT-TESTING ONLY
031 *
032 * @since 4.0
033 */
034public class InputFileBuilder {
035
036  private final Map<String, String> attributes = new HashMap<String, String>();
037  private final File file;
038  private final String relativePath;
039
040  public static void _FOR_UNIT_TESTING_ONLY_() {
041    // For those who don't read javadoc
042  }
043
044  public InputFileBuilder(File file, String relativePath) {
045    this.file = file;
046    this.relativePath = relativePath;
047  }
048
049  public InputFileBuilder attribute(String key, @Nullable String value) {
050    if (value != null) {
051      attributes.put(key, value);
052    }
053    return this;
054  }
055
056  public InputFileBuilder type(@Nullable String type) {
057    return attribute(InputFile.ATTRIBUTE_TYPE, type);
058  }
059
060  public InputFileBuilder language(@Nullable String language) {
061    return attribute(InputFile.ATTRIBUTE_LANGUAGE, language);
062  }
063
064  public InputFileBuilder hash(@Nullable String hash) {
065    return attribute(InputFile.ATTRIBUTE_HASH, hash);
066  }
067
068  public InputFileBuilder status(@Nullable String status) {
069    return attribute(InputFile.ATTRIBUTE_STATUS, status);
070  }
071
072  public InputFileBuilder sourceDir(File dir) {
073    return attribute(InputFile.ATTRIBUTE_SOURCEDIR_PATH, PathUtils.canonicalPath(dir));
074  }
075
076  public InputFileBuilder sourceDir(@Nullable String path) {
077    return attribute(InputFile.ATTRIBUTE_SOURCEDIR_PATH, PathUtils.sanitize(path));
078  }
079
080  public DefaultInputFile build() {
081    return DefaultInputFile.create(file, relativePath, attributes);
082  }
083}