Logo Search packages:      
Sourcecode: uimaj version File versions  Download package

AnnotatorElement.java

/*
 * 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.uima.caseditor.core.model;

import java.io.File;

import org.apache.uima.UIMAFramework;
import org.apache.uima.caseditor.CasEditorPlugin;
import org.apache.uima.caseditor.core.model.delta.INlpElementDelta;
import org.apache.uima.caseditor.core.uima.AnnotatorConfiguration;
import org.apache.uima.caseditor.core.util.MarkerUtil;
import org.apache.uima.resource.ResourceSpecifier;
import org.apache.uima.util.InvalidXMLException;
import org.apache.uima.util.XMLInputSource;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

/**
 * TODO: add javadoc here
 */
00040 public class AnnotatorElement extends AbstractNlpElement {
  private CasProcessorFolder mParent;

  private IFile mAnnotatorResource;

  private AnnotatorConfiguration mAnnotatorConfig;

  /**
   * Initializes the current instance.
   * 
   * @param config
   * @param annotatorFile
   */
00053   AnnotatorElement(CasProcessorFolder config, IFile annotatorFile) {
    mParent = config;
    mAnnotatorResource = annotatorFile;

    mAnnotatorConfig = createAnnotatorConfiguration();
  }

  /**
   * Retrieves the {@link AnnotatorConfiguration}.
   * 
   * @return the {@link AnnotatorConfiguration}
   */
00065   public AnnotatorConfiguration getAnnotatorConfiguration() {
    return mAnnotatorConfig;
  }

  private AnnotatorConfiguration createAnnotatorConfiguration() {

    Runnable clearMarkers = new Runnable() {
      public void run() {
        try {
          MarkerUtil.clearMarkers(mAnnotatorResource, MarkerUtil.PROBLEM_MARKER);
        } catch (CoreException e) {
          CasEditorPlugin.log(e);
        }
      }
    };
    ((NlpModel) getNlpProject().getParent()).asyncExcuteQueue(clearMarkers);

    XMLInputSource inAnnotator;
    try {
      String dataPath = ((IFolder) mParent.getResource()).getLocation().toOSString();
      inAnnotator = new XMLInputSource(mAnnotatorResource.getContents(), new File(dataPath));
    } catch (final CoreException e2) {
      Runnable createMarker = new Runnable() {
        public void run() {
          try {
            MarkerUtil.clearMarkers(mAnnotatorResource, e2.getMessage());
          } catch (CoreException e) {
            CasEditorPlugin.log(e);
          }
        }
      };
      ((NlpModel) getNlpProject().getParent()).asyncExcuteQueue(createMarker);

      return null;
    }

    ResourceSpecifier specifier;
    try {
      specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(inAnnotator);
    } catch (final InvalidXMLException e) {
      Runnable createMarker = new Runnable() {
        public void run() {
          try {
            MarkerUtil.createMarker(mAnnotatorResource, e.getMessage());
          } catch (CoreException e2) {
            CasEditorPlugin.log(e2);
          }
        }
      };
      ((NlpModel) getNlpProject().getParent()).asyncExcuteQueue(createMarker);

      return null;
    }

    // TODO: refactor here
    AnnotatorConfiguration annotatorConfiguration = new AnnotatorConfiguration(this, specifier);

    // TODO: cast is unsafe !!!
    annotatorConfiguration.setBaseFolder((IFolder) mParent.getResource());

    return annotatorConfiguration;
  }

  // private void createMarker()

  @Override
00131   void addResource(INlpElementDelta delta, IResource resource) {
    // just do nothing, no children
  }

  @Override
00136   void changedResource(IResource resource, INlpElementDelta delta) throws CoreException {
    mAnnotatorConfig = createAnnotatorConfiguration();
  }

  @Override
00141   void removeResource(INlpElementDelta delta, IResource resource) {
    // just do nothing, no children
  }

  /**
   * Retrieves the name.
   */
00148   public String getName() {
    return getResource().getName();
  }

  /**
   * Retrieves the nlp projects.
   */
00155   public NlpProject getNlpProject() {
    return getParent().getNlpProject();
  }

  /**
   * Retrieves the parent.
   */
00162   public INlpElement getParent() {
    return mParent;
  }

  /**
   * Retrieves the underlying resource.
   */
00169   public IResource getResource() {
    return mAnnotatorResource;
  }
}

Generated by  Doxygen 1.6.0   Back to index