001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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     */
020    package org.sonar.squid.api;
021    
022    import org.apache.commons.lang.builder.ToStringBuilder;
023    import org.sonar.check.Message;
024    
025    import java.text.MessageFormat;
026    import java.util.Locale;
027    
028    public class CheckMessage implements Message {
029    
030      private Integer line;
031      private Double cost;
032      private SourceCode sourceCode;
033      private Object check;
034      private String defaultMessage;
035      private Object[] messageArguments;
036      private Boolean bypassExclusion;
037    
038      public CheckMessage(Object check, String message, Object... messageArguments) {
039        this.check = check;
040        this.defaultMessage = message;
041        this.messageArguments = messageArguments;
042      }
043    
044      /**
045       * @deprecated replaced by the other constructor since 2.12. See SONAR-2875.
046       */
047      @Deprecated
048      public CheckMessage(CodeCheck check, String message, Object... messageArguments) {
049        this((Object)check, message, messageArguments);
050      }
051    
052      public void setSourceCode(SourceCode sourceCode) {
053        this.sourceCode = sourceCode;
054      }
055    
056      public SourceCode getSourceCode() {
057        return sourceCode;
058      }
059    
060      public void setLine(int line) {
061        this.line = line;
062      }
063    
064      public Integer getLine() {
065        return line;
066      }
067    
068      public void setCost(double cost) {
069        this.cost = cost;
070      }
071    
072      public Double getCost() {
073        return cost;
074      }
075    
076      public void setBypassExclusion(boolean bypassExclusion) {
077        this.bypassExclusion = bypassExclusion;
078      }
079    
080      public boolean isBypassExclusion() {
081        return bypassExclusion == null ? false : bypassExclusion;
082      }
083    
084      /**
085       * @deprecated replaced by getCheck() since 2.12. Warning, to be called only if check is CodeCheck.
086       */
087      @Deprecated
088      public CodeCheck getChecker() {
089        return (CodeCheck)check;
090      }
091    
092      public Object getCheck() {
093        return check;
094      }
095    
096      public String getDefaultMessage() {
097        return defaultMessage;
098      }
099    
100      public Object[] getMessageArguments() {
101        return messageArguments;
102      }
103    
104      public String getText(Locale locale) {
105        return formatDefaultMessage();
106      }
107    
108      @Override
109      public String toString() {
110        return new ToStringBuilder(this).append("source", sourceCode).append("check", check).append("msg", defaultMessage)
111            .append("line", line).toString();
112      }
113    
114      public String formatDefaultMessage() {
115        if (messageArguments.length == 0) {
116          return defaultMessage;
117        } else {
118          return MessageFormat.format(defaultMessage, messageArguments);
119        }
120      }
121    
122    }