1 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 41 public class XMLLogger 42 extends AutomaticBean 43 implements AuditListener 44 { 45 46 private static final int BASE_10 = 10; 47 48 49 private static final int BASE_16 = 16; 50 51 52 private boolean mCloseStream; 53 54 55 private PrintWriter mWriter; 56 57 58 private static final String [] ENTITIES = {"gt", "amp", "lt", "apos", 59 "quot", }; 60 61 67 public XMLLogger(OutputStream aOS, boolean aCloseStream) 68 { 69 setOutputStream(aOS); 70 mCloseStream = aCloseStream; 71 } 72 73 77 private void setOutputStream(OutputStream aOS) 78 { 79 try { 80 final OutputStreamWriter osw = new OutputStreamWriter (aOS, "UTF-8"); 81 mWriter = new PrintWriter (osw); 82 } 83 catch (final UnsupportedEncodingException e) { 84 throw new ExceptionInInitializerError (e); 86 } 87 } 88 89 90 public void auditStarted(AuditEvent aEvt) 91 { 92 mWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 93 94 final ResourceBundle compilationProperties = 95 ResourceBundle.getBundle("checkstylecompilation"); 96 final String version = 97 compilationProperties.getString("checkstyle.compile.version"); 98 99 mWriter.println("<checkstyle version=\"" + version + "\">"); 100 } 101 102 103 public void auditFinished(AuditEvent aEvt) 104 { 105 mWriter.println("</checkstyle>"); 106 if (mCloseStream) { 107 mWriter.close(); 108 } 109 else { 110 mWriter.flush(); 111 } 112 } 113 114 115 public void fileStarted(AuditEvent aEvt) 116 { 117 mWriter.println("<file name=\"" + aEvt.getFileName() + "\">"); 118 } 119 120 121 public void fileFinished(AuditEvent aEvt) 122 { 123 mWriter.println("</file>"); 124 } 125 126 127 public void addError(AuditEvent aEvt) 128 { 129 if (!SeverityLevel.IGNORE.equals(aEvt.getSeverityLevel())) { 130 mWriter.print("<error" + " line=\"" + aEvt.getLine() + "\""); 131 if (aEvt.getColumn() > 0) { 132 mWriter.print(" column=\"" + aEvt.getColumn() + "\""); 133 } 134 mWriter.print(" severity=\"" 135 + aEvt.getSeverityLevel().getName() 136 + "\""); 137 mWriter.print(" message=\"" 138 + encode(aEvt.getMessage()) 139 + "\""); 140 mWriter.println(" source=\"" 141 + encode(aEvt.getSourceName()) 142 + "\"/>"); 143 } 144 } 145 146 147 public void addException(AuditEvent aEvt, Throwable aThrowable) 148 { 149 final StringWriter sw = new StringWriter (); 150 final PrintWriter pw = new PrintWriter (sw); 151 pw.println("<exception>"); 152 pw.println("<![CDATA["); 153 aThrowable.printStackTrace(pw); 154 pw.println("]]>"); 155 pw.println("</exception>"); 156 pw.flush(); 157 mWriter.println(encode(sw.toString())); 158 } 159 160 165 public String encode(String aValue) 166 { 167 final StringBuffer sb = new StringBuffer (); 168 for (int i = 0; i < aValue.length(); i++) { 169 final char c = aValue.charAt(i); 170 switch (c) { 171 case '<': 172 sb.append("<"); 173 break; 174 case '>': 175 sb.append(">"); 176 break; 177 case '\'': 178 sb.append("'"); 179 break; 180 case '\"': 181 sb.append("""); 182 break; 183 case '&': 184 final int nextSemi = aValue.indexOf(";", i); 185 if ((nextSemi < 0) 186 || !isReference(aValue.substring(i, nextSemi + 1))) 187 { 188 sb.append("&"); 189 } 190 else { 191 sb.append('&'); 192 } 193 break; 194 default: 195 sb.append(c); 196 break; 197 } 198 } 199 return sb.toString(); 200 } 201 202 206 public boolean isReference(String aEnt) 207 { 208 if (!(aEnt.charAt(0) == '&') || !aEnt.endsWith(";")) { 209 return false; 210 } 211 212 if (aEnt.charAt(1) == '#') { 213 int prefixLength = 2; int radix = BASE_10; 215 if (aEnt.charAt(2) == 'x') { 216 prefixLength++; 217 radix = BASE_16; 218 } 219 try { 220 Integer.parseInt( 221 aEnt.substring(prefixLength, aEnt.length() - 1), radix); 222 return true; 223 } 224 catch (final NumberFormatException nfe) { 225 return false; 226 } 227 } 228 229 final String name = aEnt.substring(1, aEnt.length() - 1); 230 for (int i = 0; i < ENTITIES.length; i++) { 231 if (name.equals(ENTITIES[i])) { 232 return true; 233 } 234 } 235 return false; 236 } 237 } 238 | Popular Tags |