Package org.apache.uima.resource

Examples of org.apache.uima.resource.ResourceAccessException


      // custom object (should return null)
      URL url3 = mContext.getResourceURL("TestLanguageResourceObject", new String[] { "en" });
      Assert.assertNull(url3);

      // parameter values for which no resource exists (should fail)
      ResourceAccessException ex = null;
      try {
        mContext.getResourceURL("TestFileLanguageResource", new String[] { "zh" });
      } catch (ResourceAccessException e) {
        ex = e;
      }
View Full Code Here


      // custom object (should return null)
      URI uri3 = mContext.getResourceURI("TestLanguageResourceObject", new String[] { "en" });
      Assert.assertNull(uri3);

      // parameter values for which no resource exists (should fail)
      ResourceAccessException ex = null;
      try {
        mContext.getResourceURI("TestFileLanguageResource", new String[] { "zh" });
      } catch (ResourceAccessException e) {
        ex = e;
      }
View Full Code Here

      String path3 = mContext.getResourceFilePath("TestLanguageResourceObject",
              new String[] { "en" });
      Assert.assertNull(path3);

      // parameter values for which no resource exists (should fail)
      ResourceAccessException ex = null;
      try {
        mContext.getResourceFilePath("TestFileLanguageResource", new String[] { "zh" });
      } catch (ResourceAccessException e) {
        ex = e;
      }
View Full Code Here

   */
  public Object getResource(String aName) throws ResourceAccessException {
    Object r = mResourceMap.get(aName);
    // if this is a ParameterizedDataResource, it is an error
    if (r instanceof ParameterizedDataResource) {
      throw new ResourceAccessException(ResourceAccessException.PARAMETERS_REQUIRED,
              new Object[] { aName });
    }
    return r;

  }
View Full Code Here

    if (r == null) {
      return null;
    }
    // if not a ParameterizedDataResource, it is an error
    if (!(r instanceof ParameterizedDataResource)) {
      throw new ResourceAccessException(ResourceAccessException.PARAMETERS_NOT_ALLOWED,
              new Object[] { aName });
    }
    ParameterizedDataResource pdr = (ParameterizedDataResource) r;

    // get a particular DataResource instance for the specified parameters
    DataResource dr;
    try {
      dr = pdr.getDataResource(aParams);
    } catch (ResourceInitializationException e) {
      throw new ResourceAccessException(e);
    }

    // see if we've already encountered this DataResource under this resource name
    ArrayList nameAndResource = new ArrayList();
    nameAndResource.add(aName);
    nameAndResource.add(dr);
    Object resourceInstance = mParameterizedResourceInstanceMap.get(nameAndResource);
    if (resourceInstance != null) {
      return resourceInstance;
    }

    // We haven't encountered this before. See if we need to instantiate a
    // SharedResourceObject
    Class sharedResourceObjectClass = (Class) mParameterizedResourceImplClassMap.get(aName);
    if (sharedResourceObjectClass != null) {
      try {
        SharedResourceObject sro = (SharedResourceObject) sharedResourceObjectClass.newInstance();
        sro.load(dr);
        mParameterizedResourceInstanceMap.put(nameAndResource, sro);
        return sro;
      } catch (InstantiationException e) {
        throw new ResourceAccessException(e);
      } catch (IllegalAccessException e) {
        throw new ResourceAccessException(e);
      } catch (ResourceInitializationException e) {
        throw new ResourceAccessException(e);
      }
    } else
    // no impl. class - just return the DataResource
    {
      mParameterizedResourceInstanceMap.put(nameAndResource, dr);
View Full Code Here

        return ((DataResource) r).getInputStream();
      } else {
        return null;
      }
    } catch (IOException e) {
      throw new ResourceAccessException(e);
    }
  }
View Full Code Here

        return ((DataResource) r).getInputStream();
      } else {
        return null;
      }
    } catch (IOException e) {
      throw new ResourceAccessException(e);
    }
  }
