Merge pull request #22 from apache/feature/UIMA-5820-Add-CasFactory

[UIMA-5820] Add CasFactory
diff --git a/uimafit-core/src/main/java/org/apache/uima/fit/factory/CasFactory.java b/uimafit-core/src/main/java/org/apache/uima/fit/factory/CasFactory.java
new file mode 100644
index 0000000..5c57b35
--- /dev/null
+++ b/uimafit-core/src/main/java/org/apache/uima/fit/factory/CasFactory.java
@@ -0,0 +1,172 @@
+/*
+ * 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.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 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.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 CAS} objects.
+ */
+public final class CasFactory {
+  private CasFactory() {
+    // This class is not meant to be instantiated
+  }
+
+  /**
+   * 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 CAS.
+   * @return a new CAS
+   * @throws UIMAException
+   *           if the CAS could not be initialized
+   */
+  public static CAS createText(String aText) throws UIMAException {
+    return createText(aText, null);
+  }
+
+  /**
+   * 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 CAS.
+   * @param aLanguage 
+   *          the document language to be set in the new CAS.
+   * @return a new CAS
+   * @throws UIMAException
+   *           if the CAS could not be initialized
+   */
+  public static CAS createText(String aText, String aLanguage) throws UIMAException {
+    CAS cas = createCas();
+    if (aText != null) {
+      cas.setDocumentText(aText);
+    }
+    if (aLanguage != null) {
+      cas.setDocumentLanguage(aLanguage);
+    }
+    return cas;
+  }
+  
+  /**
+   * 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 CAS
+   * @throws UIMAException
+   *           if the CAS could not be initialized
+   */
+  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);
+  }
+
+  /**
+   * 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 CAS (in
+   *          Java notation, e.g. "my.package.TypeSystem" without the ".xml" extension)
+   * @return a new CAS
+   * @throws UIMAException
+   *           if the CAS could not be initialized
+   */
+  public static CAS createCas(String... typeSystemDescriptorNames) throws UIMAException {
+    return CasCreationUtils.createCas(createTypeSystemDescription(typeSystemDescriptorNames), null,
+            null);
+  }
+
+  /**
+   * 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 CAS
+   * @throws UIMAException
+   *           if the CAS could not be initialized
+   */
+  public static CAS createCasFromPath(String... typeSystemDescriptorPaths) throws UIMAException {
+    return createCas(createTypeSystemDescriptionFromPath(typeSystemDescriptorPaths));
+  }
+
+  /**
+   * 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 CAS
+   * @return a new CAS
+   * @throws UIMAException
+   *           if the CAS could not be initialized
+   */
+  public static CAS createCas(TypeSystemDescription typeSystemDescription) throws UIMAException {
+    return CasCreationUtils.createCas(typeSystemDescription, null, null);
+  }
+
+  /**
+   * 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 UIMAException
+   *           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 UIMAException, IOException {
+    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 @@
    *           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 @@
    *           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 @@
    *           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 @@
    *           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 @@
    *           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 @@
    *           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 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();
   }
 }