/*******************************************************************************
* Copyright 2015-2016 - CNRS (Centre National de Recherche Scientifique)
*
* 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.
*
*******************************************************************************/
/*
* 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 2, 2015nership. 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 eu.project.ttc.engines.exporter;
import java.io.File;
import org.apache.uima.UimaContext;
import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.text.AnnotationIndex;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import com.google.common.base.Preconditions;
import eu.project.ttc.types.SourceDocumentInformation;
public abstract class CasExporter extends JCasAnnotator_ImplBase {
public static final String OUTPUT_DIRECTORY = "OutputDirectory";
@ConfigurationParameter(name = OUTPUT_DIRECTORY, mandatory=true)
protected String directoryPath;
protected File directoryFile;
@Override
public void initialize(UimaContext context)
throws ResourceInitializationException {
super.initialize(context);
this.directoryFile = new File(this.directoryPath);
if(!this.directoryFile.exists())
this.directoryFile.mkdirs();
Preconditions.checkState(this.directoryFile.exists(), String.format("Directory %s does noes exists", this.directoryPath));
Preconditions.checkNotNull(this.directoryFile.isDirectory(), String.format("Not a directory: %s", this.directoryPath));
Preconditions.checkState(this.directoryFile.canWrite(), String.format("Cannot write to directory %s.", this.directoryPath));
}
protected String getExportFilePath(JCas cas, String extension) {
AnnotationIndex<Annotation> index = cas.getAnnotationIndex(SourceDocumentInformation.type);
FSIterator<Annotation> iterator = index.iterator();
if (iterator.hasNext()) {
SourceDocumentInformation annotation = (SourceDocumentInformation) iterator.next();
File file = new File(annotation.getUri());
String name = file.getName();
int i = name.lastIndexOf('.');
if (i == -1) {
return name + "." + extension;
} else {
return name.substring(0, i) + "." + extension;
}
} else {
return null;
}
}
}