View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.util;
20  
21  /**
22   * This class contains utility methods to detect special cases to be handled on "script" or
23   * "style" tags by HtmlResponseWriterImpl.
24   */
25  public class CommentUtils
26  {
27      public static final String INLINE_SCRIPT_COMMENT = "//";
28      public static final String START_SCRIPT_COMMENT = "/*";
29      public static final String END_SCRIPT_COMMENT = "*/";
30      public static final String CDATA_SIMPLE_START = "<![CDATA[";
31      public static final String CDATA_SIMPLE_END = "]]>";
32      public static final String COMMENT_SIMPLE_START = "<!--";
33      public static final String COMMENT_SIMPLE_END = "-->";
34  
35      public static boolean isStartMatchWithCommentedCDATA(String trimmedContent)
36      {
37          if (trimmedContent.startsWith(START_SCRIPT_COMMENT))
38          {
39              int offset = 2;
40              while (trimmedContent.charAt(offset) <= ' ')
41              {
42                  offset++;
43              }
44              if (trimmedContent.startsWith(CDATA_SIMPLE_START, offset))
45              {
46                  return true;
47              }
48          }
49          return false;
50      }
51      
52      public static boolean isEndMatchWithCommentedCDATA(String trimmedContent)
53      {
54          if (trimmedContent.endsWith(END_SCRIPT_COMMENT))
55          {
56              int offset = trimmedContent.length()-3;
57              while (trimmedContent.charAt(offset) <= ' ')
58              {
59                  offset--;
60              }
61              // CDATA_SIMPLE_END.length() = 3
62              // So, -3 +1 = -2
63              if (trimmedContent.startsWith(CDATA_SIMPLE_END, offset - 2))
64              {
65                  return true;
66              }
67          }
68          return false;
69      }
70  
71      public static boolean isEndMatchtWithInlineCommentedXmlCommentTag(String trimmedContent)
72      {
73          if (trimmedContent.endsWith(COMMENT_SIMPLE_END))
74          {
75              int offset = trimmedContent.length()-4;
76              while (trimmedContent.charAt(offset) <= ' ' &&
77                      trimmedContent.charAt(offset) != '\n')
78              {
79                  offset--;
80              }
81              // INLINE_SCRIPT_COMMENT.length() = 2
82              // So, -2 +1 = -1
83              if (trimmedContent.startsWith(INLINE_SCRIPT_COMMENT, offset - 1))
84              {
85                  return true;
86              }
87          }
88          return false;        
89      }
90      
91      public static boolean isStartMatchWithInlineCommentedCDATA(String trimmedContent)
92      {
93          if (trimmedContent.startsWith(INLINE_SCRIPT_COMMENT))
94          {
95              int offset = 2;
96              while (trimmedContent.charAt(offset) <= ' ' &&
97                      trimmedContent.charAt(offset) != '\n')
98              {
99                  offset++;
100             }
101             if (trimmedContent.startsWith(CDATA_SIMPLE_START, offset))
102             {
103                 return true;
104             }
105         }
106         return false;
107     }
108     
109     public static boolean isEndMatchWithInlineCommentedCDATA(String trimmedContent)
110     {
111         if (trimmedContent.endsWith(CDATA_SIMPLE_END))
112         {
113             int offset = trimmedContent.length()- 4;
114             while (trimmedContent.charAt(offset) <= ' ' &&
115                     trimmedContent.charAt(offset) != '\n')
116             {
117                 offset--;
118             }
119             // INLINE_SCRIPT_COMMENT.length() = 2
120             // So, -2 +1 = -1
121             if (trimmedContent.startsWith(INLINE_SCRIPT_COMMENT, offset - 1))
122             {
123                 return true;
124             }
125         }
126         return false;
127     }
128 }