Return-Path: X-Original-To: apmail-incubator-ctakes-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-ctakes-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EB3B0D00F for ; Fri, 21 Dec 2012 17:58:23 +0000 (UTC) Received: (qmail 17706 invoked by uid 500); 21 Dec 2012 17:58:23 -0000 Delivered-To: apmail-incubator-ctakes-commits-archive@incubator.apache.org Received: (qmail 17679 invoked by uid 500); 21 Dec 2012 17:58:23 -0000 Mailing-List: contact ctakes-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ctakes-dev@incubator.apache.org Delivered-To: mailing list ctakes-commits@incubator.apache.org Received: (qmail 17672 invoked by uid 99); 21 Dec 2012 17:58:23 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Dec 2012 17:58:23 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Dec 2012 17:58:22 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 933CD23888D2; Fri, 21 Dec 2012 17:58:02 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1425060 - /incubator/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/cr/XmiCollectionReaderCtakes.java Date: Fri, 21 Dec 2012 17:58:02 -0000 To: ctakes-commits@incubator.apache.org From: mattcoarr@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121221175802.933CD23888D2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: mattcoarr Date: Fri Dec 21 17:58:02 2012 New Revision: 1425060 URL: http://svn.apache.org/viewvc?rev=1425060&view=rev Log: adding collection reader for xmi files Added: incubator/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/cr/XmiCollectionReaderCtakes.java Added: incubator/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/cr/XmiCollectionReaderCtakes.java URL: http://svn.apache.org/viewvc/incubator/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/cr/XmiCollectionReaderCtakes.java?rev=1425060&view=auto ============================================================================== --- incubator/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/cr/XmiCollectionReaderCtakes.java (added) +++ incubator/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/cr/XmiCollectionReaderCtakes.java Fri Dec 21 17:58:02 2012 @@ -0,0 +1,121 @@ +/* + * 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. + */ + +package org.apache.ctakes.core.cr; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.uima.cas.CAS; +import org.apache.uima.cas.impl.XmiCasDeserializer; +import org.apache.uima.collection.CollectionException; +import org.apache.uima.collection.CollectionReader_ImplBase; +import org.apache.uima.resource.ResourceConfigurationException; +import org.apache.uima.resource.ResourceInitializationException; +import org.apache.uima.util.Progress; +import org.apache.uima.util.ProgressImpl; +import org.xml.sax.SAXException; + +/** + * A simple collection reader that reads CASes in XMI format from a directory in the filesystem. + */ +public class XmiCollectionReaderCtakes extends CollectionReader_ImplBase { + /** + * Name of configuration parameter that must be set to the path of a directory containing the XMI + * files. + */ + public static final String PARAM_INPUTDIR = "InputDirectory"; + + /** + * Name of the configuration parameter that must be set to indicate if the + * execution fails if an encountered type is unknown + */ + public static final String PARAM_FAILUNKNOWN = "FailOnUnknownType"; + + private Boolean mFailOnUnknownType; + + private ArrayList mFiles; + + private int mCurrentIndex; + + /** + * @see org.apache.uima.collection.CollectionReader_ImplBase#initialize() + */ + public void initialize() throws ResourceInitializationException { + mFailOnUnknownType = (Boolean) getConfigParameterValue(PARAM_FAILUNKNOWN); + if (null == mFailOnUnknownType) { + mFailOnUnknownType = true; // default to true if not specified + } + File directory = new File(((String) getConfigParameterValue(PARAM_INPUTDIR)).trim()); + mCurrentIndex = 0; + + // if input directory does not exist or is not a directory, throw exception + if (!directory.exists() || !directory.isDirectory()) { + throw new ResourceInitializationException(ResourceConfigurationException.DIRECTORY_NOT_FOUND, + new Object[] { PARAM_INPUTDIR, this.getMetaData().getName(), directory.getPath() }); + } + + // get list of .xmi files in the specified directory + mFiles = new ArrayList(); + File[] files = directory.listFiles(); + for (int i = 0; i < files.length; i++) { + if (!files[i].isDirectory() && files[i].getName().endsWith(".xmi")) { + mFiles.add(files[i]); + } + } + } + + /** + * @see org.apache.uima.collection.CollectionReader#hasNext() + */ + public boolean hasNext() { + return mCurrentIndex < mFiles.size(); + } + + /** + * @see org.apache.uima.collection.CollectionReader#getNext(org.apache.uima.cas.CAS) + */ + public void getNext(CAS aCAS) throws IOException, CollectionException { + File currentFile = (File) mFiles.get(mCurrentIndex++); + FileInputStream inputStream = new FileInputStream(currentFile); + try { + XmiCasDeserializer.deserialize(inputStream, aCAS, ! mFailOnUnknownType); + } catch (SAXException e) { + throw new CollectionException(e); + } finally { + inputStream.close(); + } + } + + /** + * @see org.apache.uima.collection.base_cpm.BaseCollectionReader#close() + */ + public void close() throws IOException { + } + + /** + * @see org.apache.uima.collection.base_cpm.BaseCollectionReader#getProgress() + */ + public Progress[] getProgress() { + return new Progress[] { new ProgressImpl(mCurrentIndex, mFiles.size(), Progress.ENTITIES) }; + } + +}