001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.jci.compilers;
019    
020    import org.apache.commons.jci.problems.CompilationProblem;
021    import org.codehaus.groovy.control.messages.ExceptionMessage;
022    import org.codehaus.groovy.control.messages.Message;
023    import org.codehaus.groovy.control.messages.SimpleMessage;
024    import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
025    import org.codehaus.groovy.syntax.SyntaxException;
026    
027    /**
028     * Groovy version of a CompilationProblem
029     * 
030     * @author tcurdt
031     */
032    public final class GroovyCompilationProblem implements CompilationProblem {
033    
034        private final String fileName;
035        private final String message;
036        private final boolean error;
037        private final int startLine;
038        private final int startColumn;
039        private final int endLine;
040        private final int endColumn;
041    
042        public GroovyCompilationProblem(final Message pMessage) {
043            if (pMessage instanceof SimpleMessage) {
044                error = false;
045            } else {
046                error = true;
047            }
048            if (pMessage instanceof SyntaxErrorMessage) {
049                SyntaxErrorMessage syntaxErrorMessage = (SyntaxErrorMessage)pMessage;
050                SyntaxException syntaxException = syntaxErrorMessage.getCause();
051                message = syntaxException.getMessage();
052                fileName = syntaxException.getSourceLocator();
053                // FIXME: getStartLine() vs. getLine()
054                startLine = syntaxException.getStartLine();
055                startColumn = syntaxException.getStartColumn();
056                endLine = syntaxException.getLine();
057                endColumn = syntaxException.getEndColumn();
058            } else {
059                fileName = "";
060                startLine = 0;
061                startColumn = 0;
062                endLine = 0;
063                endColumn = 0;
064                if (pMessage instanceof ExceptionMessage) {
065                    message = ((ExceptionMessage)pMessage).getCause().getMessage();
066                } else if (pMessage instanceof SimpleMessage) {
067                    message = ((SimpleMessage)pMessage).getMessage();
068                } else {
069                    message = pMessage.toString();
070                }
071            }
072        }
073    
074        public boolean isError() {
075            return error;
076        }
077    
078        public String getFileName() {
079            return fileName;
080        }
081    
082        public int getStartLine() {
083            return startLine;
084        }
085    
086        public int getStartColumn() {
087            return startColumn;
088        }
089    
090        public int getEndLine() {
091            return endLine;
092        }
093    
094        public int getEndColumn() {
095            return endColumn;
096        }
097    
098        public String getMessage() {
099            return message;
100        }
101    
102        @Override
103        public String toString() {
104            return getMessage();
105        }
106    
107    }