You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by al...@apache.org on 2007/01/31 18:16:23 UTC

svn commit: r501907 - /incubator/uima/uimaj/trunk/uimaj-tools/src/main/java/org/apache/uima/tools/migration/IbmUimaToApacheUima.java

Author: alally
Date: Wed Jan 31 09:16:21 2007
New Revision: 501907

URL: http://svn.apache.org/viewvc?view=rev&rev=501907
Log:
Added user feedback to migration tool.
UIMA-241: https://issues.apache.org/jira/browse/UIMA-241

Modified:
    incubator/uima/uimaj/trunk/uimaj-tools/src/main/java/org/apache/uima/tools/migration/IbmUimaToApacheUima.java

Modified: incubator/uima/uimaj/trunk/uimaj-tools/src/main/java/org/apache/uima/tools/migration/IbmUimaToApacheUima.java
URL: http://svn.apache.org/viewvc/incubator/uima/uimaj/trunk/uimaj-tools/src/main/java/org/apache/uima/tools/migration/IbmUimaToApacheUima.java?view=diff&rev=501907&r1=501906&r2=501907
==============================================================================
--- incubator/uima/uimaj/trunk/uimaj-tools/src/main/java/org/apache/uima/tools/migration/IbmUimaToApacheUima.java (original)
+++ incubator/uima/uimaj/trunk/uimaj-tools/src/main/java/org/apache/uima/tools/migration/IbmUimaToApacheUima.java Wed Jan 31 09:16:21 2007
@@ -46,12 +46,18 @@
   private static List replacements = new ArrayList();
   private static int MAX_FILE_SIZE = 1000000; //don't update files bigger than this
   private static Set extensions = new HashSet();
+  private static int filesScanned = 0;
+  private static int filesModified = 0;
+  private static List filesNeedingManualAttention = new ArrayList();
+  private static Set ibmPackageNames = new HashSet();
+
   private static final Pattern IMPORT_PATTERN = Pattern.compile("(?m)^\\s*import\\s+([^;]*);\\s*$");
   private static final Pattern CLASS_NAME_PATTERN = Pattern.compile("public\\s+(final\\s+|abstract\\s+)*class\\s+([A-Za-z0-9_]+)");
   private static final Pattern GET_NEXT_INDEX_PATTERN = Pattern.compile("JCas\\.getNextIndex\\(\\)");
   private static final Pattern THROW_FEAT_MISSING_PATTERN = Pattern.compile("JCas\\.throwFeatMissing");
+  private static final Pattern PACKAGE_PATTERN = Pattern.compile("(?m)^\\s*package\\s+([A-Za-z0-9_.]+)\\s*;") ;
+  private static final Pattern GETDOCANNOT_PATTERN = Pattern.compile("[Jj][Cc][Aa][Ss](\\(\\))?\\s*\\.\\s*getDocumentAnnotation\\(");
 
-  
   /**
    * Main program.  Expects one argument, the name of a directory containing files to
    * update.  Subdirectories are processed recursively.   
@@ -96,7 +102,25 @@
     //from system property, get list of file extensions to exclude
     
     //do the replacements
+    System.out.println("Migrating your files...");
     replaceInAllFiles(new File(args[0]));
+    
+    System.out.println("Migration complete.");
+    System.out.println("Scanned " + filesScanned + " files.  " + filesModified + " files modified.");
+    if (filesNeedingManualAttention.size() > 0) {
+      System.out.println("The following files may need manual attention:");
+      for (int i = 0; i < filesNeedingManualAttention.size(); i++) {
+        System.out.println("   " + filesNeedingManualAttention.get(i));
+      }
+      System.out.println("See the \"Migrating from IBM UIMA to Apache UIMA\" chapter in the " +
+              "\"UIMA Overview and Setup\" document for details.");
+    }
+    else {
+      System.out.println("No problems were detected.  However, if the code does not compilie " +
+              "and run, see the \"Migrating from IBM UIMA to Apache UIMA\" chapter in the " +
+              "\"UIMA Overview and Setup\" document for assistance.");
+      
+    }
   }
 
   /**
@@ -208,9 +232,16 @@
     //write file if it changed
     if (!contents.equals(original)) {
       FileUtils.saveString2File(contents, file);
+      filesModified++;
     }
+    filesScanned++;
+    
+    //check for situations that may need manual attention,
+    //updates filesNeedingManualAttention field
+    checkForManualAttentionNeeded(file, original);
+    
   }
-  
+
   /**
    * Applies changes needed due to JCas/JCasRegistry refactoring.  These are a little
    * more complicated than simple regex replacements.
@@ -275,6 +306,40 @@
       return result.toString();
     }
   }
+  
+  
+  /**
+   * Scans for certain patterns in the string that indicate situations
+   * that the migration tool doesn't resolve and may require user 
+   * attention.  Updated the filesNeedingManualAttention field with a String
+   * which is the file path plus the reason the file was flagged.
+   * 
+   * @param contents string to scan
+   * @return true if the file needs manual attention
+   */
+  private static void checkForManualAttentionNeeded(File file, String contents) {
+    // UIMA package name (includes most common case of DocumentAnnotation)
+    Matcher packageNameMatcher = PACKAGE_PATTERN.matcher(contents);
+    if (packageNameMatcher.find()) {
+      String packageName = packageNameMatcher.group(1);
+      if (ibmPackageNames.contains(packageName)) {
+        filesNeedingManualAttention.add(file.getPath() + " (Uses an IBM UIMA Package Name)");
+        return;
+      }
+    }
+    //JCas.getDocumentAnnotation (fuzzy, only matches if variable name / method
+    //ends with jcas)
+    if (GETDOCANNOT_PATTERN.matcher(contents).find()) {
+      filesNeedingManualAttention.add(file.getPath() + " (Calls JCas.getDocumentAnnotation())");
+      return;
+    }
+    
+    //xi:include
+    if (contents.indexOf("<xi:include") >= 0) {
+      filesNeedingManualAttention.add(file.getPath() + " (Uses xi:include)");
+      return;
+    }    
+  }
 
   /**
    * Reads a mapping from a resource file, and populates a List of
@@ -286,7 +351,8 @@
    *   Each object contains the regex to search for and the replacement string.
    * @param treatAsPackageNames if true, the keys in the resource file will be considered
    *   package names, and this routine will produce regexes that replace any fully-qualified
-   *   class name belonging to that package.
+   *   class name belonging to that package.  Also in this case updates the
+   *   static ibmPackageNames HashSet.
    */
   private static void readMapping(String fileName, List mappings, boolean treatAsPackageNames) throws IOException {
     URL pkgListFile = IbmUimaToApacheUima.class.getResource(fileName);
@@ -308,6 +374,7 @@
         //form regex that will find any fully-qualified class name in this package
         regex = pkgRegex+"(\\.(\\*|[A-Z]\\w*))";
         replaceStr = mapping[1] + "$1";
+        ibmPackageNames.add(mapping[0]);
       }
       else {
         //form regex from src, by escaping dots and allowing whitespace