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.xsl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.portletbridge.PortletBridgeException;
22  import org.portletbridge.StyleSheetRewriter;
23  import org.portletbridge.UrlRewriter;
24  import org.xml.sax.Attributes;
25  import org.xml.sax.SAXException;
26  import org.xml.sax.XMLReader;
27  import org.xml.sax.helpers.AttributesImpl;
28  import org.xml.sax.helpers.XMLFilterImpl;
29  
30  /***
31   * This will not work with a base tag that points to a different host
32   * 
33   * @author JMcCrindle
34   */
35  public class LinkRewriterXmlFilter extends XMLFilterImpl {
36  
37      private String baseUrl = null;
38  
39      private UrlRewriter urlRewriter = null;
40      
41      private StyleSheetRewriter styleSheetRewriter = null;
42  
43      private String originalUrl;
44      
45      private Map context = new HashMap();
46      
47      private boolean inStyle = false;
48      
49      private StringBuffer styleContent = null;
50  
51      private static final Map elementsWithUrls = new HashMap();
52  
53      static {
54          elementsWithUrls.put("a", "href");
55          elementsWithUrls.put("base", "href");
56          elementsWithUrls.put("link", "href");
57          elementsWithUrls.put("area", "href");
58          elementsWithUrls.put("img", "src");
59          elementsWithUrls.put("input", "src");
60          elementsWithUrls.put("frame", "src");
61          elementsWithUrls.put("iframe", "src");
62          elementsWithUrls.put("embed", "src");
63          elementsWithUrls.put("xml", "src");
64          elementsWithUrls.put("script", "src");
65          elementsWithUrls.put("form", "action");
66      }
67  
68      /***
69       * Default Constructor
70       */
71      public LinkRewriterXmlFilter() {
72          super();
73      }
74  
75      /***
76       * @param parent
77       */
78      public LinkRewriterXmlFilter(XMLReader parent) {
79          super(parent);
80      }
81  
82      /*
83       * (non-Javadoc)
84       * 
85       * @see org.xml.sax.helpers.XMLFilterImpl#startElement(java.lang.String,
86       *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
87       */
88      public void startElement(final String uri, final String localName,
89              final String qName, final Attributes atts) throws SAXException {
90          
91          if("style".equalsIgnoreCase(localName) && !inStyle) {
92              inStyle = true;
93              styleContent = new StringBuffer();
94          } else if(inStyle) {
95              // rewrite
96              String rewrittenStyleContent;
97              try {
98                  rewrittenStyleContent = styleSheetRewriter.rewrite(styleContent.toString());
99              } catch (PortletBridgeException e) {
100                 throw new SAXException(e);
101             }
102             char[] chars = rewrittenStyleContent.toCharArray();
103             super.characters(chars, 0, chars.length);
104             inStyle = false;
105         }
106         
107         String attr = (String) elementsWithUrls.get(localName.toLowerCase());
108         if (attr != null) {
109             int urlIndex = atts.getIndex(attr);
110             if (urlIndex != -1) {
111                 final String url = atts.getValue(urlIndex);
112                 final AttributesImpl attributes = new AttributesImpl(atts);
113                 try {
114                     attributes.setAttribute(urlIndex, atts.getURI(urlIndex),
115                             atts.getLocalName(urlIndex), atts
116                                     .getQName(urlIndex),
117                             atts.getType(urlIndex), urlRewriter.rewrite(
118                                     url));
119                 } catch (PortletBridgeException e) {
120                     throw new SAXException(e);
121                 }
122                 super.startElement(uri, localName,
123                         qName, attributes);
124             } else {
125                 super.startElement(uri, localName, qName, atts);
126             }
127         } else {
128             super.startElement(uri, localName, qName, atts);
129         }
130     }
131 
132     /* (non-Javadoc)
133      * @see org.xml.sax.helpers.XMLFilterImpl#endElement(java.lang.String, java.lang.String, java.lang.String)
134      */
135     public void endElement(String uri, String localName, String qName)
136             throws SAXException {
137         if("style".equalsIgnoreCase(localName) && inStyle) {
138             String rewrittenStyleContent;
139             try {
140                 rewrittenStyleContent = styleSheetRewriter.rewrite(styleContent.toString());
141                 char[] chars = rewrittenStyleContent.toCharArray();
142                 super.characters(chars, 0, chars.length);
143                 inStyle = false;
144             } catch (PortletBridgeException e) {
145                 throw new SAXException(e);
146             }
147         }
148         super.endElement(uri, localName, qName);
149     }
150     
151     /* (non-Javadoc)
152      * @see org.xml.sax.helpers.XMLFilterImpl#characters(char[], int, int)
153      */
154     public void characters(char[] ch, int start, int length)
155             throws SAXException {
156         if(inStyle) {
157             styleContent.append(ch, start, length);
158         } else {
159             super.characters(ch, start, length);
160         }
161     }
162 
163     public void setUrlRewriter(UrlRewriter urlRewriter) {
164         this.urlRewriter = urlRewriter;
165     }
166 
167     public void setOriginalUrl(String originalUrl) {
168         this.originalUrl = originalUrl;
169     }
170 
171     public String getBaseUrl() {
172         return baseUrl;
173     }
174 
175     public void setStyleSheetRewriter(StyleSheetRewriter styleSheetRewriter) {
176         this.styleSheetRewriter = styleSheetRewriter;
177     }
178 }