001    /*
002     * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    package com.sun.tools.javac.util;
026    
027    import java.util.Locale;
028    
029    import com.sun.tools.javac.api.Formattable;
030    import static com.sun.tools.javac.api.DiagnosticFormatter.PositionKind.*;
031    
032    /**
033     * A raw formatter for diagnostic messages.
034     * The raw formatter will format a diagnostic according to one of two format patterns, depending on whether
035     * or not the source name and position are set. This formatter provides a standardized, localize-independent
036     * implementation of a diagnostic formatter; as such, this formatter is best suited for testing purposes.
037     */
038    public class RawDiagnosticFormatter extends AbstractDiagnosticFormatter {
039    
040        /**
041         * Create a formatter based on the supplied options.
042         * @param msgs
043         */
044        public RawDiagnosticFormatter(Options opts) {
045            super(null, opts, false);
046        }
047    
048        //provide common default formats
049        public String format(JCDiagnostic d, Locale l) {
050            try {
051                StringBuffer buf = new StringBuffer();
052                if (d.getPosition() != Position.NOPOS) {
053                    buf.append(formatSource(d, false, null));
054                    buf.append(':');
055                    buf.append(formatPosition(d, LINE, null));
056                    buf.append(':');
057                    buf.append(formatPosition(d, COLUMN, null));
058                    buf.append(':');
059                }
060                else
061                    buf.append('-');
062                buf.append(' ');
063                buf.append(formatMessage(d, null));
064                if (displaySource(d))
065                    buf.append("\n" + formatSourceLine(d));
066                return buf.toString();
067            }
068            catch (Exception e) {
069                e.printStackTrace();
070                return null;
071            }
072        }
073    
074        @Override
075        protected String formatArgument(JCDiagnostic diag, Object arg, Locale l) {
076            String s;
077            if (arg instanceof Formattable)
078                s = arg.toString();
079            else
080                s = super.formatArgument(diag, arg, null);
081            if (arg instanceof JCDiagnostic)
082                return "(" + s + ")";
083            else
084                return s;
085        }
086    
087        @Override
088        protected String formatSubdiagnostics(JCDiagnostic d, Locale l) {
089            StringBuilder buf = new StringBuilder();
090            String sep = "";
091            buf.append(",{");
092            for (JCDiagnostic d2 : d.getSubdiagnostics()) {
093                buf.append(sep);
094                buf.append("(" + format(d2, l) + ")");
095                sep = ",";
096            }
097            buf.append('}');
098            return buf.toString();
099        }
100    
101        @Override
102        protected String localize(Locale l, String s, Object... args) {
103            StringBuffer buf = new StringBuffer();
104            buf.append(s);
105            String sep = ": ";
106            for (Object o : args) {
107                buf.append(sep);
108                buf.append(o);
109                sep = ", ";
110            }
111            return buf.toString();
112        }
113    }