1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.portletbridge.portlet;
17  
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import junit.framework.TestCase;
22  
23  public class RewriterTest extends TestCase {
24      public void testOrs() {
25          Pattern urlPattern = Pattern
26                  .compile("(url//((?:'|\")?)(.*?)((?:'|\")?//))|(@import//s+[^url](?:'|\")?)(.*?)((?:'|\")|;|//s+|$)");
27          Matcher matcher = urlPattern.matcher("" + "@import 'one';\n"
28                  + "@import(url('two');\n" + "url('three');\n");
29          StringBuffer sb = new StringBuffer();
30          while (matcher.find()) {
31              String before = matcher.group(1);
32              String url = matcher.group(2);
33              String after = matcher.group(3);
34              matcher.appendReplacement(sb, before + "MATCHED_URL(" + url + ")"
35                      + after);
36          }
37          matcher.appendTail(sb);
38          assertEquals("@import 'MATCHED_URL(one)';\n" +
39              "@import(url('MATCHED_URL(two)');\n" + 
40              "url('MATCHED_URL(three)');\n", sb.toString());
41      }
42      public void testImport() {
43          Pattern urlPattern = Pattern
44          .compile("(@import//s+[^url](?:'|\")?)(.*?)((?:'|\")|;|//s+|$)");
45          String css = "@import('one');";        
46          Matcher matcher = urlPattern.matcher(css);
47          StringBuffer sb = new StringBuffer();
48          while (matcher.find()) {
49              String before = matcher.group(1);
50              String url = matcher.group(2);
51              String after = matcher.group(3);
52              matcher.appendReplacement(sb, before + "MATCHED_URL(" + url + ")"
53                      + after);
54          }
55          matcher.appendTail(sb);
56          assertEquals("@import 'MATCHED_URL(one)' ;", sb.toString());
57      }
58      
59      public void testCssRewriter() {
60          Pattern urlPattern = Pattern
61          .compile("(?:url//((?:'|\")?(.*?)(?:'|\")?//))|(?:@import//s+[^url](?:'|\")?(.*?)(?:'|\")|;|//s+|$)");
62          String string = "" + "@import 'one';\n"
63                  + "@import url('two');\n" + "url('three');\n";
64          Matcher matcher = urlPattern.matcher(string);
65          StringBuffer sb = new StringBuffer();
66          matcher.find();
67          do {
68              int group = extractGroup(matcher);
69              if(group > 0) {
70                  String before = string.substring(matcher.start(), matcher.start(group));
71                  String url = matcher.group(group);
72                  String after = string.substring(matcher.end(group), matcher.end());
73                  System.out.println(before + "[" + url + "]" + after);
74                  matcher.appendReplacement(sb, before + "MATCHED_URL(" + url + ")" + after);
75              }
76          } while (matcher.find());
77          matcher.appendTail(sb);
78          assertEquals("@import 'MATCHED_URL(one)';\n"
79                  + "@import url('MATCHED_URL(two))';\n"
80                  + "url('MATCHED_URL(three)');\n", sb.toString());
81      }
82      
83      public void testMatching() {
84          String string = "123456";
85          Matcher matcher = Pattern.compile("4(5)6|.(.).").matcher(string);
86          assertEquals(2, matcher.groupCount());
87          matcher.find();
88          int matchingGroup = extractGroup(matcher);
89          assertEquals(0, matcher.start());
90          assertEquals(3, matcher.end());
91          assertEquals("123", string.substring(matcher.start(), matcher.end()));
92          assertEquals(1, matcher.start(matchingGroup));
93          assertEquals(2, matcher.end(matchingGroup));
94          matcher.find(matcher.end());
95          for(int i = 1; i <= matcher.groupCount(); i++) {
96              if(matcher.start(i) > -1) {
97                  matchingGroup = i;
98                  break;
99              }
100         }
101         assertEquals(3, matcher.start());
102         assertEquals(6, matcher.end());
103         assertEquals("456", string.substring(matcher.start(), matcher.end()));
104         assertEquals(4, matcher.start(matchingGroup));
105         assertEquals(5, matcher.end(matchingGroup));
106     }
107     
108     private int extractGroup(Matcher matcher) {
109         int matchingGroup = -1;
110         for(int i = 1; i <= matcher.groupCount(); i++) {
111             if(matcher.start(i) > -1) {
112                 matchingGroup = i;
113                 break;
114             }
115         }
116         return matchingGroup;
117     }
118 }