Differences between revisions 2 and 3
Revision 2 as of 2007-02-20 14:17:47
Size: 4408
Editor: host-85-118-226-179
Comment:
Revision 3 as of 2008-02-20 19:57:24
Size: 5310
Editor: nat7
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Main]]
 .+ [[What_Is_Subversion_For|What Is Subversion For]]
 .+ [[Setting_Up_A_Subversion_Repository|Setting Up A Subversion Repository]]
 .+ [[SVNKit_Architecture|SVNKit Architecture]]
 .+ [[Getting_Started_With_SVNKit|Getting Started With SVNKit]]
 .- Authentication
 .+ [[Managing_Repository_With_SVNKit|Managing Repository With SVNKit]]
  .+ [[Printing_Out_A_Subversion_Repository_Tree|Printing Out A Subversion Repository Tree]]
  .+ [[Printing_Out_File_Contents|Printing Out File Contents]]
  .+ [[Printing_Out_Repository_History|Printing Out Repository History]]
  .+ [[Committing_To_A_Repository|Editing operation: committing to a repository]]
  .+ [[Updating_From_A_Repository|Editing Operation: receiving changes from a repository]]
  .+ [[Replicating_An_Existing_Repository|Replicating An Existing Repository]]
  .+ [[Managing_A_Working_Copy|Managing A Working Copy]]

Main

["Main"]

  • + ["What Is Subversion For"]
  • + ["Setting Up A Subversion Repository"]
  • + ["SVNKit Architecture"]
  • + ["Getting Started With SVNKit"]
  • + ["Authentication"]
  • + ["Managing Repository With SVNKit"]
    • + ["Printing Out A Subversion Repository Tree"]
    • - Printing Out File Contents
    • + ["Printing Out Repository History"]
    • + [:Committing To A Repository:Editing operation: committing to a repository]

    • + [:Updating From A Repository:Editing Operation: receiving changes from a repository]

    • + ["Replicating An Existing Repository"]
    • + ["Managing A Working Copy"]


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.

   1             ...
   2             Map fileProperties = new HashMap( );
   3             ByteArrayOutputStream baos = new ByteArrayOutputStream( );
   4             repository.getFile( filePath , -1 , fileProperties , baos );
   5             ...

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&nbsp;::&nbsp;License</title>
</head>
<body>
<h1>The TMate Open Source License.</h1>
<pre>
......................................
---------------------------------------------
Repository latest revision: 2802


Download the [http://svn.svnkit.com/repos/svnkit/trunk/doc/examples/src/org/tmatesoft/svn/examples/repository/DisplayFile.java example program source code].

Printing_Out_File_Contents (last edited 2012-01-03 18:08:01 by ip-109-80-120-205)