Ejemplo n.º 1
0
bool InitMorphologySystem(JNIEnv *env, jni_dictionary &dic){
	switch (dic.Language){
		case morphRussian:
			dic.lang=Russian;
			break;
		case morphEnglish :
		case morphGerman:
		default:
			throwEx(env, strdup("assertion error: A1"));
			return false;
	}

	string langua_str = GetStringByLanguage(dic.Language);
	dic.pLemmatizer = new T;
	string strError;
	if (!dic.pLemmatizer->LoadDictionariesRegistry(strError)){
   		char* err=str_compose("Cannot load %s morphological dictionary. Error details: %s", langua_str.c_str(), strError.c_str());
		throwEx(env, err);
		return false;
	}
	dic.pAgramtab = new Y;
	if (!dic.pAgramtab->LoadFromRegistry()){
   		char* err=str_compose("Cannot load %s gramtab.", langua_str.c_str());
		throwEx(env, err);
		return false;
	}
	return true;
}
Ejemplo n.º 2
0
/*
 * Class:     ru_aot_morph_JavaMorphAPI
 * Method:    closeImpl
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_ru_aot_morph_JavaMorphAPI_closeImpl
  (JNIEnv *env, jclass clazz){
	try{
		//dispose of dics
		if(dic.pLemmatizer!=0){delete dic.pLemmatizer;dic.pLemmatizer=0;}
		if(dic.pAgramtab!=0){delete dic.pAgramtab;dic.pAgramtab=0;}

		if(setClazz!=NULL){env->DeleteGlobalRef(setClazz);setClazz=NULL;}
		//if(setConstructor!=NULL){env->DeleteGlobalRef(setConstructor);setConstructor=NULL;}
		if(JNIAPIExceptionClass!=NULL){env->DeleteGlobalRef(JNIAPIExceptionClass);JNIAPIExceptionClass=NULL;}
		//if(method_convertFromCharsetCp1251ToJavaString!=NULL){env->DeleteGlobalRef(method_convertFromCharsetCp1251ToJavaString);method_convertFromCharsetCp1251ToJavaString=NULL;}
		//if(method_grammemSetAddGrammem!=NULL){env->DeleteGlobalRef(method_grammemSetAddGrammem);method_grammemSetAddGrammem=NULL;}
		//if(method_paradigmsetAddNewParadigm!=NULL){env->DeleteGlobalRef(method_paradigmsetAddNewParadigm);method_paradigmsetAddNewParadigm=NULL;}
		//if(method_wordresult_new!=NULL){env->DeleteGlobalRef(method_wordresult_new);method_wordresult_new=NULL;}

		inited=false;
		return;//ok
	}catch(CExpc& e){
		char* err=str_compose("C++ exception: CExpc: %s",e.m_strCause.c_str());
		throwEx(env, err);
		return;
	}catch(int e){
		string errstr("C++ exception: int: ");
		errstr+=e;
		errstr+=".";
		throwEx(env, strdup(errstr.c_str()));
		return;
	}catch(...){
		throwEx(env, strdup("Unknown C++ exception."));
		return;
	}
}
Ejemplo n.º 3
0
/*
 * Class:     ru_aot_morph_JavaMorphAPI
 * Method:    lookupWordImpl
 * Signature: (I[B)Lru/aot/morph/JavaMorphAPI/WordResult;
 */
