1 package com.puppycrawl.tools.checkstyle.filters; 20 21 import com.puppycrawl.tools.checkstyle.api.AbstractLoader; 22 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 23 import com.puppycrawl.tools.checkstyle.api.FilterSet; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.regex.PatternSyntaxException ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 39 public final class SuppressionsLoader 40 extends AbstractLoader 41 { 42 43 private static final String DTD_PUBLIC_ID_1_0 = 44 "-//Puppy Crawl//DTD Suppressions 1.0//EN"; 45 46 private static final String DTD_RESOURCE_NAME_1_0 = 47 "com/puppycrawl/tools/checkstyle/suppressions_1_0.dtd"; 48 49 private static final String DTD_PUBLIC_ID_1_1 = 50 "-//Puppy Crawl//DTD Suppressions 1.1//EN"; 51 52 private static final String DTD_RESOURCE_NAME_1_1 = 53 "com/puppycrawl/tools/checkstyle/suppressions_1_1.dtd"; 54 55 59 private final FilterSet mFilterChain = new FilterSet(); 60 61 66 private SuppressionsLoader() 67 throws ParserConfigurationException , SAXException 68 { 69 super(createIdToResourceNameMap()); 70 } 71 72 76 public FilterSet getFilterChain() 77 { 78 return mFilterChain; 79 } 80 81 82 public void startElement(String aNamespaceURI, 83 String aLocalName, 84 String aQName, 85 Attributes aAtts) 86 throws SAXException 87 { 88 if (aQName.equals("suppress")) { 89 final String files = aAtts.getValue("files"); 91 if (files == null) { 92 throw new SAXException ("missing files attribute"); 93 } 94 final String checks = aAtts.getValue("checks"); 95 final String modId = aAtts.getValue("id"); 96 if ((checks == null) && (modId == null)) { 97 throw new SAXException ("missing checks and id attribute"); 98 } 99 final SuppressElement suppress; 100 try { 101 suppress = new SuppressElement(files); 102 if (modId != null) { 103 suppress.setModuleId(modId); 104 } 105 if (checks != null) { 106 suppress.setChecks(checks); 107 } 108 } 109 catch (final PatternSyntaxException e) { 110 throw new SAXException ("invalid files or checks format"); 111 } 112 final String lines = aAtts.getValue("lines"); 113 if (lines != null) { 114 suppress.setLines(lines); 115 } 116 final String columns = aAtts.getValue("columns"); 117 if (columns != null) { 118 suppress.setColumns(columns); 119 } 120 mFilterChain.addFilter(suppress); 121 } 122 } 123 124 130 public static FilterSet loadSuppressions(String aFilename) 131 throws CheckstyleException 132 { 133 final FileInputStream fis; 134 try { 135 fis = new FileInputStream (aFilename); 136 } 137 catch (final FileNotFoundException e) { 138 throw new CheckstyleException( 139 "unable to find " + aFilename, e); 140 } 141 final InputSource source = new InputSource (fis); 142 return loadSuppressions(source, aFilename); 143 } 144 145 152 private static FilterSet loadSuppressions( 153 InputSource aSource, String aSourceName) 154 throws CheckstyleException 155 { 156 try { 157 final SuppressionsLoader suppressionsLoader = 158 new SuppressionsLoader(); 159 suppressionsLoader.parseInputSource(aSource); 160 return suppressionsLoader.getFilterChain(); 161 } 162 catch (final FileNotFoundException e) { 163 throw new CheckstyleException("unable to find " + aSourceName, e); 164 } 165 catch (final ParserConfigurationException e) { 166 throw new CheckstyleException("unable to parse " + aSourceName, e); 167 } 168 catch (final SAXException e) { 169 throw new CheckstyleException("unable to parse " 170 + aSourceName + " - " + e.getMessage(), e); 171 } 172 catch (final IOException e) { 173 throw new CheckstyleException("unable to read " + aSourceName, e); 174 } 175 catch (final NumberFormatException e) { 176 throw new CheckstyleException("number format exception " 177 + aSourceName + " - " + e.getMessage(), e); 178 } 179 } 180 181 185 private static Map createIdToResourceNameMap() 186 { 187 final Map map = new HashMap (); 188 map.put(DTD_PUBLIC_ID_1_0, DTD_RESOURCE_NAME_1_0); 189 map.put(DTD_PUBLIC_ID_1_1, DTD_RESOURCE_NAME_1_1); 190 return map; 191 } 192 } 193 | Popular Tags |