001: /*
002: * Copyright (C) 2005-2007 JasperSoft http://www.jaspersoft.com
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed WITHOUT ANY WARRANTY; and without the
010: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: * See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
015: * or write to:
016: *
017: * Free Software Foundation, Inc.,
018: * 59 Temple Place - Suite 330,
019: * Boston, MA USA 02111-1307
020: *
021: *
022: * ExtendedJRJdtCompiler.java
023: *
024: * Created on March 14, 2007, 2:28 PM
025: *
026: * To change this template, choose Tools | Template Manager
027: * and open the template in the editor.
028: */
029:
030: package it.businesslogic.ireport.compiler;
031:
032: import it.businesslogic.ireport.compiler.xml.SourceLocation;
033: import it.businesslogic.ireport.compiler.xml.SourceTraceDigester;
034: import java.util.HashMap;
035: import java.util.HashSet;
036: import java.util.Map;
037: import java.util.Set;
038: import net.sf.jasperreports.engine.JRExpression;
039: import net.sf.jasperreports.engine.design.JRCompilationSourceCode;
040: import net.sf.jasperreports.engine.design.JRCompilationUnit;
041: import net.sf.jasperreports.engine.design.JRJdtCompiler;
042: import org.eclipse.jdt.core.compiler.IProblem;
043: import org.eclipse.jdt.internal.compiler.CompilationResult;
044: import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
045:
046: /**
047: *
048: * @author gtoffoli
049: */
050: public class ExtendedJRJdtCompiler extends JRJdtCompiler {
051:
052: private JasperReportErrorHandler errorHandler = null;
053: private SourceTraceDigester digester = null;
054: private ICompilerRequestor super CompilerRequestor = null;
055:
056: protected ICompilerRequestor getCompilerRequestor(
057: JRCompilationUnit[] units, StringBuffer problemBuffer) {
058: return new CompilerRequestor(super .getCompilerRequestor(units,
059: problemBuffer), units);
060: }
061:
062: protected class CompilerRequestor implements ICompilerRequestor {
063: private ICompilerRequestor super CompilerRequestor = null;
064: private final Map expressionErrors = new HashMap();
065: private final JRCompilationUnit[] units;
066:
067: protected CompilerRequestor(
068: ICompilerRequestor super CompilerRequestor,
069: JRCompilationUnit[] units) {
070: this .super CompilerRequestor = super CompilerRequestor;
071: this .units = units;
072: }
073:
074: public void acceptResult(CompilationResult result) {
075: if (super CompilerRequestor != null)
076: super CompilerRequestor.acceptResult(result);
077: if (result.hasErrors()) {
078: String className = String.valueOf(result
079: .getCompilationUnit().getMainTypeName());
080:
081: JRCompilationUnit unit = null;
082: for (int classIdx = 0; classIdx < units.length; ++classIdx) {
083: if (className.equals(units[classIdx].getName())) {
084: unit = units[classIdx];
085: break;
086: }
087: }
088:
089: IProblem[] errors = result.getErrors();
090: for (int i = 0; i < errors.length; i++) {
091: IProblem problem = errors[i];
092: int line = problem.getSourceLineNumber();
093: JRCompilationSourceCode sourceCode = unit
094: .getCompilationSource();
095: JRExpression expression = sourceCode
096: .getExpressionAtLine(line);
097: if (expression == null) {
098: getErrorHandler().addMarker(problem, null);
099: } else if (addExpressionError(expression, problem)) {
100: SourceLocation location = getDigester()
101: .getLocation(expression);
102: getErrorHandler().addMarker(problem, location);
103: }
104: }
105: }
106: }
107:
108: protected boolean addExpressionError(JRExpression expression,
109: IProblem problem) {
110: Set errors = (Set) expressionErrors.get(expression);
111: if (errors == null) {
112: errors = new HashSet();
113: expressionErrors.put(expression, errors);
114: }
115: return errors.add(new ExpressionErrorKey(problem));
116: }
117: }
118:
119: protected static class ExpressionErrorKey {
120: private final IProblem problem;
121: private final int hash;
122:
123: public ExpressionErrorKey(IProblem problem) {
124: this .problem = problem;
125: this .hash = computeHash();
126: }
127:
128: private int computeHash() {
129: int h = problem.getMessage().hashCode();
130: return h;
131: }
132:
133: public int hashCode() {
134: return hash;
135: }
136:
137: public boolean equals(Object o) {
138: if (o == null || !(o instanceof ExpressionErrorKey)
139: || this .hash != o.hashCode()) {
140: return false;
141: }
142:
143: if (this == o) {
144: return true;
145: }
146:
147: ExpressionErrorKey k = (ExpressionErrorKey) o;
148: return problem.getMessage().equals(k.problem.getMessage());
149: }
150: }
151:
152: public JasperReportErrorHandler getErrorHandler() {
153: return errorHandler;
154: }
155:
156: public void setErrorHandler(JasperReportErrorHandler errorHandler) {
157: this .errorHandler = errorHandler;
158: }
159:
160: public SourceTraceDigester getDigester() {
161: return digester;
162: }
163:
164: public void setDigester(SourceTraceDigester digester) {
165: this.digester = digester;
166: }
167: }
|