| 1 | /* |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is Rhino code, released |
| 17 | * May 6, 1999. |
| 18 | * |
| 19 | * The Initial Developer of the Original Code is |
| 20 | * Netscape Communications Corporation. |
| 21 | * Portions created by the Initial Developer are Copyright (C) 1997-1999 |
| 22 | * the Initial Developer. All Rights Reserved. |
| 23 | * |
| 24 | * Contributor(s): |
| 25 | * Bob Jervis |
| 26 | * Google Inc. |
| 27 | * |
| 28 | * Alternatively, the contents of this file may be used under the terms of |
| 29 | * the GNU General Public License Version 2 or later (the "GPL"), in which |
| 30 | * case the provisions of the GPL are applicable instead of those above. If |
| 31 | * you wish to allow use of your version of this file only under the terms of |
| 32 | * the GPL and not to allow others to use your version of this file under the |
| 33 | * MPL, indicate your decision by deleting the provisions above and replacing |
| 34 | * them with the notice and other provisions required by the GPL. If you do |
| 35 | * not delete the provisions above, a recipient may use your version of this |
| 36 | * file under either the MPL or the GPL. |
| 37 | * |
| 38 | * ***** END LICENSE BLOCK ***** */ |
| 39 | |
| 40 | package com.google.javascript.rhino; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.List; |
| 44 | |
| 45 | /** |
| 46 | * A simple {@link ErrorReporter} that collects warnings and errors and makes |
| 47 | * them accessible via {@link #errors()} and {@link #warnings()}. |
| 48 | * |
| 49 | */ |
| 50 | public class SimpleErrorReporter implements ErrorReporter { |
| 51 | private List<String> warnings = null; |
| 52 | private List<String> errors = null; |
| 53 | |
| 54 | @Override |
| 55 | public void warning(String message, String sourceName, int line, |
| 56 | int lineOffset) { |
| 57 | if (warnings == null) { |
| 58 | warnings = new ArrayList<String>(); |
| 59 | } |
| 60 | warnings.add(formatDetailedMessage(message, sourceName, line)); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void error(String message, String sourceName, int line, |
| 65 | int lineOffset) { |
| 66 | if (errors == null) { |
| 67 | errors = new ArrayList<String>(); |
| 68 | } |
| 69 | errors.add(formatDetailedMessage(message, sourceName, line)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the list of errors, or {@code null} if there were none. |
| 74 | */ |
| 75 | public List<String> errors() { |
| 76 | return errors; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the list of warnings, or {@code null} if there were none. |
| 81 | */ |
| 82 | public List<String> warnings() { |
| 83 | return warnings; |
| 84 | } |
| 85 | |
| 86 | private String formatDetailedMessage( |
| 87 | String message, String sourceName, int lineNumber) { |
| 88 | String details = message; |
| 89 | if (sourceName == null || lineNumber <= 0) { |
| 90 | return details; |
| 91 | } |
| 92 | StringBuilder buf = new StringBuilder(details); |
| 93 | buf.append(" ("); |
| 94 | if (sourceName != null) { |
| 95 | buf.append(sourceName); |
| 96 | } |
| 97 | if (lineNumber > 0) { |
| 98 | buf.append('#'); |
| 99 | buf.append(lineNumber); |
| 100 | } |
| 101 | buf.append(')'); |
| 102 | return buf.toString(); |
| 103 | } |
| 104 | } |