001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.widgets;
003
004import javax.swing.text.AbstractDocument;
005import javax.swing.text.AttributeSet;
006import javax.swing.text.Element;
007import javax.swing.text.StyleConstants;
008import javax.swing.text.View;
009import javax.swing.text.html.HTML;
010import javax.swing.text.html.HTMLEditorKit.HTMLFactory;
011
012import org.openstreetmap.josm.Main;
013
014/**
015 * Specialized HTML Factory allowing to display SVG images.
016 * @since 8933
017 */
018public class JosmHTMLFactory extends HTMLFactory {
019
020    @Override
021    public View create(Element elem) {
022        AttributeSet attrs = elem.getAttributes();
023        Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
024        Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
025        if (o instanceof HTML.Tag) {
026            HTML.Tag kind = (HTML.Tag) o;
027            if (kind == HTML.Tag.IMG) {
028                try {
029                    return new JosmImageView(elem);
030                } catch (NoSuchFieldException | SecurityException e) {
031                    Main.error(e);
032                }
033            }
034        }
035        return super.create(elem);
036    }
037}