/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

  1. package com.google.sitebricks.compiler;
  2. import org.mvel2.ErrorDetail;
  3. /**
  4. * Represents a static analysis error or warning due to a
  5. * sitebricks static check failure.
  6. *
  7. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  8. */
  9. public abstract class AnalysisError {
  10. public abstract CompileErrors getReason();
  11. public static CompileErrorBuilder in(String fragment) {
  12. return new Builder(fragment);
  13. }
  14. public static interface CompileErrorBuilder {
  15. CompileErrorBuilder near(int line);
  16. AnalysisError causedBy(ExpressionCompileException e);
  17. AnalysisError causedBy(CompileErrors reason);
  18. AnalysisError causedBy(CompileErrors reason, ExpressionCompileException e);
  19. AnalysisError causedBy(CompileErrors reason, String cause);
  20. }
  21. private static class Builder implements CompileErrorBuilder {
  22. private final String fragment;
  23. private int line;
  24. private Builder(String fragment) {
  25. this.fragment = fragment;
  26. }
  27. public CompileErrorBuilder near(int line) {
  28. this.line = line;
  29. return this;
  30. }
  31. public AnalysisError causedBy(ExpressionCompileException e) {
  32. return new AnalysisErrorImpl(fragment, line, e.getError());
  33. }
  34. public AnalysisError causedBy(CompileErrors reason) {
  35. return new AnalysisErrorImpl(fragment, line, reason);
  36. }
  37. public AnalysisError causedBy(CompileErrors reason, ExpressionCompileException e) {
  38. return new AnalysisErrorImpl(fragment, line, e.getError(), reason);
  39. }
  40. public AnalysisError causedBy(CompileErrors reason, String cause) {
  41. return new AnalysisErrorImpl(fragment, line,
  42. new EvaluatorCompiler.CompileErrorDetail(cause, new ErrorDetail(fragment.toCharArray(), line, true, cause)), reason);
  43. }
  44. }
  45. private static class AnalysisErrorImpl extends AnalysisError {
  46. private final String fragment;
  47. private final int line;
  48. private final EvaluatorCompiler.CompileErrorDetail error;
  49. private final CompileErrors reason;
  50. public AnalysisErrorImpl(String fragment, int line, EvaluatorCompiler.CompileErrorDetail error) {
  51. this.fragment = fragment;
  52. this.line = line;
  53. this.error = error;
  54. this.reason = CompileErrors.ILLEGAL_EXPRESSION;
  55. }
  56. public AnalysisErrorImpl(String fragment, int line, CompileErrors reason) {
  57. this.fragment = fragment;
  58. this.line = line;
  59. this.reason = reason;
  60. this.error = null;
  61. }
  62. public AnalysisErrorImpl(String fragment, int line, EvaluatorCompiler.CompileErrorDetail error,
  63. CompileErrors reason) {
  64. this.fragment = fragment;
  65. this.line = line;
  66. this.error = error;
  67. this.reason = reason;
  68. }
  69. @Override
  70. public String toString() {
  71. //TODO make this nicer?
  72. return reason.toString();
  73. }
  74. public CompileErrors getReason() {
  75. return null;
  76. }
  77. }
  78. }