Work around StackOverflowError in prettify parser

Ideally we would not use a library that has the potential to overflow
the stack, especially when it can't even handle a copy of the
GPLv2(!).

Change-Id: If1fcc02c20689af20a7ccf9c301a17fd5b96b66b
diff --git a/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java b/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java
index 9d02ac6..02cb54c 100644
--- a/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java
+++ b/gitiles-servlet/src/main/java/com/google/gitiles/BlobSoyData.java
@@ -18,6 +18,7 @@
 import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
 
 import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Maps;
 import com.google.template.soy.data.SoyListData;
 import com.google.template.soy.data.SoyMapData;
@@ -30,6 +31,8 @@
 import org.eclipse.jgit.lib.ObjectLoader;
 import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.util.RawParseUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import prettify.PrettifyParser;
 import prettify.parser.Prettify;
@@ -41,6 +44,8 @@
 
 /** Soy data converter for git blobs. */
 public class BlobSoyData {
+  private static final Logger log = LoggerFactory.getLogger(BlobSoyData.class);
+
   /**
    * Maximum number of bytes to load from a supposed text file for display.
    * Files larger than this will be displayed as binary files, even if the
@@ -92,8 +97,8 @@
     return data;
   }
 
-  private static SoyListData prettify(String path, String content) {
-    List<ParseResult> results = new PrettifyParser().parse(extension(path, content), content);
+  private SoyListData prettify(String path, String content) {
+    List<ParseResult> results = parse(path, content);
     SoyListData lines = new SoyListData();
     SoyListData line = new SoyListData();
     lines.add(line);
@@ -112,6 +117,18 @@
     return lines;
   }
 
+  private List<ParseResult> parse(String path, String content) {
+    try {
+      return new PrettifyParser().parse(extension(path, content), content);
+    } catch (StackOverflowError e) {
+      // TODO(dborowitz): Aaagh. Make prettify use RE2. Or replace it something
+      // else. Or something.
+      log.warn("StackOverflowError prettifying " + view.toUrl());
+      return ImmutableList.of(
+          new ParseResult(0, content.length(), ImmutableList.of(Prettify.PR_PLAIN)));
+    }
+  }
+
   private static SoyListData writeResult(SoyListData lines, String classes,
       String s, int start, int end) {
     SoyListData line = lines.getListData(lines.length() - 1);