001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.impl.model;
011:
012: import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil;
013: import org.eclipse.ui.internal.intro.impl.util.Log;
014: import org.osgi.framework.Bundle;
015: import org.w3c.dom.Element;
016: import org.w3c.dom.NodeList;
017:
018: /**
019: * An intro HTML element. Can have text and image as fall back. "type" attribute
020: * in markup determines if it is inlined or not. if inlined, value of 'src' will
021: * be treated as a snippet of HTML to emit 'in-place'. If 'embed', a valid
022: * (full) HTML document will be embedded using HTML 'OBJECT' tag. Ecoding can be
023: * specified for inline snippets.
024: */
025: public class IntroHTML extends AbstractTextElement {
026:
027: protected static final String TAG_HTML = "html"; //$NON-NLS-1$
028:
029: private static final String ATT_SRC = "src"; //$NON-NLS-1$
030: /**
031: * type must be "inline" or "embed".
032: */
033: private static final String ATT_TYPE = "type"; //$NON-NLS-1$
034: // Default is UTF-8.
035: private static final String ATT_ENCODING = "encoding"; //$NON-NLS-1$
036:
037: private String src;
038: private String html_type;
039: private String encoding;
040: private IntroImage introImage;
041:
042: IntroHTML(Element element, Bundle bundle, String base) {
043: super (element, bundle);
044: src = getAttribute(element, ATT_SRC);
045: html_type = getAttribute(element, ATT_TYPE);
046: encoding = getAttribute(element, ATT_ENCODING);
047: if (encoding == null)
048: encoding = "UTF-8"; //$NON-NLS-1$
049: if (html_type != null && !html_type.equalsIgnoreCase("inline") //$NON-NLS-1$
050: && !html_type.equalsIgnoreCase("embed")) //$NON-NLS-1$
051: // if type is not correct, null it.
052: html_type = null;
053:
054: // description will be null if there is no description element.
055: introImage = getIntroImage(element, base);
056:
057: // Resolve.
058: src = BundleUtil.getResolvedResourceLocation(base, src, bundle);
059: }
060:
061: /**
062: * Retruns the intro image element embedded in this element.
063: */
064: private IntroImage getIntroImage(Element element, String base) {
065: try {
066: // There should only be one text element. Since elements where
067: // obtained by name, no point validating name.
068: NodeList imageElements = element
069: .getElementsByTagName(IntroImage.TAG_IMAGE);
070: if (imageElements.getLength() == 0)
071: // no contributions. done.
072: return null;
073: IntroImage image = new IntroImage((Element) imageElements
074: .item(0), getBundle(), base);
075: image.setParent(this );
076: return image;
077: } catch (Exception e) {
078: Log.error(e.getMessage(), e);
079: return null;
080: }
081: }
082:
083: /**
084: * Returns the html type. Will be either "inline" or "embed". If not, null
085: * will be returned as if the attibute was nto defined.
086: *
087: * @return Returns the html type value.
088: */
089: public boolean isInlined() {
090: return (html_type != null && html_type
091: .equalsIgnoreCase("inline")) ? true //$NON-NLS-1$
092: : false;
093: }
094:
095: /**
096: * @return Returns the src.
097: */
098: public String getSrc() {
099: return src;
100: }
101:
102: /**
103: * @return Returns the encoding of the inlined file. This is not needed for
104: * embedded files. Default is UTF-8.
105: */
106: public String getInlineEncoding() {
107: return encoding;
108: }
109:
110: /**
111: * Returns the intro image used as a replacement if this HTML element fails.
112: * May return null if there is no image child.
113: *
114: * @return Returns the introImage.
115: */
116: public IntroImage getIntroImage() {
117: return introImage;
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.eclipse.ui.internal.intro.impl.model.IntroElement#getType()
124: */
125: public int getType() {
126: return AbstractIntroElement.HTML;
127: }
128:
129: /**
130: * Deep copy since class has mutable objects.
131: */
132: public Object clone() throws CloneNotSupportedException {
133: IntroHTML clone = (IntroHTML) super .clone();
134: if (introImage != null) {
135: IntroImage cloneIntroImage = (IntroImage) introImage
136: .clone();
137: cloneIntroImage.setParent(clone);
138: clone.introImage = cloneIntroImage;
139: }
140: return clone;
141: }
142:
143: }
|