001/** 
002 * Copyright (c) 2012, 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 */
024
025package org.cleartk.util.ae;
026
027import java.io.InputStream;
028import java.net.URI;
029
030import org.apache.uima.analysis_engine.AnalysisEngineDescription;
031import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
032import org.apache.uima.cas.CAS;
033import org.apache.uima.cas.impl.XCASDeserializer;
034import org.apache.uima.cas.impl.XCASSerializer;
035import org.apache.uima.cas.impl.XmiCasDeserializer;
036import org.apache.uima.cas.impl.XmiCasSerializer;
037import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
038import org.apache.uima.fit.component.ViewCreatorAnnotator;
039import org.apache.uima.fit.descriptor.ConfigurationParameter;
040import org.apache.uima.fit.factory.AggregateBuilder;
041import org.apache.uima.fit.factory.AnalysisEngineFactory;
042import org.apache.uima.jcas.JCas;
043import org.apache.uima.resource.ResourceInitializationException;
044import org.cleartk.util.ViewUriUtil;
045
046/**
047 * <br>
048 * Copyright (c) 2012, Regents of the University of Colorado <br>
049 * All rights reserved.
050 * 
051 * Reads contents of URI in URIView and deserializes it into the CAS.
052 * 
053 * @see XmiCasSerializer
054 * @see XCASSerializer
055 * 
056 * @author Lee Becker
057 * 
058 */
059public class UriToXmiCasAnnotator extends JCasAnnotator_ImplBase {
060
061  public static AnalysisEngineDescription getDescription() throws ResourceInitializationException {
062    return AnalysisEngineFactory.createEngineDescription(UriToXmiCasAnnotator.class);
063  }
064
065  /**
066   * Use this aggregate description if UriToDocumentTextAnnotator will be writing to a mapped view.
067   */
068  public static AnalysisEngineDescription getCreateViewAggregateDescription(String targetViewName)
069      throws ResourceInitializationException {
070    AggregateBuilder builder = new AggregateBuilder();
071    builder.add(AnalysisEngineFactory.createEngineDescription(
072        ViewCreatorAnnotator.class,
073        ViewCreatorAnnotator.PARAM_VIEW_NAME,
074        targetViewName));
075    builder.add(UriToXmiCasAnnotator.getDescription(), CAS.NAME_DEFAULT_SOFA, targetViewName);
076    return builder.createAggregateDescription();
077  }
078
079  public static enum XmlScheme {
080    XMI, XCAS
081  }
082
083  public static final String PARAM_XML_SCHEME = "xmlScheme";
084
085  @ConfigurationParameter(
086      name = PARAM_XML_SCHEME,
087      defaultValue = "XMI",
088      description = "specifies the UIMA XML serialization scheme that should be used. Valid values for this parameter are 'XMI' and 'XCAS'")
089  private XmlScheme xmlScheme;
090
091  @Override
092  public void process(JCas jCas) throws AnalysisEngineProcessException {
093    URI uri = ViewUriUtil.getURI(jCas);
094
095    InputStream inputStream = null;
096    try {
097      inputStream = uri.toURL().openStream();
098      switch (this.xmlScheme) {
099        case XMI:
100          XmiCasDeserializer.deserialize(inputStream, jCas.getCas());
101          break;
102        case XCAS:
103          XCASDeserializer.deserialize(inputStream, jCas.getCas());
104          break;
105      }
106      inputStream.close();
107    } catch (Exception e) {
108      throw new AnalysisEngineProcessException(e);
109    }
110
111  }
112
113}