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 [9/9] - /incubator/uima/uimacpp/trunk/src/framework/
Propchange: incubator/uima/uimacpp/trunk/src/framework/taespecifierbuilder.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/timedatetools.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/timedatetools.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/timedatetools.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/timedatetools.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,120 @@
+/*
+-------------------------------------------------------------------------------
+
+ * 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.
+
+-------------------------------------------------------------------------------
+*/
+
+#include "uima/timedatetools.hpp"
+#include "uima/strtools.hpp"
+#include "uima/strconvert.hpp"
+#include "uima/text.h"
+
+namespace uima {
+
+
+ const size_t cuiSecDigits_After_Period = 3;
+ const size_t cuiPctDigits_After_Period = 1;
+
+
+ string Timer::getDescription(size_t padWidth) const {
+ if (iv_strDecscription.length() == 0) {
+ return "";
+ }
+ return (strpad_r_copy(iv_strDecscription, padWidth, ' ') + ": "); //lint !e1024: No function has same argument count as 'strpad_r_copy(const std::basic_string<char,std::char_traits<char>,std::allocator<char>>, unsigned int, char)', 2 candidates found
+ }
+
+ string Timer::timeString(double dTime) {
+ string s;
+ if (dTime <= FLT_MIN) {
+ s = "0";
+ } else {
+ double2String(dTime, cuiSecDigits_After_Period, s);
+ }
+ return strpad_l_copy(s, (size_t)6, ' ') + " seconds";
+ }
+
+ string Timer::timeString() const {
+ return getDescription() + timeString(getAccumulatedTime());
+ }
+
+ string
+ Timer::timeAndPercentString(double dRelativeToSeconds) const {
+ string strAccumulatedTime("0");
+ double dRelativeTime = 0.0;
+ if (getAccumulatedTime() > FLT_MIN) {
+ double2String(getAccumulatedTime(), cuiSecDigits_After_Period, strAccumulatedTime);
+ dRelativeTime = (getAccumulatedTime() / dRelativeToSeconds);
+ }
+ string strRelativePct;
+ double2String(100.0 * dRelativeTime, cuiPctDigits_After_Period , strRelativePct);
+
+ return (getDescription() +
+ strpad_l_copy(strAccumulatedTime, (size_t)6) +
+ _TEXT(" seconds (") +
+ strpad_l_copy(strRelativePct, (size_t)cuiPctDigits_After_Period+3) +
+ _TEXT("%)"));
+ }
+
+ string
+ Timer::timeAndPercentAndThroughputString(
+ double dRelativeToSeconds,
+ size_t uiRelativeToItems,
+ const string & strItemsname
+ ) const {
+ string s;
+ string s2;
+ return getDescription() +
+ strpad_l_copy(double2String(getAccumulatedTime(), cuiSecDigits_After_Period, s), (size_t)6) +
+ _TEXT(" seconds (") +
+ strpad_l_copy(double2String(100.0 * (getAccumulatedTime() / dRelativeToSeconds), cuiPctDigits_After_Period , s), (size_t)cuiPctDigits_After_Period+3) +
+ _TEXT("% ") +
+ double2String(uiRelativeToItems / getAccumulatedTime(), cuiPctDigits_After_Period, s2) +
+ _TEXT(" ") + (strItemsname.length() == 0 ? (string)_TEXT("items") : strItemsname) +
+ _TEXT("/sec)");
+ }
+
+ string
+ Timer::timeAndPercentString(const Timer& crclRelativeToTimer) const {
+ return timeAndPercentString(crclRelativeToTimer.getAccumulatedTime());
+ }
+
+ string
+ Timer::relativeThroughputString(size_t uiRelativeToItems, const string & strItemsname) const {
+ string s;
+ return getDescription() +
+ double2String(uiRelativeToItems / getAccumulatedTime(), cuiSecDigits_After_Period, s) +
+ " " + (strItemsname.length() == 0 ? (string)_TEXT("items") : strItemsname) + _TEXT("/second");
+ }
+
+ string
+ Timer::percentString(double dRelativeToSeconds) const {
+ string s;
+ return getDescription() +
+ double2String(100.0 * (getAccumulatedTime() / dRelativeToSeconds), cuiPctDigits_After_Period, s) +
+ _TEXT("%");
+ }
+
+ string
+ Timer::percentString(const Timer& crclRelativeToTimer) const {
+ return percentString(crclRelativeToTimer.getAccumulatedTime());
+ }
+
+} //namespace uima
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/timedatetools.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/token_properties.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/token_properties.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/token_properties.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/token_properties.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,134 @@
+/*
+-----------------------------------------------------------------------------
+
+ * 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:
+
+-----------------------------------------------------------------------------
+
+
+ 8/11/00 Initial creation
+
+-------------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+
+#include "uima/token_properties.hpp"
+#include "uima/unistrref.hpp"
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+ TokenProperties::TokenProperties(
+ const icu::UnicodeString & ustrInputString
+ ) {
+ initFromString(ustrInputString.getBuffer(), ustrInputString.getBuffer()+ustrInputString.length());
+ }
+
+ TokenProperties::TokenProperties(
+ const UnicodeStringRef & ulstrInputString
+ ) {
+ if (ulstrInputString.length() == 0) {
+ return;
+ }
+ initFromString(ulstrInputString.getBuffer(), ulstrInputString.getBuffer()+ulstrInputString.length());
+ }
+
+ TokenProperties::TokenProperties(
+ const UChar * cpucCurrent,
+ const UChar * cpucEnd
+ ) {
+ initFromString(cpucCurrent, cpucEnd);
+ }
+
+// some quick check macros solving the task for characters < 128 inline
+#define CHECK_U_UPPER( c ) \
+ ( ( c >= (UChar)65 && c <= (UChar)90) || u_isupper( c ) )
+#define CHECK_U_LOWER( c ) \
+ ( ( c >= (UChar)97 && c <= (UChar)122) || u_islower( c ) )
+#define CHECK_U_DIGIT( c ) \
+ ( ( c >= (UChar)48 && c <= (UChar)57) || u_isdigit( c ) )
+
+ void
+ TokenProperties::initFromString(
+ const UChar * cpucCurrent,
+ const UChar * cpucEnd
+ ) {
+ iv_w32Bits = 0;
+
+ assert(EXISTS(cpucCurrent));
+ assert(EXISTS(cpucEnd));
+
+
+ if (cpucCurrent == cpucEnd) {
+ return;
+ }
+ if (CHECK_U_LOWER(*cpucCurrent)) {
+ setLower();
+ } else if (CHECK_U_UPPER(*cpucCurrent)) {
+ setLeadingUpper();
+ } else if (CHECK_U_DIGIT(*cpucCurrent)) {
+ setNumeric();
+ } else {
+ setSpecial();
+ }
+ ++cpucCurrent;
+ while (cpucCurrent < cpucEnd) {
+ if (CHECK_U_LOWER(*cpucCurrent)) {
+ setLower();
+ } else if (CHECK_U_UPPER(*cpucCurrent)) {
+ setTrailingUpper();
+ } else if (CHECK_U_DIGIT(*cpucCurrent)) {
+ setNumeric();
+ } else {
+ setSpecial();
+ }
+ ++cpucCurrent;
+ }
+ }
+
+ string
+ TokenProperties::to_string( void ) const {
+ string s;
+ for (size_t i = UIMA_TOKEN_PROP_SPECIAL; i > 0; i /= 2) {
+ s += ((iv_w32Bits & i) != 0 ? '1' : '0');
+ }
+ return s;
+ }
+
+} //namespace uima
+
+/* ----------------------------------------------------------------------- */
+/* <EOF> */
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/token_properties.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/typesystemdescription.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/typesystemdescription.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/typesystemdescription.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/typesystemdescription.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,420 @@
+/** \file typesystemdescription.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: Resource Specifier Classes for Text Analysis Engines
+
+-----------------------------------------------------------------------------
+
+ 02/05/2003 Initial creation
+
+-------------------------------------------------------------------------- */
+
+#include "uima/typesystemdescription.hpp"
+#include "uima/taespecifierbuilder.hpp"
+#include <memory>
+#include "uima/msg.h"
+namespace uima {
+
+ TypeDescription::TypeDescription(const TypeDescription & crOther) {
+ iv_typeName = crOther.getName();
+ iv_superTypeName = crOther.getSuperTypeName();
+ iv_description = crOther.getDescription();
+
+ TyVecpFeatureDescriptions otherDescs = crOther.getFeatureDescriptions();
+ TyVecpFeatureDescriptions::iterator ite;
+ for (ite = otherDescs.begin(); ite != otherDescs.end(); ite++) {
+ bool takesMemoryOwnership;
+ auto_ptr<FeatureDescription> desc ( new FeatureDescription(**ite) );
+ addFeatureDescription(desc.get(), takesMemoryOwnership);
+ if (takesMemoryOwnership) {
+ desc.release();
+ }
+ }
+
+ TyVecpAllowedValues otherAllowedValues = crOther.getAllowedValues();
+ TyVecpAllowedValues::iterator ite1;
+ for (ite1 = otherAllowedValues.begin(); ite1 != otherAllowedValues.end(); ite1++) {
+ bool takesMemoryOwnership;
+ auto_ptr<AllowedValue> allowedval ( new AllowedValue(**ite1) );
+ addAllowedValue(allowedval.get(), takesMemoryOwnership);
+ if (takesMemoryOwnership) {
+ allowedval.release();
+ }
+ }
+
+ }
+
+ TyErrorId TypeDescription::addFeatureDescription(FeatureDescription * feature, bool & takesMemoryOwnership) {
+ takesMemoryOwnership = false;
+ if (! isModifiable()) {
+ return UIMA_ERR_CONFIG_OBJECT_COMITTED;
+ }
+ FeatureDescription * pMyFeat = getFeatureDescription(feature->getName());
+ if (pMyFeat != NULL) {
+ ErrorMessage msg(UIMA_MSG_ID_EXC_DUPLICATE_FEATURE_NAME, getName());
+ msg.addParam(feature->getName());
+ UIMA_EXC_THROW_NEW(DuplicateConfigElemException,
+ UIMA_ERR_CONFIG_DUPLICATE_FEATURE_NAME,
+ msg,
+ UIMA_MSG_ID_EXCON_BUILD_TAE_SPEC,
+ ErrorInfo::unrecoverable);
+ } else {
+ takesMemoryOwnership = true;
+ iv_vecpFeatureDescriptions.push_back(feature);
+ }
+ return UIMA_ERR_NONE;
+ }
+
+ FeatureDescription * TypeDescription::getFeatureDescription(const icu::UnicodeString & featureName) const {
+ bool found = false;
+ size_t i=0;
+ while ((! found) && i < iv_vecpFeatureDescriptions.size()) {
+ found = (featureName.compare(iv_vecpFeatureDescriptions[i]->getName()) == 0);
+ i++;
+ }
+ if (found) {
+ return(iv_vecpFeatureDescriptions[i-1]);
+ } else {
+ return NULL;
+ }
+ }
+
+ void TypeDescription::mergeFeatureDescriptions(const TyVecpFeatureDescriptions & descs) {
+ size_t i;
+ for (i=0; i < descs.size(); i++) {
+ FeatureDescription * pOtherFeatDesc = descs[i]; // *ite;
+ FeatureDescription * pMyFeatDesc = getFeatureDescription(pOtherFeatDesc->getName());
+ if (pMyFeatDesc != NULL) {
+ //the feature already exists
+ //check the rangetype
+ if (pMyFeatDesc->getRangeTypeName().compare(pOtherFeatDesc->getRangeTypeName()) == 0) {
+ //it's the same feature, we don't have to add it
+ } else {
+ bool takesMemoryOwnership;
+ //error: same name, different range. Will be raised during validation
+ //we have to add a copy of FeatureDescription, as we have to get memory ownership
+ FeatureDescription * pCopyFeatDesc = new FeatureDescription(*pOtherFeatDesc);
+ addFeatureDescription(pCopyFeatDesc, takesMemoryOwnership);
+ if (!takesMemoryOwnership) {
+ delete pCopyFeatDesc;
+ }
+ }
+ } else {
+ //add the feature
+ bool takesMemoryOwnership;
+ FeatureDescription * pCopyFeatDesc = new FeatureDescription(*pOtherFeatDesc);
+ addFeatureDescription(pCopyFeatDesc, takesMemoryOwnership);
+ assert( takesMemoryOwnership);
+ }
+ }
+ }
+
+ TyErrorId TypeSystemDescription::addTypeDescription(TypeDescription * pDesc, bool & takesMemoryOwnership) {
+ takesMemoryOwnership = false;
+ if (! isModifiable()) {
+ return UIMA_ERR_CONFIG_OBJECT_COMITTED;
+ }
+
+ TypeDescription * pMyTypeDesc = getTypeDescription(pDesc->getName());
+ if (pMyTypeDesc != NULL) {
+ //the type already exists
+ vector<TypeDescription*> v;
+ v.push_back(pDesc);
+ mergeTypeDescriptions(v);
+ } else {
+ iv_vecpTypeDescriptions.push_back(pDesc);
+ takesMemoryOwnership = true;
+ }
+
+ return UIMA_ERR_NONE;
+ }
+
+ TypeDescription * TypeSystemDescription::getTypeDescription(const icu::UnicodeString & typeName) const {
+ bool found = false;
+ size_t i=0;
+ while ( (! found) && i<iv_vecpTypeDescriptions.size()) {
+ found = (typeName.compare(iv_vecpTypeDescriptions[i]->getName()) == 0);
+ i++;
+ }
+ if (found) {
+ return(iv_vecpTypeDescriptions[i-1]);
+ } else {
+ return NULL;
+ }
+ }
+
+ void TypeSystemDescription::mergeTypeDescriptions(const TyVecpTypeDescriptions & descs) {
+ size_t i;
+ for (i=0; i < descs.size(); i++) {
+ TypeDescription * pOtherTypeDesc = descs[i];
+ TypeDescription * pMyTypeDesc = getTypeDescription(pOtherTypeDesc->getName());
+ if (EXISTS(pMyTypeDesc)) {
+ //the type already exists
+ //check the supertype name
+ if (pMyTypeDesc->getSuperTypeName().compare(pOtherTypeDesc->getSuperTypeName()) == 0) {
+ //it's the same type
+ //merge the feature descriptions
+ pMyTypeDesc->mergeFeatureDescriptions(pOtherTypeDesc->getFeatureDescriptions());
+ pMyTypeDesc->mergeAllowedValues(pOtherTypeDesc->getAllowedValues());
+ } else {
+ //error: same name and different supertype name
+ UIMA_EXC_THROW_NEW(DuplicateConfigElemException,
+ UIMA_ERR_CONFIG_DUPLICATE_TYPE_NAME,
+ ErrorMessage(UIMA_MSG_ID_EXC_DUPLICATE_TYPE_NAME, pOtherTypeDesc->getName()),
+ UIMA_MSG_ID_EXCON_BUILD_TAE_SPEC,
+ ErrorInfo::unrecoverable);
+ }
+ } else {
+ //it's a new type description, add it
+ bool takesMemoryOwnerShip;
+ addTypeDescription(new TypeDescription(*pOtherTypeDesc), takesMemoryOwnerShip);
+ assert( takesMemoryOwnerShip );
+ }
+ }
+ }
+
+
+ TyErrorId TypeSystemDescription::addImportDescription(ImportDescription * pDesc,
+ bool & takesMemoryOwnerShip) {
+ takesMemoryOwnerShip=false;
+ if (! isModifiable()) {
+ return UIMA_ERR_CONFIG_OBJECT_COMITTED;
+ }
+
+ iv_vecpImportDescriptions.push_back(pDesc);
+ takesMemoryOwnerShip=true;
+ return UIMA_ERR_NONE;
+ }
+
+
+ TyErrorId TypeSystemDescription::resolveImports(vector<icu::UnicodeString> & alreadyImportedTypeSystemLocations) {
+
+ //holds the import by name descriptors.
+ vector<uima::ImportDescription *> vecpImportByNameDescriptions;
+
+ //process each import description
+ for (size_t i=0; i < iv_vecpImportDescriptions.size(); i++) {
+ ImportDescription * pImport = iv_vecpImportDescriptions[i];
+ //only handle import by 'location'.
+ //we do not handle import by 'name' which requires looking into
+ //classpath and needs to be handle in Java.
+ if (pImport->getLocation().length() > 0) {
+ const UnicodeString & fileLocation = pImport->findAbsoluteUrl(iv_xmlFileLoc);
+ //check it is not in already Imported list
+ bool needsToBeResolved=true;
+ for (size_t j=0; j < alreadyImportedTypeSystemLocations.size() ;j++) {
+ if (fileLocation.compare(alreadyImportedTypeSystemLocations[j]) == 0 ) {
+ needsToBeResolved=false;
+ break;
+ }
+ }
+
+ if (needsToBeResolved) {
+ //resolve import
+ resolveImport(fileLocation,
+ alreadyImportedTypeSystemLocations,
+ iv_vecpTypeDescriptions);
+ }
+ //delete this import description
+ delete iv_vecpImportDescriptions[i];
+ iv_vecpImportDescriptions[i] = 0;
+ } else {
+ // save the import descriptor.
+ // will be passed to Java if calling Java
+ vecpImportByNameDescriptions.push_back(iv_vecpImportDescriptions[i]);
+ iv_vecpImportDescriptions[i] = 0;
+ }
+
+ }
+ iv_vecpImportDescriptions.clear();
+
+ //add back the import by name descriptors
+ for (size_t i=0; i < vecpImportByNameDescriptions.size(); i++) {
+ iv_vecpImportDescriptions.push_back(vecpImportByNameDescriptions[i]);
+ }
+ return UIMA_ERR_NONE;
+ }
+
+ TyErrorId TypeSystemDescription::resolveImport(const icu::UnicodeString & fileLocation,
+ vector<icu::UnicodeString> & alreadyImportedTypeSystemLocations,
+ TyVecpTypeDescriptions & result) {
+
+ //build typesystem description
+ TextAnalysisEngineSpecifierBuilder builder;
+ TypeSystemDescription * pDesc = builder.buildTypeSystemSpecifierFromFile(fileLocation);
+ if (EXISTS(pDesc)) {
+ //add this file to already imported list
+ alreadyImportedTypeSystemLocations.push_back(fileLocation);
+
+ //resolve imports within this descriptor
+ pDesc->resolveImports(alreadyImportedTypeSystemLocations);
+
+ //add type descriptions to result
+ const TyVecpTypeDescriptions & importedTypes = pDesc->getTypeDescriptions();
+ for (size_t i=0; i < importedTypes.size() ; i++) {
+ TypeDescription * pOtherTypeDesc = importedTypes[i];
+ bool takesMemoryOwnerShip;
+ TypeDescription * pType = new TypeDescription(*pOtherTypeDesc);
+ addTypeDescription(pType, takesMemoryOwnerShip);
+ if (!takesMemoryOwnerShip) {
+ delete pType;
+ }
+ }
+ delete pDesc;
+ return UIMA_ERR_NONE;
+ } else {
+ return UIMA_ERR_RESOURCE_CORRUPTED;
+ }
+
+ }
+
+
+ void TypeSystemDescription::validate() {
+
+ size_t i=0;
+
+ TypeSystemDescription::TyVecpTypeDescriptions typeDescs = getTypeDescriptions();
+
+ vector < icu::UnicodeString > typeNames;
+
+ for (i=0; i<typeDescs.size(); i++) {
+ typeNames.push_back(typeDescs[i]->getName());
+ }
+
+ //ensure that there are no two type descriptions with the same name
+ for (i=0; i < typeNames.size(); i++) {
+ if (countValues(typeNames.begin(), typeNames.end(), typeNames[i]) > 1) {
+ UIMA_EXC_THROW_NEW(ValidationException,
+ UIMA_ERR_CONFIG_DUPLICATE_TYPE_NAME,
+ ErrorMessage(UIMA_MSG_ID_EXC_DUPLICATE_TYPE_NAME, typeNames[i]),
+ UIMA_MSG_ID_EXCON_VALIDATE_TAE_SPEC,
+ ErrorInfo::unrecoverable);
+
+ }
+ }
+
+ //ensure that the type descriptions are valid
+ for (i=0; i<typeDescs.size(); i++) {
+ typeDescs[i]->validate();
+ }
+ }
+
+
+
+ //allowed values
+ TyErrorId TypeDescription::addAllowedValue(AllowedValue * allowed, bool & takesMemoryOwnership) {
+ takesMemoryOwnership = false;
+ if (! isModifiable()) {
+ return UIMA_ERR_CONFIG_OBJECT_COMITTED;
+ }
+ AllowedValue * pMy = getAllowedValue(allowed->getName());
+ if (pMy != NULL) {
+ ErrorMessage errMsg = ErrorMessage(UIMA_MSG_ID_EXC_DUPLICATE_ALLOWED_VALUE);
+ errMsg.addParam(getName());
+ errMsg.addParam(allowed->getName());
+
+ UIMA_EXC_THROW_NEW(DuplicateConfigElemException,
+ UIMA_ERR_CONFIG_DUPLICATE_ALLOWED_VALUE,
+ errMsg,
+ //ErrorMessage(UIMA_MSG_ID_EXC_DUPLICATE_ALLOWED_VALUE, allowed->getName()),
+ UIMA_MSG_ID_EXCON_BUILD_TAE_SPEC,
+ ErrorInfo::unrecoverable);
+
+ } else {
+ takesMemoryOwnership = true;
+ iv_vecpAllowedValues.push_back(allowed);
+ }
+ return UIMA_ERR_NONE;
+ }
+
+ AllowedValue * TypeDescription::getAllowedValue(const icu::UnicodeString & name) const {
+ bool found = false;
+ size_t i=0;
+ while ((! found) && i < iv_vecpAllowedValues.size()) {
+ found = (name.compare(iv_vecpAllowedValues[i]->getName()) == 0);
+ i++;
+ }
+ if (found) {
+ return(iv_vecpAllowedValues[i-1]);
+ } else {
+ return NULL;
+ }
+ }
+
+ void TypeDescription::mergeAllowedValues(const TyVecpAllowedValues & descs) {
+ size_t i;
+ for (i=0; i < descs.size(); i++) {
+ AllowedValue * pOtherDesc = descs[i]; // *ite;
+ AllowedValue * pMyDesc = getAllowedValue(pOtherDesc->getName());
+ if (pMyDesc != NULL) {
+ //already exists
+ } else {
+ //add
+ bool takesMemoryOwnership;
+ AllowedValue * pCopyDesc = new AllowedValue(*pOtherDesc);
+ addAllowedValue(pCopyDesc, takesMemoryOwnership);
+ assert( takesMemoryOwnership);
+ }
+ }
+ }
+
+
+ void TypeDescription::validate() {
+
+ // if there are allowed values
+ if (getAllowedValues().size() != 0) {
+ if (getSuperTypeName() != "uima.cas.String") {
+ UIMA_EXC_THROW_NEW(ValidationException,
+ UIMA_ERR_CONFIG_ALLOWED_VALUES_DEFINED_FOR_NON_STRING_TYPE,
+ ErrorMessage( UIMA_MSG_ID_EXC_ALLOWEDVAL_DEF_FOR_NONSTRINGTYPE,getName()),
+ UIMA_MSG_ID_EXCON_VALIDATE_TAE_SPEC,
+ ErrorInfo::unrecoverable);
+ }
+ }
+
+ size_t i=0;
+ TypeDescription::TyVecpFeatureDescriptions featDescs = getFeatureDescriptions();
+ vector<icu::UnicodeString> featureNames;
+
+ //ensure that there are no two feature descriptions with the same name
+ for (i=0; i<featDescs.size(); i++) {
+ if (countValues(featureNames.begin(), featureNames.end(), featDescs[i]->getName()) != 0) {
+ ErrorMessage errMsg(UIMA_MSG_ID_EXC_DUPLICATE_FEATURE_NAME);
+ errMsg.addParam(getName());
+ errMsg.addParam(featureNames[i]);
+ UIMA_EXC_THROW_NEW(ValidationException,
+ UIMA_ERR_CONFIG_DUPLICATE_FEATURE_NAME,
+ errMsg,
+ UIMA_MSG_ID_EXCON_VALIDATE_TAE_SPEC,
+ ErrorInfo::unrecoverable);
+
+ } else {
+ featureNames.push_back(featDescs[i]->getName());
+ }
+ }
+ }
+
+
+
+
+}
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/typesystemdescription.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/u2cpcnvrt.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/u2cpcnvrt.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/u2cpcnvrt.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/u2cpcnvrt.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,141 @@
+/** @name cp2ucnvrt.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 CodePage2UnicodeConverter
+
+-----------------------------------------------------------------------------
+
+
+ 5/21/1999 Initial creation
+
+-------------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Include dependencies */
+/* ----------------------------------------------------------------------- */
+
+#include "uima/u2cpcnvrt.hpp"
+
+#include "uima/comp_ids.h"
+
+#include "uima/assertmsg.h"
+#include "uima/trace.hpp"
+#include "uima/msg.h"
+
+
+/* ----------------------------------------------------------------------- */
+/* Constants */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Forward declarations */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Private Implementation */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/* Implementation */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+ Unicode2CodePageConverter::Unicode2CodePageConverter(const char * crConverterName) :
+ iv_uconverter(NULL)
+ /* ----------------------------------------------------------------------- */
+ {
+ UErrorCode err=(UErrorCode)0;
+ iv_uconverter = ucnv_open(crConverterName, &err);
+ if (!U_SUCCESS(err)) {
+ cerr << "CodePage2UnicodeConverter ERROR could not open converter for " << crConverterName << endl;
+ ErrorMessage errMsg = ErrorMessage(UIMA_MSG_ID_CODEPAGE_CONV_ERROR);
+ errMsg.addParam(err);
+ errMsg.addParam(crConverterName);
+ UIMA_EXC_THROW_NEW(CodePageConversionException,
+ UIMA_ERR_CODEPAGE,
+ errMsg,
+ UIMA_MSG_ID_CODEPAGE_CONV_ERROR,
+ ErrorInfo::unrecoverable);
+ }
+ assert(iv_uconverter != NULL);
+ }
+
+ size_t Unicode2CodePageConverter::getMaximumSize(const UChar * cpclSource,
+ size_t uiSourceLength) {
+ size_t uiTargetSize;
+ assert(iv_uconverter !=NULL);
+ UErrorCode err=(UErrorCode)0;
+ uiTargetSize = ucnv_fromUChars(iv_uconverter, NULL,
+ 0, cpclSource, uiSourceLength, &err);
+
+ if (!U_SUCCESS(err) && err != U_BUFFER_OVERFLOW_ERROR) {
+ ///cerr << "ERROR getMaximumSize() " << err << endl;
+ ErrorMessage errMsg = ErrorMessage(UIMA_MSG_ID_CODEPAGE_CONV_ERROR);
+ errMsg.addParam(err);
+ UIMA_EXC_THROW_NEW(CodePageConversionException,
+ UIMA_ERR_CODEPAGE,
+ errMsg,
+ UIMA_MSG_ID_CODEPAGE_CONV_ERROR,
+ ErrorInfo::unrecoverable);
+ }
+
+ assert(sizeof(char) == 1);
+ return(uiTargetSize); /* as bytes */
+ }
+
+ size_t Unicode2CodePageConverter::convertFromUnicode(char * pacTarget,
+ size_t uiTargetMaxLength,
+ const UChar * cpclSource,
+ size_t uiSourceLength)
+ /* ----------------------------------------------------------------------- */
+ {
+ size_t uiTargetSize;
+ assert(iv_uconverter !=NULL);
+ UErrorCode err=(UErrorCode)0;
+ uiTargetSize = ucnv_fromUChars(iv_uconverter, pacTarget,
+ uiTargetMaxLength,
+ cpclSource, uiSourceLength, &err);
+
+ if (!U_SUCCESS(err) && err != U_BUFFER_OVERFLOW_ERROR) {
+ ///cerr << "ERROR getMaximumSize() " << err << endl;
+ ErrorMessage errMsg = ErrorMessage(UIMA_MSG_ID_CODEPAGE_CONV_ERROR);
+ errMsg.addParam(err);
+ UIMA_EXC_THROW_NEW(CodePageConversionException,
+ UIMA_ERR_CODEPAGE,
+ errMsg,
+ UIMA_MSG_ID_CODEPAGE_CONV_ERROR,
+ ErrorInfo::unrecoverable);
+ }
+
+ assert(sizeof(char) == 1);
+ return(uiTargetSize); /* as bytes */
+ }
+
+}
+
+/* <EOF> */
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/u2cpcnvrt.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/uima/uimacpp/trunk/src/framework/unistrref.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/unistrref.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/unistrref.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/unistrref.cpp Sat Feb 3 08:57:13 2007
@@ -0,0 +1,456 @@
+/**
+-----------------------------------------------------------------------------
+
+ string interface of icu::UnicodeString
+
+ * 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.
+
+-----------------------------------------------------------------------------
+
+
+ 6/26/1998 Initial creation
+
+-----------------------------------------------------------------------------
+*/
+
+#include "uima/unistrref.hpp"
+#include <algorithm> // for min
+#ifdef _MSC_VER
+#include <minmax.h> // for min
+#endif
+/* ----------------------------------------------------------------------- */
+/* Types / Classes */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+
+//========================================
+// Read-only implementation
+//========================================
+
+ int8_t
+ UnicodeStringRef::doCompare( int32_t start,
+ int32_t length,
+ const UChar *srcChars,
+ int32_t srcStart,
+ int32_t srcLength) const {
+ // compare illegal string values
+ if (srcChars==0) {
+ return 1;
+ }
+
+ // pin indices to legal values
+ pinIndices(start, length);
+
+ // get the correct pointer
+ const UChar *chars = getBuffer();
+
+ chars += start;
+ srcChars += srcStart;
+
+ int32_t minLength;
+ int8_t lengthResult;
+
+ // are we comparing different lengths?
+ if (length != srcLength) {
+ if (length < srcLength) {
+ minLength = length;
+ lengthResult = -1;
+ } else {
+ minLength = srcLength;
+ lengthResult = 1;
+ }
+ } else {
+ minLength = length;
+ lengthResult = 0;
+ }
+
+ /*
+ * note that uprv_memcmp() returns an int but we return an int8_t;
+ * we need to take care not to truncate the result -
+ * one way to do this is to right-shift the value to
+ * move the sign bit into the lower 8 bits and making sure that this
+ * does not become 0 itself
+ */
+
+ if (minLength > 0 && chars != srcChars) {
+ int32_t result;
+
+# ifdef WORDS_BIGENDIAN
+ // big-endian: byte comparison works
+ result = memcmp(chars, srcChars, minLength * sizeof(UChar));
+ if (result != 0) {
+ return (int8_t)(result >> 15 | 1);
+ }
+# else
+ // little-endian: compare UChar units
+ do {
+ result = ((int32_t)*(chars++) - (int32_t)*(srcChars++));
+ if (result != 0) {
+ return (int8_t)(result >> 15 | 1);
+ }
+ } while (--minLength > 0);
+# endif
+ }
+ return lengthResult;
+ }
+
+
+ /* String compare in code point order - doCompare() compares in code unit order. */
+ int8_t
+ UnicodeStringRef::doCompareCodePointOrder(int32_t start,
+ int32_t length,
+ const UChar *srcChars,
+ int32_t srcStart,
+ int32_t srcLength) const {
+ if (srcChars==NULL) {
+ return 1;
+ }
+
+ // pin indices to legal values
+ pinIndices(start, length);
+
+ int32_t diff = u_strncmpCodePointOrder(getBuffer() + start, srcChars + srcStart, min(length, srcLength));
+ /* translate the 32-bit result into an 8-bit one */
+ if (diff!=0) {
+ return (int8_t)(diff >> 15 | 1);
+ } else {
+ return 0;
+ }
+ }
+
+ int32_t UnicodeStringRef::indexOf(UChar const *srcChars,
+ int32_t srcStart,
+ int32_t srcLength,
+ int32_t start,
+ int32_t length) const {
+ if (srcChars == 0 || srcLength == 0) {
+ return -1;
+ }
+
+ // get the indices within bounds
+ pinIndices(start, length);
+
+ if (length < srcLength) {
+ return -1;
+ }
+
+ // now we will only work with srcLength-1
+ --srcLength;
+
+ // set length for the last possible match start position
+ // note the --srcLength above
+ length -= srcLength;
+
+
+ const UChar *array = getBuffer();
+ int32_t limit = start + length;
+
+ // search for the first char, then compare the rest of the string
+ // increment srcStart here for that, matching the --srcLength above
+ UChar ch = srcChars[srcStart++];
+
+ do {
+ if (array[start] == ch && (srcLength == 0 || compare(start + 1, srcLength, srcChars, srcStart, srcLength) == 0)) {
+ return start;
+ }
+ } while (++start < limit);
+
+ return -1;
+ }
+
+ int32_t UnicodeStringRef::lastIndexOf(UChar const *srcChars,
+ int32_t srcStart,
+ int32_t srcLength,
+ int32_t start,
+ int32_t length) const {
+ if (srcChars == 0 || srcLength == 0) {
+ return -1;
+ }
+
+ // get the indices within bounds
+ pinIndices(start, length);
+
+ if (length < srcLength) {
+ return -1;
+ }
+
+ // now we will only work with srcLength-1
+ --srcLength;
+
+ // set length for the last possible match start position
+ // note the --srcLength above
+ length -= srcLength;
+
+ const UChar *array = getBuffer();
+ int32_t pos;
+
+ // search for the first char, then compare the rest of the string
+ // increment srcStart here for that, matching the --srcLength above
+ UChar ch = srcChars[srcStart++];
+
+ pos = start + length;
+ do {
+ if (array[--pos] == ch && (srcLength == 0 || compare(pos + 1, srcLength, srcChars, srcStart, srcLength) == 0)) {
+ return pos;
+ }
+ } while (pos > start);
+
+ return -1;
+ }
+
+
+ int32_t
+ UnicodeStringRef::doIndexOf(UChar c,
+ int32_t start,
+ int32_t length) const {
+ // pin indices
+ pinIndices(start, length);
+ if (length == 0) {
+ return -1;
+ }
+
+ // find the first occurrence of c
+ const UChar *begin = getBuffer() + start;
+ const UChar *limit = begin + length;
+
+ do {
+ if (*begin == c) {
+ return (int32_t)(begin - getBuffer());
+ }
+ } while (++begin < limit);
+
+ return -1;
+ }
+
+ int32_t
+ UnicodeStringRef::doLastIndexOf(UChar c,
+ int32_t start,
+ int32_t length) const {
+ // pin indices
+ pinIndices(start, length);
+ if (length == 0) {
+ return -1;
+ }
+
+ const UChar *begin = getBuffer() + start;
+ const UChar *limit = begin + length;
+
+ do {
+ if (*--limit == c) {
+ return (int32_t)(limit - getBuffer());
+ }
+ } while (limit > begin);
+
+ return -1;
+ }
+
+ int32_t UnicodeStringRef::moveIndex32(int32_t index, int32_t delta) const {
+ icu::UnicodeString s((UBool)false, getBuffer(), length());
+ return s.moveIndex32(index, delta);
+ }
+
+ int32_t
+ UnicodeStringRef::extract(UChar *dest, int32_t destCapacity,
+ UErrorCode &errorCode) const {
+ // This readonly aliasing constructor should be cheap as no copy is done
+ icu::UnicodeString s((UBool)false, getBuffer(), length());
+ return s.extract(dest, destCapacity, errorCode);
+ }
+
+ int32_t UnicodeStringRef::extract(int32_t start,
+ int32_t startLength,
+ char *target,
+ uint32_t targetLength,
+ const char *codepage) const {
+ icu::UnicodeString s((UBool)false, getBuffer(), length());
+ return s.extract(start, startLength, target, targetLength, codepage);
+ }
+
+ int32_t UnicodeStringRef::extract(char *target, int32_t targetCapacity,
+ UConverter *cnv,
+ UErrorCode &errorCode) const {
+ icu::UnicodeString s((UBool)false, getBuffer(), length());
+ return s.extract(target, targetCapacity, cnv, errorCode);
+ }
+
+// Copy with conversion into a std::string
+ int32_t UnicodeStringRef::extract(int32_t start,
+ int32_t startLength,
+ std::string & target,
+ const char *codepage) const {
+ if (length() == 0) {
+ target.clear();
+ return 0;
+ }
+
+ // First use a buffer on the stack ... if too small allocate and try again
+ const int32_t STACK_BUF_SIZE = 256;
+ char stackBuf [STACK_BUF_SIZE];
+ char* heapBuf = NULL;
+ char* buf = stackBuf;
+
+ // Use a converter so can be left open if have to convert twice
+ // If fail to open converter simply return empty string ... must be unknown!
+ UErrorCode err = U_ZERO_ERROR;
+ UConverter* cnv = ucnv_open(codepage, &err);
+ if ( U_FAILURE(err) ) {
+ target.clear();
+ return 0;
+ }
+
+ const UChar* src = getBuffer() + start;
+ int len = ucnv_fromUChars(cnv, buf, STACK_BUF_SIZE, src, startLength, &err);
+ if ( err == U_BUFFER_OVERFLOW_ERROR || err == U_STRING_NOT_TERMINATED_WARNING ) {
+ buf = heapBuf = new char [len+1];
+ err = U_ZERO_ERROR;
+ len = ucnv_fromUChars(cnv, buf, len+1, src, startLength, &err);
+ }
+
+ target.assign(buf, len); // Copy the result to the string
+
+ if (heapBuf != NULL)
+ delete [] heapBuf;
+ ucnv_close(cnv);
+
+ return len;
+ }
+
+// Extract into a UTF-8 std::string
+ int32_t UnicodeStringRef::extractUTF8(std::string & target) const {
+ if (length() == 0) {
+ target.clear();
+ return 0;
+ }
+
+ // First use a buffer on the stack ... if too small allocate and try again
+ const int32_t STACK_BUF_SIZE = 256;
+ char stackBuf [STACK_BUF_SIZE];
+ char* heapBuf = NULL;
+ char* buf = stackBuf;
+ int32_t len;
+
+ UErrorCode err = U_ZERO_ERROR;
+ u_strToUTF8(buf, STACK_BUF_SIZE, &len, getBuffer(), length(), &err);
+ if ( err == U_BUFFER_OVERFLOW_ERROR || err == U_STRING_NOT_TERMINATED_WARNING ) {
+ buf = heapBuf = new char [len+1];
+ err = U_ZERO_ERROR;
+ u_strToUTF8(buf, len+1, &len, getBuffer(), length(), &err);
+ }
+
+ target.assign(buf, len); // Copy the result to the string
+
+ if (heapBuf != NULL)
+ delete [] heapBuf;
+
+ return len;
+ }
+
+// Static method releases contents of string container allocated by extract methods
+ void UnicodeStringRef::release(std::string & target) {
+ target.clear(); // Empty string
+ target.reserve(1); // Reduce capacity so will use internal buffer & free external one
+ }
+
+
+ void
+ UnicodeStringRef::toSingleByteStream(std::ostream & outStream) const {
+ const char* codepage;
+
+ // If output goes to console use default encoding
+ if (outStream.rdbuf() == cout.rdbuf() || outStream.rdbuf() == cerr.rdbuf()) {
+ codepage = 0;
+ } else {
+ codepage = "utf-8";
+ }
+ std::string s;
+ extract(s, codepage); // get a single byte string
+ outStream << s;
+ }
+
+ std::ostream &
+ operator << (
+ std::ostream & outStream,
+ const uima::UnicodeStringRef & crUStrRef
+ ) {
+ crUStrRef.toSingleByteStream(outStream);
+ return outStream;
+ }
+
+ int32_t
+ delimitedUnicodeStringRef2Vector(
+ std::vector< uima::UnicodeStringRef > & rveclstrOutput,
+ const UChar * pcInput,
+ int32_t uiInputLength,
+ const UChar * cpszDelimiters,
+ bool bTrimString,
+ bool bInsertEmptyStrings
+ ) {
+ UChar const * pcBegin = pcInput;
+ int32_t uiEnd;
+ UChar const * pcEnd = pcBegin;
+ int32_t uiNumFound = 0;
+ int32_t uiDelimitersLen = u_strlen(cpszDelimiters);
+
+ if (uiInputLength == 0) {
+ return 0;
+ }
+ UChar const * pcInputEnd = pcInput + uiInputLength;
+ UnicodeStringRef _s;
+
+ while (pcBegin < pcInputEnd) {
+ // uiBegin--;
+ uiEnd = str_find_first_of(cpszDelimiters, uiDelimitersLen, pcBegin, (int32_t)(pcInputEnd-pcBegin));
+ pcEnd = pcBegin+uiEnd;
+ if (uiEnd != STRING_NPOS) {
+ ++pcEnd;
+ }
+ if (uiEnd == STRING_NPOS) {
+ uiEnd = uiInputLength+1;
+ pcEnd = pcInputEnd+1;
+ }
+ assert(pcEnd > pcBegin);
+ _s = UnicodeStringRef(pcBegin, pcEnd-pcBegin-1);
+ if (bTrimString) {
+ _s = strtrim(_s);
+ }
+ if (bInsertEmptyStrings || _s.length() > 0) {
+ rveclstrOutput.push_back(_s);
+ uiNumFound++;
+ }
+ pcBegin = pcEnd;
+ }
+ return uiNumFound;
+ }
+
+} // namespace uima
+
+std::ostream &
+operator << (
+ std::ostream & outStream,
+ const uima::UnicodeStringRef & crUStrRef
+) {
+ crUStrRef.toSingleByteStream(outStream);
+ return outStream;
+}
+
+
+
+/* <EOF> */
+
Propchange: incubator/uima/uimacpp/trunk/src/framework/unistrref.cpp
------------------------------------------------------------------------------
svn:eol-style = native