[jboss-svn-commits] JBL Code SVN: r6084 - labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 5 09:19:45 EDT 2006


Author: wrzep
Date: 2006-09-05 09:19:42 -0400 (Tue, 05 Sep 2006)
New Revision: 6084

Added:
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CheckOutEditor.java
Modified:
   labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CodeLinesEvaluator.java
Log:
JBLAB-751
Evaluating # of code lines with JavaSVN ISVNEditor.
Pawel


Added: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CheckOutEditor.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CheckOutEditor.java	2006-09-05 09:42:46 UTC (rev 6083)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CheckOutEditor.java	2006-09-05 13:19:42 UTC (rev 6084)
@@ -0,0 +1,178 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.forge.status.plugins.codelines;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.LineNumberReader;
+import java.io.OutputStream;
+
+import org.jboss.logging.Logger;
+import org.tmatesoft.svn.core.SVNCommitInfo;
+import org.tmatesoft.svn.core.SVNException;
+import org.tmatesoft.svn.core.SVNProperty;
+import org.tmatesoft.svn.core.io.ISVNEditor;
+import org.tmatesoft.svn.core.io.diff.SVNDeltaProcessor;
+import org.tmatesoft.svn.core.io.diff.SVNDiffWindow;
+
+/**
+* @author Pawel Wrzeszcz (pawel . wrzeszcz [at] jboss . com)
+*/
+
+public class CheckOutEditor implements ISVNEditor {
+
+	private long codeLines;
+	
+	private boolean isTextMimeType;
+
+	private SVNDeltaProcessor myDeltaProcessor;
+	
+	private File file;
+	
+	private Logger log;
+	
+	
+	public CheckOutEditor() {
+		
+		myDeltaProcessor = new SVNDeltaProcessor();
+		
+		log = Logger.getLogger(CheckOutEditor.class);
+	}
+	
+	public long getCodeLines() {
+		
+		return codeLines;
+	}
+	
+	
+	/* Editor methods */
+
+	public void targetRevision(long arg0) throws SVNException {
+		
+		codeLines = 0;
+	}
+
+	
+	public void addFile(String path, String copyFromPath, long copyFromRevision)
+			throws SVNException {
+		
+		log.debug("Adding file " + path + "(" + codeLines + ")");
+		
+		isTextMimeType = true;
+	}
+
+	public void changeFileProperty(String path, String name, String value) 
+			throws SVNException {
+		
+		if (name.equals(SVNProperty.MIME_TYPE)) {
+					
+			isTextMimeType = value.startsWith("text/");
+		}
+	}
+
+	public void applyTextDelta(String path, String baseChecksum) throws SVNException {
+		
+		if (!isTextMimeType) {
+			return;
+		}
+		
+		try {
+			file = File.createTempFile("JBoss", "CodeLinesEvaluator");
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		
+		myDeltaProcessor.applyTextDelta(null, file, false);
+	}
+
+	public OutputStream textDeltaChunk(String path, SVNDiffWindow diffWindow) 
+			throws SVNException {
+		
+		if (!isTextMimeType) {
+			return null;
+		}
+		
+		return myDeltaProcessor.textDeltaChunk(diffWindow);
+	}
+
+	public void textDeltaEnd(String path) throws SVNException {
+
+		if (!isTextMimeType) {
+			return;
+		}
+		
+		myDeltaProcessor.textDeltaEnd();
+		
+		FileReader fileReader;
+		try {
+			
+			fileReader = new FileReader(file);
+			LineNumberReader lnReader = new LineNumberReader(fileReader);
+			lnReader.skip(file.length());
+			
+			codeLines += lnReader.getLineNumber();
+			
+			log.debug("File: " + path + " Number of code lines: "
+											+ lnReader.getLineNumber() + ".");
+			
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		
+		file.delete();
+	}
+
+	/* Empty methods */
+	
+	public void abortEdit() throws SVNException {}
+
+	public void absentDir(String arg0) throws SVNException {}
+
+	public void absentFile(String arg0) throws SVNException {}
+
+	public void addDir(String arg0, String arg1, long arg2)
+														throws SVNException {}
+	
+	public void changeDirProperty(String arg0, String arg1) throws SVNException {}
+
+	public void closeDir() throws SVNException {}
+	
+	public void closeFile(String arg0, String arg1) throws SVNException {}
+
+	public SVNCommitInfo closeEdit() throws SVNException {
+		
+		return null;
+	}
+
+	public void deleteEntry(String arg0, long arg1) throws SVNException {}
+
+	public void openDir(String arg0, long arg1) throws SVNException {}
+
+	public void openFile(String arg0, long arg1) throws SVNException {}
+	
+	public void openRoot(long arg0) throws SVNException {}
+}

Modified: labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CodeLinesEvaluator.java
===================================================================
--- labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CodeLinesEvaluator.java	2006-09-05 09:42:46 UTC (rev 6083)
+++ labs/jbosslabs/trunk/portal-extensions/forge-status/src/java/org/jboss/forge/status/plugins/codelines/CodeLinesEvaluator.java	2006-09-05 13:19:42 UTC (rev 6084)
@@ -27,7 +27,6 @@
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Date;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.HashMap;
 
@@ -84,65 +83,20 @@
 		
 		SVNRepository repository = getRepository(url, userName, password);
 		
-		if (repository == null ) {
-			return 0;
-		}
-			
-		SVNRevision fromRev = getFirstRevision(repository);
-										
-		long ret = 0;
+		CheckOutEditor editor = new CheckOutEditor();
 		
 		try {
-			ret = getAddedCodeLines(repository, "", fromRev, SVNRevision.HEAD);
+			repository.checkout(SVNRevision.HEAD.getNumber(), "", true, editor);
 		} catch (SVNException e) {
 			e.printStackTrace();
+			return 0;
+			//TODO exceptions should be handled inside plugin not evaluator
 		}
         
-        return ret;
+        return editor.getCodeLines();
 	}
 
-	private static SVNRevision getFirstRevision(SVNRepository repository) {
-		
-		long left = 0;
-		long right;
-		
-		try {
-			right = repository.getLatestRevision();
-		} catch (SVNException e1) {
-			e1.printStackTrace();
-			return null;
-		}
-		long mid;
-		
-	    while (left < right) {
-	    	
-	    		mid = Math.round(Math.floor((left+right)/2));
-	    
-	    		if (isValidRevision(repository, mid)) {
-	    			right = mid;
-	    		} else {
-	    			left = mid + 1;
-	    		}
-	    }
-	    
-	    System.out.println("The oldest revision: " + left);
-		
-	    return SVNRevision.create(left);		
-	}
 
-	private static boolean isValidRevision(SVNRepository repository,
-												long rev) {
-		
-		try {
-			repository.getDir(".", rev, false, new HashSet());
-		} catch (SVNException e) {
-			System.out.println(rev + " is NOT valid revision (" + e.getMessage() + ")");
-			return false;
-		}
-		
-		return true;
-	}
-
 	/**
 	 * Evaluates number of code lines in files located in anonymus SVN repository.
 	 * 
@@ -281,19 +235,24 @@
         			"\nCause:" + svne.getMessage());
             return 0;
         }
-    
+        
 		/* Get file type */
         String mimeType = (String) fileProperties.get(SVNProperty.MIME_TYPE);
         boolean isTextType = SVNProperty.isTextMimeType(mimeType);
  
         /* Count number of lines */
         if (isTextType) {
-            cl = baos.toString().split("\n").length;
+            cl = estimateCodeLinesNumber(baos.toString());
         }
         
         return cl;
 	}
 
+	public static long estimateCodeLinesNumber(String string) {
+		
+		return string.split("\n").length;
+	}
+
 	/**
 	 * Returns SVN directory entries.
 	 * 




More information about the jboss-svn-commits mailing list