Author: pkluegl
Date: Fri Jul  6 11:39:31 2012
New Revision: 1358135

URL: http://svn.apache.org/viewvc?rev=1358135&view=rev
Log:
UIMA-2415
added (adapted) changes provided in attached files: loading from input streams

Modified:
    
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
    
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/DefaultEngineLoader.java
    
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/TextMarkerEngineLoader.java

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java?rev=1358135&r1=1358134&r2=1358135&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/engine/TextMarkerEngine.java
 Fri Jul  6 11:39:31 2012
@@ -20,7 +20,9 @@
 package org.apache.uima.textmarker.engine;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -30,6 +32,7 @@ import java.util.Map;
 import java.util.Set;
 
 import org.antlr.runtime.ANTLRFileStream;
+import org.antlr.runtime.ANTLRInputStream;
 import org.antlr.runtime.CharStream;
 import org.antlr.runtime.CommonTokenStream;
 import org.antlr.runtime.RecognitionException;
@@ -116,7 +119,7 @@ public class TextMarkerEngine extends JC
   public static final String RELOAD_SCRIPT = "reloadScript";
 
   public static final String LOW_MEMORY_PROFILE = "lowMemoryProfile";
-  
+
   private String[] seeders;
 
   private Boolean createDebugInfo;
@@ -170,7 +173,7 @@ public class TextMarkerEngine extends JC
   private Boolean lowMemoryProfile;
 
   private boolean initialized = false;
-  
+
   private List<Type> seedTypes;
 
   @Override
@@ -202,7 +205,7 @@ public class TextMarkerEngine extends JC
     dynamicAnchoring = (Boolean) 
aContext.getConfigParameterValue(DYNAMIC_ANCHORING);
     reloadScript = (Boolean) aContext.getConfigParameterValue(RELOAD_SCRIPT);
     lowMemoryProfile = (Boolean) 
aContext.getConfigParameterValue(LOW_MEMORY_PROFILE);
-    
+
     removeBasics = removeBasics == null ? false : removeBasics;
     createDebugInfo = createDebugInfo == null ? false : createDebugInfo;
     createDebugOnlyFor = createDebugOnlyFor == null ? new String[0] : 
createDebugOnlyFor;
@@ -215,7 +218,7 @@ public class TextMarkerEngine extends JC
     dynamicAnchoring = dynamicAnchoring == null ? false : dynamicAnchoring;
     reloadScript = reloadScript == null ? false : reloadScript;
     lowMemoryProfile = lowMemoryProfile == null ? false : lowMemoryProfile;
-    
+
     this.context = aContext;
 
     factory = new TextMarkerExternalFactory();
@@ -385,7 +388,7 @@ public class TextMarkerEngine extends JC
     Type basicType = typeSystem.getType(BASIC_TYPE);
     seedTypes = seedAnnotations(cas);
     TextMarkerStream stream = new TextMarkerStream(cas, basicType, filter, 
lowMemoryProfile);
-    
+
     stream.initalizeBasics();
     return stream;
   }
