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 [3/9] - /incubator/uima/uimacpp/trunk/src/framework/

Added: incubator/uima/uimacpp/trunk/src/framework/consoleui.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/consoleui.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/consoleui.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/consoleui.cpp Sat Feb  3 08:57:13 2007
@@ -0,0 +1,281 @@
+/** \file consoleui.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.
+
+-----------------------------------------------------------------------------
+
+  Options used to be of the form "-opt value" or "-switch"
+  APR follows standard convention of short and long options, e.g.
+        "-c -x"   "-cx"   "--opt value"   "--opt=value"   "--switch"
+
+-------------------------------------------------------------------------- */
+
+#ifndef __UIMA_CONSOLEUI_CPP
+#define __UIMA_CONSOLEUI_CPP
+
+/* ----------------------------------------------------------------------- */
+/*       Include dependencies                                              */
+/* ----------------------------------------------------------------------- */
+
+#include <vector>
+#include "uima/pragmas.hpp" //must be first include to surpress warnings
+#include "uima/consoleui.hpp"
+#include "apr_strings.h"
+#include "apr_version.h"
+
+/* ----------------------------------------------------------------------- */
+/*       Constants                                                         */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Types / Classes                                                   */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+  namespace util {
+
+    /* ----------------------------------------------------------------------- */
+    /*       Implementation                                                    */
+    /* ----------------------------------------------------------------------- */
+
+// -----------------------------------------------------------------------
+//  Conctructor & Destructor
+// -----------------------------------------------------------------------
+    ConsoleUI::ConsoleUI(int argc, char * argv[],
+                         const char * cpszTitle, const char * cpszCopyright)
+        : iv_bQuiet(false),
+        iv_cpszHelp(NULL),
+        iv_szProcessName(""),
+        iv_currentArg(99999) {
+      // Catch any APR errors here in case caller does not expect this constructor to fail
+      try {
+
+        apr_status_t rv = apr_initialize();
+        if (rv != APR_SUCCESS) {
+          char errBuf[256];
+          apr_strerror(rv, errBuf, sizeof(errBuf));
+          UIMA_EXC_THROW_NEW(AprFailureException,
+                             UIMA_ERR_APR_FAILURE,
+                             ErrorMessage(UIMA_MSG_ID_EXC_APR_ERROR,errBuf),
+                             ErrorMessage(UIMA_MSG_ID_EXCON_APR_FUNCTION,"apr_initialize"),
+                             ErrorInfo::unrecoverable);
+        }
+
+        rv = apr_pool_create(&consPool, NULL);
+        if (rv != APR_SUCCESS) {
+          UIMA_EXC_THROW_NEW(ExcOutOfMemory,
+                             UIMA_ERR_ENGINE_OUT_OF_MEMORY,
+                             UIMA_MSG_ID_EXC_OUT_OF_MEMORY,
+                             ErrorMessage(UIMA_MSG_ID_EXCON_CREATING_POOL_FOR_CLASS,"uima::util::ConsoleUI"),
+                             ErrorInfo::unrecoverable);
+        }
+
+        iv_cpszUsage[0] = iv_cpszUsage[1] = "";
+        iv_argc = argc;
+        iv_argv = (const char**) argv;
+        // Get program name from first arg
+        if ( cpszTitle != NULL ) {
+          util::Filename progname(argv[0]);
+          iv_szProcessName = (char *)apr_palloc(consPool, 1+strlen(progname.getName()));
+          progname.extractBaseName(iv_szProcessName);
+          cout << endl << iv_szProcessName << " - " << cpszTitle;
+          if ( cpszCopyright != NULL )
+            cout << "   " << cpszCopyright;
+          cout << "  [APR version " << apr_version_string() << "]" << endl;
+        }
+
+        // Log and rethrow and APR failures
+      } catch (Exception & rclException) {
+        cerr << rclException;
+        UIMA_EXC_RETHROW(rclException,
+                         ErrorMessage(UIMA_MSG_ID_EXCON_CONSTRUCTING_CLASS,"uima::util::ConsoleUI"));
+      }
+    }
+
+    /* ----------------------------------------------------------------------- */
+    ConsoleUI::~ConsoleUI(void) {
+      if (consPool != NULL)
+        apr_pool_destroy(consPool);
+      apr_terminate();
+    }
+
+// ------------------------------------------------------------------------
+// Save the help string and check that all options are legal
+// ------------------------------------------------------------------------
+
+    void ConsoleUI::handleUsageHelp(const char * cpszUsage, const char * cpszHelp, const char * cpszHelpFlags) {
+      std::vector<const char *>     opts;        // Vector of possible option (start points)
+      unsigned int                  i;
+      apr_status_t                  rv;
+      unsigned int                  nHelpFlags;
+
+      if (cpszHelpFlags == NULL)
+        cpszHelpFlags = "[--help | -?]\t\n";
+      iv_cpszUsage[0] = cpszHelpFlags;
+      iv_cpszUsage[1] = cpszUsage;
+      iv_cpszHelp     = cpszHelp;
+
+      // Step thru the usage msg to and create a vector addressing the start of each option
+      // Options can be switches or take a value, and be marked as optional (most options are
+      // are optional [!] but taftest_app requires one and only one of --config & --configs)
+
+      //      --opt      --opt <val>      --opt=<val>
+      //     [--opt]    [--opt <val>]    [--opt=<val>]  [-c] [-f foo] [-x | -y]
+
+      // Assume a long option starts with -- preceeded by [ or whitespace
+      // Assume a short option starts with - preceeded by [ or space and is followed by ] or space
+
+      // First process help flags then the remainder of the usage message.
+      for ( i = 0; i < 2; ++i ) {
+        const char * str = iv_cpszUsage[i];
+        if ( *str == '-' && *++str == '-' )           // Check for initial -- where str[-1] would be bad!
+          opts.push_back(str);
+        while ((str = strchr(str+1, '-')) != NULL) {
+          if ( ( str[1]=='-' && (str[-1]=='[' || isspace(str[-1])) ) ||
+               ( str[1]!='-' && isgraph(str[1]) && (str[-1]=='[' || str[-1]==' ') && (str[2]==']' || str[2]==' ') ) )
+            opts.push_back(++str);
+        }
+        if (i == 0)
+          nHelpFlags = opts.size();
+      }
+
+      iv_pOpts = (apr_getopt_option_t *)apr_palloc( consPool, (opts.size()+1) * sizeof(apr_getopt_option_t) );
+
+      // Process short and long options in two passes as must use a wierd value for optch to
+      // locate long options, so can have no more than 47 long options (1 - '0')
+
+      int j = 0;
+      const char * opt, * str;
+      int k = 0;
+      char helpChars[20];
+
+      for ( i = 0; i < opts.size(); ++i ) {
+        opt = opts[i];
+        if ( *opt == '-' ) {                        // Long option
+          str = strpbrk( ++opt, "]= \n" );
+          if (str == NULL)                          // Must be last option!
+            str = opt + strlen(opt);
+          iv_pOpts[j].name = apr_pstrndup(consPool, opt, str - opt);
+          iv_pOpts[j].has_arg = (*str == '=' || (*str == ' ' && *(str+1) == '<'));
+          iv_pOpts[j].optch = j+1;                  // Use 1-based index as "short" option
+          if (i < nHelpFlags)
+            helpChars[k++] = j+1;
+          ++j;
+        }
+      }
+      iv_maxOptIndex = j;                           // Largest wierd value marking a long option
+      // NOTE: should check that this is < any alphanumeric char, i.e. < 48 ('0')
+
+      for ( i = 0; i < opts.size(); ++i ) {
+        opt = opts[i];
+        if ( *opt != '-' ) {                        // Short option
+          iv_pOpts[j].optch = *opt;                 // Use real character
+          iv_pOpts[j].name = NULL;
+          iv_pOpts[j++].has_arg = (opt[1] == ' ' && opt[2] != '|' );
+          if (i < nHelpFlags)
+            helpChars[k++] = *opt;
+        }
+      }
+      iv_pOpts[j].optch = 0;                        // Mark end of list
+      helpChars[k] = '\0';
+
+#ifndef NDEBUG
+      // Dump all legal options when called with just 1 arg of "--"
+      if ( iv_argc == 2 && strcmp(iv_argv[1],"--")==0 )
+        debugDisplayOptions(j);
+#endif
+
+      // Set up processing of interleaved options and arguments
+      apr_getopt_init(&iv_hGetopt, consPool, iv_argc, iv_argv);
+      iv_hGetopt->interleave = 1;
+
+      // Can't have more options than args!
+      iv_pFoundOpts = (found_opt_t *)apr_palloc(consPool, iv_argc * sizeof(found_opt_t));
+      iv_nFoundOpts = 0;
+
+      // Check that provided options are valid and save them.
+      int          index;
+      const char * optarg;
+      bool         needHelp = false;
+
+      while ((rv = apr_getopt_long(iv_hGetopt, iv_pOpts, &index, &optarg)) == APR_SUCCESS) {
+        iv_pFoundOpts[iv_nFoundOpts].index = index;
+        iv_pFoundOpts[iv_nFoundOpts++].value = optarg;
+        if (strchr(helpChars,index) != NULL)
+          needHelp = true;
+      }
+
+      // Two errors are:  Invalid option & missing argument
+      // getopt prints these on error streamwith fprintf(stderr but can override
+      // via iv_hGetopt->errfn        typedef void( apr_getopt_err_fn_t)(void *arg, const char *err,...)
+
+      if ( rv != APR_EOF )
+        displayUsage();
+
+      // Args are valid ... now check for the help flags
+      else
+        if (needHelp)
+          displayHelp();
+    }
+
+// ------------------------------------------------------------------------
+// Search for a specified option and return its value
+// ------------------------------------------------------------------------
+
+    bool ConsoleUI::hasOption(const char * cpszArgument, const char *& cpszrValue) const {
+      for ( int i = 0; i < iv_nFoundOpts; ++i ) {
+        int index = iv_pFoundOpts[i].index;               // Index if a long option or a single char
+        if ( index > iv_maxOptIndex ) {
+          if ( cpszArgument[0] == index && cpszArgument[1] == '\0' ) {
+            cpszrValue = iv_pFoundOpts[i].value;
+            return true;
+          }
+        } else
+          if ( strcmp(cpszArgument, iv_pOpts[index-1].name) == 0 ) {
+            cpszrValue = iv_pFoundOpts[i].value;
+            return true;
+          }
+      }
+      return false;
+    }
+
+// ------------------------------------------------------------------------
+//  Routine to help debug problems with parsing Usage msg
+// ------------------------------------------------------------------------
+
+#ifndef NDEBUG
+    void ConsoleUI::debugDisplayOptions(int numOpts) {
+      int i;
+      const char * valMsg[] = { "\n", " <value>\n"
+                              };
+
+      cout << "DEBUG: Found " << numOpts << " options in Help message" << endl;
+      for ( i = 0; i < iv_maxOptIndex; ++i )
+        cout << "  --" << iv_pOpts[i].name << valMsg[iv_pOpts[i].has_arg];
+      for ( i = iv_maxOptIndex; i < numOpts; ++i )
+        cout << "   -" << (char)iv_pOpts[i].optch << valMsg[iv_pOpts[i].has_arg];
+    }
+#endif
+
+  }  // namespace util
+}  // namespace uima
+
+#endif /* __UIMA_CONSOLEUI_CPP */
+
+/* <EOF> */

