* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
[svn.git] / subversion / bindings / javahl / native / CopySources.cpp
blob86ec1be18935e99b2f1dd96389468c80cc1de65f
1 /**
2 * @copyright
3 * ====================================================================
4 * Copyright (c) 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 CopySources.cpp
19 * @brief Implementation of the class CopySources
22 #include <apr_pools.h>
23 #include "svn_client.h"
25 #include "Pool.h"
26 #include "JNIUtil.h"
27 #include "JNIStringHolder.h"
28 #include "Revision.h"
29 #include "CopySources.h"
31 CopySources::CopySources(jobjectArray jcopySources)
33 m_copySources = jcopySources;
36 CopySources::~CopySources()
38 // m_copySources does not need to be destroyed, because it is a
39 // parameter to the Java SVNClient.copy() method, and thus not
40 // explicitly managed.
43 jobject
44 CopySources::makeJCopySource(const char *path, svn_revnum_t rev, Pool &pool)
46 JNIEnv *env = JNIUtil::getEnv();
48 jobject jpath = JNIUtil::makeJString(path);
49 if (JNIUtil::isJavaExceptionThrown())
50 return NULL;
52 jobject jrevision = Revision::makeJRevision(rev);
53 if (JNIUtil::isJavaExceptionThrown())
54 return NULL;
56 jclass clazz = env->FindClass(JAVA_PACKAGE "/CopySource");
57 if (JNIUtil::isJavaExceptionThrown())
58 return NULL;
60 static jmethodID ctor = 0;
61 if (ctor == 0)
63 ctor = env->GetMethodID(clazz, "<init>",
64 "(Ljava/lang/String;"
65 "L" JAVA_PACKAGE "/Revision;"
66 "L" JAVA_PACKAGE "/Revision;)V");
67 if (JNIUtil::isExceptionThrown())
68 return NULL;
71 jobject jcopySource = env->NewObject(clazz, ctor, jpath, jrevision, NULL);
72 if (JNIUtil::isJavaExceptionThrown())
73 return NULL;
75 env->DeleteLocalRef(jpath);
76 if (JNIUtil::isJavaExceptionThrown())
77 return NULL;
79 env->DeleteLocalRef(jrevision);
80 if (JNIUtil::isJavaExceptionThrown())
81 return NULL;
83 return jcopySource;
86 apr_array_header_t *
87 CopySources::array(Pool &pool)
89 apr_pool_t *p = pool.pool();
90 if (m_copySources == NULL)
91 return apr_array_make(p, 0, sizeof(svn_client_copy_source_t *));
93 JNIEnv *env = JNIUtil::getEnv();
94 jint nbrSources = env->GetArrayLength(m_copySources);
95 if (JNIUtil::isJavaExceptionThrown())
96 return NULL;
98 jclass clazz = env->FindClass(JAVA_PACKAGE "/CopySource");
99 if (JNIUtil::isJavaExceptionThrown())
100 return NULL;
102 apr_array_header_t *copySources =
103 apr_array_make(p, nbrSources, sizeof(svn_client_copy_source_t *));
104 for (int i = 0; i < nbrSources; ++i)
106 jobject copySource = env->GetObjectArrayElement(m_copySources, i);
107 if (JNIUtil::isJavaExceptionThrown())
108 return NULL;
110 if (env->IsInstanceOf(copySource, clazz))
112 svn_client_copy_source_t *src =
113 (svn_client_copy_source_t *) apr_palloc(p, sizeof(*src));
115 // Extract the path or URL from the copy source.
116 static jmethodID getPath = 0;
117 if (getPath == 0)
119 getPath = env->GetMethodID(clazz, "getPath",
120 "()Ljava/lang/String;");
121 if (JNIUtil::isJavaExceptionThrown() || getPath == 0)
122 return NULL;
124 jstring jpath = (jstring)
125 env->CallObjectMethod(copySource, getPath);
126 if (JNIUtil::isJavaExceptionThrown())
127 return NULL;
129 JNIStringHolder path(jpath);
130 if (JNIUtil::isJavaExceptionThrown())
131 return NULL;
133 src->path = apr_pstrdup(p, (const char *) path);
134 SVN_JNI_ERR(JNIUtil::preprocessPath(src->path, pool.pool()),
135 NULL);
136 env->DeleteLocalRef(jpath);
137 if (JNIUtil::isJavaExceptionThrown())
138 return NULL;
140 // Extract source revision from the copy source.
141 static jmethodID getRevision = 0;
142 if (getRevision == 0)
144 getRevision = env->GetMethodID(clazz, "getRevision",
145 "()L"JAVA_PACKAGE"/Revision;");
146 if (JNIUtil::isJavaExceptionThrown() || getRevision == 0)
147 return NULL;
149 jobject jrev = env->CallObjectMethod(copySource, getRevision);
150 if (JNIUtil::isJavaExceptionThrown())
151 return NULL;
153 // TODO: Default this to svn_opt_revision_undefined (or HEAD)
154 Revision rev(jrev);
155 src->revision = (const svn_opt_revision_t *)
156 apr_palloc(p, sizeof(*src->revision));
157 memcpy((void *) src->revision, rev.revision(),
158 sizeof(*src->revision));
159 env->DeleteLocalRef(jrev);
160 if (JNIUtil::isJavaExceptionThrown())
161 return NULL;
163 // Extract pegRevision from the copy source.
164 static jmethodID getPegRevision = 0;
165 if (getPegRevision == 0)
167 getPegRevision = env->GetMethodID(clazz, "getRevision",
168 "()L"JAVA_PACKAGE"/Revision;");
169 if (JNIUtil::isJavaExceptionThrown() || getPegRevision == 0)
170 return NULL;
172 jobject jPegRev = env->CallObjectMethod(copySource,
173 getPegRevision);
174 if (JNIUtil::isJavaExceptionThrown())
175 return NULL;
177 Revision pegRev(jPegRev, true);
178 src->peg_revision = (const svn_opt_revision_t *)
179 apr_palloc(p, sizeof(*src->peg_revision));
180 memcpy((void *) src->peg_revision, pegRev.revision(),
181 sizeof(*src->peg_revision));
182 env->DeleteLocalRef(jPegRev);
183 if (JNIUtil::isJavaExceptionThrown())
184 return NULL;
186 APR_ARRAY_PUSH(copySources, svn_client_copy_source_t *) = src;
188 env->DeleteLocalRef(copySource);
189 if (JNIUtil::isJavaExceptionThrown())
190 return NULL;
193 env->DeleteLocalRef(clazz);
194 if (JNIUtil::isJavaExceptionThrown())
195 return NULL;
197 return copySources;