/*******************************************************************************
 * Copyright (c) 2004, 2006 svnClientAdapter project and others.
 *
 * Licensed 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.
 *
 * Contributors:
 *     svnClientAdapter project committers - initial API and implementation
 ******************************************************************************/
package org.tigris.subversion.svnclientadapter.javahl;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import org.apache.subversion.javahl.ClientException;
import org.apache.subversion.javahl.callback.BlameCallback;
import org.tigris.subversion.svnclientadapter.Annotations;
import org.tigris.subversion.svnclientadapter.utils.SafeSimpleDateFormat;

/**
 * JavaHL specific subclass of {@link Annotations}.
 * It implements a {@link org.tigris.subversion.javahl.BlameCallback}
 * as means of constructing the annotation records.  
 * 
 */
public class JhlAnnotations extends Annotations implements BlameCallback {

	private static final SafeSimpleDateFormat df = new SafeSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
	
	private void singleLine(Date changed, long revision, String author,
			Date merged_date, long merged_revision, String merged_author,
			String mergedPath, String line) {
		if (merged_revision == -1 || revision <= merged_revision)
			addAnnotation(new Annotation(revision, author, changed, line));
		else
			addAnnotation(new Annotation(merged_revision, merged_author, merged_date, line));
	}

	public void singleLine(long lineNum, long revision,
			Map<String, byte[]> revProps, long mergedRevision,
			Map<String, byte[]> mergedRevProps, String mergedPath, String line,
			boolean localChange) throws ClientException {

		String author = null;
		String mergedAuthor = null;
		
		byte[] authorBytes = revProps.get("svn:author");		
		if (authorBytes != null) {
			try {
				author = new String(revProps.get("svn:author"), "UTF8");
			} catch (UnsupportedEncodingException e) {
				author = new String(revProps.get("svn:author"));
			}
		}
		else {
			author = "unknown";
		}
		
		if (mergedRevProps != null) {			
			byte[] mergedAuthorBytes = mergedRevProps.get("svn:author");
			if (mergedAuthorBytes != null) {
				try {
					mergedAuthor = new String(mergedRevProps.get("svn:author"), "UTF8");
				} catch (UnsupportedEncodingException e) {
					mergedAuthor = new String(mergedRevProps.get("svn:author"));
				}
			}
			else {
				mergedAuthor = "unknown";
			}
		}
		
		try {
            singleLine(
                df.parse(new String(revProps.get("svn:date"))),
                revision,
                author,
                mergedRevProps == null ? null
                    : df.parse(new String(mergedRevProps.get("svn:date"))),
                mergedRevision,
                mergedAuthor,
                mergedPath, line);
        } catch (ParseException e) {
            throw ClientException.fromException(e);
        }
	}
	
}

@@ -2349,16 +2349,43 @@
 ull;%0A%09%09%0A
+%09%09if (revProps != null) %7B%0A%09
 %09%09byte%5B%5D
@@ -2427,24 +2427,25 @@
 thor%22);%09%09%0A%09%09
+%09
 if (authorBy
@@ -2459,16 +2459,17 @@
 null) %7B%0A
+%09
 %09%09%09try %7B
@@ -2469,24 +2469,25 @@
 %09%09try %7B%0A%09%09%09%09
+%09
 author = new
@@ -2531,16 +2531,17 @@
 UTF8%22);%0A
+%09
 %09%09%09%7D cat
@@ -2578,24 +2578,25 @@
 on e) %7B%0A%09%09%09%09
+%09
 author = new
@@ -2639,23 +2639,27 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A%09%09
+%09
 else %7B%0A
+%09
 %09%09%09a
@@ -2677,16 +2677,21 @@
 known%22;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09
@@ -3087,17 +3087,41 @@
 %7D%0A%09%09
-%0A
+if (revProps != null) %7B%0A%09
 %09%09try %7B%0A
     
@@ -3116,16 +3116,17 @@
 %09%09try %7B%0A
+%09
         
@@ -3141,16 +3141,17 @@
 leLine(%0A
+%09
         
@@ -3198,32 +3198,33 @@
 (%22svn:date%22))),%0A
+%09
                 
@@ -3225,32 +3225,33 @@
       revision,%0A
+%09
                 
@@ -3254,24 +3254,25 @@
     author,%0A
+%09
             
@@ -3305,16 +3305,17 @@
  ? null%0A
+%09
         
@@ -3382,16 +3382,17 @@
 te%22))),%0A
+%09
         
@@ -3415,16 +3415,17 @@
 vision,%0A
+%09
         
@@ -3446,16 +3446,17 @@
 Author,%0A
+%09
         
@@ -3478,24 +3478,25 @@
 ath, line);%0A
+%09
         %7D ca
@@ -3520,16 +3520,17 @@
 on e) %7B%0A
+%09
         
@@ -3573,16 +3573,17 @@
 ion(e);%0A
+%09
         
@@ -3584,15 +3584,19 @@
       %7D%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%7D%0A

Fix show annotations NPE when revision range includes unchanged lines Issue #: 1589