Propchange: incubator/uima/uimacpp/trunk/src/framework/consoleui.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/uima/uimacpp/trunk/src/framework/cp2ucnvrt.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/cp2ucnvrt.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/cp2ucnvrt.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/cp2ucnvrt.cpp Sat Feb  3 08:57:13 2007
@@ -0,0 +1,157 @@
+/** @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
+   8/18/1999   CodePage2UTF8Converter added
+
+-------------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Include dependencies                                              */
+/* ----------------------------------------------------------------------- */
+
+#include "uima/cp2ucnvrt.hpp"
+
+#include "uima/assertmsg.h"
+#include "uima/msg.h"
+
+/* ----------------------------------------------------------------------- */
+/*       Constants                                                         */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Forward declarations                                              */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Types / Classes                                                   */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Private Implementation                                            */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Implementation                                                    */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+  CodePage2UnicodeConverter::CodePage2UnicodeConverter( 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 CodePage2UnicodeConverter::convertBytes(UChar * pclTarget,
+      size_t uiTargetMaxSize,
+      const char * cpacSource,
+      size_t uiSourceSize)
+  /* ----------------------------------------------------------------------- */
+
+  {
+    size_t                     uiTargetSize;
+    uiTargetSize = convertToUnicode(pclTarget, uiTargetMaxSize, cpacSource, uiSourceSize);
+    return(uiSourceSize);
+  }
+
+
+
+  size_t CodePage2UnicodeConverter::getMaximumLength(const char * cpacSource,
+      size_t uiSourceLength) const
+  /* ----------------------------------------------------------------------- */
+  {
+    size_t                     uiTargetSize;
+
+    assert(iv_uconverter !=NULL);
+    UErrorCode err=(UErrorCode)0;
+    uiTargetSize = ucnv_toUChars(iv_uconverter, NULL, 0, cpacSource, uiSourceLength, &err);
+
+    if (!U_SUCCESS(err) && err != U_BUFFER_OVERFLOW_ERROR) {
+      cerr << "CodePage2UnicodeConverter::getMaximumLength() rc= " << 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);
+    }
+
+    ////return(uiTargetSize / sizeof(UChar));       /* as characters */
+    return (uiTargetSize);
+  }
+
+
+
+
+  size_t CodePage2UnicodeConverter::convertToUnicode(UChar * pclTarget,
+      size_t uiTargetMaxLength,
+      const char * cpacSource,
+      size_t uiSourceLength)
+  /* ----------------------------------------------------------------------- */
+  {
+    size_t                     uiTargetSize;
+
+    assert(iv_uconverter !=NULL);
+    UErrorCode err=(UErrorCode)0;
+    uiTargetSize = ucnv_toUChars(iv_uconverter, pclTarget, uiTargetMaxLength, cpacSource, uiSourceLength, &err);
+
+    if (!U_SUCCESS(err) &&  err != U_BUFFER_OVERFLOW_ERROR) {
+      cout << "ERROR: convertToUnicode " << err << endl;
+      ///cerr << "CodePage2UnicodeConverter::getMaximumLength() rc= " << 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);
+    }
+    return uiTargetSize;
+    //// return(uiTargetSize / sizeof(UChar));       /* as characters */
+  }
+
+}

Propchange: incubator/uima/uimacpp/trunk/src/framework/cp2ucnvrt.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/uima/uimacpp/trunk/src/framework/dottypesystemwriter.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/dottypesystemwriter.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/dottypesystemwriter.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/dottypesystemwriter.cpp Sat Feb  3 08:57:13 2007
@@ -0,0 +1,182 @@
+/** \file dottypesystemwriter.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:
+
+-----------------------------------------------------------------------------
+
+
+   09/20/2002  Initial creation
+
+-------------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+/*       Include dependencies                                              */
+/* ----------------------------------------------------------------------- */
+#include "uima/pragmas.hpp"
+#include "uima/dottypesystemwriter.hpp"
+#include "uima/timedatetools.hpp"
+#include "unicode/ustring.h"
+#include "uima/unistrref.hpp"
+#include "uima/typesystem.hpp"
+#include "uima/cas.hpp"
+#include <time.h>
+#ifdef _MSC_VER
+#include <minmax.h> // for min
+#endif
+
+/* ----------------------------------------------------------------------- */
+/*       Constants                                                         */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Forward declarations                                              */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Types / Classes                                                   */
+/* ----------------------------------------------------------------------- */
+
+using namespace std;
+
+namespace uima {
+
+  const size_t cuiINDENT = 3;
+
+  std::string
+  currDateString() {
+    time_t ltime;
+    (void)time(&ltime);   //get time, ignore return value
+    const size_t uiMAXSTR = 64;
+    char pszDate[uiMAXSTR];
+    strftime(pszDate, uiMAXSTR, "%x", localtime(&ltime));
+    string t(pszDate);
+    return t; //time string
+  }
+
+  UnicodeStringRef adjustTypeName(UnicodeStringRef ulstrName) {
+    UnicodeString ustrTypePfx( "uima" );                         // Our namespace
+    ustrTypePfx.append( (UChar) uima::TypeSystem::NAMESPACE_SEPARATOR );
+    if (u_strncmp(ulstrName.getBuffer(), ustrTypePfx.getBuffer(), min(ulstrName.length(), ustrTypePfx.length())) == 0) {
+      ulstrName = UnicodeStringRef(ulstrName.getBuffer() + ustrTypePfx.length(),
+                                   ulstrName.length()- ustrTypePfx.length());
+    }
+    return ulstrName;
+  }
+
+  UnicodeStringRef adjustFeatName(UnicodeStringRef ulstrName) {
+    return ulstrName;
+  }
+
+  DotTypeSystemWriter::DotTypeSystemWriter(CAS const & crCAS, bool bNoCASTypes, bool bOnlyCASTypes ) :
+      iv_crCAS(crCAS),
+      iv_bNoCASTypes(bNoCASTypes),
+      iv_bOnlyCASTypes(bOnlyCASTypes) {}
+
+  static inline bool isCasType(uima::Type const & crType) {
+    return    crType.getName().startsWith(icu::UnicodeString("uima.cas." ))
+              || crType.getName().startsWith(icu::UnicodeString("uima.tcas."));
+  }
+
+  void DotTypeSystemWriter::writeType(ostream & os, uima::Type const & crType, size_t uiIndent) const {
+    if (iv_bOnlyCASTypes &&
+        (crType != crType.getTypeSystem().getTopType()) && (!isCasType(crType))) {
+      return;
+    }
+    if (iv_bNoCASTypes && isCasType(crType)) {
+      return;
+    }
+    string strIndentString(uiIndent, ' ');
+    os << strIndentString;
+    os << adjustTypeName(crType.getName())
+    << "[label=\"" << adjustTypeName(crType.getName());
+    vector<Feature> features;
+    crType.getAppropriateFeatures(features);
+    size_t i;
+    for (i=0; i<features.size(); ++i) {
+      assert( features[i].isValid());
+      // print only features which are introduced at this type
+      Type introType;
+      features[i].getIntroType(introType);
+      assert( introType.isValid() );
+      if (introType == crType) {
+        Type range;
+        features[i].getRangeType(range);
+        assert( range.isValid());
+        os << "|{" << adjustFeatName(features[i].getName()) << ": " << adjustTypeName(range.getName()) << "}";
+      }
+    }
+    os << "\"];" << endl;
+    vector<Type> subTypes;
+    crType.getDirectSubTypes(subTypes);
+    if (subTypes.size() > 0) {
+      // print <typename> -> {<sub-type1>, <sub-type2>, ...};
+      os << strIndentString;
+      os << adjustTypeName(crType.getName()) << " -> {";
+      size_t uiTypesWritten = 0;
+      for (i=0; i<subTypes.size(); ++i) {
+        if (iv_bOnlyCASTypes && !isCasType(subTypes[i]) ) {
+          continue;
+        }
+        if (iv_bNoCASTypes && isCasType(subTypes[i]) ) {
+          continue;
+        }
+        if (uiTypesWritten > 0 ) {
+          os << "; ";
+        }
+        os << adjustTypeName(subTypes[i].getName());
+        ++uiTypesWritten;
+      }
+      os << "};" << endl;
+    }
+    for (i=0; i<subTypes.size(); ++i) {
+      writeType(os, subTypes[i], uiIndent+cuiINDENT);
+    }
+  }
+
+  void DotTypeSystemWriter::write(std::ostream & os) const {
+    os << "digraph G {\n"
+    "   graph [rankdir=LR];\n"
+    "   label=\"     Type\\n     Hierarchy\\n     " <<
+    currDateString() << "\";\n";
+    os << "   fontsize=24;\n"
+    "   fontname=Helvetica;\n"
+    "   color=white;\n"
+    "   node [shape=Mrecord, /*style=bold, */ fontsize=10, width=2.4, height=.28];\n";
+    Type top = iv_crCAS.getTypeSystem().getTopType();
+    writeType(os, top, cuiINDENT);
+    os << "}" << endl;
+  }
+
+} // namespace uima
+
+/* ----------------------------------------------------------------------- */
+/*       Implementation                                                    */
+/* ----------------------------------------------------------------------- */
+
+
+/* ----------------------------------------------------------------------- */
+
+
+

