1 20 package org.apache.cactus.build.documentation; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 import org.apache.tools.ant.types.XMLCatalog; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.NodeList ; 36 import org.xml.sax.SAXException ; 37 38 43 public class CheckSitemapTask extends Task 44 { 45 48 private File sitemap; 49 50 53 private boolean failOnError = true; 54 55 58 private File outputDir; 59 60 63 private XMLCatalog xmlCatalog = new XMLCatalog(); 64 65 70 public void setSitemap(File theSitemap) 71 { 72 this.sitemap = theSitemap; 73 } 74 75 79 public void setOutputDir(File theOutputDir) 80 { 81 this.outputDir = theOutputDir; 82 } 83 84 89 public void setFailOnError(boolean theFailOnError) 90 { 91 this.failOnError = theFailOnError; 92 } 93 94 99 public void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog) 100 { 101 this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog); 102 } 103 104 107 public void init() throws BuildException 108 { 109 super.init(); 110 this.xmlCatalog.setProject(getProject()); 112 } 113 114 119 public void execute() throws BuildException 120 { 121 if (this.sitemap == null) 122 { 123 throw new BuildException("The [sitemap] attribute must be set"); 124 } 125 if (this.outputDir == null) 126 { 127 throw new BuildException("The [outputDir] attribute must be set"); 128 } 129 130 if (!this.sitemap.exists()) 131 { 132 throw new BuildException( 133 "The [sitemap] attribute must point to an existing file"); 134 } 135 136 log("Checking sitemap at [" + sitemap + "]", Project.MSG_INFO); 137 log("Generated doc output directory is [" + outputDir + "]", 138 Project.MSG_VERBOSE); 139 140 try 141 { 142 DocumentBuilder builder = getDocumentBuilder(); 143 builder.setEntityResolver(this.xmlCatalog); 144 Document document = builder.parse(sitemap); 145 if (!checkSitemap(document) && (this.failOnError)) 146 { 147 throw new BuildException("Found errors in sitemap."); 148 } 149 } 150 catch (SAXException e) 151 { 152 throw new BuildException(e); 153 } 154 catch (IOException e) 155 { 156 throw new BuildException(e); 157 } 158 catch (ParserConfigurationException e) 159 { 160 throw new BuildException(e); 161 } 162 } 163 164 171 private DocumentBuilder getDocumentBuilder() 172 throws ParserConfigurationException 173 { 174 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 175 factory.setNamespaceAware(false); 176 factory.setValidating(false); 177 return factory.newDocumentBuilder(); 178 } 179 180 186 private boolean checkSitemap(Document theDocument) 187 { 188 Element sitemap = 189 (Element ) theDocument.getElementsByTagName("sitemap").item(0); 190 NodeList resources = sitemap.getElementsByTagName("resource"); 191 boolean success = true; 192 for (int i = 0; i < resources.getLength(); i++) 193 { 194 Element resource = (Element ) resources.item(i); 195 if (!checkSitemapResource(resource)) 196 { 197 success = false; 198 } 199 } 200 return success; 201 } 202 203 209 private boolean checkSitemapResource(Element theElement) 210 { 211 boolean isResourcePresent = true; 212 String id = theElement.getAttribute("id"); 213 214 if (isResourceToBeChecked(theElement)) 215 { 216 String target = theElement.getAttribute("target"); 217 218 if ((target == null) || (target.length() == 0)) 219 { 220 log("Skipping remote resource [" + id + "]", 221 Project.MSG_VERBOSE); 222 } 223 else 224 { 225 isResourcePresent = checkLocalSitemapResource(id, target); 226 } 227 } 228 else 229 { 230 log("This resource should not be checked: [" + id + "]", 231 Project.MSG_VERBOSE); 232 } 233 return isResourcePresent; 234 } 235 236 241 private boolean isResourceToBeChecked(Element theElement) 242 { 243 boolean isToBeChecked = true; 245 246 if ((theElement.getAttribute("check") != null) 247 && (theElement.getAttribute("check").length() > 0)) 248 { 249 isToBeChecked = Boolean.valueOf( 250 theElement.getAttribute("check")).booleanValue(); 251 } 252 return isToBeChecked; 253 } 254 255 265 private boolean checkLocalSitemapResource(String theId, String theTarget) 266 { 267 File targetFile = new File (this.outputDir, theTarget); 268 log("Checking resource [" + theId + "] at [" + theTarget + "]", 269 Project.MSG_DEBUG); 270 if (!targetFile.exists()) 271 { 272 log("Sitemap resource [" + theId + "] not found under [" 273 + targetFile + "]", Project.MSG_ERR); 274 return false; 275 } 276 return true; 277 } 278 279 } 280 | Popular Tags |