001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.cfml.tag;
020
021import lucee.commons.lang.StringUtil;
022import lucee.runtime.PageSource;
023import lucee.runtime.op.Caster;
024import lucee.transformer.util.CFMLString;
025
026/**
027 * Die Klasse TemplateException wird durch den CFMLTransformer geworfen, 
028 * wenn dieser auf einen grammatikalischen Fehler in dem zu verarbeitenden CFML Code stoesst 
029 * oder wenn ein Tag oder eine Funktion von der 
030 * Definition innerhalb der Tag- bzw. der Funktions- Library abweicht.
031 */
032public final class CFMLTransformerException extends Exception {
033        private CFMLString cfml;
034        //private String htmlMessage;
035        
036        /**
037         * Konstruktor mit einem CFMLString und einer anderen Exception.
038         * @param cfml
039         * @param e
040         */
041        public CFMLTransformerException(CFMLString cfml, Exception e) {
042                this(
043                                cfml,
044                                StringUtil.isEmpty(e.getMessage())?
045                                                (Caster.toClassName(e)):
046                                                e.getMessage());
047        }
048        
049        /**
050         * Konstruktor ohne Message, nur mit CFMLString.
051         * @param cfml
052         
053        public TemplateException(CFMLString cfml) {
054                this(cfml,"Error while transforming CFML File");
055        }*/
056        
057        /**
058         * Hauptkonstruktor, mit CFMLString und message.
059         * @param cfml CFMLString
060         * @param message Fehlermeldung
061         */
062        public CFMLTransformerException(CFMLString cfml,String message) {
063                super(message);
064                this.cfml=cfml;
065                
066        }
067
068        /**
069         * Gibt eine detaillierte Fehlermeldung zurueck.
070         * ueberschreibt toString Methode von java.lang.Objekt, alias fuer getMessage().
071         * @return Fehlermeldung als Plain Text Ausgabe
072         */
073        public String toString()        {
074                boolean hasCFML=cfml!=null;
075                StringBuffer sb=new StringBuffer();
076                sb.append("Error\n");
077                sb.append("----------------------------------\n");
078                if(hasCFML && cfml.getPageSource()!=null) {
079                        sb.append("File: "+cfml.getPageSource().getDisplayPath()+"\n");
080                }
081                if(hasCFML) {
082                        int line=cfml.getLine();
083                        
084                        int counter=0;
085                        sb.append("Line: "+line+"\n");
086                        sb.append("Column: "+cfml.getColumn()+"\n");
087                        sb.append("Type: Syntax\n");
088                        sb.append("Code Outprint: \n");
089                        line=(line-2<1)?1:line-2;
090                        int lineDescLen=(((line+5)+"").length());
091                        for(int i=line;;i++) {
092                                if(i>0) {
093                                        String strLine=cfml.getLineAsString(i);
094                                        if(strLine==null)break;
095                                        String desc=((""+i).length()<lineDescLen)?"0"+i:""+i;
096                                        sb.append(desc+": "+strLine+"\n");
097                                        counter++;
098                                }
099                                if(counter==5) break;
100                        }
101                        sb.append("\n");
102                }
103                sb.append("Message:\n");
104                sb.append(""+super.getMessage()+"\n");
105                return sb.toString();
106        }
107
108        /**
109         * Gibt die Zeilennummer zurueck
110         * @return Zeilennummer
111         */
112        public int getLine() {
113                return cfml.getLine();
114        }
115        
116        /**
117         * Gibt die Column der aktuellen Zeile zurueck
118         * @return Column der Zeile
119         */
120        public int getColumn() {
121                return cfml.getColumn();
122        }
123        
124        /**
125         * Source Dokument
126         * @return Source Dokument
127         */
128        public PageSource getSource() {
129                return cfml.getPageSource();
130        }
131
132    /**
133     * Returns the value of cfml.
134     * @return value cfml
135     */
136    public CFMLString getCfml() {
137        return cfml;
138    }
139
140
141
142}