Propchange: incubator/uima/uimacpp/trunk/src/framework/dottypesystemwriter.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/uima/uimacpp/trunk/src/framework/engine.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/engine.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/engine.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/engine.cpp Sat Feb  3 08:57:13 2007
@@ -0,0 +1,595 @@
+/** \file 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                                              */
+/* ----------------------------------------------------------------------- */
+#define UIMA_ENGINE_MAIN_CPP
+
+#include "uima/pragmas.hpp" // must be first file to be included to get pragmas
+
+#include <memory>
+
+#include "uima/macros.h"
+
+#include "uima/engine.hpp"
+#include "uima/strconvert.hpp"
+#include "uima/internal_aggregate_engine.hpp"
+#include "uima/internal_primitive_engine.hpp"
+#include "uima/internal_jedii_engine.hpp"
+#include "uima/taespecifierbuilder.hpp"
+#include "uima/internal_casimpl.hpp"
+#include "uima/resmgr.hpp"
+#include "uima/msg.h"
+#include "uima/casdefinition.hpp"
+#include "xercesc/framework/LocalFileInputSource.hpp"
+#include "xercesc/framework/MemBufInputSource.hpp"
+XERCES_CPP_NAMESPACE_USE
+
+/* ----------------------------------------------------------------------- */
+/*       Constants                                                         */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Forward declarations                                              */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Types / Classes                                                   */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*       Implementation                                                    */
+/* ----------------------------------------------------------------------- */
+
+namespace uima {
+
+  UIMA_EXC_CLASSIMPLEMENT(UnknownTypeException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(UnknownFeatureException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(UnknownRangeTypeException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(IncompatibleRangeTypeException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(AllowedStringValuesIncompatibleException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(TypePriorityConflictException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(IncompatibleIndexDefinitionsException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(IncompatibleParentTypesException, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(CASIncompatibilityException, Exception);
+
+  
+  AnalysisEngineMetaData const & AnalysisEngine::getAnalysisEngineMetaData() const {
+    assert( EXISTS(getAnnotatorContext().getTaeSpecifier().getAnalysisEngineMetaData()) );
+    return *getAnnotatorContext().getTaeSpecifier().getAnalysisEngineMetaData();
+  }
+
+  TyErrorId AnalysisEngine::process(CAS & tcas) {
+    return ((AnalysisEngine*) this)->process(tcas);
+  }
+
+  TyErrorId AnalysisEngine::process(CAS & tcas, ResultSpecification const & resultSpec) {
+    return ((AnalysisEngine*) this)->process(tcas, resultSpec);
+  }
+
+  CASIterator  AnalysisEngine::processAndOutputNewCASes(CAS & tcas) {
+    TyErrorId rc = ((AnalysisEngine*) this)->process(tcas);
+    if (rc != UIMA_ERR_NONE) {
+      UIMA_EXC_THROW_NEW(CASIteratorException,
+                         UIMA_ERR_ENGINE_INVALID_CALLING_SEQUENCE,
+                         UIMA_MSG_ID_EXCON_CALLING_ANNOTATOR_FUNCTION,
+                         UIMA_MSG_ID_EXCON_CALLING_ANNOTATOR_FUNCTION,
+                         ErrorInfo::unrecoverable);
+    }
+    return CASIterator(this);
+  }
+
+  /* static */ const char * AnalysisEngine::getVersionInfo(void)
+  /* ----------------------------------------------------------------------- */
+  {
+    return(UIMA_STRINGIFY(UIMA_VERSION));
+  }
+
+  /* static */ const char * AnalysisEngine::getLevelInfo(void)
+  /* ----------------------------------------------------------------------- */
+  {
+    return(TextAnalysisEngine::getVersionInfo());
+  }
+
+  /* static */ const char * AnalysisEngine::getErrorIdAsCString(TyErrorId utErrorId)
+  /* ----------------------------------------------------------------------- */
+  {
+    const StErrorId2StringMapping * cpstErrorId2StringMapping = gs_astErrorId2StringMapping;
+    size_t u;
+
+    /* originally, we had a real big switch/case statement but this caused an
+       "internal error" with the SUN compiler.
+       now we perform a linear search - this method is not considered time critical */
+    for (u = 0; u < NUMBEROF(gs_astErrorId2StringMapping); u++) {
+      assert(EXISTS(cpstErrorId2StringMapping));
+      if (utErrorId == cpstErrorId2StringMapping->iv_utErrorId) {
+        assert(EXISTS(cpstErrorId2StringMapping->iv_cpszErrorId));
+        return(cpstErrorId2StringMapping->iv_cpszErrorId);
+      }
+      ++cpstErrorId2StringMapping;
+    }
+#ifdef NDEBUG
+    return(_TEXT("UIMA_ERR_UNKNOWN"));
+#else
+    string *            pstrRetVal = new string(_TEXT("??? UNKNOWN UIMACPP ERROR ID:")); /* this memory gets wasted - but this is only NDEBUG code!!! */
+    string              strValue;
+
+    long2String((long) utErrorId, strValue);
+    assert(EXISTS(pstrRetVal));
+    *pstrRetVal += strValue;
+    *pstrRetVal += _TEXT(" - Please update file '" __FILE__ "' ???");
+    /* this memory gets wasted - but this is only NDEBUG code!!! */
+    return(pstrRetVal->c_str());  //lint !e429: Custodial pointer 'pstrRetVal' (line 596) has not been freed or returned
+#endif
+  }
+
+  /* static */ void AnalysisEngine::printErrorIdTable(ostream & rclOutStream)
+  /* ----------------------------------------------------------------------- */
+  {
+    const StErrorId2StringMapping * cpstErrorId2StringMapping = gs_astErrorId2StringMapping;
+    size_t                     u;
+
+    /* originally, we had a real big switch/case statement but this caused an
+       "internal error" with the SUN compiler.
+       now we perform a linear search - this method is not considered time critical */
+    for (u = 0; u < NUMBEROF(gs_astErrorId2StringMapping); u++) {
+      assert(EXISTS(cpstErrorId2StringMapping));
+      assert(EXISTS(cpstErrorId2StringMapping->iv_cpszErrorId));
+      rclOutStream << cpstErrorId2StringMapping->iv_utErrorId
+      << " = "
+      << cpstErrorId2StringMapping->iv_cpszErrorId
+      << "\n";
+      ++cpstErrorId2StringMapping;
+    }
+  }
+
+
+  /**  
+   * All variants of creating a TAE will eventually be mapped to this call.
+   * Depending on the context, memory ownership over the different objects passed to this 
+   * method may vary, thus the boolean parameters.
+   * Toplevel TAEs alwasy own the ANC and the CAS Definition but if they are created
+   * with an external TAESpec (see API calls for createTextAnalysisEngine()), they don't 
+   * own this object. On the other hand, delegate AEs never own neither the ANC, the TAESpec
+   * or the CASDefinition. 
+   */
+  /*static*/ AnalysisEngine * Framework::createAnalysisEngine(AnnotatorContext & rANC,
+      bool bOwnsANC,
+      bool bOwnsTAESpecifier,
+      uima::internal::CASDefinition & casDefinition,
+      bool ownsCASDefintion,
+      ErrorInfo & rErrorInfo) {
+    AnalysisEngine * pResult = NULL;
+    assert( rErrorInfo.getErrorId() == UIMA_ERR_NONE );
+    try {
+      if (!bOwnsANC) {
+        assert( ! bOwnsTAESpecifier );
+      }
+
+      // create the engine depending on the framework (UIMACPP or JEDII) or if it is primitive or aggregate.
+      AnalysisEngineDescription const & crTAESpecifier = rANC.getTaeSpecifier();
+      if (crTAESpecifier.getFrameworkImplName() == AnalysisEngineDescription::JAVA) {
+        pResult = new uima::internal::JEDIIEngine( rANC, bOwnsANC, bOwnsTAESpecifier, casDefinition, ownsCASDefintion );
+      } else {
+        if (crTAESpecifier.isPrimitive()) {
+          pResult = new uima::internal::PrimitiveEngine( rANC, bOwnsANC, bOwnsTAESpecifier, casDefinition, ownsCASDefintion );
+        } else {
+          pResult = new uima::internal::AggregateEngine( rANC, bOwnsANC, bOwnsTAESpecifier, casDefinition, ownsCASDefintion );
+        }
+      }
+
+      if (pResult == NULL) {
+        rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+        return NULL;
+      }
+
+      assert( EXISTS(pResult) );
+      TyErrorId utErrorID = pResult->initialize( crTAESpecifier );
+      if (utErrorID != UIMA_ERR_NONE) {
+        ErrorInfo const & crLastError = pResult->getAnnotatorContext().getLogger().getLastError();
+        if (crLastError.getErrorId() != UIMA_ERR_NONE) {
+          rErrorInfo = crLastError;
+        }
+        // overwrite the error ID
+        rErrorInfo.setErrorId( utErrorID );
+        delete pResult;
+        return NULL;
+      }
+
+      assert( EXISTS(pResult) );
+
+      return  pResult;
+    } catch (Exception & rExc) {
+      rErrorInfo = rExc.getErrorInfo();
+    }
+    assert( rErrorInfo.getErrorId() != UIMA_ERR_NONE );
+    if (pResult != NULL) {
+      delete pResult;
+    }
+    return NULL;
+  }
+
+
+  TextAnalysisEngine * TextAnalysisEngine::createTAE(bool bIsFilename,
+      icu::UnicodeString const & crString,
+      ErrorInfo & rErrorInfo) {
+		  return (TextAnalysisEngine*) Framework::createAnalysisEngine(bIsFilename,
+			  crString, rErrorInfo);
+	  }
+
+    /*static*/
+  /**   
+   * create a TAE from a file name or an XML buffer (depending on first argument).
+   */
+  AnalysisEngine * Framework::createAnalysisEngine(bool bIsFilename,
+      icu::UnicodeString const & crString,
+      ErrorInfo & rErrorInfo) {
+    try {
+      rErrorInfo.reset();
+      if (! ResourceManager::hasInstance()) {
+        rErrorInfo.setErrorId(UIMA_ERR_ENGINE_RESMGR_NOT_INITIALIZED);
+        return NULL;
+      }
+
+      /*
+      We use quite a lot of auto_ptrs here because many of those methods can go wrong
+      and throw exceptions but we still want to clean up all the memory we used thus far.
+      */
+      
+	  XMLParser builder;
+      auto_ptr<AnalysisEngineDescription> apTAESpecifier( new AnalysisEngineDescription() );
+      if (apTAESpecifier.get() == NULL) {
+        rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+        return NULL;
+      }
+
+      if (bIsFilename) {
+        builder.parseAnalysisEngineDescription(* apTAESpecifier.get(), crString);
+      } else {
+        /*
+           Some XML4C routines which have to do wiht parsing an in-memory 
+           buffer don't work, so this is why we don't call
+             builder.buildTaeFromMemory(* apTAESpecifier.get(), crString);
+           here but rather create a temp file.
+        */
+        // Use what appears to be a thread-safe routine on Linux & Windows
+        char* tmpFileName = tempnam(NULL, "TMP");
+
+        ofstream ofs(tmpFileName);
+        uima::operator<<(ofs, crString);
+        ofs << endl;
+        ofs.close();
+        UIMA_TPRINT("Wrote descriptor to temp file:");
+        UIMA_TPRINT(tmpFileName);
+        builder.parseAnalysisEngineDescription(* apTAESpecifier.get(), tmpFileName);
+
+        remove(tmpFileName);                // or unlink ??
+        free(tmpFileName);                  // Free storage allocated by tempnam
+      }
+
+      apTAESpecifier->validate();
+      apTAESpecifier->commit();
+
+      auto_ptr<AnnotatorContext> apANC( new AnnotatorContext(apTAESpecifier.get()) );
+      if (apANC.get() == NULL) {
+        rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+        return NULL;
+      }
+
+      assert( EXISTS(apANC.get()) );
+
+      auto_ptr<uima::internal::CASDefinition> apCASDef( uima::internal::CASDefinition::createCASDefinition(*apANC.get()) );
+      // release auto_ptrs here because the createTAE transfers ownership to the engine
+      //  Warning: this could cause a memory leak if the createTAE() method coudl not create
+      //           the actual engine object.     suhre 02/11/03
+      apTAESpecifier.release();
+
+      AnalysisEngine * pResult = createAnalysisEngine(* apANC.release(), true,
+                                     true,
+                                     * apCASDef.release(), true,
+                                     rErrorInfo);
+      return pResult;
+    } catch (Exception & rExc) {
+      rErrorInfo = rExc.getErrorInfo();
+    }
+    assert( rErrorInfo.getErrorId() != UIMA_ERR_NONE );
+    return NULL;
+  }
+
+
+     /*static*/
+  TextAnalysisEngine * TextAnalysisEngine::createTextAnalysisEngine(char const * cpFileName, ErrorInfo & rErrorInfo) {
+    icu::UnicodeString usFileName( cpFileName );
+	return (TextAnalysisEngine*)Framework::createAnalysisEngine(cpFileName, rErrorInfo);
+  }
+    /*static*/
+  AnalysisEngine * Framework::createAnalysisEngine(char const * cpFileName, ErrorInfo & rErrorInfo) {
+    icu::UnicodeString usFileName( cpFileName );
+    return createAnalysisEngine(true, usFileName, rErrorInfo);
+  }
+
+
+    /*static*/
+  TextAnalysisEngine * TextAnalysisEngine::createTextAnalysisEngine(UChar const * cpBuffer, size_t uiLength, ErrorInfo & rErrorInfo) {
+    
+	  return (TextAnalysisEngine*)Framework::createAnalysisEngine(cpBuffer,uiLength, rErrorInfo);
+  }
+
+    /*static*/
+  AnalysisEngine * Framework::createAnalysisEngine(UChar const * cpBuffer, 
+				size_t uiLength, ErrorInfo & rErrorInfo) {
+    // read-only constructor of unicode string
+    icu::UnicodeString usXMLBuffer(false, cpBuffer, uiLength);
+    return createAnalysisEngine(false, usXMLBuffer, rErrorInfo);
+  }
+
+    /*static*/
+  TextAnalysisEngine * TextAnalysisEngine::createTextAnalysisEngine(AnalysisEngineDescription & crAEDesc, 
+									ErrorInfo & rErrorInfo) {
+		return (TextAnalysisEngine*) Framework::createAnalysisEngine(crAEDesc, rErrorInfo);
+  }
+
+    /*static*/
+  AnalysisEngine * Framework::createAnalysisEngine(AnalysisEngineDescription & crTAESpec, ErrorInfo & rErrorInfo) {
+    try {
+      rErrorInfo.reset();
+      if (! ResourceManager::hasInstance()) {
+        rErrorInfo.setErrorId(UIMA_ERR_ENGINE_RESMGR_NOT_INITIALIZED);
+        return NULL;
+      }
+
+      auto_ptr<AnnotatorContext> apANC( new AnnotatorContext(& crTAESpec) );
+      if (apANC.get() == NULL) {
+        rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+        return NULL;
+      }
+
+      assert( EXISTS(apANC.get()) );
+
+      auto_ptr<uima::internal::CASDefinition> apCASDef( uima::internal::CASDefinition::createCASDefinition(*apANC.get()) );
+      // release auto_ptrs here because the createTAE transfers ownership to the engine
+      //  Warning: this could cause a memory leak if the createTAE() method coudl not create (construct)
+      //           the actual engine object.     suhre 02/11/03
+      AnalysisEngine * pResult = createAnalysisEngine(*apANC.release(), true,
+                                     false,
+                                     *apCASDef.release(), true,
+                                     rErrorInfo);
+      return pResult;
+    } catch (Exception & rExc) {
+      rErrorInfo = rExc.getErrorInfo();
+    }
+    return NULL;
+  }
+
+
+
+
+  /* ------------------------------------------------
+   *  uima::Framework methods
+   * ----------------------------------------------- */
+  //create typesystem and set type priorities
+  TypeSystem * Framework::createTypeSystem(AnalysisEngineMetaData const & ae, ErrorInfo& rErrorInfo) {
+    rErrorInfo.reset();
+    if (! ResourceManager::hasInstance()) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_RESMGR_NOT_INITIALIZED);
+      return NULL;
+    }
+
+    uima::TypeSystem * pTs =  uima::internal::CASDefinition::createTypeSystem(ae);
+
+    return pTs;
+  }
+
+  //create typesystem from description
+  TypeSystem * Framework::createTypeSystem(TypeSystemDescription const & tsDesc,
+      icu::UnicodeString const & creatorID,
+      ErrorInfo& rErrorInfo) {
+    rErrorInfo.reset();
+    if (! ResourceManager::hasInstance()) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_RESMGR_NOT_INITIALIZED);
+      return NULL;
+    }
+
+    uima::TypeSystem * pTs =  uima::internal::CASDefinition::createTypeSystem(tsDesc,creatorID);
+
+    return pTs;
+  }
+
+  //create typesystem from description and set type priorities
+  TypeSystem * Framework::createTypeSystem(TypeSystemDescription const & tsDesc,
+      uima::AnalysisEngineMetaData::TyVecpTypePriorities const & typePriorities,
+      icu::UnicodeString const & creatorID,
+      ErrorInfo& rErrorInfo) {
+    rErrorInfo.reset();
+    if (! ResourceManager::hasInstance()) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_RESMGR_NOT_INITIALIZED);
+      return NULL;
+    }
+
+    uima::TypeSystem * pTs =  uima::internal::CASDefinition::createTypeSystem(tsDesc,typePriorities,creatorID);
+
+    return pTs;
+  }
+
+
+  //read in descriptor from and create TypeSystem per the specification
+  TypeSystem * Framework::createTypeSystem(char const * crFileName, ErrorInfo& rErrorInfo) {
+
+    XMLParser builder;
+    AnalysisEngineMetaData * pAe = new AnalysisEngineMetaData();
+	TypeSystemDescription * tsDesc = new TypeSystemDescription();
+	LocalFileInputSource fileIS((XMLCh const *)crFileName);
+    pAe->setTypeSystemDescription(tsDesc);
+
+    auto_ptr<AnalysisEngineMetaData> apSpecifier(pAe  );
+    if (apSpecifier.get() == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+
+    uima::TypeSystem * pTs =  uima::internal::CASDefinition::createTypeSystem(*apSpecifier.get());
+    return pTs;
+  }
+
+
+  //read in xml descriptor from buffer and create typesystem per those specifications
+  TypeSystem * Framework::createTypeSystem(UChar const * cpBuffer,
+      size_t uiLength,
+      ErrorInfo& rErrorInfo) {
+    UnicodeStringRef usRef(cpBuffer, uiLength);
+    XMLParser builder;
+    AnalysisEngineMetaData * pAe = new AnalysisEngineMetaData();
+	TypeSystemDescription * tsDesc = new TypeSystemDescription();
+    MemBufInputSource memIS((XMLByte const *)usRef.asUTF8().c_str(),
+		                    usRef.asUTF8().length(),
+							"sysID");
+    pAe->setTypeSystemDescription(tsDesc);
+
+    auto_ptr<AnalysisEngineMetaData> apSpecifier(pAe  );
+    if (apSpecifier.get() == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+
+    uima::TypeSystem * pTs =  uima::internal::CASDefinition::createTypeSystem(*apSpecifier.get());
+    return pTs;
+
+  }
+
+  //read in xml descriptor from buffer and create typesystem per those specifications
+  TypeSystem * Framework::createTypeSystemFromXMLBuffer( char const * cpBuffer,
+      ErrorInfo& rErrorInfo) {
+    XMLParser builder;
+    AnalysisEngineMetaData * pAe = new AnalysisEngineMetaData();
+	MemBufInputSource memIS((XMLByte const *) cpBuffer, strlen(cpBuffer), "sysID");
+	TypeSystemDescription * tsDesc = new TypeSystemDescription();
+	builder.parseTypeSystemDescription(*tsDesc, memIS);
+    pAe->setTypeSystemDescription(tsDesc);
+
+    auto_ptr<AnalysisEngineMetaData> apSpecifier(pAe  );
+    if (apSpecifier.get() == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+
+    uima::TypeSystem * pTs =  uima::internal::CASDefinition::createTypeSystem(*apSpecifier.get());
+    return pTs;
+
+  }
+
+
+
+  //create CAS with specified typesystem and only built in indices
+  CAS * Framework::createCAS(TypeSystem & typesystem, ErrorInfo& rErrorInfo) {
+    auto_ptr<uima::internal::CASDefinition> apCASDef( uima::internal::CASDefinition::createCASDefinition(typesystem) );
+    if (apCASDef.get() == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+
+    CAS * pCas = uima::internal::CASImpl::createCASImpl(*apCASDef.release(), true);
+
+    return pCas->getInitialView();
+  }
+
+
+
+  //create CAS with specified typesystem and indices/typePriorities defined in the AE descriptor
+  CAS * Framework::createCAS(TypeSystem & typesystem, AnalysisEngineMetaData & aeDesc, ErrorInfo & rErrorInfo) {
+    auto_ptr<uima::internal::CASDefinition> apCASDef( uima::internal::CASDefinition::createCASDefinition(typesystem, aeDesc) );
+    if (apCASDef.get() == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+    CAS * pCas = uima::internal::CASImpl::createCASImpl(*apCASDef.release(), true);
+    return pCas->getInitialView();
+  }
+
+
+  CAS * Framework::createCAS(TypeSystem & typesystem,
+                             AnalysisEngineMetaData::TyVecpFSIndexDescriptions & fsIndexDesc,
+                             AnalysisEngineMetaData::TyVecpTypePriorities  & prioDesc,
+                             ErrorInfo & rErrorInfo)  {
+    auto_ptr<uima::internal::CASDefinition>
+    apCASDef( uima::internal::CASDefinition::createCASDefinition(typesystem, fsIndexDesc, prioDesc) );
+    if (apCASDef.get() == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+    CAS * pCas = uima::internal::CASImpl::createCASImpl(*apCASDef.release(), true);
+    return pCas->getInitialView();
+
+
+  }
+
+  //create TypeSystemDescription object from xml spec in buffer
+  TypeSystemDescription * Framework::createTypeSystemDescription(UChar const * cpBuffer, size_t uiLength) {
+    UnicodeStringRef usRef(cpBuffer, uiLength);
+    XMLParser builder;
+    TypeSystemDescription * tsDesc = new TypeSystemDescription();
+    MemBufInputSource memIS((XMLByte const *) usRef.asUTF8().c_str(), 
+		                     usRef.asUTF8().length(), "sysID");
+	builder.parseTypeSystemDescription(*tsDesc,memIS);
+    return tsDesc;
+  }
+
+  //read in xml from a file and create a TypeSystemDescription object
+  TypeSystemDescription * Framework::createTypeSystemDescription(char const * crFileName) {
+    XMLParser builder;
+	LocalFileInputSource fileIS((XMLCh const *)crFileName);
+	TypeSystemDescription * tsDesc = new TypeSystemDescription();
+    builder.parseTypeSystemDescription(*tsDesc,fileIS);
+	return tsDesc;
+  }
+
+
+  CAS * Framework::createCAS(uima::internal::CASDefinition & casdef, ErrorInfo & rErrorInfo) {
+    CAS * pCas = uima::internal::CASImpl::createCASImpl(casdef, false);
+    if (pCas == NULL) {
+      rErrorInfo.setErrorId(UIMA_ERR_ENGINE_OUT_OF_MEMORY);
+      return NULL;
+    }
+    return pCas->getInitialView();
+  }
+
+
+
+
+
+
+}
+
+
+/* ----------------------------------------------------------------------- */
+
+
+

Propchange: incubator/uima/uimacpp/trunk/src/framework/engine.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/uima/uimacpp/trunk/src/framework/exceptions.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/exceptions.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/exceptions.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/exceptions.cpp Sat Feb  3 08:57:13 2007
@@ -0,0 +1,739 @@
+/*
+-------------------------------------------------------------------------------
+
+ * 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.
+
+-------------------------------------------------------------------------------
+
+
+-------------------------------------------------------------------------------
+*/
+
+#if defined(_MSC_VER)
+#include <stdio.h>
+#include <eh.h>
+#include <windows.h>
+#include <winbase.h>
+#endif
+
+#include "uima/pragmas.hpp" //must be included first to disable warnings
+#include "uima/msg.h"
+#include <string>
+#include <sstream>
+
+#include "uima/macros.h"
+#include "uima/trace.hpp"
+
+#include "uima/strconvert.hpp"
+#include "uima/unistrref.hpp"
+#include "uima/comp_ids.h"
+#include "uima/exceptions.hpp"
+#include "uima/msgstrtab.h"
+
+using namespace std;
+
+namespace uima {
+
+///Constructor with just the message id
+  ErrorMessage::ErrorMessage(
+    TyMessageId                          utMsgId
+  ) :
+      iv_utMsgId(utMsgId) {
+    if ( iv_utMsgId == 0) {
+      iv_utMsgId = UIMA_MSG_ID_NO_MESSAGE_AVAILABLE;
+    }
+  }
+///Constructor with a single char * parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId                          utMsgId,
+    const char *                            cpszParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    iv_vecParams.push_back((string)cpszParam1);
+  }
+
+///Constructor with a single string parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    const string &           crstrParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    iv_vecParams.push_back(crstrParam1);
+  }
+
+///Constructor with a single UChar * parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId                         utMsgId,
+    const UChar *                          cpuszParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    UnicodeStringRef(cpuszParam1).extract(s);         // Convert to default encoding for platform
+    iv_vecParams.push_back(s);
+  }
+
+///Constructor with a single UnicodeString parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId                         utMsgId,
+    const icu::UnicodeString &             crustrParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    UnicodeStringRef(crustrParam1).extract(s);         // Convert to default encoding for platform
+    iv_vecParams.push_back(s);
+  }
+
+///Constructor with a single int parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    int                      iParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    iv_vecParams.push_back(long2String(iParam1, s));
+  }
+
+///Constructor with a single unsigned int parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    unsigned int             uiParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    iv_vecParams.push_back(long2String((int) uiParam1, s));
+  }
+
+
+///Constructor with a single long parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    long                     lParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    iv_vecParams.push_back(long2String(lParam1, s));
+  }
+
+///Constructor with a single unsigned long parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    unsigned long            ulParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    iv_vecParams.push_back(long2String((long) ulParam1, s));
+  }
+
+///Constructor with a single double parameter
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    const double             dParam1
+  ) :
+      iv_utMsgId(utMsgId) {
+    assert( iv_utMsgId != 0 );
+    string s;
+    iv_vecParams.push_back(double2String(dParam1, s));
+  }
+
+///Constructor with a full parameter vector
+  ErrorMessage::ErrorMessage(
+    TyMessageId           utMsgId,
+    const vector<string> &   crvecParams
+  ) :
+      iv_utMsgId(utMsgId),
+      iv_vecParams(crvecParams) {
+    assert( iv_utMsgId != 0 );
+  }
+
+
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(const char * cpsz) {
+    iv_vecParams.push_back(cpsz);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(const string & str) {
+    iv_vecParams.push_back(str);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(const UChar * cpuszParam) {
+    string s;
+    UnicodeStringRef(cpuszParam).extract(s);         // Convert to default encoding for platform
+    iv_vecParams.push_back(s);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(const icu::UnicodeString & crustrParam) {
+    string s;
+    UnicodeStringRef(crustrParam).extract(s);         // Convert to default encoding for platform
+    iv_vecParams.push_back(s);
+  }
+
+  void ErrorMessage::addParam(uima::UnicodeStringRef const & crParam) {
+    icu::UnicodeString str(crParam.getBuffer(), crParam.length() );
+    addParam(str);
+  }
+
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(long l) {
+    string str;
+    convertToString(l, str);
+    iv_vecParams.push_back(str);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(unsigned long ul) {
+    string str;
+    convertToString(ul, str);
+    iv_vecParams.push_back(str);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(int i) {
+    string str;
+    convertToString(i, str);
+    iv_vecParams.push_back(str);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(unsigned int ui) {
+    string str;
+    convertToString(ui, str);
+    iv_vecParams.push_back(str);
+  }
+  ///add parameter to message id
+  void
+  ErrorMessage::addParam(double d) {
+    string str;
+    convertToString(d, str);
+    iv_vecParams.push_back(str);
+  }
+
+  /*------------------------------- Constructors -------------------------------*/
+
+  ErrorContext::ErrorContext(
+    const ErrorMessage &     crclMessage,
+    const char*                   pszFilename,
+    const char*                   pszFunction,
+    unsigned long                 ulLineNumber
+  ):
+      iv_clMessage(crclMessage),
+      iv_pszFilename(pszFilename),
+      iv_pszFunction(pszFunction),
+      iv_ulLineNo(ulLineNumber) {}
+
+#ifdef OS_STL
+  Exception::~Exception()
+#else
+  Exception::~Exception() UIMA_THROW0()
+#endif
+  {}
+
+  /*------------------------------ Output Support ------------------------------*/
+
+  string
+  ErrorMessage::asString() const {
+
+    size_t numparams = getMessageParams().size();
+
+    // Check for unknown message id
+    if (iv_utMsgId < 0 || iv_utMsgId > UIMA_MSG_ID_SIGNATURE_END) {
+      return string("Unknown message id " + iv_utMsgId);
+    }
+
+    //parameter substitution
+
+    //locate message in message table
+    const TCHAR ** messages = gs_aszMessageStringTable;
+    const TCHAR * msg = messages[iv_utMsgId];
+
+    TCHAR * buf = new TCHAR[UIMA_MSG_MAX_STR_LEN];
+    memset(buf,'\0',UIMA_MSG_MAX_STR_LEN);
+
+    int numwritten=0;
+
+    TCHAR * trg = buf;
+    while (*msg) {
+      if (numwritten > UIMA_MSG_MAX_STR_LEN) {
+        break;
+      }
+      if (*msg == UIMA_MSG_REPLACE_CHAR) {
+        msg = msg+1;
+        if (*msg == UIMA_MSG_REPLACE_CHAR) {
+          *trg = *msg;
+          trg = trg +1;
+          ;
+          msg = msg+1;
+          numwritten++;
+        } else {  //replace %n with the corresponding param
+          unsigned long index;
+          int len;
+          string arg;
+          // determine the number of the specified argument ...
+          index = (unsigned long) atol(msg);
+          if (index > numparams)       // param not defined
+          {
+            arg = "???";      // replace it by "dont-know"
+          } else {
+            // ... the indexed argument ...
+            arg = iv_vecParams[index - 1]; // zero-based array!
+            //assert(arg.length() > 0);
+          }
+          len = arg.length();
+          // ... and then copy the argument
+          if (UIMA_MSG_MAX_STR_LEN-numwritten < len) {
+            len = UIMA_MSG_MAX_STR_LEN - numwritten;
+          }
+          if (len > 0) {
+            strncpy(trg, arg.c_str(), len);
+            trg = trg+len;
+          }
+          msg = msg+1;
+          while ( isdigit(*msg) ) { //handle arg number 10 or more
+            msg = msg+1;
+          }
+        }
+      } else {
+        *trg = *msg;
+        trg = trg+1;
+        msg = msg+1;
+      }
+    }
+    //cout << buf << endl;
+    return string(buf);
+
+  }
+
+  ostream &
+  operator << (
+    ostream &            os,
+    const ErrorMessage & errorMsg
+  ) {
+    os << errorMsg.asString();
+    return(os);
+  }
+
+  string
+  ErrorContext::asString() const {
+    string s;
+    s += getMessage().asString();
+    if (getFileName() != NULL) {
+      s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + "     File     : " + getFileName();
+    }
+    if (getFunctionName() != NULL) {
+      s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + "     Function : " + getFunctionName();
+    }
+    if (getLineNumber() != 0) {
+      string sNum;
+      long2String(getLineNumber(), sNum);
+      s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + "     Line     : " + sNum;
+    }
+
+    return(s);
+  }
+
+  ostream &
+  operator << (
+    ostream            & os,
+    const ErrorContext & errContext
+  ) {
+    os << errContext.asString();
+    return (os);
+  }
+
+  void ErrorMessage::reset(void) {
+    iv_utMsgId = UIMA_MSG_ID_NO_MESSAGE_AVAILABLE;
+    iv_vecParams.clear();
+  }
+
+  /* ----------------------------------------------------------------------- */
+  /*   ErrorInfo                                                        */
+  /* ----------------------------------------------------------------------- */
+
+  /*------------------------------- Statics  -----------------------------------*/
+//initialize static member var to an initial value
+
+  const char * ErrorInfo::cv_cpszContextPrefix   = "   While      : ";
+  const char * ErrorInfo::cv_cpszIndent          = "";
+
+
+  /*------------------------------- Constructors -------------------------------*/
+
+  ErrorInfo::ErrorInfo(
+    const ErrorMessage     & rclMessage,
+    TyErrorId                ulErrorId,
+    ErrorInfo::EnSeverity    enSeverity
+  ) :
+      iv_clErrorMsg(rclMessage),
+      iv_ulErrorId(ulErrorId),
+      iv_enSeverity(enSeverity),
+      iv_vecContexts() {}
+
+  ErrorInfo::ErrorInfo( void ) :
+      iv_clErrorMsg(),
+      iv_ulErrorId(UIMA_ERR_NONE),
+      iv_enSeverity(recoverable),
+      iv_vecContexts() {}
+
+  ErrorInfo::~ErrorInfo() {}
+
+//constructor (copy)
+//? Exception::Exception(
+//?   const Exception&            rclException
+//? )
+//? {
+//? }
+
+  /*---------------------------- Exception Context ----------------------------*/
+  void
+  ErrorInfo::addContext( const ErrorContext & crclContext ) {
+    iv_vecContexts.push_back(crclContext);
+  }
+
+  const ErrorContext *
+  ErrorInfo::contextPtrAtIndex( size_t uiContextIndex ) const {
+    if (uiContextIndex >= iv_vecContexts.size()) {
+      return(NULL);
+    }
+    return(&iv_vecContexts[uiContextIndex]);
+  }
+
+  /*------------------------------ Output Support ------------------------------*/
+  string
+  ErrorInfo::asString() const {
+    string s;
+    if (getErrorId() == UIMA_ERR_NONE) {
+      s += string("No Error\n");
+      return (s);
+    }
+    if (getErrorId() != UIMA_ERR_NONE) {
+      s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + "Error number  : ";
+      string sErr;
+      long2String( getErrorId(), sErr);
+      s += sErr;
+    }
+    s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + "Recoverable   : " + (isRecoverable() ? "Yes" : "No");
+    s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + "Error         : " + getMessage().asString();
+    for (size_t i = 0; i < contextCount(); ++i) {
+      s += string("\n") + ErrorInfo::getGlobalErrorInfoIndent() + ErrorInfo::getGlobalErrorInfoContextPrefix();
+      assert(EXISTS(contextPtrAtIndex(i)));   //lint !e666: Expression with side effects passed to repeated parameter 1 in macro EXISTS
+      s += contextPtrAtIndex(i)->asString();
+    }
+    s += string("\n");
+
+    return (s);
+  }
+
+///output support for streams
+  ostream &
+  operator << (
+    ostream         & os,
+    const ErrorInfo & errInfo
+  ) {
+    os << errInfo.asString();
+    return(os);
+  }
+
+  void ErrorInfo::reset(void) {
+    iv_clErrorMsg.reset();
+    iv_ulErrorId = UIMA_ERR_NONE;
+    iv_enSeverity = recoverable;
+    iv_vecContexts.clear();
+  }
+
+  /* ----------------------------------------------------------------------- */
+  /*   Exception                                                        */
+  /* ----------------------------------------------------------------------- */
+
+  Exception::Exception(
+    const ErrorMessage    & rclMessage,
+    TyErrorId                 ulErrorId,
+    ErrorInfo::EnSeverity   enSeverity
+  ) :
+      EXCEPTION_BASE_CLASS(),
+      iv_clErrorInfo(rclMessage, ulErrorId, enSeverity) {}
+
+  Exception::Exception(
+    const ErrorInfo & crclErrorInfo
+  ) :
+      EXCEPTION_BASE_CLASS(),
+      iv_clErrorInfo(crclErrorInfo) {}
+
+  /*------------------------------ Exception Type ------------------------------*/
+
+  const char * Exception::getName() const {
+    return(_TEXT("unspecified exception"));
+  }
+
+  /*------------------------- Application Termination --------------------------*/
+  void Exception::terminate() {
+    exit(iv_clErrorInfo.getErrorId());  //lint !e713: Loss of precision (arg. no. 1) (unsigned long to int)
+  }
+
+  /*------------------------------ Throw Support -------------------------------*/
+  void Exception::assertParameter(
+    const char*       /*exceptionText*/,
+    ErrorContext   /*context*/ ) {
+    assert(false);
+    //not tested yet (and not used anywhere!)
+  }
+
+  /*------------------------- Logging Support ----------------------------------*/
+
+  void Exception::logExceptionData() {
+    util::Trace clTrace(util::enTraceDetailLow, UIMA_TRACE_ORIGIN, UIMA_TRACE_COMPID_EXCEPTIONS);
+
+    clTrace.dump(_TEXT("Exception occured"), asString().c_str());
+    //not implemented yet
+  }
+
+  /*------------------------------ Output Support ------------------------------*/
+  string
+  Exception::asString() const {
+    //output the (class) name of the exception and its error info after it.
+    return string("\n") +
+           ErrorInfo::getGlobalErrorInfoIndent() +
+           "Exception     : " +
+           getName() +
+           "\n" +
+           getErrorInfo().asString() +
+           "\n";
+  }
+
+
+
+  ostream &
+  operator << (
+    ostream            & os,
+    const Exception & exception
+  ) {
+    //output the (class) name of the exception and its error info after it.
+    os << exception.asString();
+    return(os);
+  }
+
+  /*------------------------------ Static Method -------------------------------*/
+
+  // Release contents of string container allocated by asString method
+
+  void
+  Exception::release(std::string & msg) {
+    msg.clear();               // Empty string
+    msg.reserve(1);            // Reduce capacity so will use internal buffer & free external one
+  }
+
+//private:
+  /*----------------------------- Hidden Functions -----------------------------*/
+  Exception& Exception::operator=( const Exception&   /*exc*/ ) {
+    assert(false);
+    return(*this);                                     //lint !e527  unreachable
+  }  //lint !e1745: member not assigned by private assignment operator
+
+  /* ----------------------------------------------------------------------- */
+  /*   Implementations of predefined exceptions                              */
+  /* ----------------------------------------------------------------------- */
+
+
+  /*
+    The following classes reimplement the ANSI standard exception hierarchy from
+    stdexcept.h as UIMACPP exceptions with context and message id support
+  */
+  UIMA_EXC_CLASSIMPLEMENT(Uima_logic_error        ,Exception);
+  UIMA_EXC_CLASSIMPLEMENT(Uima_runtime_error      ,Exception);
+
+  UIMA_EXC_CLASSIMPLEMENT(Uima_domain_error       ,Uima_logic_error);
+  UIMA_EXC_CLASSIMPLEMENT(Uima_invalid_argument   ,Uima_logic_error);
+  UIMA_EXC_CLASSIMPLEMENT(Uima_length_error       ,Uima_logic_error);
+  UIMA_EXC_CLASSIMPLEMENT(Uima_out_of_range       ,Uima_logic_error);
+
+  UIMA_EXC_CLASSIMPLEMENT(Uima_range_error        ,Uima_runtime_error);
+  UIMA_EXC_CLASSIMPLEMENT(Uima_overflow_error     ,Uima_runtime_error);
+  UIMA_EXC_CLASSIMPLEMENT(Uima_underflow_error    ,Uima_runtime_error);
+
+
+  /**
+    The following exceptions are used by helper test classes that are no longer in the UIMACPP library:
+          CommandLineDriver DocBuffer ParseHandlers
+  */
+  UIMA_EXC_CLASSIMPLEMENT(ConsoleAbortExc        ,Exception);
+  UIMA_EXC_CLASSIMPLEMENT(ParseHandlerExc        ,Exception);
+  UIMA_EXC_CLASSIMPLEMENT(ExcDocBuffer           ,Uima_out_of_range);
+
+  /** code page conversion errors */
+  UIMA_EXC_CLASSIMPLEMENT(CodePageConversionException, uima::Exception);
+  /**
+    The following exception is used to report failures from APR functions
+  */
+  UIMA_EXC_CLASSIMPLEMENT(AprFailureException, Exception);
+
+  /*
+    The following classes provide a starting point for an exception hierarchy
+  */
+//? UIMA_EXC_CLASSIMPLEMENT(ExcAssertionFailure, Exception);
+  UIMA_EXC_CLASSIMPLEMENT(ExcIllFormedInputError , Uima_runtime_error);
+  UIMA_EXC_CLASSIMPLEMENT(ExcInvalidParameter    , Uima_invalid_argument);
+  UIMA_EXC_CLASSIMPLEMENT(ExcIndexOutOfRange     , Uima_out_of_range);
+//? UIMA_EXC_CLASSIMPLEMENT(ExcDeviceError      ,Uima_runtime_error);
+  UIMA_EXC_CLASSIMPLEMENT(ExcInvalidRequest      , Uima_runtime_error);
+  UIMA_EXC_CLASSIMPLEMENT(ExcResourceExhausted   , Uima_runtime_error);
+  UIMA_EXC_CLASSIMPLEMENT(ExcOutOfMemory         , ExcResourceExhausted);
+//? UIMA_EXC_CLASSIMPLEMENT(ExcOutOfSystemResource, ResourceExhausted);
+//? UIMA_EXC_CLASSIMPLEMENT(ExcOutOfWindowResource, ResourceExhausted);
+  UIMA_EXC_CLASSIMPLEMENT(ExcFileNotFoundError   , Uima_runtime_error);
+
+// Windows specific CException
+  ExcWinCException::ExcWinCException(
+    ErrorMessage             clMessage,
+    TyErrorId                  ulErrorId,
+    ErrorInfo::EnSeverity    enSeverity
+  )
+      : Uima_runtime_error (clMessage, ulErrorId, enSeverity) {
+    ;
+  }
+
+  const char*
+  ExcWinCException :: getName() const {
+    return( "ExcWinCException" );
+  }
+
+  ExcWinCException::~ExcWinCException () CHILD_DESTRUCT_THROW0() {
+    ;
+  }
+
+  ExcWinCException::ExcWinCException (const ExcWinCException & a) : Uima_runtime_error (a) {
+    ;
+  }
+
+
+  // Windows exceptions can be mapped only when compiled with MS VC++
+#if defined(_MSC_VER)
+
+  void translation_func( unsigned int u, _EXCEPTION_POINTERS* pExp ) {
+    const char * cpszMsg = NULL;
+    switch (u) {
+    case EXCEPTION_ACCESS_VIOLATION:
+      cpszMsg ="\"ACCESS VIOLATION\"";
+      break;
+    case EXCEPTION_DATATYPE_MISALIGNMENT:
+      cpszMsg ="\"DATATYPE MISALIGNMENT\"";
+      break;
+    case EXCEPTION_BREAKPOINT:
+      cpszMsg ="\"BREAKPOINT\"";
+      break;
+    case EXCEPTION_SINGLE_STEP:
+      cpszMsg ="\"SINGLE STEP\"";
+      break;
+    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
+      cpszMsg ="\"ARRAY BOUNDS EXCEEDED\"";
+      break;
+    case EXCEPTION_FLT_DENORMAL_OPERAND:
+      cpszMsg ="\"FLT DENORMAL OPERAND\"";
+      break;
+    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
+      cpszMsg ="\"FLT DIVIDE_BY ZERO\"";
+      break;
+    case EXCEPTION_FLT_INEXACT_RESULT:
+      cpszMsg ="\"FLT INEXACT_RESULT\"";
+      break;
+    case EXCEPTION_FLT_INVALID_OPERATION:
+      cpszMsg ="\"FLT INVALID OPERATION\"";
+      break;
+    case EXCEPTION_FLT_OVERFLOW:
+      cpszMsg ="\"FLT OVERFLOW\"";
+      break;
+    case EXCEPTION_FLT_STACK_CHECK:
+      cpszMsg ="\"FLT STACK_CHECK\"";
+      break;
+    case EXCEPTION_FLT_UNDERFLOW:
+      cpszMsg ="\"FLT UNDERFLOW\"";
+      break;
+    case EXCEPTION_INT_DIVIDE_BY_ZERO:
+      cpszMsg ="\"INT DIVIDE BY ZERO\"";
+      break;
+    case EXCEPTION_INT_OVERFLOW:
+      cpszMsg ="\"INT OVERFLOW\"";
+      break;
+    case EXCEPTION_PRIV_INSTRUCTION:
+      cpszMsg ="\"PRIV INSTRUCTION\"";
+      break;
+    case EXCEPTION_IN_PAGE_ERROR:
+      cpszMsg ="\"IN PAGE_ERROR\"";
+      break;
+    case EXCEPTION_ILLEGAL_INSTRUCTION:
+      cpszMsg ="\"ILLEGAL INSTRUCTION\"";
+      break;
+    case EXCEPTION_NONCONTINUABLE_EXCEPTION:
+      cpszMsg ="\"NONCONTINUABLE EXCEPTION\"";
+      break;
+    case EXCEPTION_STACK_OVERFLOW:
+      cpszMsg ="\"STACK OVERFLOW\"";
+      break;
+    case EXCEPTION_INVALID_DISPOSITION:
+      cpszMsg ="\"INVALID DISPOSITION\"";
+      break;
+    case EXCEPTION_GUARD_PAGE:
+      cpszMsg ="\"GUARD PAGE\"";
+      break;
+    case EXCEPTION_INVALID_HANDLE:
+      cpszMsg ="\"INVALID HANDLE\"";
+      break;
+    case CONTROL_C_EXIT:
+      cpszMsg ="\"CONTROL C EXIT\"";
+      break;
+    default:
+      cpszMsg = "Unknows Windows C Exception";
+      break;
+    }
+    // throw our own type of exception so we know at least what was going on
+    // instead of just getting an unknown ... C++ excepition
+    throw ExcWinCException( ErrorMessage(UIMA_MSG_ID_EXC_WINDOWS_EXCEPTION, cpszMsg),
+                            (TyErrorId)UIMA_ERR_ENGINE_WINDOWS_EXCEPTION,
+                            ErrorInfo::unrecoverable);
+  }
+
+#define UIMA_ENVVAR_DONT_MAP_WINDOWS_EXCEPTIONS     "UIMA_DONT_MAP_WINDOWS_EXCEPTIONS"
+
+  // _set_se_translator should be called at the beginning of main
+  // since we have no access to main here, we declare a static var of a dummy
+  // type which does the _set_se_translator call in it's constructor
+  // Note: this only works if the compiler properly executes the ctors of
+  // such static vars in DLLs
+  class Dummy {
+  public:
+    Dummy( void ) {
+      if ( getenv(UIMA_ENVVAR_DONT_MAP_WINDOWS_EXCEPTIONS) == NULL) {
+        _set_se_translator( translation_func );
+      }
+    }
+  };
+  // static var of our dummy type
+  Dummy clDummy;
+#endif
+
+}

Propchange: incubator/uima/uimacpp/trunk/src/framework/exceptions.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/uima/uimacpp/trunk/src/framework/ftools.cpp
URL: http://svn.apache.org/viewvc/incubator/uima/uimacpp/trunk/src/framework/ftools.cpp?view=auto&rev=503250
==============================================================================
--- incubator/uima/uimacpp/trunk/src/framework/ftools.cpp (added)
+++ incubator/uima/uimacpp/trunk/src/framework/ftools.cpp Sat Feb  3 08:57:13 2007
@@ -0,0 +1,135 @@
+/** \file ftools.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 "uima/ftools.hpp"
+
+#include <stdio.h>
+#include <limits.h>
+
+#include <fstream>
+#include <iomanip>
+#include <string>
+using namespace std;
+
+#include "uima/filename.hpp"
+using namespace uima;
+
+#include "uima/strtools.hpp"
+
+/* ----------------------------------------------------------------------- */
+/*      Constants                                                          */
+/* ----------------------------------------------------------------------- */
+
+/* ----------------------------------------------------------------------- */
+/*      Implementation                                                     */
+/* ----------------------------------------------------------------------- */
+
+
+
+
+size_t
+ftool_ReadFileToBuffer(
+  const util::Filename& rclFileName,
+  char *&             rpszData
+) {
+  rpszData = NULL;
+  unsigned long lFSize = rclFileName.getFileSize();
+  if (lFSize == 0) {
+    return 0;
+  }
+// **need to check this**    assert(sizeof(size_t) == sizeof(unsigned int)); //make sure size_t is indeed an unsigned int
+  assert((unsigned long)lFSize <= (unsigned long)UINT_MAX-1); //we cannot read more than that in one go
+
+  FILE * pfStream = NULL;
+  pfStream = fopen(rclFileName.getAsCString(), "rb");
+  if (pfStream == NULL) {
+    return 0;
+  }
+
+  rpszData = (char*)malloc((size_t)lFSize + 1);
+  assert(rpszData != NULL);
+
+  size_t uiBytesRead;
+  uiBytesRead = fread(rpszData, 1, (size_t)lFSize, pfStream );
+  fclose(pfStream);
+
+  if (uiBytesRead != lFSize) {
+    free(rpszData);
+    rpszData = NULL;
+    return 0;
+  }
+  assert((unsigned long)uiBytesRead <= lFSize);
+  rpszData[uiBytesRead] = '\0';  //zero terminate buffer
+  return (size_t) uiBytesRead;
+}
+
+
+size_t
+ftool_ReadFileToString(
+  const util::Filename& rclFileName,
+  string&             rsData
+) {
+  size_t                     uiRetVal;
+
+  rsData = "";
+  char * pszBuff  = NULL;
+
+  uiRetVal = ftool_ReadFileToBuffer(rclFileName, pszBuff);
+  if (uiRetVal != 0) {
+    rsData.insert(0, pszBuff, uiRetVal);
+    free(pszBuff);
+  }
+
+  return uiRetVal;
+}
+
+
+/* counts the number of lines in a text file                          */
+/* return: -1 if file not readable, number of lines else              */
+long
+ftool_NbrOfLines(
+  const util::Filename& rclFileName
+) {
+  ifstream sin(rclFileName.getAsCString());
+  if (!sin) {
+    return -1;
+  }
+  string data;
+  long lines = 0;
+  while (!sin.eof()) {
+    lines++;
+    // ... we can use ANSI function getline
+    (void)getline(sin, data, '\n');
+  }
+  return lines;
+}
+
+
+

Propchange: incubator/uima/uimacpp/trunk/src/framework/ftools.cpp
------------------------------------------------------------------------------
    svn:eol-style = native