PageRenderTime 34ms CodeModel.GetById 22ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/main/java/com/google/sitebricks/compiler/AnalysisError.java

http://github.com/dhanji/sitebricks
Java | 104 lines | 71 code | 26 blank | 7 comment | 0 complexity | 5f9e1eba18aafa3b1b564abea058a5e9 MD5 | raw file
  1package com.google.sitebricks.compiler;
  2
  3import org.mvel2.ErrorDetail;
  4
  5/**
  6 * Represents a static analysis error or warning due to a
  7 * sitebricks static check failure.
  8 *
  9 * @author Dhanji R. Prasanna (dhanji@gmail.com)
 10 */
 11public abstract class AnalysisError {
 12
 13  public abstract CompileErrors getReason();
 14
 15  public static CompileErrorBuilder in(String fragment) {
 16    return new Builder(fragment);
 17  }
 18
 19  public static interface CompileErrorBuilder {
 20    CompileErrorBuilder near(int line);
 21
 22    AnalysisError causedBy(ExpressionCompileException e);
 23
 24    AnalysisError causedBy(CompileErrors reason);
 25
 26    AnalysisError causedBy(CompileErrors reason, ExpressionCompileException e);
 27
 28    AnalysisError causedBy(CompileErrors reason, String cause);
 29  }
 30
 31  private static class Builder implements CompileErrorBuilder {
 32    private final String fragment;
 33    private int line;
 34
 35    private Builder(String fragment) {
 36      this.fragment = fragment;
 37    }
 38
 39    public CompileErrorBuilder near(int line) {
 40      this.line = line;
 41
 42      return this;
 43    }
 44
 45    public AnalysisError causedBy(ExpressionCompileException e) {
 46      return new AnalysisErrorImpl(fragment, line, e.getError());
 47    }
 48
 49    public AnalysisError causedBy(CompileErrors reason) {
 50      return new AnalysisErrorImpl(fragment, line, reason);
 51    }
 52
 53    public AnalysisError causedBy(CompileErrors reason, ExpressionCompileException e) {
 54      return new AnalysisErrorImpl(fragment, line, e.getError(), reason);
 55    }
 56
 57    public AnalysisError causedBy(CompileErrors reason, String cause) {
 58      return new AnalysisErrorImpl(fragment, line,
 59          new EvaluatorCompiler.CompileErrorDetail(cause, new ErrorDetail(fragment.toCharArray(), line, true, cause)), reason);
 60    }
 61  }
 62
 63  private static class AnalysisErrorImpl extends AnalysisError {
 64    private final String fragment;
 65    private final int line;
 66    private final EvaluatorCompiler.CompileErrorDetail error;
 67    private final CompileErrors reason;
 68
 69    public AnalysisErrorImpl(String fragment, int line, EvaluatorCompiler.CompileErrorDetail error) {
 70      this.fragment = fragment;
 71      this.line = line;
 72      this.error = error;
 73
 74      this.reason = CompileErrors.ILLEGAL_EXPRESSION;
 75    }
 76
 77    public AnalysisErrorImpl(String fragment, int line, CompileErrors reason) {
 78      this.fragment = fragment;
 79      this.line = line;
 80      this.reason = reason;
 81
 82      this.error = null;
 83    }
 84
 85    public AnalysisErrorImpl(String fragment, int line, EvaluatorCompiler.CompileErrorDetail error,
 86                            CompileErrors reason) {
 87
 88      this.fragment = fragment;
 89      this.line = line;
 90      this.error = error;
 91      this.reason = reason;
 92    }
 93
 94    @Override
 95    public String toString() {
 96      //TODO make this nicer?
 97      return reason.toString();
 98    }
 99
100    public CompileErrors getReason() {
101      return null;
102    }
103  }
104}