JNIEXPORT jobject JNICALL Java_ru_aot_morph_JavaMorphAPI_lookupWordImpl
  (JNIEnv *env, jclass clazz, jint languageId, jbyteArray word){
	jbyte* bytes=NULL;
	char* chars=NULL;
	try{
		if(!inited||dic.pAgramtab==0||dic.pLemmatizer==0){
			throwEx(env, strdup("Dictionaries are not loaded. Call JavaMorphAPI.initDictionaries() first!"));
			return NULL;
		}
		if(languageId!=0){
			throwEx(env, strdup("The only language implemented is Russian."));
			return NULL;
		}
		if(word==NULL){
			throwEx(env, strdup("word is null"));
			return NULL;
		}

		jsize len=env->GetArrayLength(word);
		bytes=env->GetByteArrayElements(word,NULL);
		if(bytes==0){
			throwEx(env, strdup("Out of memory"));
			return NULL;
		}
		chars=(char*)malloc(len+1);
		if(chars==0){
			throwEx(env, strdup("Out of memory"));
			return NULL;
		}
		for(jsize i=0;i<len;i++){chars[i]=(char)bytes[i];}
		chars[len]=(char)0;
		string s = chars;
		free(chars);chars=0;
		env->ReleaseByteArrayElements(word,bytes,JNI_ABORT);
		bytes=NULL;
		Trim(s);
		if (s.empty()){
			throwEx(env, strdup("Empty or whitespace-only string instead of a word."));
			return NULL;
		}
		return GetMorphInfo(env, clazz, dic, s);
	}catch(CExpc& e){
		char* err=str_compose("C++ exception: CExpc: %s",e.m_strCause.c_str());
		throwEx(env, err);
		if(chars!=0){free(chars);chars=0;}
		if(bytes!=NULL){env->ReleaseByteArrayElements(word,bytes,JNI_ABORT);bytes=NULL;}
	}catch(int e){
		string errstr("C++ exception: int: ");
		errstr+=e;
		errstr+=".";
		throwEx(env, strdup(errstr.c_str()));
		if(chars!=0){free(chars);chars=0;}
		if(bytes!=NULL){env->ReleaseByteArrayElements(word,bytes,JNI_ABORT);bytes=NULL;}
	}catch(...){
		throwEx(env, strdup("Unknown C++ exception."));
		if(chars!=0){free(chars);chars=0;}
		if(bytes!=NULL){env->ReleaseByteArrayElements(word,bytes,JNI_ABORT);bytes=NULL;}
	}
	return NULL;
}
Ejemplo n.º 4
0
/*
 * Class:     ru_aot_morph_JavaMorphAPI
 * Method:    initImpl
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_ru_aot_morph_JavaMorphAPI_initImpl
  (JNIEnv *env, jclass clazz, jint languagesBitSet){
	dic.pAgramtab=0;
	dic.pLemmatizer=0;
	setClazz=NULL;
	setConstructor=NULL;
	JNIAPIExceptionClass=NULL;
	method_convertFromCharsetCp1251ToJavaString=NULL;
	method_grammemSetAddGrammem=NULL;
	method_paradigmsetAddNewParadigm=NULL;
	method_wordresult_new=NULL;
	JNIAPIExceptionClass=env->FindClass("ru/aot/morph/JavaMorphAPI$JavaMorphAPIException");if(JNIAPIExceptionClass==NULL||env->ExceptionOccurred()){if(!env->ExceptionOccurred())env->FatalError("JNIMorphAPI JNI: Cannot resolve exception class");return;}
	JNIAPIExceptionClass=(jclass)env->NewGlobalRef(JNIAPIExceptionClass);if(JNIAPIExceptionClass==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	setClazz=env->FindClass("java/util/HashSet");if(setClazz==NULL||env->ExceptionOccurred()){throwEx(env,strdup("Out of memory"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	setClazz=(jclass)env->NewGlobalRef(setClazz);if(setClazz==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	setConstructor=env->GetMethodID(setClazz, "<init>", "()V");if(setConstructor==NULL||env->ExceptionOccurred()){throwEx(env,strdup("Out of memory"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	//setConstructor=(jmethodID)env->NewGlobalRef(setConstructor);if(setConstructor==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	method_convertFromCharsetCp1251ToJavaString=env->GetStaticMethodID(clazz, "convertFromCP1251", "([B)Ljava/lang/String;");if(method_convertFromCharsetCp1251ToJavaString==NULL||env->ExceptionOccurred()){throwEx(env,strdup("Out of memory"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	//method_convertFromCharsetCp1251ToJavaString=(jmethodID)env->NewGlobalRef(method_convertFromCharsetCp1251ToJavaString);if(method_convertFromCharsetCp1251ToJavaString==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	method_grammemSetAddGrammem=env->GetStaticMethodID(clazz, "addGrammemToSet", "(Ljava/util/HashSet;I)V");if(method_grammemSetAddGrammem==NULL||env->ExceptionOccurred()){throwEx(env,strdup("Out of memory"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	//method_grammemSetAddGrammem=(jmethodID)env->NewGlobalRef(method_grammemSetAddGrammem);if(method_grammemSetAddGrammem==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	method_paradigmsetAddNewParadigm=env->GetStaticMethodID(clazz, "addParadigmToSet", "(Ljava/util/HashSet;Ljava/util/HashSet;Ljava/lang/String;ZI)V");if(method_paradigmsetAddNewParadigm==NULL||env->ExceptionOccurred()){throwEx(env,strdup("Out of memory"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	//method_paradigmsetAddNewParadigm=(jmethodID)env->NewGlobalRef(method_paradigmsetAddNewParadigm);if(method_paradigmsetAddNewParadigm==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	method_wordresult_new=env->GetStaticMethodID(clazz, "createWordResult", "(Ljava/util/HashSet;)Lru/aot/morph/JavaMorphAPI$WordResult;");if(method_wordresult_new==NULL||env->ExceptionOccurred()){throwEx(env,strdup("method_wordresult_new is null"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}
	//method_wordresult_new=(jmethodID)env->NewGlobalRef(method_wordresult_new);if(method_wordresult_new==NULL||env->ExceptionOccurred()){throwEx(env,strdup("global ref error"));Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);return;}

	inited=false;
	if(languagesBitSet==0){
		throwEx(env, strdup("The set of languages is empty."));
		return;
	}
	if(languagesBitSet!=1){
		throwEx(env, strdup("Russian is the only language supported by JavaMorphAPI."));
		return;
	}
	dic.Language=morphRussian;
	try{
		//dic.Language=morphEnglish;
		//dic.Language=morphGerman;

		//LOADING DICTS
		bool bResult = false;
		switch (dic.Language){
			case morphRussian:
				bResult = InitMorphologySystem<CLemmatizerRussian, CRusGramTab>(env,dic);
				break;
/*			case morphEnglish :
				bResult = InitMorphologySystem<CLemmatizerEnglish, CEngGramTab>(env,dic);
				break;
			case morphGerman:
				bResult = InitMorphologySystem<CLemmatizerGerman, CGerGramTab>(env,dic);
				break; */
			default:
				throwEx(env,strdup("assertion error: A2."));
				return;
		};
		if (!bResult){
			Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);
			return;//exception was thrown by InitMorphologySystem
		}

		inited=true;
		return;//ok
	}catch(CExpc& e){
		const char* ca=e.m_strCause.c_str();
		char* err=str_compose("C++ exception: CExpc: %s",ca);
		throwEx(env, err);
		Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);
		return;
	}catch(int e){
		string errstr("C++ exception: int: ");
		errstr+=e;
		errstr+=".";
		throwEx(env, strdup(errstr.c_str()));
		Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);
		return;
	}catch(...){
		throwEx(env, strdup("Unknown C++ exception."));
		Java_ru_aot_morph_JavaMorphAPI_closeImpl(env,clazz);
		return;
	}
}
Ejemplo n.º 5
0
int phishing_init(struct cl_engine* engine)
{
	char *url_regex, *realurl_regex;
	struct phishcheck* pchk;
	if(!engine->phishcheck) {
		pchk = engine->phishcheck = cli_malloc(sizeof(struct phishcheck));
		if(!pchk)
			return CL_EMEM;
		pchk->is_disabled = 1;
	}
	else {
		pchk = engine->phishcheck;
		if(!pchk)
			return CL_ENULLARG;
		if(!pchk->is_disabled) {
			/* already initialized */
			return CL_SUCCESS;
		}
	}

	cli_dbgmsg("Initializing phishcheck module\n");

	if(build_regex(&pchk->preg_hexurl,cloaked_host_regex,1)) {
		free(pchk);
		engine->phishcheck = NULL;
		return CL_EFORMAT;
	}

	if(build_regex(&pchk->preg_cctld,cctld_regex,1)) {
		free(pchk);
		engine->phishcheck = NULL;
		return CL_EFORMAT;
	}
	if(build_regex(&pchk->preg_tld,tld_regex,1)) {
		free_regex(&pchk->preg_cctld);
		free(pchk);
		engine->phishcheck = NULL;
		return CL_EFORMAT;	
	}
	url_regex = str_compose("^ *(("URI_CHECK_PROTOCOLS")|("URI_fragmentaddress1,URI_fragmentaddress2,URI_fragmentaddress3")) *$");
	if(build_regex(&pchk->preg,url_regex,1)) {
		free_regex(&pchk->preg_cctld);
		free_regex(&pchk->preg_tld);
		free(url_regex);
		free(pchk);
		engine->phishcheck = NULL;
		return CL_EFORMAT;
	}
	free(url_regex);
	realurl_regex = str_compose("^ *(("URI_CHECK_PROTOCOLS")|("URI_path1,URI_fragmentaddress2,URI_fragmentaddress3")) *$");
	if(build_regex(&pchk->preg_realurl, realurl_regex,1)) {
		free_regex(&pchk->preg_cctld);
		free_regex(&pchk->preg_tld);
		free_regex(&pchk->preg);
		free(url_regex);
		free(realurl_regex);
		free(pchk);
		engine->phishcheck = NULL;
		return CL_EFORMAT;
	}
	free(realurl_regex);
	if(build_regex(&pchk->preg_numeric,numeric_url_regex,1)) {
		free_regex(&pchk->preg_cctld);
		free_regex(&pchk->preg_tld);
		free_regex(&pchk->preg);
		free_regex(&pchk->preg_realurl);
		free(pchk);
		engine->phishcheck = NULL;
		return CL_EFORMAT;
	}
	pchk->is_disabled = 0;
	cli_dbgmsg("Phishcheck module initialized\n");
	return CL_SUCCESS;
}