KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > build > documentation > CheckSitemapTask


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.build.documentation;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * Checks the sitemap for invalid resource links (currently only local).
40  *
41  * @version $Id: CheckSitemapTask.java,v 1.3 2004/02/29 09:37:36 vmassol Exp $
42  */

43 public class CheckSitemapTask extends Task
44 {
45     /**
46      * Location of the sitemap.xml file.
47      */

48     private File JavaDoc sitemap;
49
50     /**
51      * Whether the task should fail when an error occurred.
52      */

53     private boolean failOnError = true;
54
55     /**
56      * The output directory where files are generated.
57      */

58     private File JavaDoc outputDir;
59
60     /**
61      * For resolving entities such as DTDs.
62      */

63     private XMLCatalog xmlCatalog = new XMLCatalog();
64
65     /**
66      * Sets the location of the sitemap file.
67      *
68      * @param theSitemap The sitemap file
69      */

70     public void setSitemap(File JavaDoc theSitemap)
71     {
72         this.sitemap = theSitemap;
73     }
74
75     /**
76      * @param theOutputDir the location of the output directory where files are
77      * generated
78      */

79     public void setOutputDir(File JavaDoc theOutputDir)
80     {
81         this.outputDir = theOutputDir;
82     }
83
84     /**
85      * Sets whether the task should fail when an error occurs.
86      *
87      * @param theFailOnError Whether to fail on errors
88      */

89     public void setFailOnError(boolean theFailOnError)
90     {
91         this.failOnError = theFailOnError;
92     }
93
94     /**
95      * Add the catalog to our internal catalog
96      *
97      * @param theXmlCatalog the XMLCatalog instance to use to look up DTDs
98      */

99     public void addConfiguredXMLCatalog(XMLCatalog theXmlCatalog)
100     {
101         this.xmlCatalog.addConfiguredXMLCatalog(theXmlCatalog);
102     }
103
104     /**
105      * @see Task#init()
106      */

107     public void init() throws BuildException
108     {
109         super.init();
110         // Initialize internal instance of XMLCatalog
111
this.xmlCatalog.setProject(getProject());
112     }
113
114     /**
115      * Execute task.
116      *
117      * @see Task#execute()
118      */

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 JavaDoc builder = getDocumentBuilder();
143             builder.setEntityResolver(this.xmlCatalog);
144             Document JavaDoc document = builder.parse(sitemap);
145             if (!checkSitemap(document) && (this.failOnError))
146             {
147                 throw new BuildException("Found errors in sitemap.");
148             }
149         }
150         catch (SAXException JavaDoc e)
151         {
152             throw new BuildException(e);
153         }
154         catch (IOException JavaDoc e)
155         {
156             throw new BuildException(e);
157         }
158         catch (ParserConfigurationException JavaDoc e)
159         {
160             throw new BuildException(e);
161         }
162     }
163     
164     /**
165      * Instantiates and returns a JAXP DocumentBuilder.
166      *
167      * @return The DocumentBuilder instance
168      * @throws ParserConfigurationException If instantiating the builder threw
169      * a configuration exception
170      */

171     private DocumentBuilder JavaDoc getDocumentBuilder()
172         throws ParserConfigurationException JavaDoc
173     {
174         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
175         factory.setNamespaceAware(false);
176         factory.setValidating(false);
177         return factory.newDocumentBuilder();
178     }
179     
180     /**
181      * Checks the sitemap for valid links.
182      *
183      * @param theDocument The DOM representation of the Sitemap.
184      * @return Whether the check was successful
185      */

186     private boolean checkSitemap(Document JavaDoc theDocument)
187     {
188         Element JavaDoc sitemap =
189             (Element JavaDoc) theDocument.getElementsByTagName("sitemap").item(0);
190         NodeList JavaDoc resources = sitemap.getElementsByTagName("resource");
191         boolean success = true;
192         for (int i = 0; i < resources.getLength(); i++)
193         {
194             Element JavaDoc resource = (Element JavaDoc) resources.item(i);
195             if (!checkSitemapResource(resource))
196             {
197                 success = false;
198             }
199         }
200         return success;
201     }
202     
203     /**
204      * Checks a single resource in a sitemap.
205      *
206      * @param theElement The resource element
207      * @return Whether the check was successful
208      */

209     private boolean checkSitemapResource(Element JavaDoc theElement)
210     {
211         boolean isResourcePresent = true;
212         String JavaDoc id = theElement.getAttribute("id");
213
214         if (isResourceToBeChecked(theElement))
215         {
216             String JavaDoc 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     /**
237      * @param theElement the resource for which to verify if it is to be checked
238      * for existence
239      * @return true if the resource must be checked or false otherwise
240      */

241     private boolean isResourceToBeChecked(Element JavaDoc theElement)
242     {
243         // Checks are enabled by default
244
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     /**
256      * Checks whether a specified local sitemap resource points to an existing
257      * file.
258      *
259      * @param theId The <code>id</code> attribute of the resource element
260      * @param theTarget The <code>target</code> attribute of the resource
261      * element, the relative path from the directory containing the sitemap file
262      * to the generated file
263      * @return Whether the file exists
264      */

265     private boolean checkLocalSitemapResource(String JavaDoc theId, String JavaDoc theTarget)
266     {
267         File JavaDoc targetFile = new File JavaDoc(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