1 package com.puppycrawl.tools.checkstyle; 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.io.InputStream ; 27 import java.util.Iterator ; 28 import java.util.Stack ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 34 39 public final class PackageNamesLoader 40 extends AbstractLoader 41 { 42 43 private static final String DTD_PUBLIC_ID = 44 "-//Puppy Crawl//DTD Package Names 1.0//EN"; 45 46 47 private static final String DTD_RESOURCE_NAME = 48 "com/puppycrawl/tools/checkstyle/packages_1_0.dtd"; 49 50 53 private static final String DEFAULT_PACKAGES = 54 "com/puppycrawl/tools/checkstyle/checkstyle_packages.xml"; 55 56 60 private final PackageObjectFactory mModuleFactory = 61 new PackageObjectFactory(); 62 63 64 private final Stack mPackageStack = new Stack (); 65 66 71 private PackageNamesLoader() 72 throws ParserConfigurationException , SAXException 73 { 74 super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME); 75 } 76 77 78 public void startElement(String aNamespaceURI, 79 String aLocalName, 80 String aQName, 81 Attributes aAtts) 82 throws SAXException 83 { 84 if (aQName.equals("package")) { 85 final String name = aAtts.getValue("name"); 87 if (name == null) { 88 throw new SAXException ("missing package name"); 89 } 90 mPackageStack.push(name); 91 } 92 } 93 94 98 private String getPackageName() 99 { 100 final StringBuffer buf = new StringBuffer (); 101 final Iterator it = mPackageStack.iterator(); 102 while (it.hasNext()) { 103 final String subPackage = (String ) it.next(); 104 buf.append(subPackage); 105 if (!subPackage.endsWith(".")) { 106 buf.append("."); 107 } 108 } 109 return buf.toString(); 110 } 111 112 116 private ModuleFactory getModuleFactory() 117 { 118 return mModuleFactory; 119 } 120 121 122 public void endElement(String aNamespaceURI, 123 String aLocalName, 124 String aQName) 125 { 126 if (aQName.equals("package")) { 127 mModuleFactory.addPackage(getPackageName()); 128 mPackageStack.pop(); 129 } 130 } 131 132 139 public static ModuleFactory loadModuleFactory(ClassLoader aClassLoader) 140 throws CheckstyleException 141 { 142 143 final InputStream stream = 144 aClassLoader.getResourceAsStream(DEFAULT_PACKAGES); 145 final InputSource source = new InputSource (stream); 146 return loadModuleFactory(source, "default package names"); 147 } 148 149 156 public static ModuleFactory loadModuleFactory(String aFilename) 157 throws CheckstyleException 158 { 159 FileInputStream fis = null; 160 try { 161 fis = new FileInputStream (aFilename); 162 } 163 catch (final FileNotFoundException e) { 164 throw new CheckstyleException( 165 "unable to find " + aFilename, e); 166 } 167 final InputSource source = new InputSource (fis); 168 return loadModuleFactory(source, aFilename); 169 } 170 171 178 private static ModuleFactory loadModuleFactory( 179 InputSource aSource, String aSourceName) 180 throws CheckstyleException 181 { 182 try { 183 final PackageNamesLoader nameLoader = new PackageNamesLoader(); 184 nameLoader.parseInputSource(aSource); 185 return nameLoader.getModuleFactory(); 186 } 187 catch (final FileNotFoundException e) { 188 throw new CheckstyleException("unable to find " + aSourceName, e); 189 } 190 catch (final ParserConfigurationException e) { 191 throw new CheckstyleException("unable to parse " + aSourceName, e); 192 } 193 catch (final SAXException e) { 194 throw new CheckstyleException("unable to parse " 195 + aSourceName + " - " + e.getMessage(), e); 196 } 197 catch (final IOException e) { 198 throw new CheckstyleException("unable to read " + aSourceName, e); 199 } 200 } 201 } 202 | Popular Tags |