* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
[svn.git] / subversion / bindings / javahl / native / BlameCallback.cpp
blobde4d1c36c7577dae098995f1f9be5b3e3ec48928
1 /**
2 * @copyright
3 * ====================================================================
4 * Copyright (c) 2003-2007 CollabNet. All rights reserved.
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
12 * This software consists of voluntary contributions made by many
13 * individuals. For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
16 * @endcopyright
18 * @file BlameCallback.cpp
19 * @brief Implementation of the class BlameCallback
22 #include "BlameCallback.h"
23 #include "JNIUtil.h"
24 #include "svn_time.h"
25 /**
26 * Create a BlameCallback object
27 * @param jcallback the Java callback object.
29 BlameCallback::BlameCallback(jobject jcallback)
31 m_callback = jcallback;
33 /**
34 * Destroy a BlameCallback object
36 BlameCallback::~BlameCallback()
38 // the m_callback does not need to be destroyed, because it is the passed
39 // in parameter to the Java SVNClient.blame method.
42 svn_error_t *
43 BlameCallback::callback(void *baton,
44 apr_int64_t line_no,
45 svn_revnum_t revision,
46 const char *author,
47 const char *date,
48 svn_revnum_t merged_revision,
49 const char *merged_author,
50 const char *merged_date,
51 const char *merged_path,
52 const char *line,
53 apr_pool_t *pool)
55 if (baton)
56 return ((BlameCallback *)baton)->singleLine(revision, author, date,
57 merged_revision, merged_author,
58 merged_date, merged_path, line,
59 pool);
61 return SVN_NO_ERROR;
64 /**
65 * Callback called for a single line in the file, for which the blame
66 * information was requested
67 * @param revision the revision number, when the line was last changed
68 * or -1, if not changed during the request revision
69 * interval
70 * @param author the author, who performed the last change of the line
71 * @param date the date of the last change of the line
72 * @param line the content of the line
73 * @param pool memory pool for the use of this function
75 svn_error_t *
76 BlameCallback::singleLine(svn_revnum_t revision, const char *author,
77 const char *date, svn_revnum_t mergedRevision,
78 const char *mergedAuthor, const char *mergedDate,
79 const char *mergedPath, const char *line,
80 apr_pool_t *pool)
82 JNIEnv *env = JNIUtil::getEnv();
84 // The method id will not change during the time this library is
85 // loaded, so it can be cached.
86 static jmethodID mid = 0;
87 if (mid == 0)
89 jclass clazz = env->FindClass(JAVA_PACKAGE"/BlameCallback2");
90 if (JNIUtil::isJavaExceptionThrown())
91 return SVN_NO_ERROR;
93 mid = env->GetMethodID(clazz, "singleLine",
94 "(Ljava/util/Date;JLjava/lang/String;"
95 "Ljava/util/Date;JLjava/lang/String;"
96 "Ljava/lang/String;Ljava/lang/String;)V");
97 if (JNIUtil::isJavaExceptionThrown() || mid == 0)
98 return SVN_NO_ERROR;
100 env->DeleteLocalRef(clazz);
101 if (JNIUtil::isJavaExceptionThrown())
102 return SVN_NO_ERROR;
105 // convert the parameters to their Java relatives
106 jstring jauthor = JNIUtil::makeJString(author);
107 if (JNIUtil::isJavaExceptionThrown())
108 return SVN_NO_ERROR;
110 jobject jdate = NULL;
111 if (date != NULL && *date != '\0')
113 apr_time_t timeTemp;
114 svn_error_t *err = svn_time_from_cstring(&timeTemp, date, pool);
115 if (err != SVN_NO_ERROR)
116 return err;
118 jdate = JNIUtil::createDate(timeTemp);
119 if (JNIUtil::isJavaExceptionThrown())
120 return SVN_NO_ERROR;
123 jstring jmergedAuthor = JNIUtil::makeJString(mergedAuthor);
124 if (JNIUtil::isJavaExceptionThrown())
125 return SVN_NO_ERROR;
127 jobject jmergedDate = NULL;
128 if (mergedDate != NULL && *mergedDate != '\0')
130 apr_time_t timeTemp;
131 svn_error_t *err = svn_time_from_cstring(&timeTemp, mergedDate, pool);
132 if (err != SVN_NO_ERROR)
133 return err;
135 jmergedDate = JNIUtil::createDate(timeTemp);
136 if (JNIUtil::isJavaExceptionThrown())
137 return SVN_NO_ERROR;
140 jstring jmergedPath = JNIUtil::makeJString(mergedPath);
141 if (JNIUtil::isJavaExceptionThrown())
142 return SVN_NO_ERROR;
144 jstring jline = JNIUtil::makeJString(line);
145 if (JNIUtil::isJavaExceptionThrown())
146 return SVN_NO_ERROR;
148 // call the Java method
149 env->CallVoidMethod(m_callback, mid, jdate, (jlong)revision, jauthor,
150 jmergedDate, (jlong)mergedRevision, jmergedAuthor,
151 jmergedPath, jline);
152 if (JNIUtil::isJavaExceptionThrown())
153 return SVN_NO_ERROR;
155 // cleanup the temporary Java objects
156 env->DeleteLocalRef(jauthor);
157 if (JNIUtil::isJavaExceptionThrown())
158 return SVN_NO_ERROR;
160 env->DeleteLocalRef(jdate);
161 if (JNIUtil::isJavaExceptionThrown())
162 return SVN_NO_ERROR;
164 env->DeleteLocalRef(jmergedAuthor);
165 if (JNIUtil::isJavaExceptionThrown())
166 return SVN_NO_ERROR;
168 env->DeleteLocalRef(jmergedDate);
169 if (JNIUtil::isJavaExceptionThrown())
170 return SVN_NO_ERROR;
172 env->DeleteLocalRef(jmergedPath);
173 if (JNIUtil::isJavaExceptionThrown())
174 return SVN_NO_ERROR;
176 env->DeleteLocalRef(jline);
177 // No need to check for an exception here, because we return anyway.
179 return SVN_NO_ERROR;