View Full Code Here

         throws ResourceAccessException {
      final String extResrcKey = rootDictEl.getAttributeValue( "externalResourceKey" );
      // UimaContext.getResourceObject(..) throws ResourceAccessException
      final Object extResrc = annotCtx.getResourceObject( extResrcKey );
      if ( extResrc == null ) {
         throw new ResourceAccessException( "Unable to find external resource with key:" + extResrcKey, null );
      }

      final Element lookupFieldEl = rootDictEl.getChild( "lookupField" );
      final String lookupFieldName = lookupFieldEl.getAttributeValue( "fieldName" );

      Dictionary dict;
      try {
         if (rootDictEl.getChild( "implementation" ).getChildren().isEmpty() ) {
            throw new ResourceAccessException( new IndexOutOfBoundsException() );
         }
         final Element implEl = (Element) rootDictEl.getChild( "implementation" ).getChildren().get( 0 );
         final String implType = implEl.getName();
         if ( implType.equals( "luceneImpl" ) ) {
            if ( !(extResrc instanceof LuceneIndexReaderResource) ) {
               throw new ResourceAccessException( "Expected external resource to be:"
                                          + LuceneIndexReaderResource.class, new Object[]{extResrc} );
            }
            final IndexReader indexReader = ((LuceneIndexReaderResource) extResrc).getIndexReader();
            final IndexSearcher indexSearcher = new IndexSearcher( indexReader );
            // Added 'MaxListSize' ohnlp-Bugs-3296301
            dict = new LuceneDictionaryImpl( indexSearcher, lookupFieldName, MAX_LIST_SIZE );
         } else if ( implType.equals( "jdbcImpl" ) ) {
            final String tableName = implEl.getAttributeValue( "tableName" );
            if ( !(extResrc instanceof JdbcConnectionResource) ) {
               throw new ResourceAccessException( "Expected external resource to be:"
                                          + JdbcConnectionResource.class, new Object[]{extResrc} );
            }
            final Connection conn = ((JdbcConnectionResource) extResrc).getConnection();
            dict = new JdbcDictionaryImpl( conn, tableName, lookupFieldName );
         } else if ( implType.equals( "csvImpl" ) ) {
            final String fieldDelimiter = implEl.getAttributeValue( "delimiter" );
            if ( !(extResrc instanceof FileResource) ) {
               throw new ResourceAccessException( "Expected external resource to be:"
                                          + FileResource.class, new Object[]{extResrc} );
            }

            final String idxFieldNameStr = implEl.getAttributeValue( "indexedFieldNames" );
            final StringTokenizer st = new StringTokenizer( idxFieldNameStr, "," );
            int arrIdx = 0;
            String[] idxFieldNameArr = new String[st.countTokens()];
            while ( st.hasMoreTokens() ) {
               idxFieldNameArr[arrIdx++] = st.nextToken().trim();
            }

            final File csvFile = ((FileResource) extResrc).getFile();
            try {
               final StringTable strTable = StringTableFactory.build( new FileReader( csvFile ),
                     fieldDelimiter, idxFieldNameArr, true );
               dict = new StringTableDictionaryImpl( strTable, lookupFieldName );
            } catch ( FileNotFoundException fnfE ) {
               throw new ResourceAccessException( "Could not open csv file", new Object[]{csvFile} );
            } catch (IOException ioE ) {
               throw new ResourceAccessException( "Could not open csv file", new Object[]{csvFile} );
            }
         } else {
            throw new ResourceAccessException( "Unsupported impl type:" + implType, new Object[]{implType} );
         }

         final List<Element> rootDictChildren = rootDictEl.getChild( "metaFields" ).getChildren();
         for ( Element metaFieldEl : rootDictChildren ) {
            final String metaFieldName = metaFieldEl.getAttributeValue( "fieldName" );
            dict.retainMetaData( metaFieldName );
         }
      } catch ( NullPointerException npE ) {
         // thrown all over this method ...
         throw new ResourceAccessException( npE );
      }
      final boolean keepCase = Boolean.parseBoolean( rootDictEl.getAttributeValue( "caseSensitive" ) );
      final DictionaryEngine dictEngine = new DictionaryEngine( dict, keepCase );
      final Element excludeList = rootDictEl.getChild( "excludeList" );
      if ( excludeList != null && excludeList.getChildren() != null && !excludeList.getChildren().isEmpty() ) {
View Full Code Here

         throws AnnotatorContextException, ResourceAccessException {
      final String extResrcKey = rootDictEl.getAttributeValue( "externalResourceKey" );
      // UimaContext.getResourceObject(..) throws ResourceAccessException
      final Object extResrc = annotCtx.getResourceObject( extResrcKey );
      if ( extResrc == null ) {
         throw new ResourceAccessException( "Unable to find external resource with key:" + extResrcKey, null );
      }

      final Element lookupFieldEl = rootDictEl.getChild( "lookupField" );
      final String lookupFieldName = lookupFieldEl.getAttributeValue( "fieldName" );

      Dictionary dict;
      try {
         if (rootDictEl.getChild( "implementation" ).getChildren().isEmpty() ) {
            throw new ResourceAccessException( new IndexOutOfBoundsException() );
         }
         final Element implEl = (Element) rootDictEl.getChild( "implementation" ).getChildren().get( 0 );
         final String implType = implEl.getName();
         if ( implType.equals( "luceneImpl" ) ) {
            if ( !(extResrc instanceof LuceneIndexReaderResource) ) {
               throw new ResourceAccessException( "Expected external resource to be:"
                                          + LuceneIndexReaderResource.class, new Object[]{extResrc} );
            }
            final IndexReader indexReader = ((LuceneIndexReaderResource) extResrc).getIndexReader();
            final IndexSearcher indexSearcher = new IndexSearcher( indexReader );
            // Added 'MaxListSize' ohnlp-Bugs-3296301
            dict = new LuceneDictionaryImpl( indexSearcher, lookupFieldName, MAX_LIST_SIZE );
         } else if ( implType.equals( "jdbcImpl" ) ) {
            final String tableName = implEl.getAttributeValue( "tableName" );
            if ( !(extResrc instanceof JdbcConnectionResource) ) {
               throw new ResourceAccessException( "Expected external resource to be:"
                                          + JdbcConnectionResource.class, new Object[]{extResrc} );
            }
            final Connection conn = ((JdbcConnectionResource) extResrc).getConnection();
            dict = new JdbcDictionaryImpl( conn, tableName, lookupFieldName );
         } else if ( implType.equals( "csvImpl" ) ) {
            final String fieldDelimiter = implEl.getAttributeValue( "delimiter" );
            if ( !(extResrc instanceof FileResource) ) {
               throw new ResourceAccessException( "Expected external resource to be:"
                                          + FileResource.class, new Object[]{extResrc} );
            }

            final String idxFieldNameStr = implEl.getAttributeValue( "indexedFieldNames" );
            final StringTokenizer st = new StringTokenizer( idxFieldNameStr, "," );
            int arrIdx = 0;
            String[] idxFieldNameArr = new String[st.countTokens()];
            while ( st.hasMoreTokens() ) {
               idxFieldNameArr[arrIdx++] = st.nextToken().trim();
            }

            final File csvFile = ((FileResource) extResrc).getFile();
            try {
               final StringTable strTable = StringTableFactory.build( new FileReader( csvFile ),
                     fieldDelimiter, idxFieldNameArr, true );
               dict = new StringTableDictionaryImpl( strTable, lookupFieldName );
            } catch ( FileNotFoundException fnfE ) {
               throw new ResourceAccessException( "Could not open csv file", new Object[]{csvFile} );
            } catch (IOException ioE ) {
               throw new ResourceAccessException( "Could not open csv file", new Object[]{csvFile} );
            }
         } else {
            throw new ResourceAccessException( "Unsupported impl type:" + implType, new Object[]{implType} );
         }

         final List<Element> rootDictChildren = rootDictEl.getChild( "metaFields" ).getChildren();
         for ( Element metaFieldEl : rootDictChildren ) {
            final String metaFieldName = metaFieldEl.getAttributeValue( "fieldName" );
            dict.retainMetaData( metaFieldName );
         }
      } catch ( NullPointerException npE ) {
         // thrown all over this method ...
         throw new ResourceAccessException( npE );
      }
      final boolean keepCase = Boolean.parseBoolean( rootDictEl.getAttributeValue( "caseSensitive" ) );
      final DictionaryEngine dictEngine = new DictionaryEngine( dict, keepCase );
      final Element excludeList = rootDictEl.getChild( "excludeList" );
      if ( excludeList != null && excludeList.getChildren() != null && !excludeList.getChildren().isEmpty() ) {
View Full Code Here

      Object r2 = mContext.getResourceObject("TestFileResource");
      Assert.assertNotNull(r2);
      Assert.assertTrue(r2 instanceof DataResource);

      // parameterized resources (should fail)
      ResourceAccessException ex = null;
      try {
        mContext.getResourceObject("TestFileLanguageResource");
      } catch (ResourceAccessException e) {
        ex = e;
      }
View Full Code Here

TOP

Related Classes of org.apache.uima.resource.ResourceAccessException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.