1 /* 
2  * Copyright 2005 Paul Hinds
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 */
16package org.tp23.antinstaller.renderer.swing;
17
18import java.net.URL;
19
20import javax.swing.text.Element;
21import javax.swing.text.View;
22import javax.swing.text.ViewFactory;
23import javax.swing.text.html.HTML;
24import javax.swing.text.html.HTMLEditorKit;
25import javax.swing.text.html.ImageView;
26/**
27 * HTMLEditor kit that replaces the source of images in the document
28 * with resources loaded from the classpath.
29 * @author teknopaul
30 */
31public class ClasspathHTMLEditorKit extends HTMLEditorKit {
32
33    public ViewFactory getViewFactory(){
34        
35        return new HTMLEditorKit.HTMLFactory(){
36            
37            public View create(Element elem){
38                if(! elem.getName().equals("img")){
39                    return super.create(elem);
40                }
41                return new ImageView(elem){
42                    public URL getImageURL() {
43                        String src = (String)getElement().getAttributes().
44                            getAttribute(HTML.Attribute.SRC);
45                        return TextPageRenderer.class.getResource(src);
46                    }
47                };
48            }
49            
50        };
51        
52    }
53
54}
55