Author: vsiveton
Date: Sat Nov 1 06:21:04 2008
New Revision: 709689
URL: http://svn.apache.org/viewvc?rev=709689&view=rev
Log:
o added encoding support
Modified:
maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/DefaultLinkCheck.java
maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java
maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java
maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
Modified: maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/DefaultLinkCheck.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/DefaultLinkCheck.java?rev=709689&r1=709688&r2=709689&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/DefaultLinkCheck.java
(original)
+++ maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/DefaultLinkCheck.java
Sat Nov 1 06:21:04 2008
@@ -23,6 +23,9 @@
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
@@ -44,7 +47,9 @@
import org.apache.maven.doxia.linkcheck.validation.OfflineHTTPLinkValidator;
import org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator;
import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.WriterFactory;
/**
* The main bean to be called whenever a set of documents should have their links checked.
@@ -119,6 +124,9 @@
/** The linkcheck model */
private LinkcheckModel model = new LinkcheckModel();
+ /** The encoding used to process files, UTF-8 by default. */
+ private String encoding = ReaderFactory.UTF_8;
+
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
@@ -227,9 +235,10 @@
{
createDocument();
}
- catch ( IOException e )
+ catch ( Exception e )
{
- LOG.error( "Could not write to output file, results will be lost!", e );
+ LOG.error( "Could not write to output file. Maybe try to specify an other encoding
instead of '"
+ + encoding + "'.", e );
}
validator.saveCache( this.linkCheckCache );
@@ -239,6 +248,25 @@
return model;
}
+ /** {@inheritDoc} */
+ public void setEncoding( String encoding )
+ {
+ if ( StringUtils.isEmpty( encoding ) )
+ {
+ throw new IllegalArgumentException( "encoding is required");
+ }
+ try
+ {
+ Charset.forName( encoding );
+ }
+ catch ( UnsupportedCharsetException e )
+ {
+ throw new IllegalArgumentException( "encoding '" + encoding + "' is unsupported"
);
+ }
+
+ this.encoding = encoding;
+ }
+
// ----------------------------------------------------------------------
// Private methods
// ----------------------------------------------------------------------
@@ -356,7 +384,7 @@
this.lvm.setExcludedLinks( getExcludedLinks() );
}
- this.lvm.addLinkValidator( new FileLinkValidator() );
+ this.lvm.addLinkValidator( new FileLinkValidator( encoding ) );
if ( isOnline() )
{
@@ -473,7 +501,7 @@
try
{
- hrefs = LinkMatcher.match( new File( linkcheckFile.getAbsolutePath() ) );
+ hrefs = LinkMatcher.match( new File( linkcheckFile.getAbsolutePath() ), encoding
);
}
catch ( Throwable t )
{
@@ -632,11 +660,11 @@
dir.mkdirs();
}
- FileWriter writer = null;
+ Writer writer = null;
LinkcheckModelXpp3Writer xpp3Writer = new LinkcheckModelXpp3Writer();
try
{
- writer = new FileWriter( this.reportOutput );
+ writer = WriterFactory.newXmlWriter( this.reportOutput );
xpp3Writer.write( writer, getModel() );
}
finally
Modified: maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java?rev=709689&r1=709688&r2=709689&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java
(original)
+++ maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java
Sat Nov 1 06:21:04 2008
@@ -122,4 +122,12 @@
* @return the analysis in a <code>LinkCheck</code> model.
*/
public LinkcheckModel execute();
+
+ /**
+ * Set the encoding to use when processing files.
+ *
+ * @param encoding a valid encoding
+ * @see <a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html">Supported
encodings</a>
+ */
+ public void setEncoding( String encoding );
}
Modified: maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java?rev=709689&r1=709688&r2=709689&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java
(original)
+++ maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java
Sat Nov 1 06:21:04 2008
@@ -23,6 +23,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.io.Reader;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
@@ -30,6 +31,7 @@
import java.util.regex.Pattern;
import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.ReaderFactory;
/**
* Link matcher. Reads the contents of a file and tries to match the following: <code>
@@ -59,24 +61,23 @@
/**
* Reads a file and returns a StringBuffer with its contents.
*
- * TODO: Check for encoding issues
- *
* @param file the file we are reading
+ * @param encoding the encoding file used
* @return a StringBuffer with file's contents.
* @throws IOException if something goes wrong.
*/
- private static StringBuffer fileToStringBuffer( File file ) throws IOException
+ private static StringBuffer fileToStringBuffer( File file, String encoding ) throws IOException
{
- BufferedReader reader = null;
-
final StringBuffer pageBuffer = new StringBuffer();
+ BufferedReader reader = null;
+ Reader r = null;
try
{
- reader = new BufferedReader( new FileReader( file ) );
+ r = ReaderFactory.newReader( file, encoding ) ;
+ reader = new BufferedReader( r );
String line;
-
while ( ( line = reader.readLine() ) != null )
{
pageBuffer.append( line );
@@ -84,6 +85,7 @@
}
finally
{
+ IOUtil.close( r );
IOUtil.close( reader );
}
@@ -94,14 +96,15 @@
* Performs the actual matching.
*
* @param file the file to check
+ * @param encoding the encoding file used
* @return a set with all links to check
* @throws IOException if something goes wrong
*/
- static Set match( File file ) throws IOException
+ static Set match( File file, String encoding ) throws IOException
{
LINK_LIST.clear();
- final Matcher m = MATCH_PATTERN.matcher( fileToStringBuffer( file ) );
+ final Matcher m = MATCH_PATTERN.matcher( fileToStringBuffer( file, encoding ) );
String link;
Modified: maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java?rev=709689&r1=709688&r2=709689&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
(original)
+++ maven/doxia/doxia-tools/trunk/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
Sat Nov 1 06:21:04 2008
@@ -27,6 +27,8 @@
import org.apache.maven.doxia.linkcheck.model.LinkcheckFileResult;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.WriterFactory;
/**
* A link validator solely for files on the local filesystem.
@@ -37,6 +39,20 @@
*/
public final class FileLinkValidator implements LinkValidator
{
+ private String encoding;
+
+ /**
+ * @param encoding the encoding file used. If empty, using UTF-8.
+ */
+ public FileLinkValidator( String encoding )
+ {
+ if ( StringUtils.isEmpty( encoding ) )
+ {
+ encoding = WriterFactory.UTF_8;
+ }
+ this.encoding = encoding;
+ }
+
/** {@inheritDoc} */
public LinkValidationResult validateLink( LinkValidationItem lvi )
{
@@ -89,7 +105,7 @@
if ( link.trim().length() == 0 ) // in the same file
{
// the anchor exists?
- String content = read( lvi.getSource() );
+ String content = read( lvi.getSource(), encoding );
if ( content != null && content.indexOf( "name=\"" + anchor + "\""
) != -1 )
{
return lvi.getSource();
@@ -100,7 +116,7 @@
}
// the anchor exists?
- String content = read( new File( lvi.getSource().getParentFile(), link ) );
+ String content = read( new File( lvi.getSource().getParentFile(), link ), encoding
);
if ( content != null && content.indexOf( "name=\"" + anchor + "\"" )
!= -1 )
{
return new File( lvi.getSource().getParentFile(), link );
@@ -126,17 +142,16 @@
}
/**
- * TODO take care of encoding
- *
* @param f not null
+ * @param encoding the encoding file used
* @return the content of the file or null if an error occurred.
*/
- private static String read( File f )
+ private static String read( File f, String encoding )
{
Reader reader = null;
try
{
- reader = ReaderFactory.newPlatformReader( f );
+ reader = ReaderFactory.newReader( f, encoding );
return IOUtil.toString( reader );
}
catch ( IOException e )
|