001: package org.drools.compiler;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.drools.commons.jci.problems.CompilationProblem;
020: import org.drools.lang.descr.BaseDescr;
021: import org.drools.rule.Rule;
022:
023: public class RuleError extends DroolsError {
024: private Rule rule;
025: private BaseDescr descr;
026: private Object object;
027: private String message;
028: private int[] errorLines = new int[0];
029:
030: public RuleError(final Rule rule, final BaseDescr descr,
031: final Object object, final String message) {
032: super ();
033: this .rule = rule;
034: this .descr = descr;
035: this .object = object;
036: this .message = message;
037: }
038:
039: public Rule getRule() {
040: return this .rule;
041: }
042:
043: public BaseDescr getDescr() {
044: return this .descr;
045: }
046:
047: public Object getObject() {
048: return this .object;
049: }
050:
051: public int[] getErrorLines() {
052: return this .errorLines;
053: }
054:
055: /**
056: * This will return the line number of the error, if possible
057: * Otherwise it will be -1
058: */
059: public int getLine() {
060: return this .descr != null ? this .descr.getLine() : -1;
061: }
062:
063: public String getMessage() {
064: String summary = this .message;
065: if (this .object instanceof CompilationProblem[]) {
066: final CompilationProblem[] problem = (CompilationProblem[]) this .object;
067: for (int i = 0; i < problem.length; i++) {
068: if (i != 0) {
069: summary = summary + "\n" + problem[i].getMessage();
070: } else {
071: summary = summary + " " + problem[i].getMessage();
072: }
073: }
074:
075: }
076: return summary;
077: }
078:
079: public String toString() {
080: final StringBuffer buf = new StringBuffer();
081: buf.append(this .message);
082: buf.append(" : ");
083: buf.append(this .rule);
084: buf.append("\n");
085: if (this .object instanceof CompilationProblem[]) {
086: final CompilationProblem[] problem = (CompilationProblem[]) this .object;
087: for (int i = 0; i < problem.length; i++) {
088: buf.append("\t");
089: buf.append(problem[i]);
090: buf.append("\n");
091: }
092: } else if (this .object != null) {
093: buf.append(this .object);
094: }
095: return buf.toString();
096: }
097:
098: // private String createMessage( String message ) {
099: // StringBuffer detail = new StringBuffer();
100: // detail.append( this.message );
101: // detail.append( " : " );
102: // detail.append( this.rule );
103: // detail.append( "\n" );
104: // if( object instanceof CompilationProblem[] ) {
105: // CompilationProblem[] cp = (CompilationProblem[]) object;
106: // this.errorLines = new int[cp.length];
107: // for( int i = 0; i < cp.length ; i ++ ) {
108: // this.errorLines[i] = cp[i].getStartLine() - this.descr.getOffset() + this.descr.getLine() - 1;
109: // detail.append( this.rule.getName() );
110: // detail.append( " (line:" );
111: // detail.append( this.errorLines[i] );
112: // detail.append( "): " );
113: // detail.append( cp[i].getMessage() );
114: // detail.append( "\n" );
115: // }
116: // } else {
117: // this.errorLines = new int[0];
118: // }
119: // return "[ "+this.rule.getName()+" : "+message + "\n"+detail.toString()+" ]";
120: // }
121:
122: }
|