This is an automated email from the ASF dual-hosted git repository. rec pushed a commit to branch feature/UIMA-5820-Add-CasFactory in repository https://gitbox.apache.org/repos/asf/uima-uimafit.git
commit eb00612b9329577da01f8d9fb8f64ede92ed8a14 Author: Richard Eckart de Castilho <[email protected]> AuthorDate: Fri Jul 13 14:39:07 2018 +0200 [UIMA-5820] Add CasFactory - Added CasFactory and changed JCasFactory to delegate calls to CasFactory. --- .../factory/{JCasFactory.java => CasFactory.java} | 111 +++++++++++---------- .../org/apache/uima/fit/factory/JCasFactory.java | 40 ++------ 2 files changed, 64 insertions(+), 87 deletions(-) diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java b/uimafit-core/src/main/java/org/apache/uima/fit/factory/CasFactory.java similarity index 59% copy from uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java copy to uimafit-core/src/main/java/org/apache/uima/fit/factory/CasFactory.java index 6964102..5c57b35 100644 --- a/uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java +++ b/uimafit-core/src/main/java/org/apache/uima/fit/factory/CasFactory.java @@ -18,152 +18,155 @@ */ package org.apache.uima.fit.factory; +import static org.apache.uima.fit.factory.FsIndexFactory.createFsIndexCollection; +import static org.apache.uima.fit.factory.TypePrioritiesFactory.createTypePriorities; import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription; import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath; -import static org.apache.uima.fit.factory.TypePrioritiesFactory.createTypePriorities; -import static org.apache.uima.fit.factory.FsIndexFactory.createFsIndexCollection; -import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import org.apache.uima.UIMAException; +import org.apache.uima.cas.CAS; import org.apache.uima.fit.internal.ResourceManagerFactory; -import org.apache.uima.fit.util.CasIOUtil; -import org.apache.uima.jcas.JCas; import org.apache.uima.resource.ResourceManager; import org.apache.uima.resource.metadata.FsIndexCollection; import org.apache.uima.resource.metadata.TypePriorities; import org.apache.uima.resource.metadata.TypeSystemDescription; import org.apache.uima.util.CasCreationUtils; +import org.apache.uima.util.CasIOUtils; /** - * Convenience methods to create {@link JCas} objects. + * Convenience methods to create {@link CAS} objects. */ -public final class JCasFactory { - private JCasFactory() { +public final class CasFactory { + private CasFactory() { // This class is not meant to be instantiated } /** - * Creates a new JCas with the given text. The type system is detected automatically using + * Creates a new CAS with the given text. The type system is detected automatically using * {@link TypeSystemDescriptionFactory#createTypeSystemDescription()}. Type priorities are * detected automatically using {@link TypePrioritiesFactory#createTypePriorities()}. Indexes are * detected automatically using {@link FsIndexFactory#createFsIndexCollection()}. * * @param aText - * the document text to be set in the new JCas. - * @return a new JCas + * the document text to be set in the new CAS. + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized */ - public static JCas createText(String aText) throws UIMAException { + public static CAS createText(String aText) throws UIMAException { return createText(aText, null); } /** - * Creates a new JCas with the given text and language. The type system is detected automatically + * Creates a new CAS with the given text and language. The type system is detected automatically * using {@link TypeSystemDescriptionFactory#createTypeSystemDescription()}. Type priorities are * detected automatically using {@link TypePrioritiesFactory#createTypePriorities()}. Indexes are * detected automatically using {@link FsIndexFactory#createFsIndexCollection()}. * * @param aText - * the document text to be set in the new JCas. + * the document text to be set in the new CAS. * @param aLanguage - * the document language to be set in the new JCas. - * @return a new JCas + * the document language to be set in the new CAS. + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized */ - public static JCas createText(String aText, String aLanguage) throws UIMAException { - JCas jcas = createJCas(); + public static CAS createText(String aText, String aLanguage) throws UIMAException { + CAS cas = createCas(); if (aText != null) { - jcas.setDocumentText(aText); + cas.setDocumentText(aText); } if (aLanguage != null) { - jcas.setDocumentLanguage(aLanguage); + cas.setDocumentLanguage(aLanguage); } - return jcas; + return cas; } /** - * Creates a new {@link JCas}. The type system is detected automatically using + * Creates a new {@link CAS}. The type system is detected automatically using * {@link TypeSystemDescriptionFactory#createTypeSystemDescription()}. Type priorities are * detected automatically using {@link TypePrioritiesFactory#createTypePriorities()}. Indexes are * detected automatically using {@link FsIndexFactory#createFsIndexCollection()}. * - * @return a new JCas + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized */ - public static JCas createJCas() throws UIMAException { + public static CAS createCas() throws UIMAException { TypeSystemDescription tsd = createTypeSystemDescription(); TypePriorities tp = createTypePriorities(); FsIndexCollection indexes = createFsIndexCollection(); ResourceManager resMgr = ResourceManagerFactory.newResourceManager(); - return CasCreationUtils.createCas(tsd, tp, indexes.getFsIndexes(), null, resMgr).getJCas(); + return CasCreationUtils.createCas(tsd, tp, indexes.getFsIndexes(), null, resMgr); } /** - * Creates a new JCas from type system descriptor files found by name. No auto-detection for type - * priorities, or indexes is performed. + * Creates a new {@link CAS} from type system descriptor files found by name. No auto-detection + * for type priorities, or indexes is performed. * * @param typeSystemDescriptorNames - * names of the type system descriptors on the classpath used to initialize the JCas (in + * names of the type system descriptors on the classpath used to initialize the CAS (in * Java notation, e.g. "my.package.TypeSystem" without the ".xml" extension) - * @return a new JCas + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized */ - public static JCas createJCas(String... typeSystemDescriptorNames) throws UIMAException { + public static CAS createCas(String... typeSystemDescriptorNames) throws UIMAException { return CasCreationUtils.createCas(createTypeSystemDescription(typeSystemDescriptorNames), null, - null).getJCas(); + null); } /** - * Creates a new JCas from type system descriptor files. No auto-detection for type priorities, or + * Creates a new CAS from type system descriptor files. No auto-detection for type priorities, or * indexes is performed. * * @param typeSystemDescriptorPaths * paths to type system descriptor files - * @return a new JCas + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized */ - public static JCas createJCasFromPath(String... typeSystemDescriptorPaths) throws UIMAException { - return createJCas(createTypeSystemDescriptionFromPath(typeSystemDescriptorPaths)); + public static CAS createCasFromPath(String... typeSystemDescriptorPaths) throws UIMAException { + return createCas(createTypeSystemDescriptionFromPath(typeSystemDescriptorPaths)); } /** - * Create a new JCas for the given type system description. No auto-detection type priorities, or + * Create a new CAS for the given type system description. No auto-detection type priorities, or * indexes is performed. * * @param typeSystemDescription - * a type system description to initialize the JCas - * @return a new JCas + * a type system description to initialize the CAS + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized */ - public static JCas createJCas(TypeSystemDescription typeSystemDescription) throws UIMAException { - return CasCreationUtils.createCas(typeSystemDescription, null, null).getJCas(); + public static CAS createCas(TypeSystemDescription typeSystemDescription) throws UIMAException { + return CasCreationUtils.createCas(typeSystemDescription, null, null); } /** - * This method creates a new JCas and loads the contents of an XMI or XCAS file into it. + * 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 JCas - * @return a new JCas + * a type system description to initialize the CAS + * @return a new CAS * @throws UIMAException - * if the JCas could not be initialized + * if the CAS could not be initialized * @throws IOException * if there is a problem reading the file */ - public static JCas createJCas(String fileName, TypeSystemDescription typeSystemDescription) + public static CAS createCas(String fileName, TypeSystemDescription typeSystemDescription) throws UIMAException, IOException { - JCas jCas = createJCas(typeSystemDescription); - CasIOUtil.readJCas(jCas, new File(fileName)); - return jCas; + CAS cas = createCas(typeSystemDescription); + try (InputStream is = new FileInputStream(fileName)) { + CasIOUtils.load(is, cas); + } + return cas; } } diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java b/uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java index 6964102..9597d65 100644 --- a/uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java +++ b/uimafit-core/src/main/java/org/apache/uima/fit/factory/JCasFactory.java @@ -18,23 +18,11 @@ */ package org.apache.uima.fit.factory; -import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription; -import static org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath; -import static org.apache.uima.fit.factory.TypePrioritiesFactory.createTypePriorities; -import static org.apache.uima.fit.factory.FsIndexFactory.createFsIndexCollection; - -import java.io.File; import java.io.IOException; import org.apache.uima.UIMAException; -import org.apache.uima.fit.internal.ResourceManagerFactory; -import org.apache.uima.fit.util.CasIOUtil; import org.apache.uima.jcas.JCas; -import org.apache.uima.resource.ResourceManager; -import org.apache.uima.resource.metadata.FsIndexCollection; -import org.apache.uima.resource.metadata.TypePriorities; import org.apache.uima.resource.metadata.TypeSystemDescription; -import org.apache.uima.util.CasCreationUtils; /** * Convenience methods to create {@link JCas} objects. @@ -57,7 +45,7 @@ public final class JCasFactory { * if the JCas could not be initialized */ public static JCas createText(String aText) throws UIMAException { - return createText(aText, null); + return CasFactory.createText(aText, null).getJCas(); } /** @@ -75,14 +63,7 @@ public final class JCasFactory { * if the JCas could not be initialized */ public static JCas createText(String aText, String aLanguage) throws UIMAException { - JCas jcas = createJCas(); - if (aText != null) { - jcas.setDocumentText(aText); - } - if (aLanguage != null) { - jcas.setDocumentLanguage(aLanguage); - } - return jcas; + return CasFactory.createText(aText, aLanguage).getJCas(); } /** @@ -96,11 +77,7 @@ public final class JCasFactory { * if the JCas could not be initialized */ public static JCas createJCas() throws UIMAException { - TypeSystemDescription tsd = createTypeSystemDescription(); - TypePriorities tp = createTypePriorities(); - FsIndexCollection indexes = createFsIndexCollection(); - ResourceManager resMgr = ResourceManagerFactory.newResourceManager(); - return CasCreationUtils.createCas(tsd, tp, indexes.getFsIndexes(), null, resMgr).getJCas(); + return CasFactory.createCas().getJCas(); } /** @@ -115,8 +92,7 @@ public final class JCasFactory { * if the JCas could not be initialized */ public static JCas createJCas(String... typeSystemDescriptorNames) throws UIMAException { - return CasCreationUtils.createCas(createTypeSystemDescription(typeSystemDescriptorNames), null, - null).getJCas(); + return CasFactory.createCas(typeSystemDescriptorNames).getJCas(); } /** @@ -130,7 +106,7 @@ public final class JCasFactory { * if the JCas could not be initialized */ public static JCas createJCasFromPath(String... typeSystemDescriptorPaths) throws UIMAException { - return createJCas(createTypeSystemDescriptionFromPath(typeSystemDescriptorPaths)); + return CasFactory.createCasFromPath(typeSystemDescriptorPaths).getJCas(); } /** @@ -144,7 +120,7 @@ public final class JCasFactory { * if the JCas could not be initialized */ public static JCas createJCas(TypeSystemDescription typeSystemDescription) throws UIMAException { - return CasCreationUtils.createCas(typeSystemDescription, null, null).getJCas(); + return CasFactory.createCas(typeSystemDescription).getJCas(); } /** @@ -162,8 +138,6 @@ public final class JCasFactory { */ public static JCas createJCas(String fileName, TypeSystemDescription typeSystemDescription) throws UIMAException, IOException { - JCas jCas = createJCas(typeSystemDescription); - CasIOUtil.readJCas(jCas, new File(fileName)); - return jCas; + return CasFactory.createCas(fileName, typeSystemDescription).getJCas(); } }
