You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by cm...@apache.org on 2003/01/15 13:43:01 UTC
cvs commit: jakarta-cactus/anttasks/src/java/org/apache/cactus/ant CheckSitemapTask.java
cmlenz 2003/01/15 04:43:01
Added: anttasks/src/java/org/apache/cactus/ant
CheckSitemapTask.java
Log:
Simplish task that checks the documentation sitemap for invalid links.
Currently, it only checks local resources.
Revision Changes Path
1.1 jakarta-cactus/anttasks/src/java/org/apache/cactus/ant/CheckSitemapTask.java
Index: CheckSitemapTask.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus.ant;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* Checks the sitemap for invalid resource links (currently only local).
*
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
*
* @version $Id: CheckSitemapTask.java,v 1.1 2003/01/15 12:43:01 cmlenz Exp $
*/
public class CheckSitemapTask extends Task
{
/**
* Location of the sitemap.xml file.
*/
private File sitemap;
/**
* Whether the task should fail when an error occurred.
*/
private boolean failOnError = true;
/**
* The directory in which the sitemap is located.
*/
private File sitemapDir;
/**
* Sets the location of the sitemap file.
*
* @param theSitemap The sitemap file
*/
public void setSitemap(File theSitemap)
{
this.sitemap = theSitemap;
}
/**
* Sets whether the task should fail when an error occurs.
*
* @param theFailOnError Whether to fail on errors
*/
public void setFailOnError(boolean theFailOnError)
{
this.failOnError = theFailOnError;
}
/**
* Execute task.
*
* @see Task#execute()
*/
public void execute() throws BuildException
{
if (this.sitemap == null)
{
throw new BuildException("The [sitemap] attribute must be set");
}
if (!this.sitemap.exists())
{
throw new BuildException(
"The [sitemap] attribute must point to an existing file");
}
this.sitemapDir = sitemap.getParentFile();
try
{
DocumentBuilder builder = getDocumentBuilder();
Document document = builder.parse(sitemap);
if (!checkSitemap(document) && (this.failOnError))
{
throw new BuildException("Found errors in sitemap.");
}
}
catch (SAXException e)
{
throw new BuildException(e);
}
catch (IOException e)
{
throw new BuildException(e);
}
catch (ParserConfigurationException e)
{
throw new BuildException(e);
}
}
/**
* Instantiates and returns a JAXP DocumentBuilder.
*
* @return The DocumentBuilder instance
* @throws ParserConfigurationException If instantiating the builder threw
* a configuration exception
*/
private DocumentBuilder getDocumentBuilder()
throws ParserConfigurationException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
return factory.newDocumentBuilder();
}
/**
* Checks the sitemap for valid links.
*
* @param theDocument The DOM representation of the Sitemap.
* @return Whether the check was successful
*/
private boolean checkSitemap(Document theDocument)
{
Element root =
(Element) theDocument.getElementsByTagName("document").item(0);
Element body = (Element) root.getElementsByTagName("body").item(0);
Element sitemap =
(Element) body.getElementsByTagName("sitemap").item(0);
NodeList resources = sitemap.getElementsByTagName("resource");
boolean success = true;
for (int i = 0; i < resources.getLength(); i++)
{
Element resource = (Element) resources.item(i);
if (!checkSitemapResource(resource))
{
success = false;
}
}
return success;
}
/**
* Checks a single resource in a sitemap.
*
* @param theElement The resource element
* @return Whether the check was successful
*/
private boolean checkSitemapResource(Element theElement)
{
String id = theElement.getAttribute("id");
String source = theElement.getAttribute("source");
if ((source == null) || (source.length() == 0))
{
log("Skipping remote resource '" + id + "'", Project.MSG_VERBOSE);
}
else
{
checkLocalSitemapResource(id, source);
}
return true;
}
/**
* Checks whether a specified local sitemap resource points to an existing
* file.
*
* @param theId The 'id' attribute of the resource element
* @param theSource The 'source' attribute of the resource element, the
* relative path from the directory containing the sitemap file to
* the resource file
* @return Whether the file exists
*/
private boolean checkLocalSitemapResource(String theId, String theSource)
{
File sourceFile = new File(sitemapDir, theSource);
log("Checking resource '" + theId + "' at '" + theSource + "'",
Project.MSG_DEBUG);
if (!sourceFile.exists())
{
log("Sitemap resource '" + theId + "' not found under '" +
theSource + "'", Project.MSG_ERR);
return false;
}
return true;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: cvs commit: jakarta-cactus/anttasks/src/java/org/apache/cactus/ant CheckSitemapTask.java
Posted by Vincent Massol <vm...@octo.com>.
> -----Original Message-----
> From: Christopher Lenz [mailto:cmlenz@gmx.de]
> Sent: 22 January 2003 17:13
> To: Cactus Developers List
> Subject: Re: cvs commit: jakarta-
> cactus/anttasks/src/java/org/apache/cactus/ant CheckSitemapTask.java
>
> Vincent Massol wrote:
> >>-----Original Message-----
> >>From: Christopher Lenz [mailto:cmlenz@gmx.de]
> [snip]
> >>Thinking about the proposed directory reorganization, and the
current
> >>anttasks-module eventually going away in the not-so-distant future,
I
> >>now think it would be cleaner to put this task in the documentation
> >>module, because it is only used there:
> >>
> >>jakarta-cactus/
> >> documentation/
> >> src/
> >> java/
> >> org.apache.cactus.documentation.CheckSitemapTask
> >
> >
> > +1
>
> Just to clarify, you'd prefer the 'documentation' package, right?
Yep.
[snip]
Thanks
-Vincent
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: cvs commit: jakarta-cactus/anttasks/src/java/org/apache/cactus/ant
CheckSitemapTask.java
Posted by Christopher Lenz <cm...@gmx.de>.
Vincent Massol wrote:
>>-----Original Message-----
>>From: Christopher Lenz [mailto:cmlenz@gmx.de]
[snip]
>>Thinking about the proposed directory reorganization, and the current
>>anttasks-module eventually going away in the not-so-distant future, I
>>now think it would be cleaner to put this task in the documentation
>>module, because it is only used there:
>>
>>jakarta-cactus/
>> documentation/
>> src/
>> java/
>> org.apache.cactus.documentation.CheckSitemapTask
>
>
> +1
Just to clarify, you'd prefer the 'documentation' package, right?
>>or maybe
>> org.apache.cactus.tools.CheckSitemapTask
>>(to have a general package name that can be used for build-time
>
> utility
>
>>classes like this)
[snip]
--
Christopher Lenz
/=/ cmlenz at gmx.de
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: cvs commit: jakarta-cactus/anttasks/src/java/org/apache/cactus/ant CheckSitemapTask.java
Posted by Vincent Massol <vm...@octo.com>.
Hi Chris,
> -----Original Message-----
> From: Christopher Lenz [mailto:cmlenz@gmx.de]
> Sent: 21 January 2003 13:23
> To: Cactus Developers List
> Subject: Re: cvs commit: jakarta-
> cactus/anttasks/src/java/org/apache/cactus/ant CheckSitemapTask.java
>
> cmlenz@apache.org wrote:
> > cmlenz 2003/01/15 04:43:01
> >
> > Added: anttasks/src/java/org/apache/cactus/ant
> > CheckSitemapTask.java
> > Log:
> > Simplish task that checks the documentation sitemap for invalid
links.
> > Currently, it only checks local resources.
>
> Thinking about the proposed directory reorganization, and the current
> anttasks-module eventually going away in the not-so-distant future, I
> now think it would be cleaner to put this task in the documentation
> module, because it is only used there:
>
> jakarta-cactus/
> documentation/
> src/
> java/
> org.apache.cactus.documentation.CheckSitemapTask
+1
> or maybe
> org.apache.cactus.tools.CheckSitemapTask
> (to have a general package name that can be used for build-time
utility
> classes like this)
>
> The task would get built by the documentation build file, and also
used
> there. Javadocs etc are not needed, so it'd be simply a matter of
> <javac>. A big benefit is that the current dependancy of documentation
> on anttasks would be eliminated, and thus gump descriptor and master
> build file be simplified.
>
> What do you think?
+1, good idea :-)
Thanks
-Vincent
>
> --
> Christopher Lenz
> /=/ cmlenz at gmx.de
>
>
> --
> To unsubscribe, e-mail: <mailto:cactus-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:cactus-dev-
> help@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: cvs commit: jakarta-cactus/anttasks/src/java/org/apache/cactus/ant
CheckSitemapTask.java
Posted by Christopher Lenz <cm...@gmx.de>.
cmlenz@apache.org wrote:
> cmlenz 2003/01/15 04:43:01
>
> Added: anttasks/src/java/org/apache/cactus/ant
> CheckSitemapTask.java
> Log:
> Simplish task that checks the documentation sitemap for invalid links.
> Currently, it only checks local resources.
Thinking about the proposed directory reorganization, and the current
anttasks-module eventually going away in the not-so-distant future, I
now think it would be cleaner to put this task in the documentation
module, because it is only used there:
jakarta-cactus/
documentation/
src/
java/
org.apache.cactus.documentation.CheckSitemapTask
or maybe
org.apache.cactus.tools.CheckSitemapTask
(to have a general package name that can be used for build-time utility
classes like this)
The task would get built by the documentation build file, and also used
there. Javadocs etc are not needed, so it'd be simply a matter of
<javac>. A big benefit is that the current dependancy of documentation
on anttasks would be eliminated, and thus gump descriptor and master
build file be simplified.
What do you think?
--
Christopher Lenz
/=/ cmlenz at gmx.de
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>