You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2013/01/09 20:59:28 UTC
svn commit: r1431035 - in /incubator/jspwiki/trunk: src/org/apache/wiki/
src/org/apache/wiki/auth/login/ src/org/apache/wiki/diff/
src/org/apache/wiki/filters/ src/org/apache/wiki/modules/
src/org/apache/wiki/plugin/ src/org/apache/wiki/providers/ src/...
Author: juanpablo
Date: Wed Jan 9 19:59:27 2013
New Revision: 1431035
URL: http://svn.apache.org/viewvc?rev=1431035&view=rev
Log:
* org.apache.wiki.FileUtil moved to org.apache.wiki.util.FileUtil. Original class marked
with @Deprecated and forwarding to the new one until 2.10
Added:
incubator/jspwiki/trunk/src/org/apache/wiki/util/FileUtil.java
incubator/jspwiki/trunk/tests/org/apache/wiki/util/FileUtilTest.java
- copied, changed from r1431004, incubator/jspwiki/trunk/tests/org/apache/wiki/FileUtilTest.java
Removed:
incubator/jspwiki/trunk/tests/org/apache/wiki/FileUtilTest.java
Modified:
incubator/jspwiki/trunk/src/org/apache/wiki/FileUtil.java
incubator/jspwiki/trunk/src/org/apache/wiki/auth/login/CookieAuthenticationLoginModule.java
incubator/jspwiki/trunk/src/org/apache/wiki/diff/ExternalDiffProvider.java
incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java
incubator/jspwiki/trunk/src/org/apache/wiki/modules/WikiModuleInfo.java
incubator/jspwiki/trunk/src/org/apache/wiki/plugin/DefaultPluginManager.java
incubator/jspwiki/trunk/src/org/apache/wiki/providers/AbstractFileProvider.java
incubator/jspwiki/trunk/src/org/apache/wiki/providers/BasicAttachmentProvider.java
incubator/jspwiki/trunk/src/org/apache/wiki/providers/RCSFileProvider.java
incubator/jspwiki/trunk/src/org/apache/wiki/providers/VersioningFileProvider.java
incubator/jspwiki/trunk/src/org/apache/wiki/rss/RSSThread.java
incubator/jspwiki/trunk/src/org/apache/wiki/search/LuceneSearchProvider.java
incubator/jspwiki/trunk/src/org/apache/wiki/util/CommentedProperties.java
incubator/jspwiki/trunk/src/webdocs/Error.jsp
incubator/jspwiki/trunk/tests/org/apache/wiki/AllTests.java
incubator/jspwiki/trunk/tests/org/apache/wiki/TestEngine.java
incubator/jspwiki/trunk/tests/org/apache/wiki/WikiEngineTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/attachment/AttachmentManagerTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/providers/BasicAttachmentProviderTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/providers/CachingProviderTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/providers/FileSystemProviderTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/providers/RCSFileProviderTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/providers/VersioningFileProviderTest.java
incubator/jspwiki/trunk/tests/org/apache/wiki/stress/StressTestSpeed.java
incubator/jspwiki/trunk/tests/org/apache/wiki/util/AllTests.java
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/FileUtil.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/FileUtil.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/FileUtil.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/FileUtil.java Wed Jan 9 19:59:27 2013
@@ -19,25 +19,15 @@
package org.apache.wiki;
import java.io.*;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-import java.nio.charset.CharacterCodingException;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetDecoder;
-import java.nio.charset.CodingErrorAction;
-
-import org.apache.log4j.Logger;
/**
* Generic utilities related to file and stream handling.
+ * @deprecated will be removed in 2.10 scope. Consider using {@link org.apache.wiki.util.FileUtil}
+ * instead
*/
-// FIXME3.0: This class will move to "util" directory in 3.0
+@Deprecated
public final class FileUtil
{
- /** Size of the buffer used when copying large chunks of data. */
- private static final int BUFFER_SIZE = 4096;
- private static final Logger log = Logger.getLogger(FileUtil.class);
-
/**
* Private constructor prevents instantiation.
*/
@@ -58,28 +48,7 @@ public final class FileUtil
public static File newTmpFile( String content, String encoding )
throws IOException
{
- Writer out = null;
- Reader in = null;
- File f = null;
-
- try
- {
- f = File.createTempFile( "jspwiki", null );
-
- in = new StringReader( content );
-
- out = new OutputStreamWriter( new FileOutputStream( f ),
- encoding );
-
- copyContents( in, out );
- }
- finally
- {
- if( in != null ) in.close();
- if( out != null ) out.close();
- }
-
- return f;
+ return org.apache.wiki.util.FileUtil.newTmpFile( content, encoding );
}
/**
@@ -95,7 +64,7 @@ public final class FileUtil
public static File newTmpFile( String content )
throws IOException
{
- return newTmpFile( content, "ISO-8859-1" );
+ return org.apache.wiki.util.FileUtil.newTmpFile( content, "ISO-8859-1" );
}
/**
@@ -113,50 +82,7 @@ public final class FileUtil
throws IOException,
InterruptedException
{
- StringBuffer result = new StringBuffer();
-
- log.info("Running simple command "+command+" in "+directory);
-
- Process process = Runtime.getRuntime().exec( command, null, new File(directory) );
-
- BufferedReader stdout = null;
- BufferedReader stderr = null;
-
- try
- {
- stdout = new BufferedReader( new InputStreamReader(process.getInputStream()) );
- stderr = new BufferedReader( new InputStreamReader(process.getErrorStream()) );
-
- String line;
-
- while( (line = stdout.readLine()) != null )
- {
- result.append( line+"\n");
- }
-
- StringBuffer error = new StringBuffer();
- while( (line = stderr.readLine()) != null )
- {
- error.append( line+"\n");
- }
-
- if( error.length() > 0 )
- {
- log.error("Command failed, error stream is: "+error);
- }
-
- process.waitFor();
-
- }
- finally
- {
- // we must close all by exec(..) opened streams: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692
- process.getInputStream().close();
- if( stdout != null ) stdout.close();
- if( stderr != null ) stderr.close();
- }
-
- return result.toString();
+ return org.apache.wiki.util.FileUtil.runSimpleCommand( command, directory );
}
@@ -172,15 +98,7 @@ public final class FileUtil
public static void copyContents( Reader in, Writer out )
throws IOException
{
- char[] buf = new char[BUFFER_SIZE];
- int bytesRead = 0;
-
- while ((bytesRead = in.read(buf)) > 0)
- {
- out.write(buf, 0, bytesRead);
- }
-
- out.flush();
+ org.apache.wiki.util.FileUtil.copyContents( in, out );
}
/**
@@ -195,15 +113,7 @@ public final class FileUtil
public static void copyContents( InputStream in, OutputStream out )
throws IOException
{
- byte[] buf = new byte[BUFFER_SIZE];
- int bytesRead = 0;
-
- while ((bytesRead = in.read(buf)) > 0)
- {
- out.write(buf, 0, bytesRead);
- }
-
- out.flush();
+ org.apache.wiki.util.FileUtil.copyContents( in, out );
}
/**
@@ -221,44 +131,7 @@ public final class FileUtil
public static String readContents( InputStream input, String encoding )
throws IOException
{
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- FileUtil.copyContents( input, out );
-
- ByteBuffer bbuf = ByteBuffer.wrap( out.toByteArray() );
-
- Charset cset = Charset.forName( encoding );
- CharsetDecoder csetdecoder = cset.newDecoder();
-
- csetdecoder.onMalformedInput( CodingErrorAction.REPORT );
- csetdecoder.onUnmappableCharacter( CodingErrorAction.REPORT );
-
- try
- {
- CharBuffer cbuf = csetdecoder.decode( bbuf );
-
- return cbuf.toString();
- }
- catch( CharacterCodingException e )
- {
- Charset latin1 = Charset.forName("ISO-8859-1");
- CharsetDecoder l1decoder = latin1.newDecoder();
-
- l1decoder.onMalformedInput( CodingErrorAction.REPORT );
- l1decoder.onUnmappableCharacter( CodingErrorAction.REPORT );
-
- try
- {
- bbuf = ByteBuffer.wrap( out.toByteArray() );
-
- CharBuffer cbuf = l1decoder.decode( bbuf );
-
- return cbuf.toString();
- }
- catch( CharacterCodingException ex )
- {
- throw (CharacterCodingException) ex.fillInStackTrace();
- }
- }
+ return org.apache.wiki.util.FileUtil.readContents( input, encoding );
}
/**
@@ -272,27 +145,7 @@ public final class FileUtil
public static String readContents( Reader in )
throws IOException
{
- Writer out = null;
-
- try
- {
- out = new StringWriter();
-
- copyContents( in, out );
-
- return out.toString();
- }
- finally
- {
- try
- {
- out.close();
- }
- catch( Exception e )
- {
- log.error("Not able to close the stream while reading contents.");
- }
- }
+ return org.apache.wiki.util.FileUtil.readContents( in );
}
/**
@@ -305,20 +158,7 @@ public final class FileUtil
*/
public static String getThrowingMethod( Throwable t )
{
- StackTraceElement[] trace = t.getStackTrace();
- StringBuffer sb = new StringBuffer();
-
- if( trace == null || trace.length == 0 )
- {
- sb.append( "[Stack trace not available]" );
- }
- else
- {
- sb.append( trace[0].isNativeMethod() ? "native method" : "" );
- sb.append( trace[0].getClassName() );
- sb.append(".");
- sb.append(trace[0].getMethodName()+"(), line "+trace[0].getLineNumber());
- }
- return sb.toString();
+ return org.apache.wiki.util.FileUtil.getThrowingMethod( t );
}
+
}
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/auth/login/CookieAuthenticationLoginModule.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/auth/login/CookieAuthenticationLoginModule.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/auth/login/CookieAuthenticationLoginModule.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/auth/login/CookieAuthenticationLoginModule.java Wed Jan 9 19:59:27 2013
@@ -29,11 +29,10 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
-
-import org.apache.wiki.FileUtil;
import org.apache.wiki.TextUtil;
import org.apache.wiki.WikiEngine;
import org.apache.wiki.auth.WikiPrincipal;
+import org.apache.wiki.util.FileUtil;
import org.apache.wiki.util.HttpUtil;
/**
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/diff/ExternalDiffProvider.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/diff/ExternalDiffProvider.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/diff/ExternalDiffProvider.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/diff/ExternalDiffProvider.java Wed Jan 9 19:59:27 2013
@@ -26,8 +26,11 @@ import java.io.StringReader;
import java.util.Properties;
import org.apache.log4j.Logger;
-
-import org.apache.wiki.*;
+import org.apache.wiki.NoRequiredPropertyException;
+import org.apache.wiki.TextUtil;
+import org.apache.wiki.WikiContext;
+import org.apache.wiki.WikiEngine;
+import org.apache.wiki.util.FileUtil;
/**
* This DiffProvider allows external command line tools to be used to generate
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/filters/SpamFilter.java Wed Jan 9 19:59:27 2013
@@ -37,6 +37,7 @@ import org.apache.wiki.attachment.Attach
import org.apache.wiki.auth.user.UserProfile;
import org.apache.wiki.providers.ProviderException;
import org.apache.wiki.ui.EditorManager;
+import org.apache.wiki.util.FileUtil;
import org.suigeneris.jrcs.diff.Diff;
import org.suigeneris.jrcs.diff.DifferentiationFailedException;
import org.suigeneris.jrcs.diff.Revision;
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/modules/WikiModuleInfo.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/modules/WikiModuleInfo.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/modules/WikiModuleInfo.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/modules/WikiModuleInfo.java Wed Jan 9 19:59:27 2013
@@ -25,7 +25,7 @@ import java.net.URL;
import org.jdom.Element;
-import org.apache.wiki.FileUtil;
+import org.apache.wiki.util.FileUtil;
/**
* A WikiModule describes whatever JSPWiki plugin there is: it can be a plugin,
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/plugin/DefaultPluginManager.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/plugin/DefaultPluginManager.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/plugin/DefaultPluginManager.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/plugin/DefaultPluginManager.java Wed Jan 9 19:59:27 2013
@@ -27,7 +27,10 @@ import org.apache.commons.lang.ClassUtil
import org.apache.ecs.xhtml.*;
import org.apache.log4j.Logger;
import org.apache.oro.text.regex.*;
-import org.apache.wiki.*;
+import org.apache.wiki.InternalWikiException;
+import org.apache.wiki.TextUtil;
+import org.apache.wiki.WikiContext;
+import org.apache.wiki.WikiEngine;
import org.apache.wiki.api.engine.PluginManager;
import org.apache.wiki.api.exceptions.PluginException;
import org.apache.wiki.api.plugin.InitializablePlugin;
@@ -37,6 +40,7 @@ import org.apache.wiki.modules.ModuleMan
import org.apache.wiki.modules.WikiModuleInfo;
import org.apache.wiki.parser.PluginContent;
import org.apache.wiki.util.ClassUtil;
+import org.apache.wiki.util.FileUtil;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/providers/AbstractFileProvider.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/providers/AbstractFileProvider.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/providers/AbstractFileProvider.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/providers/AbstractFileProvider.java Wed Jan 9 19:59:27 2013
@@ -19,15 +19,11 @@
package org.apache.wiki.providers;
import java.io.*;
-import java.util.Properties;
-import java.util.Collection;
-import java.util.Date;
-import java.util.TreeSet;
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.log4j.Logger;
+import java.util.*;
+import org.apache.log4j.Logger;
import org.apache.wiki.*;
+import org.apache.wiki.util.FileUtil;
/**
* Provides a simple directory based repository for Wiki pages.
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/providers/BasicAttachmentProvider.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/providers/BasicAttachmentProvider.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/providers/BasicAttachmentProvider.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/providers/BasicAttachmentProvider.java Wed Jan 9 19:59:27 2013
@@ -18,29 +18,15 @@
*/
package org.apache.wiki.providers;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.IOException;
-import java.io.FileNotFoundException;
-import java.io.File;
-import java.io.FilenameFilter;
-import java.io.FileOutputStream;
-import java.io.FileInputStream;
-
-import java.util.Collection;
-import java.util.Properties;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.Date;
-import java.util.List;
-import java.util.Collections;
+import java.io.*;
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
-
import org.apache.wiki.*;
import org.apache.wiki.attachment.Attachment;
+import org.apache.wiki.util.FileUtil;
/**
* Provides basic, versioning attachments.
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/providers/RCSFileProvider.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/providers/RCSFileProvider.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/providers/RCSFileProvider.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/providers/RCSFileProvider.java Wed Jan 9 19:59:27 2013
@@ -18,22 +18,15 @@
*/
package org.apache.wiki.providers;
-import java.io.File;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.io.InputStream;
-import java.io.IOException;
-import java.util.Properties;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Date;
-import java.util.Iterator;
-import java.text.SimpleDateFormat;
+import java.io.*;
import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
import org.apache.log4j.Logger;
import org.apache.oro.text.regex.*;
-
import org.apache.wiki.*;
+import org.apache.wiki.util.FileUtil;
/**
* This class implements a simple RCS file provider. NOTE: You MUST
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/providers/VersioningFileProvider.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/providers/VersioningFileProvider.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/providers/VersioningFileProvider.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/providers/VersioningFileProvider.java Wed Jan 9 19:59:27 2013
@@ -19,15 +19,11 @@
package org.apache.wiki.providers;
import java.io.*;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Properties;
-import java.util.Date;
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.log4j.Logger;
+import java.util.*;
+import org.apache.log4j.Logger;
import org.apache.wiki.*;
+import org.apache.wiki.util.FileUtil;
/**
* Provides a simple directory based repository for Wiki pages.
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/rss/RSSThread.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/rss/RSSThread.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/rss/RSSThread.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/rss/RSSThread.java Wed Jan 9 19:59:27 2013
@@ -19,21 +19,11 @@
package org.apache.wiki.rss;
-import java.io.BufferedWriter;
-import java.io.File;
-
-
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.io.StringReader;
-import java.io.Writer;
+import java.io.*;
import org.apache.log4j.Logger;
-
-import org.apache.wiki.FileUtil;
import org.apache.wiki.WikiEngine;
+import org.apache.wiki.util.FileUtil;
import org.apache.wiki.util.WatchDog;
import org.apache.wiki.util.WikiBackgroundThread;
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/search/LuceneSearchProvider.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/search/LuceneSearchProvider.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/search/LuceneSearchProvider.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/search/LuceneSearchProvider.java Wed Jan 9 19:59:27 2013
@@ -49,6 +49,7 @@ import org.apache.wiki.parser.MarkupPars
import org.apache.wiki.providers.ProviderException;
import org.apache.wiki.providers.WikiPageProvider;
import org.apache.wiki.util.ClassUtil;
+import org.apache.wiki.util.FileUtil;
import org.apache.wiki.util.WatchDog;
import org.apache.wiki.util.WikiBackgroundThread;
Modified: incubator/jspwiki/trunk/src/org/apache/wiki/util/CommentedProperties.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/util/CommentedProperties.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/util/CommentedProperties.java (original)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/util/CommentedProperties.java Wed Jan 9 19:59:27 2013
@@ -24,7 +24,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
-import org.apache.wiki.FileUtil;
import org.apache.wiki.TextUtil;
/**
Added: incubator/jspwiki/trunk/src/org/apache/wiki/util/FileUtil.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/org/apache/wiki/util/FileUtil.java?rev=1431035&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/org/apache/wiki/util/FileUtil.java (added)
+++ incubator/jspwiki/trunk/src/org/apache/wiki/util/FileUtil.java Wed Jan 9 19:59:27 2013
@@ -0,0 +1,323 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+package org.apache.wiki.util;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CodingErrorAction;
+
+import org.apache.log4j.Logger;
+
+/**
+ * Generic utilities related to file and stream handling.
+ */
+public final class FileUtil
+{
+ /** Size of the buffer used when copying large chunks of data. */
+ private static final int BUFFER_SIZE = 4096;
+ private static final Logger log = Logger.getLogger(FileUtil.class);
+
+ /**
+ * Private constructor prevents instantiation.
+ */
+ private FileUtil()
+ {}
+
+ /**
+ * Makes a new temporary file and writes content into it. The temporary
+ * file is created using <code>File.createTempFile()</code>, and the usual
+ * semantics apply. The files are not deleted automatically in exit.
+ *
+ * @param content Initial content of the temporary file.
+ * @param encoding Encoding to use.
+ * @return The handle to the new temporary file
+ * @throws IOException If the content creation failed.
+ * @see java.io.File#createTempFile(String,String,File)
+ */
+ public static File newTmpFile( String content, String encoding )
+ throws IOException
+ {
+ Writer out = null;
+ Reader in = null;
+ File f = null;
+
+ try
+ {
+ f = File.createTempFile( "jspwiki", null );
+
+ in = new StringReader( content );
+
+ out = new OutputStreamWriter( new FileOutputStream( f ),
+ encoding );
+
+ copyContents( in, out );
+ }
+ finally
+ {
+ if( in != null ) in.close();
+ if( out != null ) out.close();
+ }
+
+ return f;
+ }
+
+ /**
+ * Creates a new temporary file using the default encoding
+ * of ISO-8859-1 (Latin1).
+ *
+ * @param content The content to put into the file.
+ * @throws IOException If writing was unsuccessful.
+ * @return A handle to the newly created file.
+ * @see #newTmpFile( String, String )
+ * @see java.io.File#createTempFile(String,String,File)
+ */
+ public static File newTmpFile( String content )
+ throws IOException
+ {
+ return newTmpFile( content, "ISO-8859-1" );
+ }
+
+ /**
+ * Runs a simple command in given directory.
+ * The environment is inherited from the parent process (e.g. the
+ * one in which this Java VM runs).
+ *
+ * @return Standard output from the command.
+ * @param command The command to run
+ * @param directory The working directory to run the command in
+ * @throws IOException If the command failed
+ * @throws InterruptedException If the command was halted
+ */
+ public static String runSimpleCommand( String command, String directory )
+ throws IOException,
+ InterruptedException
+ {
+ StringBuffer result = new StringBuffer();
+
+ log.info("Running simple command "+command+" in "+directory);
+
+ Process process = Runtime.getRuntime().exec( command, null, new File(directory) );
+
+ BufferedReader stdout = null;
+ BufferedReader stderr = null;
+
+ try
+ {
+ stdout = new BufferedReader( new InputStreamReader(process.getInputStream()) );
+ stderr = new BufferedReader( new InputStreamReader(process.getErrorStream()) );
+
+ String line;
+
+ while( (line = stdout.readLine()) != null )
+ {
+ result.append( line+"\n");
+ }
+
+ StringBuffer error = new StringBuffer();
+ while( (line = stderr.readLine()) != null )
+ {
+ error.append( line+"\n");
+ }
+
+ if( error.length() > 0 )
+ {
+ log.error("Command failed, error stream is: "+error);
+ }
+
+ process.waitFor();
+
+ }
+ finally
+ {
+ // we must close all by exec(..) opened streams: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692
+ process.getInputStream().close();
+ if( stdout != null ) stdout.close();
+ if( stderr != null ) stderr.close();
+ }
+
+ return result.toString();
+ }
+
+
+ /**
+ * Just copies all characters from <I>in</I> to <I>out</I>. The copying
+ * is performed using a buffer of bytes.
+ *
+ * @since 1.5.8
+ * @param in The reader to copy from
+ * @param out The reader to copy to
+ * @throws IOException If reading or writing failed.
+ */
+ public static void copyContents( Reader in, Writer out )
+ throws IOException
+ {
+ char[] buf = new char[BUFFER_SIZE];
+ int bytesRead = 0;
+
+ while ((bytesRead = in.read(buf)) > 0)
+ {
+ out.write(buf, 0, bytesRead);
+ }
+
+ out.flush();
+ }
+
+ /**
+ * Just copies all bytes from <I>in</I> to <I>out</I>. The copying is
+ * performed using a buffer of bytes.
+ *
+ * @since 1.9.31
+ * @param in The inputstream to copy from
+ * @param out The outputstream to copy to
+ * @throws IOException In case reading or writing fails.
+ */
+ public static void copyContents( InputStream in, OutputStream out )
+ throws IOException
+ {
+ byte[] buf = new byte[BUFFER_SIZE];
+ int bytesRead = 0;
+
+ while ((bytesRead = in.read(buf)) > 0)
+ {
+ out.write(buf, 0, bytesRead);
+ }
+
+ out.flush();
+ }
+
+ /**
+ * Reads in file contents.
+ * <P>
+ * This method is smart and falls back to ISO-8859-1 if the input stream does not
+ * seem to be in the specified encoding.
+ *
+ * @param input The InputStream to read from.
+ * @param encoding The encoding to assume at first.
+ * @return A String, interpreted in the "encoding", or, if it fails, in Latin1.
+ * @throws IOException If the stream cannot be read or the stream cannot be
+ * decoded (even) in Latin1
+ */
+ public static String readContents( InputStream input, String encoding )
+ throws IOException
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ FileUtil.copyContents( input, out );
+
+ ByteBuffer bbuf = ByteBuffer.wrap( out.toByteArray() );
+
+ Charset cset = Charset.forName( encoding );
+ CharsetDecoder csetdecoder = cset.newDecoder();
+
+ csetdecoder.onMalformedInput( CodingErrorAction.REPORT );
+ csetdecoder.onUnmappableCharacter( CodingErrorAction.REPORT );
+
+ try
+ {
+ CharBuffer cbuf = csetdecoder.decode( bbuf );
+
+ return cbuf.toString();
+ }
+ catch( CharacterCodingException e )
+ {
+ Charset latin1 = Charset.forName("ISO-8859-1");
+ CharsetDecoder l1decoder = latin1.newDecoder();
+
+ l1decoder.onMalformedInput( CodingErrorAction.REPORT );
+ l1decoder.onUnmappableCharacter( CodingErrorAction.REPORT );
+
+ try
+ {
+ bbuf = ByteBuffer.wrap( out.toByteArray() );
+
+ CharBuffer cbuf = l1decoder.decode( bbuf );
+
+ return cbuf.toString();
+ }
+ catch( CharacterCodingException ex )
+ {
+ throw (CharacterCodingException) ex.fillInStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Returns the full contents of the Reader as a String.
+ *
+ * @since 1.5.8
+ * @param in The reader from which the contents shall be read.
+ * @return String read from the Reader
+ * @throws IOException If reading fails.
+ */
+ public static String readContents( Reader in )
+ throws IOException
+ {
+ Writer out = null;
+
+ try
+ {
+ out = new StringWriter();
+
+ copyContents( in, out );
+
+ return out.toString();
+ }
+ finally
+ {
+ try
+ {
+ out.close();
+ }
+ catch( Exception e )
+ {
+ log.error("Not able to close the stream while reading contents.");
+ }
+ }
+ }
+
+ /**
+ * Returns the class and method name (+a line number) in which the
+ * Throwable was thrown.
+ *
+ * @param t A Throwable to analyze.
+ * @return A human-readable string stating the class and method. Do not rely
+ * the format to be anything fixed.
+ */
+ public static String getThrowingMethod( Throwable t )
+ {
+ StackTraceElement[] trace = t.getStackTrace();
+ StringBuffer sb = new StringBuffer();
+
+ if( trace == null || trace.length == 0 )
+ {
+ sb.append( "[Stack trace not available]" );
+ }
+ else
+ {
+ sb.append( trace[0].isNativeMethod() ? "native method" : "" );
+ sb.append( trace[0].getClassName() );
+ sb.append(".");
+ sb.append(trace[0].getMethodName()+"(), line "+trace[0].getLineNumber());
+ }
+ return sb.toString();
+ }
+}
Modified: incubator/jspwiki/trunk/src/webdocs/Error.jsp
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/webdocs/Error.jsp?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/webdocs/Error.jsp (original)
+++ incubator/jspwiki/trunk/src/webdocs/Error.jsp Wed Jan 9 19:59:27 2013
@@ -20,6 +20,7 @@
<%@ page isErrorPage="true" %>
<%@ page import="org.apache.log4j.*" %>
<%@ page import="org.apache.wiki.*" %>
+<%@ page import="org.apache.wiki.util.FileUtil" %>
<%@ taglib uri="/WEB-INF/jspwiki.tld" prefix="wiki" %>
<%!
Logger log = Logger.getLogger("JSPWiki");
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/AllTests.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/AllTests.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/AllTests.java Wed Jan 9 19:59:27 2013
@@ -62,7 +62,6 @@ public class AllTests extends TestCase
{
TestSuite suite = new TestSuite("JSPWiki Unit Tests");
- suite.addTest( FileUtilTest.suite() );
suite.addTest( PageManagerTest.suite() );
suite.addTest( PropertyReaderTest.suite() );
suite.addTest( ReferenceManagerTest.suite() );
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/TestEngine.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/TestEngine.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/TestEngine.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/TestEngine.java Wed Jan 9 19:59:27 2013
@@ -41,6 +41,7 @@ import org.apache.wiki.providers.Abstrac
import org.apache.wiki.providers.BasicAttachmentProvider;
import org.apache.wiki.providers.FileSystemProvider;
import org.apache.wiki.providers.ProviderException;
+import org.apache.wiki.util.FileUtil;
/**
* Simple test engine that always assumes pages are found.
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/WikiEngineTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/WikiEngineTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/WikiEngineTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/WikiEngineTest.java Wed Jan 9 19:59:27 2013
@@ -37,6 +37,7 @@ import org.apache.wiki.providers.BasicAt
import org.apache.wiki.providers.CachingProvider;
import org.apache.wiki.providers.FileSystemProvider;
import org.apache.wiki.providers.VerySimpleProvider;
+import org.apache.wiki.util.FileUtil;
public class WikiEngineTest extends TestCase
{
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/attachment/AttachmentManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/attachment/AttachmentManagerTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/attachment/AttachmentManagerTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/attachment/AttachmentManagerTest.java Wed Jan 9 19:59:27 2013
@@ -21,11 +21,11 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.wiki.FileUtil;
import org.apache.wiki.TestEngine;
import org.apache.wiki.WikiContext;
import org.apache.wiki.WikiPage;
import org.apache.wiki.providers.ProviderException;
+import org.apache.wiki.util.FileUtil;
public class AttachmentManagerTest extends TestCase
{
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/providers/BasicAttachmentProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/providers/BasicAttachmentProviderTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/providers/BasicAttachmentProviderTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/providers/BasicAttachmentProviderTest.java Wed Jan 9 19:59:27 2013
@@ -19,12 +19,22 @@
package org.apache.wiki.providers;
-import junit.framework.*;
-import java.io.*;
-import java.util.*;
-
-import org.apache.wiki.*;
-import org.apache.wiki.attachment.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.StringReader;
+import java.util.Date;
+import java.util.List;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.TextUtil;
+import org.apache.wiki.attachment.Attachment;
+import org.apache.wiki.util.FileUtil;
public class BasicAttachmentProviderTest extends TestCase
{
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/providers/CachingProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/providers/CachingProviderTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/providers/CachingProviderTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/providers/CachingProviderTest.java Wed Jan 9 19:59:27 2013
@@ -19,17 +19,20 @@
package org.apache.wiki.providers;
-import junit.framework.*;
-
+import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
-import java.io.File;
import java.io.StringReader;
-import java.util.*;
-
-import org.apache.log4j.*;
+import java.util.Properties;
-import org.apache.wiki.*;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.WikiPage;
+import org.apache.wiki.util.FileUtil;
public class CachingProviderTest extends TestCase
{
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/providers/FileSystemProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/providers/FileSystemProviderTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/providers/FileSystemProviderTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/providers/FileSystemProviderTest.java Wed Jan 9 19:59:27 2013
@@ -19,13 +19,21 @@
package org.apache.wiki.providers;
-import junit.framework.*;
-import java.io.*;
-import java.util.*;
-
-import org.apache.log4j.*;
-
-import org.apache.wiki.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.wiki.PageManager;
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.WikiEngine;
+import org.apache.wiki.WikiPage;
+import org.apache.wiki.util.FileUtil;
public class FileSystemProviderTest extends TestCase
{
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/providers/RCSFileProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/providers/RCSFileProviderTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/providers/RCSFileProviderTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/providers/RCSFileProviderTest.java Wed Jan 9 19:59:27 2013
@@ -19,13 +19,17 @@
package org.apache.wiki.providers;
-import junit.framework.*;
import java.io.*;
-import java.util.*;
+import java.util.List;
+import java.util.Properties;
-import org.apache.log4j.Logger;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.log4j.Logger;
import org.apache.wiki.*;
+import org.apache.wiki.util.FileUtil;
/**
* Tests the RCSFileProvider. If you are getting strange errors, please check that you
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/providers/VersioningFileProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/providers/VersioningFileProviderTest.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/providers/VersioningFileProviderTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/providers/VersioningFileProviderTest.java Wed Jan 9 19:59:27 2013
@@ -19,11 +19,20 @@
package org.apache.wiki.providers;
-import junit.framework.*;
import java.io.*;
-import java.util.*;
-
-import org.apache.wiki.*;
+import java.util.Collection;
+import java.util.List;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.wiki.PageManager;
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.WikiContext;
+import org.apache.wiki.WikiPage;
+import org.apache.wiki.util.FileUtil;
// FIXME: Should this thingy go directly to the VersioningFileProvider,
// or should it rely on the WikiEngine API?
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/stress/StressTestSpeed.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/stress/StressTestSpeed.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/stress/StressTestSpeed.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/stress/StressTestSpeed.java Wed Jan 9 19:59:27 2013
@@ -18,11 +18,16 @@
*/
package org.apache.wiki.stress;
-import junit.framework.*;
import java.io.*;
-import java.util.*;
-import org.apache.wiki.*;
-import org.apache.wiki.providers.*;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.wiki.TestEngine;
+import org.apache.wiki.providers.FileSystemProvider;
+import org.apache.wiki.util.FileUtil;
public final class StressTestSpeed extends TestCase
{
Modified: incubator/jspwiki/trunk/tests/org/apache/wiki/util/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/util/AllTests.java?rev=1431035&r1=1431034&r2=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/util/AllTests.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/util/AllTests.java Wed Jan 9 19:59:27 2013
@@ -19,8 +19,10 @@
package org.apache.wiki.util;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
-import junit.framework.*;
public class AllTests extends TestCase
{
@@ -36,6 +38,7 @@ public class AllTests extends TestCase
suite.addTest( ClassUtilTest.suite() );
suite.addTest( CommentedPropertiesTest.suite() );
suite.addTest( CryptoUtilTest.suite() );
+ suite.addTest( FileUtilTest.suite() );
suite.addTest( org.apache.wiki.util.comparators.AllTests.suite() );
suite.addTest( MailUtilTest.suite() );
suite.addTest( PageSorterTest.suite() );
Copied: incubator/jspwiki/trunk/tests/org/apache/wiki/util/FileUtilTest.java (from r1431004, incubator/jspwiki/trunk/tests/org/apache/wiki/FileUtilTest.java)
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/org/apache/wiki/util/FileUtilTest.java?p2=incubator/jspwiki/trunk/tests/org/apache/wiki/util/FileUtilTest.java&p1=incubator/jspwiki/trunk/tests/org/apache/wiki/FileUtilTest.java&r1=1431004&r2=1431035&rev=1431035&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/org/apache/wiki/FileUtilTest.java (original)
+++ incubator/jspwiki/trunk/tests/org/apache/wiki/util/FileUtilTest.java Wed Jan 9 19:59:27 2013
@@ -17,12 +17,20 @@
under the License.
*/
-package org.apache.wiki;
+package org.apache.wiki.util;
-import junit.framework.*;
-import java.io.*;
-import java.util.*;
-import org.apache.log4j.*;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.log4j.PropertyConfigurator;
+import org.apache.wiki.TestEngine;
public class FileUtilTest extends TestCase
{