1 package com.puppycrawl.tools.checkstyle.checks.imports; 20 21 import com.puppycrawl.tools.checkstyle.api.AbstractLoader; 22 import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.util.Stack ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.SAXException ; 31 32 36 final class ImportControlLoader extends AbstractLoader 37 { 38 39 private static final String DTD_PUBLIC_ID = 40 "-//Puppy Crawl//DTD Import Control 1.0//EN"; 41 42 43 private static final String DTD_RESOURCE_NAME = 44 "com/puppycrawl/tools/checkstyle/checks/imports/import_control_1_0.dtd"; 45 46 47 private final Stack mStack = new Stack (); 48 49 54 private ImportControlLoader() throws ParserConfigurationException , 55 SAXException 56 { 57 super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME); 58 } 59 60 61 public void startElement(final String aNamespaceURI, 62 final String aLocalName, 63 final String aQName, 64 final Attributes aAtts) 65 throws SAXException 66 { 67 if (aQName.equals("import-control")) { 68 final String pkg = safeGet(aAtts, "pkg"); 69 mStack.push(new PkgControl(pkg)); 70 } 71 else if (aQName.equals("subpackage")) { 72 assert mStack.size() > 0; 73 final String name = safeGet(aAtts, "name"); 74 mStack.push(new PkgControl((PkgControl) mStack.peek(), name)); 75 } 76 else if (aQName.equals("allow") || aQName.equals("disallow")) { 77 assert mStack.size() > 0; 78 final boolean isAllow = aQName.equals("allow"); 82 final boolean isLocalOnly = (aAtts.getValue("local-only") != null); 83 final String pkg = aAtts.getValue("pkg"); 84 final Guard g; 85 if (pkg != null) { 86 final boolean exactMatch = 87 (aAtts.getValue("exact-match") != null); 88 g = new Guard(isAllow, isLocalOnly, pkg, exactMatch); 89 } 90 else { 91 final String clazz = safeGet(aAtts, "class"); 92 g = new Guard(isAllow, isLocalOnly, clazz); 93 } 94 95 final PkgControl pc = (PkgControl) mStack.peek(); 96 pc.addGuard(g); 97 } 98 } 99 100 101 public void endElement(final String aNamespaceURI, final String aLocalName, 102 final String aQName) 103 { 104 if (aQName.equals("subpackage")) { 105 assert mStack.size() > 1; 106 mStack.pop(); 107 } 108 } 109 110 116 static PkgControl load(final String aFilename) throws CheckstyleException 117 { 118 FileInputStream fis = null; 119 try { 120 fis = new FileInputStream (aFilename); 121 } 122 catch (final FileNotFoundException e) { 123 throw new CheckstyleException("unable to find " + aFilename, e); 124 } 125 final InputSource source = new InputSource (fis); 126 return load(source, aFilename); 127 } 128 129 136 private static PkgControl load(final InputSource aSource, 137 final String aSourceName) throws CheckstyleException 138 { 139 try { 140 final ImportControlLoader loader = new ImportControlLoader(); 141 loader.parseInputSource(aSource); 142 return loader.getRoot(); 143 } 144 catch (final ParserConfigurationException e) { 145 throw new CheckstyleException("unable to parse " + aSourceName, e); 146 } 147 catch (final SAXException e) { 148 throw new CheckstyleException("unable to parse " + aSourceName 149 + " - " + e.getMessage(), e); 150 } 151 catch (final IOException e) { 152 throw new CheckstyleException("unable to read " + aSourceName, e); 153 } 154 } 155 156 159 private PkgControl getRoot() 160 { 161 assert mStack.size() == 1; 162 return (PkgControl) mStack.peek(); 163 } 164 165 173 private String safeGet(final Attributes aAtts, String aName) 174 throws SAXException 175 { 176 final String retVal = aAtts.getValue(aName); 177 if (retVal == null) { 178 throw new SAXException ("missing attibute " + aName); 179 } 180 return retVal; 181 } 182 } 183 | Popular Tags |