001    package railo.transformer.cfml.tag;
002    
003    import railo.commons.lang.StringUtil;
004    import railo.runtime.SourceFile;
005    import railo.runtime.op.Caster;
006    import railo.transformer.util.CFMLString;
007    
008    /**
009     * Die Klasse TemplateException wird durch den CFMLTransformer geworfen, 
010     * wenn dieser auf einen grammatikalischen Fehler in dem zu verarbeitenden CFML Code st�sst 
011     * oder wenn ein Tag oder eine Funktion von der 
012     * Definition innerhalb der Tag- bzw. der Funktions- Library abweicht.
013     */
014    public final class CFMLTransformerException extends Exception {
015            private CFMLString cfml;
016            //private String htmlMessage;
017            
018            /**
019             * Konstruktor mit einem CFMLString und einer anderen Exception.
020             * @param cfml
021             * @param e
022             */
023            public CFMLTransformerException(CFMLString cfml, Exception e) {
024                    this(
025                                    cfml,
026                                    StringUtil.isEmpty(e.getMessage())?
027                                                    (Caster.toClassName(e)):
028                                                    e.getMessage());
029            }
030            
031            /**
032             * Konstruktor ohne Message, nur mit CFMLString.
033             * @param cfml
034             
035            public TemplateException(CFMLString cfml) {
036                    this(cfml,"Error while transforming CFML File");
037            }*/
038            
039            /**
040             * Hauptkonstruktor, mit CFMLString und message.
041             * @param cfml CFMLString
042             * @param message Fehlermeldung
043             */
044            public CFMLTransformerException(CFMLString cfml,String message) {
045                    super(message);
046                    this.cfml=cfml;
047                    
048            }
049    
050            /**
051             * Gibt eine detaillierte Fehlermeldung zur�ck.
052             * �berschreibt toString Methode von java.lang.Objekt, alias f�r getMessage().
053             * @return Fehlermeldung als Plain Text Ausgabe
054             */
055            public String toString()        {
056                    boolean hasCFML=cfml!=null;
057                    StringBuffer sb=new StringBuffer();
058                    sb.append("Error\n");
059                    sb.append("----------------------------------\n");
060                    if(hasCFML && cfml.getSourceFile()!=null) {
061                            sb.append("File: "+cfml.getSourceFile().getDisplayPath()+"\n");
062                    }
063                    if(hasCFML) {
064                            int line=cfml.getLine();
065                            
066                            int counter=0;
067                            sb.append("Line: "+line+"\n");
068                            sb.append("Column: "+cfml.getColumn()+"\n");
069                            sb.append("Type: Syntax\n");
070                            sb.append("Code Outprint: \n");
071                            line=(line-2<1)?1:line-2;
072                            int lineDescLen=(((line+5)+"").length());
073                            for(int i=line;;i++) {
074                                    if(i>0) {
075                                            String strLine=cfml.getLineAsString(i);
076                                            if(strLine==null)break;
077                                            String desc=((""+i).length()<lineDescLen)?"0"+i:""+i;
078                                            sb.append(desc+": "+strLine+"\n");
079                                            counter++;
080                                    }
081                                    if(counter==5) break;
082                            }
083                            sb.append("\n");
084                    }
085                    sb.append("Message:\n");
086                    sb.append(""+super.getMessage()+"\n");
087                    return sb.toString();
088            }
089            
090            /* *
091             * Gibt eine detaillierte Fehlermeldung als HTML Ausgabe zur�ck.
092             * @return Fehlermeldung als HTML Ausgabe.
093             * /
094            public String getMessageAsHTML()        {
095                    boolean hasCFML=cfml!=null;
096                    
097                    String str=HTMLOutput.getStyle("fnf","#ff4400","#ff954f","#4f1500");
098    
099                    str+=HTMLOutput.getHead("fnf","Railo - ParserException");
100                    if(hasCFML && cfml.getSourceFile()!=null) {
101                            str+=HTMLOutput.getItem("fnf","File",cfml.getSourceFile().getDisplayPath());
102                    }
103                    if(hasCFML) {
104                            int line=cfml.getLine();
105                            str+=HTMLOutput.getItem("fnf","Line", line+"");
106                            str+=HTMLOutput.getItem("fnf","Column", cfml.getColumn()+"");
107                            str+=HTMLOutput.getItem("fnf","Type", "Syntax");
108                            int failureLine=line;
109                            line=(line-2<1)?1:line-2;
110                            int lineDescLen=(((line+5)+"").length());
111                            int counter=0;
112                            StringBuffer sb=new StringBuffer();
113                            for(int i=line;;i++) {
114                                    if(i>0) {
115                                            String strLine=cfml.getLineAsString(i);
116                                            if(strLine==null)break;
117                                            String desc=((""+i).length()<lineDescLen)?"0"+i:""+i;
118                                            sb.append(desc+": ");
119                                            if(i==failureLine)sb.append("<b>");
120                                            sb.append((strLine.replaceAll("<","&lt;").replaceAll(">","&gt;")));
121                                            if(i==failureLine)sb.append("</b>");
122                                            sb.append("\n");
123                                            counter++;
124                                    }
125                                    if(counter==5) break;
126                            }
127                            str+=HTMLOutput.getItem("fnf","Code","<pre>"+sb+"</pre>");
128                    }
129                    str+=HTMLOutput.getItem("fnf","Message",StringUtil.replace(super.getMessage(),"\n","<br>",false));
130                    str+=HTMLOutput.getBottom();
131                    
132                    return str;
133            }*/
134    
135            /**
136             * Gibt die Zeilennummer zur�ck
137             * @return Zeilennummer
138             */
139            public int getLine() {
140                    return cfml.getLine();
141            }
142            
143            /**
144             * Gibt die Column der aktuellen Zeile zur�ck
145             * @return Column der Zeile
146             */
147            public int getColumn() {
148                    return cfml.getColumn();
149            }
150            
151            /**
152             * Source Dokument
153             * @return Source Dokument
154             */
155            public SourceFile getSource() {
156                    return cfml.getSourceFile();
157            }
158    
159        /**
160         * Returns the value of cfml.
161         * @return value cfml
162         */
163        public CFMLString getCfml() {
164            return cfml;
165        }
166    
167    
168    
169    }