Printing Out File Contents
In this example we will study how to read contents of a file from a particular revision of a repository. The first steps are just like in the previous example:
1 ...
2
3 public class DisplayFile {
4
5 public static void main( String[] args ) {
6 DAVRepositoryFactory.setup( );
7
8 String url = "http://svn.svnkit.com/repos/svnkit/trunk";
9 String name = "anonymous";
10 String password = "anonymous";
11 String filePath = "www/license.html";
12
13 SVNRepository repository = null;
14 try {
15 repository = SVNRepositoryFactory.create( SVNURL.parseURIEncoded( url ) );
16 ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( name , password );
17 repository.setAuthenticationManager( authManager );
18
19 SVNNodeKind nodeKind = repository.checkPath( filePath , -1 );
20
21 if ( nodeKind == SVNNodeKind.NONE ) {
22 System.err.println( "There is no entry at '" + url + "'." );
23 System.exit( 1 );
24 } else if ( nodeKind == SVNNodeKind.DIR ) {
25 System.err.println( "The entry at '" + url + "' is a directory while a file was expected." );
26 System.exit( 1 );
27 }
28
29 ...
Now when we are sure that our url corresponds to a file we read its contents into an OutputStream and collect its properties into a Map.
Then we find out whether the file is a text or binary file using the MIME type property. If it's a binary we don't print its contents. We also print out versioned file properties.
1 ...
2 String mimeType = ( String ) fileProperties.get( SVNProperty.MIME_TYPE );
3 boolean isTextType = SVNProperty.isTextMimeType( mimeType );
4
5 Iterator iterator = fileProperties.keySet( ).iterator( );
6 while ( iterator.hasNext( ) ) {
7 String propertyName = ( String ) iterator.next( );
8 String propertyValue = ( String ) fileProperties.get( propertyName );
9 System.out.println( "File property: " + propertyName + "=" + propertyValue );
10 }
11
12 if ( isTextType ) {
13 System.out.println( "File contents:" );
14 System.out.println( );
15 try {
16 baos.writeTo( System.out );
17 } catch ( IOException ioe ) {
18 ioe.printStackTrace( );
19 }
20 } else {
21 System.out.println( "Not a text file." );
22 }
23 ...
And finally we run the program and have the following output in our console:
File property: svn:entry:revision=2802 File property: svn:entry:checksum=435f2f0d33d12907ddb6dfd611825ec9 File property: svn:wc:ra_dav:version-url=/repos/svnkit/!svn/ver/2795/trunk/www/license.html File property: svn:entry:last-author=alex File property: svn:entry:committed-date=2006-11-13T21:34:27.908657Z File property: svn:entry:committed-rev=2795 File contents: <html> <head> <link rel="shortcut icon" href="img/favicon.ico"/> <title>SVNKit :: License</title> </head> <body> <h1>The TMate Open Source License.</h1> <pre> ...................................... --------------------------------------------- Repository latest revision: 2802
Download the example program source code.