You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ea...@apache.org on 2007/02/03 17:57:15 UTC
svn commit: r503250 [5/9] - /incubator/uima/uimacpp/trunk/src/framework/
Added: incubator/uima/uimacpp/trunk/src/framework/internal_jedii_engine.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/internal_jedii_engine.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/internal_jedii_engine.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/internal_jedii_engine.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,1049 @@
+/** \file internal_jedii_engine.cpp .
+-----------------------------------------------------------------------------
+
+
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+
+-----------------------------------------------------------------------------
+
+ Description:
+
+-----------------------------------------------------------------------------
+
+
+-------------------------------------------------------------------------- */
+
+//#define DEBUG_VERBOSE
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+#include "uima/pragmas.hpp"
+
+#include "uima/trace.hpp"
+#include "uima/envvar.hpp"
+#include "uima/macros.h"
+#include "uima/macros.h"
+
+#include "uima/stltools.hpp"
+#include "uima/msg.h"
+#include "uima/internal_jedii_engine.hpp"
+#include "uima/internal_casserializer.hpp"
+#include "uima/internal_casdeserializer.hpp"
+#include "uima/annotator_context.hpp"
+#include "uima/resmgr.hpp"
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Implementation */
+/* ----------------------------------------------------------------------- */
+
+/*
+There's a lot of JNI code in here. If you are interested in the details you should
+consult some book or go to
+http://java.sun.com/j2se/1.3/docs/guide/jni/spec/jniTOC.doc.html
+*/
+
+namespace uima {
+ namespace internal {
+
+ UIMA_EXC_CLASSIMPLEMENT(JavaException, Exception);
+
+#define USED_JAVA_VERSION JNI_VERSION_1_2
+
+ // not NLS enabled !!!!!!!
+ icu::UnicodeString javaErrorToString(jint res) {
+ switch (res) {
+ case JNI_OK:
+ return "no error";
+ case JNI_ERR:
+ return "unknown error";
+ case JNI_EDETACHED:
+ return "thread detached from the VM";
+ case JNI_EVERSION:
+ return "JNI version error";
+ case JNI_ENOMEM:
+ return "not enough memory";
+ case JNI_EEXIST:
+ return "VM already created";
+ case JNI_EINVAL:
+ return "invalid arguments";
+ default:
+ char buf[2056];
+ sprintf(buf, "%d", res);
+ return icu::UnicodeString(buf);
+ }
+ }
+
+ JEDIIEngine::JEDIIEngine(AnnotatorContext & anc, bool bOwnsANC,
+ bool bOwnsTAESpecififer, uima::internal::CASDefinition & casdef, bool ownsCasDef)
+ : EngineBase(anc, bOwnsANC, bOwnsTAESpecififer, casdef, ownsCasDef),
+ iv_vm(NULL),
+ iv_env(NULL),
+ iv_obj(NULL),
+ iv_createdVM(false),
+ iv_initializeMethod(NULL),
+ iv_processMethod(NULL),
+ iv_destroyMethod(NULL),
+ iv_getLastExceptionStringMethod(NULL),
+ iv_getHeapMethod(NULL),
+ iv_getIndexedFSsMethod(NULL),
+ iv_getStringTableMethod(NULL),
+ iv_getVersionMethod(NULL),
+ iv_isPrimitive(0),
+ iv_JVMDllLoader(NULL),
+ iv_createJavaVMFunc(NULL),
+ iv_getCreatedJavaVMsFunc(NULL) {
+ }
+
+
+ JEDIIEngine::~JEDIIEngine() {
+ destroyIfNeeded();
+ }
+
+
+ void JEDIIEngine::checkForUnexpectedJNIException() {
+ jthrowable exc = iv_env->ExceptionOccurred();
+ if (exc != NULL) {
+#ifndef NDEBUG
+ iv_env->ExceptionDescribe();
+#endif
+ // if exception was thrown, get class name of exception plus the message
+ // and throw a C++ exception
+
+ // don't check for JNI exceptions in these calls because exception is already thrown
+ jclass throwableClass = iv_env->FindClass("java/lang/Throwable");
+ jmethodID getClassID = iv_env->GetMethodID(throwableClass, "getClass", "()Ljava/lang/Class;");
+ assert( getClassID != NULL);
+ jclass classClass = iv_env->FindClass("java/lang/Class");
+ assert( classClass != NULL);
+ jmethodID getNameID = iv_env->GetMethodID(classClass, "getName", "()Ljava/lang/String;");
+ assert( getNameID != NULL );
+ jobject classObject = iv_env->CallObjectMethod(exc, getClassID);
+ assert ( classObject != NULL );
+ jstring msg = (jstring) iv_env->CallObjectMethod(classObject, getNameID);
+ jboolean copied;
+ jchar const * jc = iv_env->GetStringChars(msg, &copied);
+ assert( jc != NULL );
+ size_t len = iv_env->GetStringLength(msg);
+ icu::UnicodeString usMsg((UChar const *)jc, len);
+ iv_env->ReleaseStringChars(msg, jc);
+ // msg now contains the class name
+
+ // add getMessage() of the throwable
+ assert( throwableClass != NULL );
+ jmethodID getMessageID = iv_env->GetMethodID(throwableClass, "getMessage", "()Ljava/lang/String;");
+ assert ( getMessageID != NULL );
+ msg = (jstring) iv_env->CallObjectMethod(exc, getMessageID);
+ if ( msg != NULL ) {
+ usMsg.append(": ");
+ jc = iv_env->GetStringChars(msg, &copied);
+ len = iv_env->GetStringLength(msg);
+ usMsg.append( icu::UnicodeString((UChar const *)jc, len) );
+ iv_env->ReleaseStringChars(msg, jc);
+ }
+
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_JAVA_EXCEPTION,
+ ErrorMessage(UIMA_MSG_ID_EXC_UNKNOWN_JAVA_EXCEPTION, usMsg),
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+ }
+ }
+
+
+ // create a new Java int[] from a C++ vector
+ jintArray JEDIIEngine::createJIntArray(vector<internal::SerializedCAS::TyNum> const & v) {
+ jintArray result = iv_env->NewIntArray(v.size());
+ checkForUnexpectedJNIException();
+ size_t i;
+ for (i=0; i<v.size(); ++i) {
+ jint n = v[i];
+ iv_env->SetIntArrayRegion(result, i,1, &n );
+ checkForUnexpectedJNIException();
+#ifndef NDEBUG
+ jint m;
+ iv_env->GetIntArrayRegion(result, i, 1, &m );
+ checkForUnexpectedJNIException();
+ assert( n == m );
+#endif
+ }
+ return result;
+ }
+
+ // create a new Java byte[] from a C++ vector
+ jbyteArray JEDIIEngine::createJByteArray(vector<char> const & v) {
+ jbyteArray result = iv_env->NewByteArray(v.size());
+ checkForUnexpectedJNIException();
+ size_t i;
+ for (i=0; i<v.size(); ++i) {
+ jbyte n = v[i];
+ iv_env->SetByteArrayRegion(result, i,1, &n );
+ checkForUnexpectedJNIException();
+#ifndef NDEBUG
+ jbyte m;
+ iv_env->GetByteArrayRegion(result, i, 1, &m );
+ checkForUnexpectedJNIException();
+ assert( n == m );
+#endif
+ }
+ return result;
+ }
+
+ // create a new Java short[] from a C++ vector
+ jshortArray JEDIIEngine::createJShortArray(vector<short> const & v) {
+ jshortArray result = iv_env->NewShortArray(v.size());
+ checkForUnexpectedJNIException();
+ size_t i;
+ for (i=0; i<v.size(); ++i) {
+ jshort n = v[i];
+ iv_env->SetShortArrayRegion(result, i,1, &n );
+ checkForUnexpectedJNIException();
+#ifndef NDEBUG
+ jshort m;
+ iv_env->GetShortArrayRegion(result, i, 1, &m );
+ checkForUnexpectedJNIException();
+ assert( n == m );
+#endif
+ }
+ return result;
+ }
+
+ // create a new Java long[] from a C++ vector
+ jlongArray JEDIIEngine::createJLongArray(vector<INT64> const & v) {
+ jlongArray result = iv_env->NewLongArray(v.size());
+ checkForUnexpectedJNIException();
+ size_t i;
+ for (i=0; i<v.size(); ++i) {
+ jlong n = v[i];
+ iv_env->SetLongArrayRegion(result, i,1, &n );
+ checkForUnexpectedJNIException();
+#ifndef NDEBUG
+ jlong m;
+ iv_env->GetLongArrayRegion(result, i, 1, &m );
+ checkForUnexpectedJNIException();
+ assert( n == m );
+#endif
+ }
+ return result;
+ }
+
+
+
+ // create a new Object[] (which all contains java.lang.Strings) from a C++ vector
+ jobjectArray JEDIIEngine::createJStringArray(vector<UnicodeStringRef> const & v) {
+ jclass stringClass = iv_env->FindClass("java/lang/String");
+ checkForUnexpectedJNIException();
+ jobjectArray result = iv_env->NewObjectArray(v.size(), stringClass, NULL);
+ checkForUnexpectedJNIException();
+ size_t i;
+ for (i=0; i<v.size(); ++i) {
+ assertWithMsg(sizeof(UChar) == sizeof(jchar), "Port required");
+ jstring str = iv_env->NewString( (const jchar*)v[i].getBuffer(), v[i].length() );
+ checkForUnexpectedJNIException();
+ iv_env->SetObjectArrayElement(result, i, str);
+ checkForUnexpectedJNIException();
+ }
+ return result;
+ }
+
+ // create a java.lang.String from a C++ unicode string
+ jstring JEDIIEngine::createJString(UnicodeStringRef const & ref) {
+ assertWithMsg(sizeof(UChar) == sizeof(jchar), "Port required");
+ jstring result = iv_env->NewString((jchar const *) ref.getBuffer(), ref.length());
+ checkForUnexpectedJNIException();
+ return result;
+ }
+
+ // create a C++ vector from a Java int[]
+ void JEDIIEngine::createIntVector(jintArray ar, vector<internal::SerializedCAS::TyNum>& result) {
+ result.clear();
+ jsize len = iv_env->GetArrayLength(ar);
+ checkForUnexpectedJNIException();
+ jint* tempAr = new jint[len];
+ iv_env->GetIntArrayRegion(ar, 0, len, tempAr);
+ checkForUnexpectedJNIException();
+
+ result.reserve(len);
+ int i;
+ for (i=0; i<len; ++i) {
+ result.push_back(tempAr[i]);
+ }
+
+ delete[] tempAr;
+ }
+
+ // create a C++ string vector from a java.lang.String[]
+ void JEDIIEngine::createStringVector(jobjectArray ar, vector<UnicodeStringRef>& result, vector<icu::UnicodeString> & pool) {
+ pool.clear();
+ result.clear();
+ jsize len = iv_env->GetArrayLength(ar);
+ checkForUnexpectedJNIException();
+ result.reserve(len);
+ pool.reserve(len); // with this reserve, the vector will never be re-allocated, thus the internal pointer in the UnicodeString objects remain valid throughout this method!
+ int i;
+ for (i=0; i<len; ++i) {
+ jstring js = (jstring) iv_env->GetObjectArrayElement(ar, i);
+ checkForUnexpectedJNIException();
+ if (js != NULL) {
+ jboolean isCopy;
+ jchar const * jc = iv_env->GetStringChars(js, &isCopy);
+ checkForUnexpectedJNIException();
+ jsize strLen = iv_env->GetStringLength(js);
+ checkForUnexpectedJNIException();
+ icu::UnicodeString us((UChar const *) jc, (size_t) strLen);
+ iv_env->ReleaseStringChars(js, jc);
+ checkForUnexpectedJNIException();
+ pool.push_back(us);
+
+ icu::UnicodeString const & poolString = pool.back();
+ UnicodeStringRef uref( poolString.getBuffer(), poolString.length() );
+ result.push_back(uref);
+ } else {
+ UnicodeStringRef emptyURef;
+ result.push_back(emptyURef);
+ }
+ iv_env->DeleteLocalRef(js);
+ }
+ }
+
+ // create a C++ vector from a Java byte[]
+ void JEDIIEngine::createByteVector(jbyteArray ar, vector<char>& result) {
+ result.clear();
+ jsize len = iv_env->GetArrayLength(ar);
+ checkForUnexpectedJNIException();
+ jbyte* tempAr = new jbyte[len];
+ iv_env->GetByteArrayRegion(ar, 0, len, tempAr);
+ checkForUnexpectedJNIException();
+
+ result.reserve(len);
+ int i;
+ for (i=0; i<len; ++i) {
+ result.push_back(tempAr[i]);
+ }
+
+ delete[] tempAr;
+ }
+
+
+ // create a C++ vector from a Java short[]
+ void JEDIIEngine::createShortVector(jshortArray ar, vector<short>& result) {
+ result.clear();
+ jsize len = iv_env->GetArrayLength(ar);
+ checkForUnexpectedJNIException();
+ jshort* tempAr = new jshort[len];
+ iv_env->GetShortArrayRegion(ar, 0, len, tempAr);
+ checkForUnexpectedJNIException();
+
+ result.reserve(len);
+ int i;
+ for (i=0; i<len; ++i) {
+ result.push_back(tempAr[i]);
+ }
+
+ delete[] tempAr;
+ }
+
+ // create a C++ vector from a Java short[]
+ void JEDIIEngine::createLongVector(jlongArray ar, vector<long>& result) {
+ result.clear();
+ jsize len = iv_env->GetArrayLength(ar);
+ checkForUnexpectedJNIException();
+ jlong* tempAr = new jlong[len];
+ iv_env->GetLongArrayRegion(ar, 0, len, tempAr);
+ checkForUnexpectedJNIException();
+
+ result.reserve(len);
+ int i;
+ for (i=0; i<len; ++i) {
+ result.push_back(tempAr[i]);
+ }
+
+ delete[] tempAr;
+ }
+
+ template <class T>
+ void printVector(ostream& os, char const * c, vector<T> const & v) {
+ os << __FILE__ << __LINE__ << ": " << c << endl;
+ size_t i;
+ for (i=0; i<v.size(); ++i) {
+ os << v[i] << " ";
+ }
+ os << endl;
+ }
+
+
+ bool JEDIIEngine::isPrimitive() const {
+ assert( iv_isPrimitive == 1 || iv_isPrimitive == 2);
+ if (iv_isPrimitive == 1) {
+ return true;
+ }
+ return false;
+ }
+
+
+ void JEDIIEngine::handleEngineProxyError(jint rc) {
+ if (rc != JNI_OK) {
+ // some error occurred in the JEDIIEngine object
+ jobject excStringObj = iv_env->CallObjectMethod(iv_obj, iv_getLastExceptionStringMethod);
+ checkForUnexpectedJNIException();
+ jstring excString = (jstring) excStringObj;
+ jboolean isCopy;
+ jchar const * jc = iv_env->GetStringChars(excString, &isCopy);
+ checkForUnexpectedJNIException();
+ jsize js = iv_env->GetStringLength(excString);
+ checkForUnexpectedJNIException();
+ icu::UnicodeString us((UChar const *) jc, (size_t) js);
+ iv_env->ReleaseStringChars(excString, jc);
+ checkForUnexpectedJNIException();
+
+ handleJNIError(rc, us);
+ }
+ }
+
+
+ void JEDIIEngine::handleJNIError(jint rc, icu::UnicodeString const & us) {
+ if (rc != JNI_OK) {
+ ErrorMessage msg(UIMA_MSG_ID_EXC_JAVA_EXCEPTION);
+ msg.addParam( javaErrorToString(rc) );
+ msg.addParam( us);
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_JAVA_EXCEPTION,
+ msg,
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+ }
+ }
+
+
+ void JEDIIEngine::handleJNIError(jint rc) {
+ if (rc != JNI_OK) {
+ ErrorMessage msg(UIMA_MSG_ID_EXC_JNI_CALL_FAILED);
+ msg.addParam( javaErrorToString(rc) );
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_JAVA_EXCEPTION,
+ msg,
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+ }
+ }
+
+ void JEDIIEngine::initializeJVMFunctions() {
+ /*
+ If we linked against the jvm DLL, the following three lines would do the trick:
+ iv_createJavaVMFunc = (TyCreateJavaVMFunc) JNI_CreateJavaVM;
+ iv_getCreatedJavaVMsFunc = (TyGetCreatedJavaVMsFunc) JNI_GetCreatedJavaVMs;
+ return ;
+ */
+ // names of the JVM library on the different platforms
+ // (APR should add the appropriate suffix)
+#if defined(WIN32)
+#define JVMDLL_FILE_NAME "jvm"
+#else
+#define JVMDLL_FILE_NAME "libjvm"
+#endif
+
+ // dynamically load jvm DLL
+ util::Filename jvmDll(JVMDLL_FILE_NAME);
+
+ assert( iv_JVMDllLoader == NULL );
+ iv_JVMDllLoader = new util::DllProcLoaderFile(jvmDll);
+ assert( EXISTS(iv_JVMDllLoader) );
+
+ if (!iv_JVMDllLoader->isValid()) {
+ delete iv_JVMDllLoader;
+ iv_JVMDllLoader = NULL;
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_COULD_NOT_LOAD_JAVA_DLL,
+ UIMA_MSG_ID_EXC_COULD_NOT_LOAD_JAVA_DLL,
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+ } else {
+ // get function pointers
+ iv_createJavaVMFunc = (TyCreateJavaVMFunc) iv_JVMDllLoader->getProcedure( "JNI_CreateJavaVM");
+ if ( iv_createJavaVMFunc == NULL ) {
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_COULD_NOT_LOAD_JAVA_DLL,
+ UIMA_MSG_ID_EXC_COULD_NOT_LOAD_JAVA_DLL,
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+
+ }
+ iv_getCreatedJavaVMsFunc = (TyGetCreatedJavaVMsFunc) iv_JVMDllLoader->getProcedure("JNI_GetCreatedJavaVMs");
+ if ( iv_getCreatedJavaVMsFunc == NULL ) {
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_COULD_NOT_LOAD_JAVA_DLL,
+ UIMA_MSG_ID_EXC_COULD_NOT_LOAD_JAVA_DLL,
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+ }
+
+ }
+
+ }
+
+ void JEDIIEngine::destroyJVMFunctions() {
+ if (iv_JVMDllLoader != NULL) {
+ assert( EXISTS(iv_JVMDllLoader) );
+ delete iv_JVMDllLoader;
+ iv_createJavaVMFunc = NULL;
+ iv_getCreatedJavaVMsFunc = NULL;
+ }
+ }
+
+
+ void JEDIIEngine::initializeJavaVM() {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ JavaVM* vms[1];
+ vms[0] = NULL;
+ jsize existingVMs = 0;
+ iv_createdVM = false;
+
+ UIMA_TPRINT("check if VM exists");
+ assert( EXISTS(iv_getCreatedJavaVMsFunc) );
+ jint existingVMsRes = (*iv_getCreatedJavaVMsFunc)(vms, 1, & existingVMs);
+ UIMA_TPRINT("JNI return code of getCreatedVMs: " << existingVMsRes);
+ if (existingVMsRes != JNI_OK) {
+ handleJNIError(existingVMsRes);
+ }
+ UIMA_TPRINT("existing VMs: " << existingVMs);
+
+ // if we don't already run within a Java context
+ if (existingVMs == 0) {
+ UIMA_TPRINT("new VM must be created");
+ iv_createdVM = true;
+ // get classpath from environment
+ util::EnvironmentVariableQueryOnly envVar("CLASSPATH");
+ char const * classpath = envVar.getValue();
+ char const * cpOption = "-Djava.class.path=";
+ char const * emptyString = "";
+ if (classpath == NULL) {
+ classpath = emptyString;
+ }
+
+ size_t cpBufLen = strlen(classpath) + strlen(cpOption) + 1;
+ auto_array<char> cpBuf( new char[cpBufLen] );
+ cpBuf[0] = 0;
+ strcat( strcat(cpBuf.get(), cpOption), classpath );
+
+ // create and initialize Java VM
+ char * optionStrings[] = {
+ cpBuf.get(),
+ "-Xmx256MB",
+#ifndef NDEBUG
+ "-Xcheck:jni",
+ "-Xcheck:nabounds",
+#endif
+ };
+
+ JavaVMInitArgs vmArgs;
+
+ vmArgs.version = USED_JAVA_VERSION;
+
+ JavaVMOption options[NUMBEROF(optionStrings)];
+ size_t i;
+ for (i=0; i<NUMBEROF(optionStrings); ++i) {
+ options[i].optionString = optionStrings[i];
+ }
+ vmArgs.nOptions = NUMBEROF(optionStrings);
+ vmArgs.options = options;
+
+ vmArgs.ignoreUnrecognized = JNI_FALSE;
+
+ UIMA_TPRINT("creating VM");
+ assert( iv_createJavaVMFunc != NULL);
+ jint vmCreationRes = (*iv_createJavaVMFunc)(& iv_vm, (void **)& iv_env, &vmArgs);
+ UIMA_TPRINT("VM created");
+ if (vmCreationRes != JNI_OK) {
+ clTrace.dump("Java VM could not be created, return code:", vmCreationRes);
+ iv_vm = NULL;
+ iv_env = NULL;
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_JAVA_EXCEPTION,
+ ErrorMessage(UIMA_MSG_ID_EXC_JAVA_VM_COULD_NOT_BE_CREATED, javaErrorToString(vmCreationRes)),
+ UIMA_MSG_ID_EXCON_PROCESSING_JAVA_TAE,
+ ErrorInfo::unrecoverable);
+ }
+ UIMA_TPRINT("VM created");
+ } else {
+ // re-use existing VM
+ UIMA_TPRINT("Using existing VM: " << vms[0]);
+ assert( existingVMsRes == JNI_OK );
+ assert( existingVMs == 1);
+
+ jint getenvRes = vms[0]->GetEnv((void**) & iv_env, USED_JAVA_VERSION);
+ handleJNIError(getenvRes);
+ assert( getenvRes == JNI_OK );
+ iv_vm = vms[0];
+ }
+
+ assert( EXISTS(iv_env) );
+ assert( EXISTS(iv_vm) );
+
+ }
+
+
+ TyErrorId JEDIIEngine::initializeImpl(AnalysisEngineDescription const & specifier) {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ UIMA_TPRINT("initializing JVM DLL");
+ initializeJVMFunctions();
+ UIMA_TPRINT("JVM DLL loaded");
+
+ assert( iv_createJavaVMFunc != NULL );
+ assert( iv_getCreatedJavaVMsFunc != NULL );
+
+ if (specifier.isPrimitive()) {
+ iv_isPrimitive = 1;
+ } else {
+ iv_isPrimitive = 2;
+ }
+
+ icu::UnicodeString config;
+ specifier.toXMLBuffer(config);
+
+// ofstream ofs("jedii_tmp_specifier.xml"); ofs << config << endl; ofs.close();
+
+ UIMA_TPRINT("Initializing Java VM");
+ initializeJavaVM();
+ UIMA_TPRINT("VM initialized");
+
+ assert( EXISTS(iv_env) );
+ assert( EXISTS(iv_vm) );
+
+ TyErrorId err = UIMA_ERR_NONE;
+
+ UIMA_TPRINT("setup VM");
+ // create Java object and initialize method IDs
+ assert( EXISTS(iv_env) );
+ jclass clazz = iv_env->FindClass("org/apache/uima/uimacpp/CppUimajEngine");
+ checkForUnexpectedJNIException();
+ jmethodID constructor = iv_env->GetMethodID(clazz, "<init>", "()V");
+ checkForUnexpectedJNIException();
+
+ iv_initializeMethod = iv_env->GetMethodID(clazz, "initialize", "("
+ "Ljava/lang/String;" // config,
+ "Ljava/lang/String;" // datapath
+ "[I" // typeInheritance
+ "[I" // typePriorities
+ "[I" // featureDefs
+ "[I" // featureOffset
+ "[Ljava/lang/String;" // typeNames
+ "[Ljava/lang/String;" // featureNames
+ "[I" // stringSubTypes
+ "[Ljava/lang/String;" // stringSubTypeValues
+ "[I" // stringSubTypeValuePos
+ "[Ljava/lang/String;" // indexIDs
+ "[I" // indexKinds
+ "[I" // compStarts
+ "[I" // compDefs
+ ")I");
+ checkForUnexpectedJNIException();
+
+ //check if UIMA Framework version is compatible
+ iv_getVersionMethod = iv_env->GetStaticMethodID(clazz, "getVersion", "()Ljava/lang/String;");
+ checkForUnexpectedJNIException();
+
+ jobject versionStringObj = iv_env->CallStaticObjectMethod(clazz, iv_getVersionMethod);
+ checkForUnexpectedJNIException();
+ jstring versionString = (jstring) versionStringObj;
+ const TCHAR ** compatibleVersions = gs_compatibleUIMAVersions;
+ if (versionString != NULL) {
+ cout << versionString << endl;
+ jboolean isCopy;
+ jchar const * jc = iv_env->GetStringChars(versionString, &isCopy);
+ checkForUnexpectedJNIException();
+ jsize js = iv_env->GetStringLength(versionString);
+ checkForUnexpectedJNIException();
+ icu::UnicodeString uVersion((UChar const *) jc, (size_t) js);
+ cout << uVersion << endl;
+ bool compatible = false;
+
+ for (int i=0; i < NUM_COMPATIBLE_UIMA_JAVA_VERSIONS; i++) {
+ if (uVersion.compare(compatibleVersions[i]) == 0) {
+ compatible=true;
+ }
+ }
+
+ if (!compatible) {
+ ErrorMessage msg = ErrorMessage(UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION);
+ msg.addParam(uVersion);
+ for (int i=0; i < NUM_COMPATIBLE_UIMA_JAVA_VERSIONS; i++) {
+ msg.addParam(compatibleVersions[i]);
+ }
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_JAVA_EXCEPTION,
+ msg,
+ UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION,
+ ErrorInfo::unrecoverable);
+ }
+ iv_env->ReleaseStringChars(versionString, jc);
+ checkForUnexpectedJNIException();
+ } else {
+ ErrorMessage msg = ErrorMessage(UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION);
+ msg.addParam("could not determine UIMA Java version");
+ UIMA_EXC_THROW_NEW(JavaException,
+ UIMA_ERR_JAVA_EXCEPTION,
+ msg,
+ UIMA_MSG_ID_INCOMPATIBLE_UIMA_JAVA_VERSION,
+ ErrorInfo::unrecoverable);
+ }
+
+
+
+
+ iv_processMethod = iv_env->GetMethodID(clazz, "process", "("
+ "Ljava/lang/String;" // doc
+ "[I" // heap
+ "[I" // fsIndex
+ "[Ljava/lang/String;" // stringTable
+ "[I" // resultSpecTypes
+ "[I" // resultSpecFeatures
+ "I" // sofanum
+ "[B" // byte heap
+ "[S" // short heap
+ "[J" // long heap
+ ")I");
+ checkForUnexpectedJNIException();
+ iv_getByteHeapMethod = iv_env->GetMethodID(clazz, "getByteHeap", "()[B");
+ checkForUnexpectedJNIException();
+ iv_getShortHeapMethod = iv_env->GetMethodID(clazz, "getShortHeap", "()[S");
+ checkForUnexpectedJNIException();
+ iv_getLongHeapMethod = iv_env->GetMethodID(clazz, "getLongHeap", "()[J");
+ checkForUnexpectedJNIException();
+
+
+
+
+
+
+ iv_destroyMethod = iv_env->GetMethodID(clazz, "destroy", "()I");
+ checkForUnexpectedJNIException();
+ iv_getLastExceptionStringMethod = iv_env->GetMethodID(clazz, "getLastExceptionString", "()Ljava/lang/String;");
+ checkForUnexpectedJNIException();
+ iv_getHeapMethod = iv_env->GetMethodID(clazz, "getHeap", "()[I");
+ checkForUnexpectedJNIException();
+ iv_getIndexedFSsMethod = iv_env->GetMethodID(clazz, "getIndexedFSs", "()[I");
+ checkForUnexpectedJNIException();
+ iv_getStringTableMethod = iv_env->GetMethodID(clazz, "getStringTable", "()[Ljava/lang/String;");
+ checkForUnexpectedJNIException();
+
+ iv_batchProcessCompleteMethod = iv_env->GetMethodID(clazz, "batchProcessComplete", "()I");
+ checkForUnexpectedJNIException();
+
+ iv_collectionProcessCompleteMethod = iv_env->GetMethodID(clazz, "collectionProcessComplete", "()I");
+ checkForUnexpectedJNIException();
+
+
+
+ iv_obj = iv_env->NewObject(clazz, constructor);
+ checkForUnexpectedJNIException();
+ iv_obj = iv_env->NewGlobalRef(iv_obj);
+ checkForUnexpectedJNIException();
+ UIMA_TPRINT("setup VM complete");
+
+
+ // serialize type system and pass it to the initialize method
+ CASSerializer serializer(true);
+ SerializedCAS serializedCAS;
+ serializer.serializeDefinitions( *iv_casDefinition, serializedCAS);
+ UIMA_TPRINT("definitions serialized");
+ {
+ jint pushFrameResult = iv_env->PushLocalFrame(serializedCAS.getNumberOfJavaObjectsToBeCreatedDefinitions() );
+ checkForUnexpectedJNIException();
+ handleEngineProxyError(pushFrameResult);
+ checkForUnexpectedJNIException();
+
+ // prepare arguments for initialize()
+ jintArray typeInheritance = createJIntArray(serializedCAS.getTypeInheritanceTable() );
+ jintArray typePriorities = createJIntArray(serializedCAS.getTypePriorityTable());
+ jintArray featureDefs = createJIntArray(serializedCAS.getFeatureDefinitionTable());
+ jintArray featureOffset = createJIntArray(serializedCAS.getFeatureOffsetTable());
+
+ jobjectArray typeNames = createJStringArray(serializedCAS.getTypeSymbolTable());
+ jobjectArray featureNames = createJStringArray(serializedCAS.getFeatureSymbolTable());
+
+ jintArray subStringTypes = createJIntArray(serializedCAS.getStringSubTypes());
+ jobjectArray subStringTypeValues = createJStringArray(serializedCAS.getStringSubTypeValues());
+ jintArray subStringTypeValuePos = createJIntArray(serializedCAS.getStringSubTypeValuePos());
+
+ jintArray indexKinds = createJIntArray(serializedCAS.getIndexKindTable());
+ jintArray compStarts = createJIntArray(serializedCAS.getComparatorStartTable());
+ jintArray compDefs = createJIntArray(serializedCAS.getComparatorDefinitionTable());
+ jobjectArray indexIDs = createJStringArray(serializedCAS.getIndexIDTable());
+
+ jstring conf = iv_env->NewString((jchar const *)config.getBuffer(), config.length());
+ checkForUnexpectedJNIException();
+
+ icu::UnicodeString dataPathUS(uima::ResourceManager::getInstance().getLocationData().getAsCString() );
+ jstring dataPath = iv_env->NewString((jchar const *)dataPathUS.getBuffer(), dataPathUS.length() );
+ checkForUnexpectedJNIException();
+
+ UIMA_TPRINT("Java args created");
+ // call initialize on org.apache.uima.uimacpp.CppUimajEngine
+ jint initResult = iv_env->CallIntMethod(iv_obj, iv_initializeMethod,
+ conf,
+ dataPath,
+
+ typeInheritance,
+ typePriorities,
+ featureDefs,
+ featureOffset,
+ typeNames,
+ featureNames,
+
+ subStringTypes,
+ subStringTypeValues,
+ subStringTypeValuePos,
+
+ indexIDs,
+ indexKinds,
+ compStarts,
+ compDefs);
+ checkForUnexpectedJNIException();
+ handleEngineProxyError(initResult);
+ UIMA_TPRINT("Java initalize called");
+
+ iv_env->PopLocalFrame(NULL);
+ checkForUnexpectedJNIException();
+ }
+
+ UIMA_TPRINT("result spec initialized");
+ return err;
+ }
+
+
+ TyErrorId JEDIIEngine::reinitTypeSystemImpl() {
+ return initializeImpl(getAnnotatorContext().getTaeSpecifier());
+ }
+
+
+ TyErrorId JEDIIEngine::processImpl(CAS & cas, ResultSpecification const & resultSpec) {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ UIMA_TPRINT("serializing data");
+ TyErrorId err = UIMA_ERR_NONE;
+
+ // serialize CAS and pass it to process method
+ CASSerializer serializer(true);
+ SerializedCAS serializedInCAS;
+// serializer.serializeData( TCAS::promoteCAS(cas), serializedInCAS);
+ serializer.serializeData(cas, serializedInCAS);
+ UIMA_TPRINT("data serialized");
+ {
+ jint pushFrameRes = iv_env->PushLocalFrame( serializedInCAS.getNumberOfJavaObjectsToBeCreatedData());
+ checkForUnexpectedJNIException();
+ handleJNIError(pushFrameRes);
+ // prepare arguments
+ jstring doc = createJString( serializedInCAS.getDocument() );
+ jintArray inHeap = createJIntArray(serializedInCAS.getFSHeapArray() );
+ jintArray inIndexedFSs = createJIntArray(serializedInCAS.getIndexedFSs() );
+ jobjectArray inStrings = createJStringArray(serializedInCAS.getStringSymbolTable());
+ jbyteArray inByteHeap = createJByteArray(serializedInCAS.getByteHeapArray() );
+ jshortArray inShortHeap = createJShortArray(serializedInCAS.getShortHeapArray() );
+ jlongArray inLongHeap = createJLongArray(serializedInCAS.getLongHeapArray() );
+ vector<internal::SerializedCAS::TyNum> resultSpecTypes;
+ vector<internal::SerializedCAS::TyNum> resultSpecFeatures;
+
+ CASSerializer::serializeResultSpec(resultSpec, resultSpecTypes, resultSpecFeatures);
+
+ jintArray inRSTypes = createJIntArray(resultSpecTypes);
+ jintArray inRSFeatures = createJIntArray(resultSpecFeatures);
+ UIMA_TPRINT("args to Java initialize created");
+
+ int sofaNum=0;
+ if (cas.isView() ) {
+ sofaNum = cas.getSofaNum();
+ }
+
+ // call process()
+ jint result = iv_env->CallIntMethod(iv_obj, iv_processMethod,
+ doc,
+ inHeap,
+ inIndexedFSs,
+ inStrings,
+ inRSTypes,
+ inRSFeatures,
+ sofaNum,
+ inByteHeap,
+ inShortHeap,
+ inLongHeap);
+ checkForUnexpectedJNIException();
+ handleEngineProxyError(result);
+ UIMA_TPRINT("Java initialize done");
+
+ iv_env->PopLocalFrame(NULL);
+ checkForUnexpectedJNIException();
+ }
+
+ if (err != UIMA_ERR_NONE) {
+ return err;
+ }
+
+ UIMA_TPRINT("De-serializing per-document data");
+
+ SerializedCAS serializedOutCAS;
+
+ // get serialized CAS from Java object
+ jintArray outHeapArray = (jintArray) iv_env->CallObjectMethod(iv_obj, iv_getHeapMethod);
+ checkForUnexpectedJNIException();
+ createIntVector(outHeapArray, serializedOutCAS.iv_vecFSHeapArray );
+
+ jbyteArray byteHeapArray = (jbyteArray) iv_env->CallObjectMethod(iv_obj, iv_getByteHeapMethod);
+ checkForUnexpectedJNIException();
+ createByteVector(byteHeapArray, serializedOutCAS.iv_vecByteHeapArray );
+
+ jintArray outIndexedFSsArray = (jintArray) iv_env->CallObjectMethod(iv_obj, iv_getIndexedFSsMethod);
+ checkForUnexpectedJNIException();
+ createIntVector(outIndexedFSsArray, serializedOutCAS.iv_vecIndexedFSs);
+
+ jobjectArray outStringsArray = (jobjectArray) iv_env->CallObjectMethod(iv_obj, iv_getStringTableMethod);
+ checkForUnexpectedJNIException();
+ createStringVector(outStringsArray, serializedOutCAS.iv_vecStringSymbolTable, iv_stringPool);
+
+ serializedOutCAS.iv_ulstrDocument = serializedInCAS.getDocument();
+ UIMA_TPRINT("De-serialized data received from Java");
+ CASDeserializer deSerializer;
+ // deSerializer.deserializeData(serializedOutCAS, TCAS::promoteCAS( cas ));
+ deSerializer.deserializeData(serializedOutCAS, cas);
+ UIMA_TPRINT("data deserialized");
+ return err;
+ }
+
+
+
+ TyErrorId JEDIIEngine::reconfigureImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ // no reconfiguration of the JEDII engine possible yet
+ return UIMA_ERR_NONE;
+ }
+
+
+ bool JEDIIEngine::hasNextImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ //TODO:
+ return false;
+ }
+
+
+ CAS & JEDIIEngine::nextImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ //TODO::return iv_pAnnotator->next();
+ UIMA_EXC_THROW_NEW(ExcInvalidRequest,
+ UIMA_ERR_NOT_YET_IMPLEMENTED,
+ UIMA_MSG_ID_EXC_INVALID_CALL_TO_NEXT,
+ UIMA_MSG_ID_EXC_INVALID_CALL_TO_NEXT,
+ ErrorInfo::unrecoverable);
+ }
+
+
+ int JEDIIEngine::getCasInstancesRequiredImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ //TODO::return iv_pAnnotator->next();
+ return 0;
+ }
+
+
+ void JEDIIEngine::destroyJavaVM() {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ if (iv_createdVM) {
+ iv_createdVM = false;
+ jint destroyRC = iv_vm->DestroyJavaVM();
+ checkForUnexpectedJNIException();
+ if (destroyRC != JNI_OK) {
+ char c[2056];
+ icu::UnicodeString us = javaErrorToString(destroyRC);
+ us.extract(0, us.length(), c);
+ clTrace.dump("Java VM could not be destroyed, return code: ", c);
+#ifndef NDEBUG
+ cerr << __FILE__ << __LINE__ << ": Java VM could not be destroyed (return code: " << javaErrorToString(destroyRC) << ")" << endl;
+#endif
+
+ }
+ }
+
+ iv_vm = NULL;
+ iv_env = NULL;
+ }
+
+
+ TyErrorId JEDIIEngine::destroyImpl() {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ if (iv_vm != NULL) {
+ assert( iv_env != NULL );
+ if (iv_obj != NULL) {
+ jint destroyResult = iv_env->CallIntMethod(iv_obj, iv_destroyMethod);
+ checkForUnexpectedJNIException();
+ handleEngineProxyError(destroyResult);
+
+ iv_env->DeleteGlobalRef(iv_obj);
+ checkForUnexpectedJNIException();
+ iv_obj = NULL;
+ }
+
+ destroyJavaVM();
+ assert( iv_vm == NULL );
+ assert( iv_env == NULL );
+
+ destroyJVMFunctions();
+ }
+ return UIMA_ERR_NONE;
+ }
+
+
+ TyErrorId JEDIIEngine::batchProcessCompleteImpl() {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ TyErrorId err = UIMA_ERR_NONE;
+ jint result = iv_env->CallIntMethod(iv_obj, iv_batchProcessCompleteMethod);
+
+ checkForUnexpectedJNIException();
+ handleEngineProxyError(result);
+ return err;
+ }
+
+ TyErrorId JEDIIEngine::collectionProcessCompleteImpl() {
+ util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ TyErrorId err = UIMA_ERR_NONE;
+ jint result = iv_env->CallIntMethod(iv_obj, iv_collectionProcessCompleteMethod);
+
+ checkForUnexpectedJNIException();
+ handleEngineProxyError(result);
+ return err;
+ }
+
+
+
+ }
+}
+
+
+
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/internal_jedii_engine.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/internal_primitive_engine.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/internal_primitive_engine.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/internal_primitive_engine.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/internal_primitive_engine.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,280 @@
+/** \file internal_primitive_engine.cpp .
+-----------------------------------------------------------------------------
+
+
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+
+-----------------------------------------------------------------------------
+
+ Description:
+
+-----------------------------------------------------------------------------
+
+
+-------------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+#include "uima/pragmas.hpp"
+#include "uima/macros.h"
+#include "uima/internal_primitive_engine.hpp"
+#include "uima/capability.hpp"
+#include "uima/casdefinition.hpp"
+#include "uima/trace.hpp"
+#include "uima/internal_capability_container.hpp"
+#include "uima/lowlevel_typesystem.hpp"
+#include "uima/annotator_mgr.hpp"
+#include "uima/log.hpp"
+#include "uima/msg.h"
+#include "uima/cas.hpp"
+#include "uima/filename.hpp"
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Implementation */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+ namespace internal {
+
+ PrimitiveEngine::PrimitiveEngine(AnnotatorContext & rAnnContext, bool bOwnsANC,
+ bool bOwnsTAESpec,
+ uima::internal::CASDefinition & casdef, bool ownsCasDef,
+ bool ) :
+ EngineBase(rAnnContext, bOwnsANC, bOwnsTAESpec, casdef, ownsCasDef),
+ iv_pAnnotator(NULL),
+ iv_pCapContainer(NULL) {}
+
+ PrimitiveEngine::~PrimitiveEngine() {
+ destroyIfNeeded();
+ }
+
+
+ bool isTypeName(TCHAR const * cpName) {
+ icu::UnicodeString target(cpName);
+ int iIndex = target.indexOf((UChar)':');
+ return iIndex == -1;
+ }
+
+ TyErrorId PrimitiveEngine::initializeImpl(AnalysisEngineDescription const & crEngineDescription) {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ TyErrorId utErrorId = UIMA_ERR_NONE;
+ UnicodeStringRef refDLLName(crEngineDescription.getAnnotatorImpName());
+ string strAnnotatorName;
+
+ refDLLName.extract( strAnnotatorName, CCSID::getDefaultName() );
+
+ UnicodeStringRef refSymbolicName(crEngineDescription.getAnalysisEngineMetaData()->getName() );
+ string strSymbolicName;
+ refSymbolicName.extract( strSymbolicName, CCSID::getDefaultName() );
+
+
+ util::Filename dllFileName( strAnnotatorName.c_str() );
+ iv_pAnnotator = new AnnotatorProxy(dllFileName, strSymbolicName.c_str() );
+ if ( iv_pAnnotator == NULL) {
+ return UIMA_ERR_ENGINE_OUT_OF_MEMORY;
+ }
+
+ if (! iv_pAnnotator->isLoadedDelayed()) {
+ utErrorId = iv_pAnnotator->load();
+ if (utErrorId == UIMA_ERR_NONE) {
+ utErrorId = iv_pAnnotator->init(getAnnotatorContext());
+ }
+ } else {
+ assertWithMsg(false, "delayed loading not yet implemented");
+ return UIMA_ERR_NOT_YET_IMPLEMENTED;
+ }
+ if (utErrorId != UIMA_ERR_NONE) {
+ if (utErrorId == UIMA_ERR_ANNOTATOR_COULD_NOT_FIND ) {
+ TyMessageId utErrorMsg = UIMA_MSG_ID_ANNOTATOR_COULD_NOT_FIND;
+ ErrorMessage errMsg(utErrorMsg);
+ errMsg.addParam(iv_pAnnotator->getFilename());
+ ErrorInfo err(errMsg, utErrorId, ErrorInfo::unrecoverable);
+ getAnnotatorContext().getLogger().logError(err);
+ } else if (utErrorId == UIMA_ERR_ANNOTATOR_MISSING_CREATE) {
+ TyMessageId utErrorMsg = UIMA_MSG_ID_ANNOTATOR_COULD_NOT_FIND_MAKEAE;
+ ErrorMessage errMsg(utErrorMsg);
+ errMsg.addParam(iv_pAnnotator->getFilename());
+ if (iv_pAnnotator->getErrorMsg() != NULL) {
+ errMsg.addParam(iv_pAnnotator->getErrorMsg());
+ }
+ ErrorInfo err(errMsg, utErrorId, ErrorInfo::unrecoverable);
+ getAnnotatorContext().getLogger().logError(err);
+ } else if (utErrorId == UIMA_ERR_ANNOTATOR_COULD_NOT_LOAD) {
+ TyMessageId utErrorMsg = UIMA_MSG_ID_ANNOTATOR_COULD_NOT_LOAD;
+ ErrorMessage errMsg(utErrorMsg);
+ errMsg.addParam(iv_pAnnotator->getFilename());
+ if (iv_pAnnotator->getErrorMsg() != NULL) {
+ errMsg.addParam(iv_pAnnotator->getErrorMsg());
+ }
+ ErrorInfo err(errMsg, utErrorId, ErrorInfo::unrecoverable);
+ getAnnotatorContext().getLogger().logError(err);
+ }
+ // we do not need to log detailed messages for the other errors
+ // like "could not init" this has been done already by the annotators
+ destroy();
+ return utErrorId;
+ }
+
+ iv_pCapContainer = new internal::CapabilityContainer();
+ assert( EXISTS(iv_pCapContainer) );
+
+ vector<Capability*> const & crCapabilities = getAnnotatorContext().getTaeSpecifier().getAnalysisEngineMetaData()->getCapabilites();
+
+ UIMA_TPRINT("number capabilities: " << crCapabilities.size());
+ assert( EXISTS(iv_casDefinition) );
+ iv_pCapContainer->init(crCapabilities, iv_casDefinition->getTypeSystem());
+
+ return typeSystemInitImpl();
+ }
+
+
+ TyErrorId PrimitiveEngine::typeSystemInitImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ try {
+ UIMA_TPRINT("Processing annotator: " << iv_pAnnotator->getSymbolicName() );
+ assert( EXISTS(iv_pAnnotator) );
+ return iv_pAnnotator->typeSystemInit(iv_casDefinition->getTypeSystem() );
+ } catch (Exception & rclException) {
+ getAnnotatorContext().getLogger().logError(rclException.getErrorInfo() );
+ return rclException.getErrorInfo().getErrorId();
+ }
+
+ }
+
+ TyErrorId PrimitiveEngine::processImpl(CAS & cas, ResultSpecification const & crResultSpec) {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ UIMA_TPRINT("Processing annotator: " << iv_pAnnotator->getSymbolicName() );
+ assert( EXISTS(iv_pAnnotator) );
+ bool bCallAnnotator = true;
+ DocumentFS docfs = cas.getDocumentAnnotation();
+ if (isTopLevelEngine()&& docfs.isValid()) {
+ vector<TypeOrFeature> tofsToBeRemoved;
+ if (! AnnotatorManager::shouldEngineBeCalled(*iv_pCapContainer,
+ crResultSpec,
+ docfs.getLanguage(),
+ tofsToBeRemoved) ) {
+ bCallAnnotator = false;
+ }
+ AnnotatorContext & rANC = getAnnotatorContext();
+ AnalysisEngineDescription const & crTAESpecifier = rANC.getTaeSpecifier();
+ FlowConstraints const * pFlow = crTAESpecifier.getAnalysisEngineMetaData()->getFlowConstraints();
+ if (pFlow == NULL) {
+ bCallAnnotator = true;
+ } else {
+ FlowConstraints * flow = CONST_CAST(FlowConstraints *, pFlow);
+ FlowConstraints::EnFlowType flowType = flow->getFlowConstraintsType();
+ //if FixedFlow specified all engines are always called so reset callEngine is true
+ if (flowType == FlowConstraints::FIXED) {
+ bCallAnnotator = true;
+ }
+ }
+ }
+ if (bCallAnnotator) {
+ // link CAS to current component
+ cas.setCurrentComponentInfo(&getAnnotatorContext());
+ try {
+ iv_pAnnotator->processDocument(cas, crResultSpec);
+ } catch (Exception e) {
+ UIMA_EXC_RETHROW(e, NULL);
+ }
+ }
+ // if annotator should not be called is not really an error
+ return UIMA_ERR_NONE;
+ }
+
+ TyErrorId PrimitiveEngine::destroyImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+
+ TyErrorId utErrorId = UIMA_ERR_NONE;
+ if (iv_pAnnotator != NULL ) {
+ if (iv_pAnnotator->isInitialized()) {
+ assert( iv_pAnnotator->isValid() );
+ utErrorId = iv_pAnnotator->deInit();
+ }
+ delete iv_pAnnotator;
+ iv_pAnnotator = NULL;
+ }
+ if (iv_pCapContainer != NULL) {
+ delete iv_pCapContainer;
+ iv_pCapContainer = NULL;
+ }
+ return utErrorId;
+ }
+
+ TyErrorId PrimitiveEngine::reconfigureImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ return iv_pAnnotator->reConfig();
+ }
+
+ TyErrorId PrimitiveEngine::batchProcessCompleteImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ return iv_pAnnotator->batchProcessComplete();
+ }
+
+ TyErrorId PrimitiveEngine::collectionProcessCompleteImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ return iv_pAnnotator->collectionProcessComplete();
+ }
+
+ bool PrimitiveEngine::hasNextImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ return iv_pAnnotator->hasNext();
+ }
+
+ CAS & PrimitiveEngine::nextImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ return iv_pAnnotator->next();
+ }
+
+ int PrimitiveEngine::getCasInstancesRequiredImpl() {
+ util::Trace trace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_ENGINE);
+ return iv_pAnnotator->getCasInstancesRequired();
+ }
+
+#ifdef UIMA_DEBUG_ANNOTATOR_TIMING
+
+ void PrimitiveEngine::displayTimingData(util::ConsoleUI const & crConsole, bool bVerbose) const {
+#ifndef NDEBUG
+ crConsole.warning("timing data not yet available for primitive engines");
+#endif
+ }
+
+#endif
+
+ }
+}
+
+/* ----------------------------------------------------------------------- */
+
+
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/internal_primitive_engine.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/language.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/language.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/language.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/language.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,156 @@
+/**
+-----------------------------------------------------------------------------
+
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+
+-----------------------------------------------------------------------------
+
+ Description:
+
+-----------------------------------------------------------------------------
+
+
+ 5/18/1999 Initial creation
+
+-------------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+
+#include "uima/language.hpp"
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+ char const * Language::INVALID = "??";
+ char const * Language::UNSPECIFIED = "";
+
+}
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Implementation */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+ // Locale is stored as ll-cc, e.g. "en-us"
+ // or just the language ll, e.g. "en",
+ // or as "" if unspecified (matches any language)
+ // or as "??" if invalid.
+ // Both fields are restricted to 2 characters in lowercase.
+ // Note: Jedii does not restrict the lengths of the fields, and normalizes both to
+ // lowercase, e.g. fr-ca or en-nzl, but does not check for legal locales.
+ // So restricting each to 2 characters for UIMA should be OK, and makes it easy
+ // to represent a language as a 4-byte number.
+
+ void
+ Language::init( const char * cpszLanguageCode ) {
+ // Check that have 2 chars (a-z) or 2 such pairs separated by - or _
+
+ static const char legalChars[] = "abcdefghijklmnopqrstuvwzyz";
+
+ memset(iv_locale, '\0', 6); // Start with all 0's
+ memset(iv_lang, '\0', 3);
+
+ unsigned int len = strlen(cpszLanguageCode);
+ if (len == 0)
+ return; // Unspecified
+
+ if (len == 2 || len == 5) {
+ iv_locale[0] = tolower(cpszLanguageCode[0]); // language "ll"
+ iv_locale[1] = tolower(cpszLanguageCode[1]);
+ if (len == 5) {
+ if (cpszLanguageCode[2] == '-' || cpszLanguageCode[2] == '_')
+ iv_locale[2] = 'a'; // Temporarily "legal"
+ else
+ iv_locale[2] = '?'; // Illegal separator
+ iv_locale[3] = tolower(cpszLanguageCode[3]); // territory "cc"
+ iv_locale[4] = tolower(cpszLanguageCode[4]);
+ }
+ } else
+ iv_locale[0] = '?'; // Illegal length 1,3,4,6,7,...
+
+ if ( strspn(iv_locale, legalChars) < len ) // Illegal characters?
+ strcpy(iv_locale, "??"); // Invalid
+ else {
+ if ( len == 5 )
+ iv_locale[2] = '-'; // Normalize to -
+ }
+ strncpy(iv_lang, iv_locale, 2); // Also save 2-char lang part
+ iv_lang[2] = '\0';
+ }
+
+ void
+ Language::_initFromString( const string & crstrLanguageCode ) {
+ init(crstrLanguageCode.c_str());
+ }
+
+ void
+ Language::_initFromString( UnicodeStringRef crustrLanguageCode ) {
+ size_t const MAX_LEN = 32;
+ char buff[MAX_LEN];
+ size_t uiLen = min(MAX_LEN-1, (size_t)crustrLanguageCode.length());
+ // this conversion does invariant chars only but it is fast
+ u_UCharsToChars(crustrLanguageCode.getBuffer(), buff, uiLen);
+ // zero terminate buffer
+ buff[uiLen] = char(0);
+ init(buff);
+ }
+
+ Language::Language( TyLanguageAsNumber lLanguageAsNumber ) {
+ unsigned long ul = (unsigned long)lLanguageAsNumber;
+
+ iv_locale[0] = ul>>24;
+ iv_locale[1] = (ul >> 16) & 0x000000ff;
+ iv_locale[3] = (ul >> 8) & 0x000000ff;
+ iv_locale[4] = ul & 0x000000ff;
+ iv_locale[5] = '\0';
+ if (iv_locale[3] == '\0') // No territory
+ iv_locale[2] = '\0';
+ else
+ iv_locale[2] = '-';
+ strncpy(iv_lang, iv_locale, 2);
+ iv_lang[2] = '\0';
+ }
+
+ bool Language::matches( const Language & crclCompareLang ) const {
+ // Full locale matchs, or
+ // one is unspecified, or
+ // language part matches and one territory is unspecified (missing)
+ return ( strcasecmp(iv_locale, crclCompareLang.iv_locale) == 0 ||
+ iv_locale[0] == '\0' ||
+ crclCompareLang.iv_locale[0] == '\0' ||
+ ( strcasecmp(iv_lang, crclCompareLang.iv_lang) == 0 &&
+ (iv_locale[3] == '\0' || crclCompareLang.iv_locale[3] == '\0') ) );
+ }
+
+} //namespace uima
+/* <EOF> */
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/language.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/log.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/log.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/log.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/log.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,308 @@
+/** @name log.cpp
+-----------------------------------------------------------------------------
+
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+
+-----------------------------------------------------------------------------
+
+ Description: This file contains class LogFacility
+
+-----------------------------------------------------------------------------
+
+
+ 6/24/1999 Initial creation
+ 7/19/1999 logError(const ErrorInfo & crclErrorInfo) added
+
+-------------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+
+#include <time.h>
+#include "uima/log.hpp"
+
+#include "uima/trace.hpp"
+#include "uima/comp_ids.h"
+#include "uima/msg.h"
+#include "uima/resmgr.hpp"
+#include "uima/exceptions.hpp"
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+#define UIMA_LOG_APPLICATION_KEY_UNKNOWN _TEXT("???")
+const size_t UIMA_LOG_STATIC_CONVERSION_BUFSIZE = 1024;
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Private Implementation */
+/* ----------------------------------------------------------------------- */
+namespace uima {
+
+ TyMessageId LogFacility::getTypeAsMessageId(LogStream::EnEntryType enType) const
+ /* ----------------------------------------------------------------------- */
+ {
+ switch (enType) {
+ case LogStream::EnMessage :
+ return(UIMA_MSG_ID_LOG_MESSAGE);
+ case LogStream::EnWarning :
+ return(UIMA_MSG_ID_LOG_WARNING);
+ case LogStream::EnError :
+ return(UIMA_MSG_ID_LOG_ERROR);
+ default:
+ assertWithMsg(false, _TEXT("Unknow EnLogEntryType")); //lint !e506: Constant value Boolean
+ }
+ return(0); /* shutup compiler */
+ }
+
+ void LogFacility::doLog(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const
+ /* ----------------------------------------------------------------------- */
+ {
+ if (isLoggable(enType)) {
+ if (ResourceManager::hasInstance() ) {
+ ResourceManager & resmgr = ResourceManager::getInstance();
+ if ( resmgr.isJavaLoggingEnabled()) {
+ //route to java logger if one is available
+ resmgr.writeToJavaLogger(enType, iv_strOrigin,
+ cpszMsg);
+ } else if (iv_logFile != NULL) {
+ //write to local log file if one is available
+ string message = format(enType, cpszMsg, lUserCode);
+ fwrite(message.c_str(), 1, message.size(), iv_logFile);
+ fflush(iv_logFile);
+ }
+ }
+ }
+
+ }
+
+ std::string LogFacility::format(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const {
+
+ //if (isLoggable(enType)) {
+ /***char dateStr [9];
+ char timeStr [9];
+ _strdate( dateStr);
+ _strtime( timeStr );
+
+ message += dateStr;
+ message += " ";
+ message += timeStr;
+ message += " ";
+ message += iv_strOrigin.c_str();
+ message += " ";
+ ***/
+
+ time_t rawtime;
+ time ( &rawtime );
+ string currts = ctime(&rawtime);
+ string message = currts.substr(0,currts.length()-1);
+
+ //map enType to Java levels
+ switch (enType) {
+ case LogStream::EnMessage :
+ message += (" INFO: ");
+ break;
+ case LogStream::EnWarning :
+ message += " WARNING: ";
+ break;
+ case LogStream::EnError :
+ message += " SEVERE: ";
+ break;
+ default:
+ message += " INFO: ";
+ }
+
+ message += cpszMsg;
+ message += "\n";
+ //}
+ return message;
+ }
+
+ bool LogFacility::isLoggable(LogStream::EnEntryType enType) const {
+ ///LogStream::EnEntryType minLogLevel = ResourceManager::getInstance().getLoggingLevel();
+ if (enType < iv_logLevel) {
+ return false;
+ }
+ return true;
+ }
+
+ void LogFacility::setLogLevel(LogStream::EnEntryType enType) {
+ ///LogStream::EnEntryType minLogLevel = ResourceManager::getInstance().getLoggingLevel();
+ iv_logLevel = enType;
+ }
+
+ void LogFacility::log(LogStream::EnEntryType enType, const TCHAR * cpszMsg, long lUserCode) const
+ /* ----------------------------------------------------------------------- */
+ {
+ doLog(enType, cpszMsg, lUserCode);
+
+ if (enType == LogStream::EnError) {
+ LogFacility * pclThis = CONST_CAST(LogFacility *, this);
+ assert(pclThis == this);
+ //remember last error as string/number
+ pclThis->iv_lLastUserCode = lUserCode;
+ pclThis->iv_strLastError = string(cpszMsg);
+ //reset error info object
+ pclThis->iv_errInfo.reset();
+ }
+ }
+
+
+ void LogFacility::log(LogStream::EnEntryType enType, ErrorInfo const & errInfo) const
+ /* ----------------------------------------------------------------------- */
+ {
+ doLog(enType, errInfo.asString().c_str());
+
+ LogFacility * pclThis = CONST_CAST(LogFacility *, this);
+ assert(pclThis == this);
+ //remember last error as error info object
+ pclThis->iv_errInfo = errInfo;
+ //reset string/number
+ // MSV6 compile error: pclThis->iv_strLastError.clear();
+ pclThis->iv_strLastError = std::string();
+ pclThis->iv_lLastUserCode = 0;
+ }
+
+
+ void LogFacility::log(LogStream::EnEntryType enType, const icu::UnicodeString & ustrMsg, long lUserCode) const
+ /* ----------------------------------------------------------------------- */
+ {
+ string strMsg;
+ UnicodeStringRef ref(ustrMsg);
+ ref.extract(strMsg, CCSID::getDefaultName());
+ log(enType, strMsg.c_str(), lUserCode);
+ }
+
+ /* ----------------------------------------------------------------------- */
+ /* Implementation */
+ /* ----------------------------------------------------------------------- */
+
+ LogStream & LogStream::flush() {
+ this->stringstream::flush();
+ iv_pLogFacility->flushLogStream();
+ str("");
+ return (*this);
+ }
+
+ LogStream & LogFacility::getLogStream(LogStream::EnEntryType enLogEntryType) {
+ iv_logStream.iv_enLogEntryType = enLogEntryType;
+ flushLogStream();
+ return iv_logStream;
+ }
+
+ void LogFacility::flushLogStream() {
+ string strMsg = iv_logStream.str();
+ if (strMsg.size() == 0) {
+ return;
+ }
+ log(iv_logStream.iv_enLogEntryType, strMsg.c_str());
+ }
+
+
+ LogFacility::LogFacility(icu::UnicodeString const & crEngineName):
+ iv_lLastUserCode(0),
+ iv_logStream(*this, LogStream::EnMessage),
+ iv_logFile(NULL),
+ iv_logLevel(LogStream::EnMessage) {
+ UnicodeStringRef ref(crEngineName);
+ ref.extract(iv_strOrigin, CCSID::getDefaultName() );
+ iv_logFile = uima::ResourceManager::getInstance().getLogFile();
+ iv_logLevel = uima::ResourceManager::getInstance().getLoggingLevel();
+ }
+
+ LogFacility::LogFacility(icu::UnicodeString const & crEngineName,
+ FILE * crpLogFile,
+ LogStream::EnEntryType crLoggingLevel):
+ iv_lLastUserCode(0),
+ iv_logStream(*this, LogStream::EnMessage),
+ iv_logFile(crpLogFile),
+ iv_logLevel(crLoggingLevel) {
+ UnicodeStringRef ref(crEngineName);
+ ref.extract(iv_strOrigin, CCSID::getDefaultName() );
+ }
+
+ LogFacility::~LogFacility() {
+ flushLogStream();
+ }
+
+ const TCHAR * LogFacility::getLastErrorAsCStr(void) const {
+ /* ----------------------------------------------------------------------- */
+ LogFacility * pclThis = CONST_CAST(LogFacility *, this);
+ ErrorInfo errInfo = getLastError();
+ if (errInfo.getErrorId() == UIMA_ERR_NONE) {
+ return (NULL);
+ }
+ pclThis->iv_strLastError = getLastError().asString();
+ return(iv_strLastError.c_str());
+ }
+
+ ErrorInfo const & LogFacility::getLastError() const {
+ /* ----------------------------------------------------------------------- */
+ // check if we have a string or info based information on last error
+ if (iv_strLastError.length() > 0) {
+ // if we have error string the error object must be clear
+ assert(iv_errInfo.getErrorId() == UIMA_ERR_NONE);
+ // now set up the object with the string information
+ LogFacility * pclThis = CONST_CAST(LogFacility *, this);
+ assert(pclThis == this);
+ if (iv_lLastUserCode == 0) {
+ pclThis->iv_errInfo.setErrorId(UIMA_ERR_USER_ANNOTATOR_ERROR_UNSPECIFIED);
+ } else {
+ pclThis->iv_errInfo.setErrorId(iv_lLastUserCode);
+ }
+ ErrorMessage msg(UIMA_MSG_ID_LOG_TO_ERROR_INFO, iv_strOrigin);
+ msg.addParam(iv_strLastError);
+ pclThis->iv_errInfo.setMessage(msg);
+ pclThis->iv_errInfo.setSeverity(ErrorInfo::unrecoverable);
+ // now that the information is transferred into the object clear string
+ pclThis->iv_lLastUserCode = 0;
+ // MSV6 compile error:
+ //pclThis->iv_strLastError.clear();
+ pclThis->iv_strLastError = std::string();
+ }
+ assert(iv_strLastError.length() == 0);
+ assert(iv_lLastUserCode == 0);
+ return iv_errInfo;
+ }
+
+
+ void LogFacility::logWarning(const ErrorInfo & errInfo) const
+ /* ----------------------------------------------------------------------- */
+ {
+ log(LogStream::EnWarning, errInfo);
+ }
+
+
+ void LogFacility::logError(const ErrorInfo & errInfo) const
+ /* ----------------------------------------------------------------------- */
+ {
+ log(LogStream::EnError, errInfo);
+ }
+
+} //namespace uima
+/* <EOF> */
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/log.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/res_abase.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/res_abase.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/res_abase.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/res_abase.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,149 @@
+/** \file res_abase.cpp .
+-----------------------------------------------------------------------------
+
+
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+
+-----------------------------------------------------------------------------
+
+ Description:
+
+-----------------------------------------------------------------------------
+
+
+-------------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+//#define DEBUG_VERBOSE
+#include "uima/pragmas.hpp"
+#include "uima/macros.h"
+#include "uima/res_abase.hpp"
+#include "uima/language.hpp"
+#include "uima/resmgr.hpp"
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Implementation */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+ ResourceABase::ResourceABase(icu::UnicodeString const & crKey, icu::UnicodeString const & crKind)
+ : iv_key(crKey),
+ iv_kind(crKind) {}
+
+ ResourceABase::~ResourceABase() {
+ UIMA_TPRINT("Deleting Resource: " << this);
+ }
+
+
+ void ResourceABase::setNewKey(icu::UnicodeString const & crNewKey) {
+ iv_key = crNewKey;
+ }
+
+ void FileResource::resolveFilename() {
+ icu::UnicodeString file = getKey();
+ file.append(".");
+ file.append(getKind());
+
+ char c[2056];
+ int32_t len = file.extract(0, file.length(), c);
+ assert( len == file.length());
+ iv_fileName.setNew( c ); // set filename from extracted C-string
+ }
+
+ ////////////////////////////////////////////////////////////////
+
+ FileResource::FileResource(icu::UnicodeString const & crKey,
+ icu::UnicodeString const & crKind)
+ : ResourceABase(crKey, crKind) {}
+
+ FileResource::~FileResource() {}
+
+ ////////////////////////////////////////////////////////////////
+
+ LanguageKindFileResource::LanguageKindFileResource (icu::UnicodeString const & crKey,
+ icu::UnicodeString const & crKind,
+ bool bTryAlternativeTerritories,
+ char cLanguageTerritorySeparator)
+ : FileResource(crKey, crKind),
+ iv_bTryAlternativeTerritories(bTryAlternativeTerritories),
+ iv_cLanguageTerritorySeparator(cLanguageTerritorySeparator) {
+ }
+
+ LanguageKindFileResource::~LanguageKindFileResource() {}
+
+
+ void LanguageKindFileResource::resolveFilename() {
+ const size_t bufLen = 2056;
+ char buf[bufLen];
+ icu::UnicodeString const & crKey = getKey();
+ int32_t iConvLen = crKey.extract(0, crKey.length(), buf);
+ assert( iConvLen < bufLen );
+ uima::Language lang(buf);
+
+ icu::UnicodeString crExt(".");
+ crExt.append(getKind());
+ iConvLen = crExt.extract(0, crExt.length(), buf);
+ assert( iConvLen < bufLen );
+
+ ResourceManager::createFilenameForLanguage(lang,
+ buf,
+ iv_bTryAlternativeTerritories,
+ ResourceManager::getInstance().getLocationData(),
+ iv_fileName);
+ }
+
+
+ /////////////////////////////////////////////////////////////////////
+
+ ResourceFactoryABase::ResourceFactoryABase(icu::UnicodeString const & crKind)
+ : iv_kind(crKind) {}
+
+ ResourceFactoryABase::~ResourceFactoryABase() {}
+
+ ///////////////////////////////////////////////////////////////////
+
+ LanguageKindFileResourceFactory::LanguageKindFileResourceFactory(icu::UnicodeString const & crKind,
+ bool bTryAlternativeTerritories)
+ : ResourceFactoryABase(crKind),
+ iv_bTryAlternativeTerritories( bTryAlternativeTerritories ) {}
+
+ LanguageKindFileResourceFactory::~LanguageKindFileResourceFactory() {}
+
+
+}
+
+/* ----------------------------------------------------------------------- */
+
+
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/res_abase.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/res_annotator.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/res_annotator.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/res_annotator.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/res_annotator.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,127 @@
+/** @name res_annotator.cpp
+-----------------------------------------------------------------------------
+
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+
+-----------------------------------------------------------------------------
+
+ Description: This file contains class
+
+-----------------------------------------------------------------------------
+
+
+ 9/9/1999 Initial creation
+
+-------------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+
+#include "uima/res_annotator.hpp"
+#include <limits.h>
+#include "uima/resmgr.hpp"
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+#define UIMA_RESOURCE_ANNOTATOR_FILENAME_EXTENSION _TEXT("$PLG")
+
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Implementation */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+ namespace internal {
+
+ void ResourceAnnotatorFile::init(uima::ErrorInfo & rErrInfo) {
+
+ icu::UnicodeString const & crAnnotatorFileName = getKey();
+
+ // first we must convert our unicode filename to single byte
+ size_t nameLen = crAnnotatorFileName.length();
+ char* buf = new char[1+nameLen];
+ crAnnotatorFileName.extract(0, nameLen, buf);
+ util::Filename filename(buf);
+ delete[] buf;
+
+ /* No longer optionally remove the path before loading, or add "itu" prefix.
+ Now load only what we're given, i.e. a full filename with path,
+ or a partial name, letting the OS search the appropriate paths. */
+
+ assert(iv_pAnnotatorFile == NULL);
+ iv_pAnnotatorFile = new util::DllProcLoaderFile(filename);
+ if ( ! (EXISTS(iv_pAnnotatorFile)) ) {
+ rErrInfo.setErrorId(UIMA_ERR_RESMGR_COULD_NOT_LOAD_RESOURCE);
+ ResourceManager::getInstance().getLogger().logError(rErrInfo);
+ } else if ( ! iv_pAnnotatorFile->isValid() ) {
+ rErrInfo.setErrorId(iv_pAnnotatorFile->getErrorId());
+ ErrorMessage msg(iv_pAnnotatorFile->getErrorMsgId(),
+ iv_pAnnotatorFile->getErrorMsg());
+ msg.addParam(filename.getAsCString());
+ msg.addParam(iv_pAnnotatorFile->getErrorMsg());
+ rErrInfo.setMessage(msg);
+ ResourceManager::getInstance().getLogger().logError(rErrInfo);
+ } else {
+ setNewKey(filename.getAsCString());
+ std::string msg = "Loaded ";
+ msg.append(filename.getAsCString());
+ ResourceManager::getInstance().getLogger().logMessage(msg);
+ }
+ }
+
+ void ResourceAnnotatorFile::deInit(void) {
+ delete iv_pAnnotatorFile;
+ iv_pAnnotatorFile = 0;
+ }
+
+
+
+ ////////////////////////////////////////////////////////////////
+
+ ResourceAnnotatorFileFactory::ResourceAnnotatorFileFactory(void)
+ : ResourceFactoryABase(UIMA_RESOURCE_ANNOTATOR_FILENAME_EXTENSION) {}
+
+
+ ResourceABase * ResourceAnnotatorFileFactory::createResource(icu::UnicodeString const & crKey) const {
+ ResourceAnnotatorFile * pclRetVal;
+
+ pclRetVal = new ResourceAnnotatorFile(crKey, getKind() );
+ assert(EXISTS(pclRetVal));
+ return(pclRetVal);
+ }
+
+ }
+
+}
+
+
+
+/* <EOF> */
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/res_annotator.cpp
------------------------------------------------------------------------------
svn:eol-style = native