@@ -418,26 +421,75 @@ public class TextMarkerEngine extends JC
   }
 
   private void initializeScript() throws AnalysisEngineProcessException {
-    String scriptLocation = locate(mainScript, scriptPaths, ".tm");
-    if (scriptLocation == null) {
-      // if someone loads an empty analysis engine and then reconfigures it
+    if(mainScript == null) {
       return;
     }
-    try {
-      script = loadScript(scriptLocation, null);
-    } catch (Exception e) {
-      throw new AnalysisEngineProcessException(e);
+    String scriptLocation = locate(mainScript, scriptPaths, ".tm");
+    if (scriptLocation == null) {
+      try {
+        String mainScriptPath = mainScript.replaceAll("\\.", "/") + ".tm";
+        script = loadScriptIS(mainScriptPath, null);
+      } catch (IOException e) {
+        throw new AnalysisEngineProcessException(new 
FileNotFoundException("Script [" + mainScript
+                + "] cannot be found at [" + collectionToString(scriptPaths)
+                + "] with extension .tm"));
+      } catch (RecognitionException e) {
+        throw new AnalysisEngineProcessException(new 
FileNotFoundException("Script [" + mainScript
+                + "] cannot be found at [" + collectionToString(scriptPaths)
+                + "] with extension .tm"));
+      }
+    } else {
+      try {
+        script = loadScript(scriptLocation, null);
+      } catch (Exception e) {
+        throw new AnalysisEngineProcessException(e);
+      }
     }
+
     Map<String, TextMarkerModule> additionalScripts = new HashMap<String, 
TextMarkerModule>();
     Map<String, AnalysisEngine> additionalEngines = new HashMap<String, 
AnalysisEngine>();
 
     if (additionalEngineLocations != null) {
       for (String eachEngineLocation : additionalEngineLocations) {
+        AnalysisEngine eachEngine;
         String location = locate(eachEngineLocation, descriptorPaths, ".xml");
+        if (location == null) {
+          String locationIS = locateIS(eachEngineLocation, descriptorPaths, 
".xml");
+          try {
+            eachEngine = engineLoader.loadEngineIS(locationIS);
+          } catch (InvalidXMLException e) {
+            throw new AnalysisEngineProcessException(new 
FileNotFoundException("Engine at ["
+                    + eachEngineLocation + "] cannot be found in ["
+                    + collectionToString(descriptorPaths)
+                    + "] with extension .xml (from mainScript=" + mainScript + 
" in "
+                    + collectionToString(scriptPaths)));
+          } catch (ResourceInitializationException e) {
+            throw new AnalysisEngineProcessException(new 
FileNotFoundException("Engine at ["
+                    + eachEngineLocation + "] cannot be found in ["
+                    + collectionToString(descriptorPaths)
+                    + "] with extension .xml (from mainScript=" + mainScript + 
" in "
+                    + collectionToString(scriptPaths)));
+          } catch (IOException e) {
+            throw new AnalysisEngineProcessException(new 
FileNotFoundException("Engine at ["
+                    + eachEngineLocation + "] cannot be found in ["
+                    + collectionToString(descriptorPaths)
+                    + "] with extension .xml (from mainScript=" + mainScript + 
" in "
+                    + collectionToString(scriptPaths)));
+          }
+        } else {
+          try {
+            eachEngine = engineLoader.loadEngine(location);
+          } catch (Exception e) {
+            throw new AnalysisEngineProcessException(e);
+          }
+        }
         try {
-          AnalysisEngine eachEngine = engineLoader.loadEngine(location);
-          configureEngine(eachEngine);
           additionalEngines.put(eachEngineLocation, eachEngine);
+          String[] eachEngineLocationPartArray = 
eachEngineLocation.split("\\.");
+          if (eachEngineLocationPartArray.length > 1) {
+            String shortEachEngineLocation = 
eachEngineLocationPartArray[eachEngineLocationPartArray.length - 1];
+            additionalEngines.put(shortEachEngineLocation, eachEngine);
+          }
         } catch (Exception e) {
           throw new AnalysisEngineProcessException(e);
         }
@@ -449,6 +501,7 @@ public class TextMarkerEngine extends JC
         recursiveLoadScript(add, additionalScripts, additionalEngines);
       }
     }
+
     for (TextMarkerModule each : additionalScripts.values()) {
       each.setScriptDependencies(additionalScripts);
     }
@@ -460,6 +513,7 @@ public class TextMarkerEngine extends JC
     script.setEngineDependencies(additionalEngines);
   }
 
+
   private void configureEngine(AnalysisEngine engine) throws 
ResourceConfigurationException {
     ConfigurationParameterDeclarations configurationParameterDeclarations = 
engine
             
.getAnalysisEngineMetaData().getConfigurationParameterDeclarations();
@@ -511,6 +565,10 @@ public class TextMarkerEngine extends JC
     return locate(name, paths, suffix, true);
   }
 
+  public static String locateIS(String name, String[] paths, String suffix) {
+    return locateIS(name, paths, suffix, true);
+  }
+
   public static String locate(String name, String[] paths, String suffix, 
boolean mustExist) {
     if (name == null || paths == null) {
       return null;
@@ -525,6 +583,14 @@ public class TextMarkerEngine extends JC
     return null;
   }
 
+  public static String locateIS(String name, String[] paths, String suffix, 
boolean mustExist) {
+    if (name == null) {
+      return null;
+    }
+    name = name.replaceAll("[.]", "/");
+    return name + suffix;
+  }
+
   private void recursiveLoadScript(String toLoad, Map<String, 
TextMarkerModule> additionalScripts,
           Map<String, AnalysisEngine> additionalEngines) throws 
AnalysisEngineProcessException {
     String location = locate(toLoad, scriptPaths, ".tm");
@@ -601,6 +667,23 @@ public class TextMarkerEngine extends JC
     return script;
   }
 
+  private TextMarkerModule loadScriptIS(String scriptLocation,
+          TypeSystemDescription localTSD) throws IOException, 
RecognitionException {
+    InputStream scriptInputStream = 
getClass().getClassLoader().getResourceAsStream(scriptLocation);
+    CharStream st = new ANTLRInputStream(scriptInputStream, scriptEncoding);
+    TextMarkerLexer lexer = new TextMarkerLexer(st);
+    CommonTokenStream tokens = new CommonTokenStream(lexer);
+    TextMarkerParser parser = new TextMarkerParser(tokens);
+    parser.setLocalTSD(localTSD);
+    parser.setExternalFactory(factory);
+    parser.setResourcePaths(resourcePaths);
+    String name = scriptLocation;
+    int lastIndexOf = name.lastIndexOf(".tm");
+    name = name.substring(0, lastIndexOf);
+    TextMarkerModule script = parser.file_input(name);
+    return script;
+  }
+
   public TextMarkerExternalFactory getFactory() {
     return factory;
   }
@@ -608,5 +691,17 @@ public class TextMarkerEngine extends JC
   public TextMarkerEngineLoader getEngineLoader() {
     return engineLoader;
   }
+  private String collectionToString(Collection collection) {
+    StringBuilder collectionSB = new StringBuilder();
+    collectionSB.append("{");
+    for (Object element : collection) {
+      collectionSB.append("[").append(element.toString()).append("]");
+    }
+    collectionSB.append("}");
+    return collectionSB.toString();
+  }
 
+  private String collectionToString(Object[] collection) {
+    return collectionToString(Arrays.asList(collection));
+  }
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/DefaultEngineLoader.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/DefaultEngineLoader.java?rev=1358135&r1=1358134&r2=1358135&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/DefaultEngineLoader.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/DefaultEngineLoader.java
 Fri Jul  6 11:39:31 2012
@@ -15,11 +15,12 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
-*/
+ */
 
 package org.apache.uima.textmarker.extensions;
 
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.uima.UIMAFramework;
 import org.apache.uima.analysis_engine.AnalysisEngine;
@@ -42,4 +43,13 @@ public abstract class DefaultEngineLoade
     AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
     return ae;
   }
+
+  protected AnalysisEngine loadEngineMyselfIS(String location) throws 
IOException,
+          InvalidXMLException, ResourceInitializationException {
+    InputStream locationIS = 
getClass().getClassLoader().getResourceAsStream(location);
+    XMLInputSource in = new XMLInputSource(locationIS, null);
+    ResourceSpecifier specifier = 
UIMAFramework.getXMLParser().parseResourceSpecifier(in);
+    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
+    return ae;
+  }
 }

Modified: 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/TextMarkerEngineLoader.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/TextMarkerEngineLoader.java?rev=1358135&r1=1358134&r2=1358135&view=diff
==============================================================================
--- 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/TextMarkerEngineLoader.java
 (original)
+++ 
uima/sandbox/trunk/TextMarker/uimaj-textmarker/src/main/java/org/apache/uima/textmarker/extensions/TextMarkerEngineLoader.java
 Fri Jul  6 11:39:31 2012
@@ -15,7 +15,7 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
-*/
+ */
 
 package org.apache.uima.textmarker.extensions;
 
@@ -51,17 +51,40 @@ public class TextMarkerEngineLoader exte
     return result;
   }
 
+  public AnalysisEngine loadEngineIS(String location) throws 
InvalidXMLException,
+          ResourceInitializationException, IOException {
+    String name = getEngineNameIS(location);
+    AnalysisEngine result = null;
+    IEngineLoader engineLoader = loaders.get(name);
+    if (engineLoader != null) {
+      result = engineLoader.loadEngine(location);
+    } else {
+      result = loadEngineMyselfIS(location);
+    }
+    return result;
+  }
+
   public void addLoader(String engine, IEngineLoader loader) {
     loaders.put(engine, loader);
   }
 
   private String getEngineName(String location) {
-    File file = new File(location);
+    File file;
+    try {
+      file = new File(location);
+    } catch (java.lang.NullPointerException e) {
+      throw new java.lang.NullPointerException("File[" + location + "] cannot 
be opened.");
+    }
     location = file.getName();
     String[] split = location.split("[.]");
     return split[split.length - 2];
   }
 
+  private String getEngineNameIS(String location) {
+    String[] split = location.split("[.]");
+    return split[split.length - 2];
+  }
+
   public boolean isInitialized() {
     return !loaders.isEmpty();
   }


Reply via email to