tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r909869 - /tomcat/trunk/java/org/apache/jasper/JspC.java
Date Sat, 13 Feb 2010 18:25:07 GMT
Author: markt
Date: Sat Feb 13 18:25:07 2010
New Revision: 909869

URL: http://svn.apache.org/viewvc?rev=909869&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=48371
Skip comments when working out where to insert generated servlets and associated mappings

Modified:
    tomcat/trunk/java/org/apache/jasper/JspC.java

Modified: tomcat/trunk/java/org/apache/jasper/JspC.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/JspC.java?rev=909869&r1=909868&r2=909869&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/JspC.java (original)
+++ tomcat/trunk/java/org/apache/jasper/JspC.java Sat Feb 13 18:25:07 2010
@@ -27,15 +27,18 @@
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.io.Reader;
 import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Set;
 import java.util.Stack;
 import java.util.StringTokenizer;
 import java.util.Vector;
@@ -123,20 +126,31 @@
     protected static final String SWITCH_ENCODING = "-javaEncoding";
     protected static final String SWITCH_SMAP = "-smap";
     protected static final String SWITCH_DUMP_SMAP = "-dumpsmap";
-
     protected static final String SHOW_SUCCESS ="-s";
     protected static final String LIST_ERRORS = "-l";
     protected static final int INC_WEBXML = 10;
     protected static final int ALL_WEBXML = 20;
     protected static final int DEFAULT_DIE_LEVEL = 1;
     protected static final int NO_DIE_LEVEL = 0;
-
-    protected static final String[] insertBefore =
-    { "</web-app>", "<servlet-mapping>", "<session-config>",
-      "<mime-mapping>", "<welcome-file-list>", "<error-page>", "<taglib>",
-      "<resource-env-ref>", "<resource-ref>", "<security-constraint>",
-      "<login-config>", "<security-role>", "<env-entry>", "<ejb-ref>",
-      "<ejb-local-ref>" };
+    protected static final Set<String> insertBefore = new HashSet<String>();
+    
+    static {
+        insertBefore.add("</web-app>");
+        insertBefore.add("<servlet-mapping>");
+        insertBefore.add("<session-config>");
+        insertBefore.add("<mime-mapping>");
+        insertBefore.add("<welcome-file-list>");
+        insertBefore.add("<error-page>");
+        insertBefore.add("<taglib>");
+        insertBefore.add("<resource-env-ref>");
+        insertBefore.add("<resource-ref>");
+        insertBefore.add("<security-constraint>");
+        insertBefore.add("<login-config>");
+        insertBefore.add("<security-role>");
+        insertBefore.add("<env-entry>");
+        insertBefore.add("<ejb-ref>");
+        insertBefore.add("<ejb-local-ref>");
+    }
 
     protected static int die;
     protected String classPath = null;
@@ -864,66 +878,37 @@
         PrintWriter writer = new PrintWriter(new FileWriter(webXml2));
 
         // Insert the <servlet> and <servlet-mapping> declarations
-        int pos = -1;
-        String line = null;
-        while (true) {
-            line = reader.readLine();
-            if (line == null) {
-                break;
-            }
-            // Skip anything previously generated by JSPC
-            if (line.indexOf(insertStartMarker) >= 0) {
-                while (true) {
-                    line = reader.readLine();
-                    if (line == null) {
-                        return;
-                    }
-                    if (line.indexOf(insertEndMarker) >= 0) {
-                        line = reader.readLine();
-                        line = reader.readLine();
+        boolean inserted = false;
+        int current = reader.read();
+        while (current > -1) {
+            if (!inserted && current == '<') {
+                String element = getElement(reader);
+                if (insertBefore.contains(element)) {
+                    // Insert generated content here
+                    writer.println(insertStartMarker);
+                    while (true) {
+                        String line = fragmentReader.readLine();
                         if (line == null) {
-                            return;
+                            writer.println();
+                            break;
                         }
-                        break;
+                        writer.println(line);
+                    }
+                    writer.println(insertEndMarker);
+                    writer.println();
+                    writer.write(element);
+                } else if (element.contains(insertStartMarker)) {
+                    // Skip the previous auto-generated content
+                    while (!element.contains(insertEndMarker)) {
+                        element = getElement(reader);
                     }
+                } else {
+                    writer.write(element);
                 }
-            }
-            for (int i = 0; i < insertBefore.length; i++) {
-                pos = line.indexOf(insertBefore[i]);
-                if (pos >= 0)
-                    break;
-            }
-            if (pos >= 0) {
-                writer.print(line.substring(0, pos));
-                break;
             } else {
-                writer.println(line);
-            }
-        }
-
-        writer.println(insertStartMarker);
-        while (true) {
-            String line2 = fragmentReader.readLine();
-            if (line2 == null) {
-                writer.println();
-                break;
+                writer.write(current);
             }
-            writer.println(line2);
-        }
-        writer.println(insertEndMarker);
-        writer.println();
-
-        for (int i = 0; i < pos; i++) {
-            writer.print(" ");
-        }
-        writer.println(line.substring(pos));
-
-        while (true) {
-            line = reader.readLine();
-            if (line == null) {
-                break;
-            }
-            writer.println(line);
+            current = reader.read();
         }
         writer.close();
 
@@ -949,6 +934,38 @@
         (new File(webxmlFile)).delete();
 
     }
+    
+    /*
+     * Assumes valid xml
+     */
+    private String getElement(Reader reader) throws IOException {
+        StringBuilder result = new StringBuilder();
+        result.append('<');
+        
+        boolean done = false;
+        
+        while (!done) {
+            int current = reader.read();
+            while (current != '>') {
+                result.append((char) current);
+                current = reader.read();
+            }
+            result.append((char) current);
+            
+            int len = result.length();
+            if (len > 7 && result.substring(0, 4).equals("<!--")) {
+                // This is a comment - make sure we are at the end
+                if (result.substring(len - 3, len).equals("-->")) {
+                    done = true;
+                }
+            } else {
+                done = true;
+            }
+        }
+        
+        
+        return result.toString();
+    }
 
     protected void processFile(String file)
         throws JasperException



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message