Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ccd5d10599d5a448b4485c137ae39c07be2ca373 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.intro.impl.model;

import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil;
import org.eclipse.ui.internal.intro.impl.util.Log;
import org.osgi.framework.Bundle;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * An intro HTML element. Can have text and image as fall back. "type" attribute
 * in markup determines if it is inlined or not. if inlined, value of 'src' will
 * be treated as a snippet of HTML to emit 'in-place'. If 'embed', a valid
 * (full) HTML document will be embedded using HTML 'OBJECT' tag. Ecoding can be
 * specified for inline snippets.
 */
public class IntroHTML extends AbstractTextElement {

    protected static final String TAG_HTML = "html"; //$NON-NLS-1$

    private static final String ATT_SRC = "src"; //$NON-NLS-1$
    /**
     * type must be "inline" or "embed".
     */
    private static final String ATT_TYPE = "type"; //$NON-NLS-1$
    // Default is UTF-8.
    private static final String ATT_ENCODING = "encoding"; //$NON-NLS-1$

    private String src;
    private String html_type;
    private String encoding;
    private IntroImage introImage;

    IntroHTML(Element element, Bundle bundle, String base) {
        super(element, bundle);
        src = getAttribute(element, ATT_SRC);
        html_type = getAttribute(element, ATT_TYPE);
        encoding = getAttribute(element, ATT_ENCODING);
        if (encoding == null)
            encoding = "UTF-8"; //$NON-NLS-1$
        if (html_type != null && !html_type.equalsIgnoreCase("inline") //$NON-NLS-1$
                && !html_type.equalsIgnoreCase("embed")) //$NON-NLS-1$
            // if type is not correct, null it.
            html_type = null;

        // description will be null if there is no description element.
        introImage = getIntroImage(element, base);

        // Resolve.
        src = BundleUtil.getResolvedResourceLocation(base, src, bundle);
    }

    /**
     * Retruns the intro image element embedded in this element.
     */
    private IntroImage getIntroImage(Element element, String base) {
        try {
            // There should only be one text element. Since elements where
            // obtained by name, no point validating name.
            NodeList imageElements = element
                .getElementsByTagName(IntroImage.TAG_IMAGE);
            if (imageElements.getLength() == 0)
                // no contributions. done.
                return null;
            IntroImage image = new IntroImage((Element) imageElements.item(0),
                getBundle(), base);
            image.setParent(this);
            return image;
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * Returns the html type. Will be either "inline" or "embed". If not, null
     * will be returned as if the attibute was nto defined.
     * 
     * @return Returns the html type value.
     */
    public boolean isInlined() {
        return (html_type != null && html_type.equalsIgnoreCase("inline")) ? true //$NON-NLS-1$
                : false;
    }

    /**
     * @return Returns the src.
     */
    public String getSrc() {
        return src;
    }

    /**
     * @return Returns the encoding of the inlined file. This is not needed for
     *         embedded files. Default is UTF-8.
     */
    public String getInlineEncoding() {
        return encoding;
    }

    /**
     * Returns the intro image used as a replacement if this HTML element fails.
     * May return null if there is no image child.
     * 
     * @return Returns the introImage.
     */
    public IntroImage getIntroImage() {
        return introImage;
    }

    @Override
	public int getType() {
        return AbstractIntroElement.HTML;
    }

    /**
     * Deep copy since class has mutable objects.
     */
    @Override
	public Object clone() throws CloneNotSupportedException {
        IntroHTML clone = (IntroHTML) super.clone();
        if (introImage != null) {
            IntroImage cloneIntroImage = (IntroImage) introImage.clone();
            cloneIntroImage.setParent(clone);
            clone.introImage = cloneIntroImage;
        }
        return clone;
    }

}

Back to the top