View Javadoc

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.rewriter;
17  
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import org.portletbridge.PortletBridgeException;
22  import org.portletbridge.StyleSheetRewriter;
23  import org.portletbridge.UrlRewriter;
24  
25  /***
26   * @author JMcCrindle
27   */
28  public class RegExStyleSheetRewriter implements StyleSheetRewriter {
29  
30      private UrlRewriter urlRewriter = null;
31      private Pattern urlPattern = Pattern.compile("(url//((?:'|\")?)(.*?)((?:'|\")?//))");
32      private Pattern importPattern = Pattern.compile("(@import//s+[^url](?:'|\")?)(.*?)((?:'|\")|;|//s+|$)");
33      
34      /***
35       * @param urlRewriter
36       */
37      public RegExStyleSheetRewriter(UrlRewriter urlRewriter) {
38          this.urlRewriter = urlRewriter;
39      }
40  
41      /* (non-Javadoc)
42       * @see org.portletbridge.StyleSheetRewriter#rewrite(java.lang.String)
43       */
44      public StringBuffer rewriteUrls(StringBuffer css) throws PortletBridgeException {
45          if(css == null) return null;
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 + urlRewriter.rewrite(url) + after);
53          }
54          matcher.appendTail(sb);
55          return sb;
56      }
57  
58      /* (non-Javadoc)
59       * @see org.portletbridge.StyleSheetRewriter#rewrite(java.lang.String)
60       */
61      public StringBuffer rewriteImports(StringBuffer css) throws PortletBridgeException {
62          if(css == null) return null;
63          Matcher matcher = importPattern.matcher(css);
64          StringBuffer sb = new StringBuffer();
65          while (matcher.find()) {
66              String before = matcher.group(1);
67              String url = matcher.group(2);
68              String after = matcher.groupCount() == 3 ? matcher.group(3) : "";
69              matcher.appendReplacement(sb, before + urlRewriter.rewrite(url) + after);
70          }
71          matcher.appendTail(sb);
72          return sb;
73      }
74  
75      /* (non-Javadoc)
76       * @see org.portletbridge.StyleSheetRewriter#rewrite(java.lang.String)
77       */
78      public String rewrite(String css) throws PortletBridgeException {
79          return rewriteUrls(rewriteImports(new StringBuffer(css))).toString();
80      }
81  
82  }