Coverage Report - com.puppycrawl.tools.checkstyle.XMLLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLLogger
97%
77/79
86%
26/30
3.4
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 package com.puppycrawl.tools.checkstyle;
 20  
 
 21  
 import java.io.OutputStream;
 22  
 import java.io.OutputStreamWriter;
 23  
 import java.io.PrintWriter;
 24  
 import java.io.StringWriter;
 25  
 import java.io.UnsupportedEncodingException;
 26  
 import java.util.ResourceBundle;
 27  
 
 28  
 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
 29  
 import com.puppycrawl.tools.checkstyle.api.AuditListener;
 30  
 import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
 31  
 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
 32  
 
 33  
 /**
 34  
  * Simple XML logger.
 35  
  * It outputs everything in UTF-8 (default XML encoding is UTF-8) in case
 36  
  * we want to localize error messages or simply that filenames are
 37  
  * localized and takes care about escaping as well.
 38  
 
 39  
  * @author <a href="mailto:stephane.bailliez@wanadoo.fr">Stephane Bailliez</a>
 40  
  */
 41  
 public class XMLLogger
 42  
     extends AutomaticBean
 43  
     implements AuditListener
 44  
 {
 45  
     /** decimal radix */
 46  
     private static final int BASE_10 = 10;
 47  
 
 48  
     /** hex radix */
 49  
     private static final int BASE_16 = 16;
 50  
 
 51  
     /** close output stream in auditFinished */
 52  
     private boolean mCloseStream;
 53  
 
 54  
     /** helper writer that allows easy encoding and printing */
 55  
     private PrintWriter mWriter;
 56  
 
 57  
     /** some known entities to detect */
 58  1
     private static final String[] ENTITIES = {"gt", "amp", "lt", "apos",
 59  
                                               "quot", };
 60  
 
 61  
     /**
 62  
      * Creates a new <code>XMLLogger</code> instance.
 63  
      * Sets the output to a defined stream.
 64  
      * @param aOS the stream to write logs to.
 65  
      * @param aCloseStream close aOS in auditFinished
 66  
      */
 67  
     public XMLLogger(OutputStream aOS, boolean aCloseStream)
 68  8
     {
 69  8
         setOutputStream(aOS);
 70  8
         mCloseStream = aCloseStream;
 71  8
     }
 72  
 
 73  
     /**
 74  
      * sets the OutputStream
 75  
      * @param aOS the OutputStream to use
 76  
      **/
 77  
     private void setOutputStream(OutputStream aOS)
 78  
     {
 79  
         try {
 80  8
             final OutputStreamWriter osw = new OutputStreamWriter(aOS, "UTF-8");
 81  8
             mWriter = new PrintWriter(osw);
 82  
         }
 83  0
         catch (final UnsupportedEncodingException e) {
 84  
             // unlikely to happen...
 85  0
             throw new ExceptionInInitializerError(e);
 86  8
         }
 87  8
     }
 88  
 
 89  
     /** {@inheritDoc} */
 90  
     public void auditStarted(AuditEvent aEvt)
 91  
     {
 92  6
         mWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
 93  
 
 94  6
         final ResourceBundle compilationProperties =
 95  
             ResourceBundle.getBundle("checkstylecompilation");
 96  6
         final String version =
 97  
             compilationProperties.getString("checkstyle.compile.version");
 98  
 
 99  6
         mWriter.println("<checkstyle version=\"" + version + "\">");
 100  6
     }
 101  
 
 102  
     /** {@inheritDoc} */
 103  
     public void auditFinished(AuditEvent aEvt)
 104  
     {
 105  6
         mWriter.println("</checkstyle>");
 106  6
         if (mCloseStream) {
 107  5
             mWriter.close();
 108  
         }
 109  
         else {
 110  1
             mWriter.flush();
 111  
         }
 112  6
     }
 113  
 
 114  
     /** {@inheritDoc} */
 115  
     public void fileStarted(AuditEvent aEvt)
 116  
     {
 117  1
         mWriter.println("<file name=\"" + encode(aEvt.getFileName()) + "\">");
 118  1
     }
 119  
 
 120  
     /** {@inheritDoc} */
 121  
     public void fileFinished(AuditEvent aEvt)
 122  
     {
 123  1
         mWriter.println("</file>");
 124  1
     }
 125  
 
 126  
     /** {@inheritDoc} */
 127  
     public void addError(AuditEvent aEvt)
 128  
     {
 129  1
         if (!SeverityLevel.IGNORE.equals(aEvt.getSeverityLevel())) {
 130  1
             mWriter.print("<error" + " line=\"" + aEvt.getLine() + "\"");
 131  1
             if (aEvt.getColumn() > 0) {
 132  1
                 mWriter.print(" column=\"" + aEvt.getColumn() + "\"");
 133  
             }
 134  1
             mWriter.print(" severity=\""
 135  
                 + aEvt.getSeverityLevel().getName()
 136  
                 + "\"");
 137  1
             mWriter.print(" message=\""
 138  
                 + encode(aEvt.getMessage())
 139  
                 + "\"");
 140  1
             mWriter.println(" source=\""
 141  
                 + encode(aEvt.getSourceName())
 142  
                 + "\"/>");
 143  
         }
 144  1
     }
 145  
 
 146  
     /** {@inheritDoc} */
 147  
     public void addException(AuditEvent aEvt, Throwable aThrowable)
 148  
     {
 149  1
         final StringWriter sw = new StringWriter();
 150  1
         final PrintWriter pw = new PrintWriter(sw);
 151  1
         pw.println("<exception>");
 152  1
         pw.println("<![CDATA[");
 153  1
         aThrowable.printStackTrace(pw);
 154  1
         pw.println("]]>");
 155  1
         pw.println("</exception>");
 156  1
         pw.flush();
 157  1
         mWriter.println(encode(sw.toString()));
 158  1
     }
 159  
 
 160  
     /**
 161  
      * Escape &lt;, &gt; &amp; &apos; and &quot; as their entities.
 162  
      * @param aValue the value to escape.
 163  
      * @return the escaped value if necessary.
 164  
      */
 165  
     public String encode(String aValue)
 166  
     {
 167  11
         final StringBuffer sb = new StringBuffer();
 168  130
         for (int i = 0; i < aValue.length(); i++) {
 169  119
             final char c = aValue.charAt(i);
 170  119
             switch (c) {
 171  
             case '<':
 172  4
                 sb.append("&lt;");
 173  4
                 break;
 174  
             case '>':
 175  4
                 sb.append("&gt;");
 176  4
                 break;
 177  
             case '\'':
 178  1
                 sb.append("&apos;");
 179  1
                 break;
 180  
             case '\"':
 181  1
                 sb.append("&quot;");
 182  1
                 break;
 183  
             case '&':
 184  2
                 final int nextSemi = aValue.indexOf(";", i);
 185  2
                 if ((nextSemi < 0)
 186  
                     || !isReference(aValue.substring(i, nextSemi + 1)))
 187  
                 {
 188  1
                     sb.append("&amp;");
 189  
                 }
 190  
                 else {
 191  1
                     sb.append('&');
 192  
                 }
 193  1
                 break;
 194  
             default:
 195  107
                 sb.append(c);
 196  
                 break;
 197  
             }
 198  
         }
 199  11
         return sb.toString();
 200  
     }
 201  
 
 202  
     /**
 203  
      * @return whether the given argument a character or entity reference
 204  
      * @param aEnt the possible entity to look for.
 205  
      */
 206  
     public boolean isReference(String aEnt)
 207  
     {
 208  10
         if (!(aEnt.charAt(0) == '&') || !aEnt.endsWith(";")) {
 209  1
             return false;
 210  
         }
 211  
 
 212  9
         if (aEnt.charAt(1) == '#') {
 213  7
             int prefixLength = 2; // "&#"
 214  7
             int radix = BASE_10;
 215  7
             if (aEnt.charAt(2) == 'x') {
 216  3
                 prefixLength++;
 217  3
                 radix = BASE_16;
 218  
             }
 219  
             try {
 220  7
                 Integer.parseInt(
 221  
                     aEnt.substring(prefixLength, aEnt.length() - 1), radix);
 222  2
                 return true;
 223  
             }
 224  5
             catch (final NumberFormatException nfe) {
 225  5
                 return false;
 226  
             }
 227  
         }
 228  
 
 229  2
         final String name = aEnt.substring(1, aEnt.length() - 1);
 230  9
         for (String element : ENTITIES) {
 231  8
             if (name.equals(element)) {
 232  1
                 return true;
 233  
             }
 234  
         }
 235  1
         return false;
 236  
     }
 237  
 }