/** * Write the CAS in the specified format. * * @param aCas * The CAS that should be serialized and stored * @param docOS * The output stream for the CAS * @param format * The SerialFormat in which the CAS should be stored. * @throws IOException * - Problem saving to the given InputStream */ public static void save(CAS aCas, OutputStream docOS, SerialFormat format) throws IOException { save(aCas, docOS, null, format); }
/** * Loads a Cas from a URL source. * For SerialFormats ending with _TSI except for COMPRESSED_FILTERED_TSI, * the CAS's type system and indexes definition are replaced. * CasLoadMode is DEFAULT. * * @param casUrl * The url containing the CAS * @param aCAS * The CAS that should be filled * @return the SerialFormat of the loaded CAS * @throws IOException * - Problem loading from given URL */ public static SerialFormat load(URL casUrl, CAS aCAS) throws IOException { return load(casUrl, null, aCAS, CasLoadMode.DEFAULT); }
/** * Loads a CAS from a URL source. The format is determined from the content. * * If the value of tsiUrl is null it is ignored. * * @param casUrl * The url to deserialize the CAS from * @param tsiUrl * null or an optional url to deserialize the type system and index definitions from * @param aCAS * The CAS that should be filled * @param casLoadMode specifies how to handle reinitialization and lenient loading * see the Javadocs for CasLoadMode * @return the SerialFormat of the loaded CAS * @throws IOException Problem loading */ public static SerialFormat load(URL casUrl, URL tsiUrl, CAS aCAS, CasLoadMode casLoadMode) throws IOException { InputStream casIS = new BufferedInputStream(casUrl.openStream()); InputStream tsIS = (tsiUrl == null) ? null : new BufferedInputStream(tsiUrl.openStream()); try { return load(casIS, tsIS, aCAS, casLoadMode); } finally { closeQuitely(casIS); closeQuitely(tsIS); } }
break; case SERIALIZED: writeJavaObject(Serialization.serializeCAS(aCas), docOS); break; case SERIALIZED_TSI: writeJavaObject(Serialization.serializeCASComplete((CASMgr) aCas), docOS); typeSystemWritten = true; // Embedded type system break; writeTypeSystem(aCas, tsiOS, true);
CasIOUtils.writeTypeSystem(cas, ostream, true);
public static void writeTypeSystem(CAS aCas, OutputStream aOS, boolean includeIndexDefs) throws IOException { writeJavaObject(includeIndexDefs ? Serialization.serializeCASMgr((CASImpl) aCas) : Serialization.serializeCASMgrTypeSystemOnly((CASImpl) aCas) , aOS); }
casImpl.setupCasFromCasMgrSerializer(readCasManager(tsiInputStream)); return casImpl.reinit(h, casInputStream, readCasManager(tsiInputStream), casLoadMode, null, AllowPreexistingFS.allow, typeSystem); Object o = ois.readObject(); if (o instanceof CASSerializer) { casImpl.setupCasFromCasMgrSerializer(readCasManager(tsiInputStream));
CasIOUtils.writeTypeSystem(cas, serializedOut, true);
/** * Loads a Cas from an Input Stream. The format is determined from the content. * For SerialFormats ending with _TSI except for COMPRESSED_FILTERED_TSI, * the CAS's type system and indexes definition are replaced. * CasLoadMode is DEFAULT. * * @param casInputStream * The input stream containing the CAS. Caller should buffer this appropriately. * @param aCAS * The CAS that should be filled * @return the SerialFormat of the loaded CAS * @throws IOException * - Problem loading from given InputStream */ public static SerialFormat load(InputStream casInputStream, CAS aCAS) throws IOException { return load(casInputStream, null, aCAS, CasLoadMode.DEFAULT); }
public static void storeCas(CAS cas, String name) { File file = new File("input/" + name + ".xmi"); file.getParentFile().mkdirs(); OutputStream fos = null; try { fos = new FileOutputStream(file); CasIOUtils.save(cas, fos , SerialFormat.XMI); } catch (IOException e) { throw new IllegalArgumentException(e); } finally { IOUtils.closeQuietly(fos); } } }
CasIOUtils.writeTypeSystem(cas, serializedOut, isTsiIncluded);
/** * Loads a CAS from a URL source. The format is determined from the content. * For SerialFormats ending with _TSI except for COMPRESSED_FILTERED_TSI, * the CAS's type system and indexes definition are replaced. * CasLoadMode is set according to the leniently flag. * * @param casUrl * The url to deserialize the CAS from * @param tsiUrl * The optional url to deserialize the type system and index definitions from * @param aCAS * The CAS that should be filled * @param leniently true means do lenient loading * @return the SerialFormat of the loaded CAS * @throws IOException Problem loading */ public static SerialFormat load(URL casUrl, URL tsiUrl, CAS aCAS, boolean leniently) throws IOException { return load(casUrl, tsiUrl, aCAS, leniently ? CasLoadMode.LENIENT : CasLoadMode.DEFAULT); }
/** * 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); } }
/** * Loads a CAS from an Input Stream. The format is determined from the content. * * For SerialFormats ending with _TSI the embedded value is used instead of any supplied external TSI information. * TSI information is available either via embedded value, or if a non-null input is passed for tsiInputStream. * * If TSI information is available, the CAS's type system and indexes definition are replaced, * except for SerialFormats COMPRESSED_FILTERED, COMPRESSED_FILTERED_TS, and COMPRESSED_FILTERED_TSI. * * The CasLoadMode is DEFAULT. * * @param casInputStream - * @param tsiInputStream - * @param aCAS - * @return - * @throws IOException - */ public static SerialFormat load(InputStream casInputStream, InputStream tsiInputStream, CAS aCAS) throws IOException { return load(casInputStream, tsiInputStream, aCAS, CasLoadMode.DEFAULT); }
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()); }
/** * Loads a CAS from an Input Stream. The format is determined from the content. * * For SerialFormats ending with _TSI the embedded value is used instead of any supplied external TSI information. * TSI information is available either via embedded value, or if a non-null input is passed for tsiInputStream. * * If TSI information is available, the CAS's type system and indexes definition are replaced, * except for SerialFormats COMPRESSED_FILTERED, COMPRESSED_FILTERED_TS, and COMPRESSED_FILTERED_TSI. * * The CasLoadMode is set to LENIENT if the leniently flag is true; otherwise it is set to DEFAULT. * * @param casInputStream - * @param tsiInputStream - * @param aCAS - * @param leniently - * @return - * @throws IOException - */ public static SerialFormat load(InputStream casInputStream, InputStream tsiInputStream, CAS aCAS, boolean leniently) throws IOException { return load(casInputStream, tsiInputStream, aCAS, leniently ? CasLoadMode.LENIENT : CasLoadMode.DEFAULT); }
return load(casInputStream, null, aCAS, CasLoadMode.DEFAULT, (TypeSystemImpl) typeSystem);
return load(casInputStream, tsiInputStream, aCAS, casLoadMode, null);
/** * 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. */ private void upgradeCas(CAS aSourceCas, CAS aTargetCas, TypeSystemDescription aTargetTypeSystem) throws UIMAException, IOException { // 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(); Serialization.serializeWithCompression(aSourceCas, serializedCasContents, sourceTypeSystem); // Re-initialize the target CAS with new type system CAS tempCas = JCasFactory.createJCas(aTargetTypeSystem).getCas(); CASCompleteSerializer serializer = Serialization.serializeCASComplete((CASImpl) tempCas); Serialization.deserializeCASComplete(serializer, (CASImpl) aTargetCas); // Leniently load the source CAS contents into the target CAS CasIOUtils.load(new ByteArrayInputStream(serializedCasContents.toByteArray()), aTargetCas, sourceTypeSystem); // Make sure JCas is properly initialized too aTargetCas.getJCas(); }