org.apache.uima.util.CasIOUtils Java Examples

The following examples show how to use org.apache.uima.util.CasIOUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentUimaImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the content. The XCAS {@link InputStream} gets parsed.
 *
 * @param casFile the new content
 * @throws CoreException the core exception
 */
private void setContent(IFile casFile) throws CoreException {

  IPreferenceStore store = CasEditorPlugin.getDefault().getPreferenceStore();
  boolean withPartialTypesystem = store
          .getBoolean(AnnotationEditorPreferenceConstants.ANNOTATION_EDITOR_PARTIAL_TYPESYSTEM);

  URI uri = casFile.getLocationURI();
  if (casFile.isLinked()) {
    uri = casFile.getRawLocationURI();
  }
  File file = EFS.getStore(uri).toLocalFile(0, new NullProgressMonitor());
  try {
    format = CasIOUtils.load(file.toURI().toURL(), null, mCAS, withPartialTypesystem);
  } catch (IOException e) {
    throwCoreException(e);
  }

}
 
Example #2
Source File: DocumentImportStructureProvider.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private InputStream getDocument(String fileName, String text, String language,
        SerialFormat format) {

  String failedToImportLine = "Failed to import: " + fileName + "\n\n";

  CAS cas = createEmtpyCAS();
  cas.setDocumentText(removeNonXmlChars(text));
  cas.setDocumentLanguage(language);

  ByteArrayOutputStream out = new ByteArrayOutputStream(40000);

  try {
    CasIOUtils.save(cas, out, format);
  } catch (IOException e) {
    throw new TaeError(failedToImportLine + e.getMessage(), e);
  }

  return new ByteArrayInputStream(out.toByteArray());
}
 
Example #3
Source File: DocumentAnnotationTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void tstSerdesB4Sofa(SerialFormat format) throws IOException {
  source.reset();
  target.reset();
  
  new DocMeta(jcas).addToIndexes();
  
  jcas.setDocumentText("something");
  
  new Annotation(jcas);
  
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  CasIOUtils.save(source, bos, format);
  bos.close();
  
  CasIOUtils.load(new ByteArrayInputStream(bos.toByteArray()), target);
  AnnotationFS c = target.getDocumentAnnotation();
  System.out.println(c);
  System.out.println(target.<DocMeta>getDocumentAnnotation());
  assertTrue(CasCompare.compareCASes((CASImpl)source, (CASImpl)target));
}
 
Example #4
Source File: MtasUimaParser.java    From inception with Apache License 2.0 5 votes vote down vote up
private CAS readCas(Reader aReader) throws UIMAException, IOException, SAXException
{
    CAS cas = createCas();

    try (InputStream in = new ByteArrayInputStream(charsToBytes(toCharArray(aReader)))) {
        CasIOUtils.load(in, getRealCas(cas));
    }
    
    return cas;
}
 
Example #5
Source File: SearchCasUtils.java    From inception with Apache License 2.0 5 votes vote down vote up
public static byte[] casToByteArray(CAS aCas) throws IOException
{
    // Index annotation document
    CAS realCas = getRealCas(aCas);
    // UIMA-6162 Workaround: synchronize CAS during de/serialization
    synchronized (((CASImpl) realCas).getBaseCAS()) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            //XmiCasSerializer.serialize(aCas, null, bos, true, null);
            CasIOUtils.save(realCas, bos, SERIALIZED_TSI);
            return bos.toByteArray();
        }
    }
}
 
Example #6
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * Load the contents from the source CAS, upgrade it to the target type system and write the
 * results to the target CAS. An in-place upgrade can be achieved by using the same CAS as
 * source and target.
 */
@Override
public void upgradeCas(CAS aSourceCas, CAS aTargetCas, TypeSystemDescription aTargetTypeSystem)
    throws UIMAException, IOException
{
    CasStorageSession.get().assertWritingPermitted(aTargetCas);
    
    // Save source CAS type system (do this early since we might do an in-place upgrade)
    TypeSystem sourceTypeSystem = aSourceCas.getTypeSystem();

    // Save source CAS contents
    ByteArrayOutputStream serializedCasContents = new ByteArrayOutputStream();
    CAS realSourceCas = getRealCas(aSourceCas);
    // UIMA-6162 Workaround: synchronize CAS during de/serialization
    synchronized (((CASImpl) realSourceCas).getBaseCAS()) {
        serializeWithCompression(realSourceCas, serializedCasContents, sourceTypeSystem);
    }

    // Re-initialize the target CAS with new type system
    CAS realTargetCas = getRealCas(aTargetCas);
    // UIMA-6162 Workaround: synchronize CAS during de/serialization
    synchronized (((CASImpl) realTargetCas).getBaseCAS()) {
        CAS tempCas = CasFactory.createCas(aTargetTypeSystem);
        CASCompleteSerializer serializer = serializeCASComplete((CASImpl) tempCas);
        deserializeCASComplete(serializer, (CASImpl) realTargetCas);

        // Leniently load the source CAS contents into the target CAS
        CasIOUtils.load(new ByteArrayInputStream(serializedCasContents.toByteArray()),
                getRealCas(aTargetCas), sourceTypeSystem);
    }
}
 
Example #7
Source File: DocumentUimaImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes the {@link CAS} to the given {@link OutputStream} in the XCAS format.
 *
 * @param out the out
 * @throws CoreException the core exception
 */
public void serialize(OutputStream out) throws CoreException {
  try {
    CasIOUtils.save(mCAS, out, format);
  } catch (IOException e) {
    throwCoreException(e);
  }
}
 
Example #8
Source File: XmiCompare.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * Compares one xmi file, if it can find the other one
   * Skips non-xmi files
   * 
   * @param p1 - path of file to compare
   * @throws IOException 
   * @throws FileNotFoundException 
   */
  void maybeCompare(Path p1) {
    String p1name = p1.toString();
    if (!p1name.endsWith(".xmi")) {
      return;
    }
    
    Path p2 = Paths.get(d2String, p1.getFileName().toString());
    
    if (!p2.toFile().exists()) {
      return;
    }
    
    itemCount++;
    
    System.out.format("%,5d Comparing %s:",
        itemCount,
        p1.getFileName().toString());
    
    if (itemCount <= skip) {
      System.out.println(" skipped");
      return;
    }

    try {
      CasIOUtils.load(new FileInputStream(p1.toFile()), c1);
      CasIOUtils.load(new FileInputStream(p2.toFile()), c2);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
             
    compareNumberOfFSsByType(c1, c2);
    
    cc = new CasCompare(c1, c2);
    cc.compareAll(true);

    
    // put temporary customizations here.
//  removeAllExcept("SomeType");

    customizeCompare();
    
    isOk = cc.compareCASes();
     
    if (isOk) {
      System.out.println("  compare:OK");
    } 

  }
 
Example #9
Source File: CasFactory.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * This method creates a new CAS and loads the contents of an XMI or XCAS file into it.
 * 
 * @param fileName
 *          a file name for the serialized CAS data
 * @param typeSystemDescription
 *          a type system description to initialize the CAS
 * @return a new CAS
 * @throws ResourceInitializationException
 *           if the CAS could not be initialized
 * @throws IOException
 *           if there is a problem reading the file
 */
public static CAS createCas(String fileName, TypeSystemDescription typeSystemDescription)
        throws ResourceInitializationException, IOException {
  CAS cas = createCas(typeSystemDescription);
  try (InputStream is = new FileInputStream(fileName)) {
    CasIOUtils.load(is, cas);
  }
  return cas;
}