001/** 
002 * Copyright (c) 2007-2008, Regents of the University of Colorado 
003 * All rights reserved.
004 * 
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 
008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
011 * 
012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE. 
023 */
024package org.cleartk.util.cr;
025
026import java.io.FileInputStream;
027import java.io.IOException;
028import java.util.Arrays;
029
030import org.apache.uima.UimaContext;
031import org.apache.uima.cas.impl.XCASDeserializer;
032import org.apache.uima.cas.impl.XCASSerializer;
033import org.apache.uima.cas.impl.XmiCasDeserializer;
034import org.apache.uima.cas.impl.XmiCasSerializer;
035import org.apache.uima.collection.CollectionException;
036import org.apache.uima.fit.descriptor.ConfigurationParameter;
037import org.apache.uima.jcas.JCas;
038import org.apache.uima.resource.ResourceInitializationException;
039import org.cleartk.util.CleartkInitializationException;
040import org.xml.sax.SAXException;
041
042/**
043 * <br>
044 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
045 * All rights reserved.
046 * 
047 * @see XmiCasSerializer
048 * @see XCASSerializer
049 */
050
051public class XReader extends FilesCollectionReader {
052
053  public static final String XMI = "XMI";
054
055  public static final String XCAS = "XCAS";
056
057  public static final String PARAM_XML_SCHEME = "xmlScheme";
058
059  @ConfigurationParameter(
060      name = PARAM_XML_SCHEME,
061      mandatory = false,
062      defaultValue = "XMI",
063      description = "specifies the UIMA XML serialization scheme that should be used. Valid values for this parameter are 'XMI' and 'XCAS'")
064  private String xmlScheme;
065
066  @Override
067  public void initialize(UimaContext context) throws ResourceInitializationException {
068    super.initialize(context);
069
070    if (!xmlScheme.equals(XMI) && !xmlScheme.equals(XCAS))
071      throw CleartkInitializationException.invalidParameterValueSelectFrom(
072          PARAM_XML_SCHEME,
073          Arrays.asList(XMI, XCAS),
074          xmlScheme);
075  }
076
077  public void getNext(JCas jCas) throws IOException, CollectionException {
078    if (!hasNext()) {
079      throw new RuntimeException("getNext(jCas) was called but hasNext() returns false");
080    }
081
082    FileInputStream inputStream = new FileInputStream(currentFile);
083
084    try {
085      if (xmlScheme.equals(XMI))
086        XmiCasDeserializer.deserialize(inputStream, jCas.getCas());
087      else
088        XCASDeserializer.deserialize(inputStream, jCas.getCas());
089    } catch (SAXException e) {
090      throw new CollectionException(e);
091    } finally {
092      inputStream.close();
093    }
094
095    completed++;
096    currentFile = null;
097  }
098
099}