KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > IntroExtensionContent


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.intro.impl.model;
13
14 import java.util.Hashtable JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.core.runtime.FileLocator;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.ui.internal.intro.impl.model.loader.IntroContentParser;
22 import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil;
23 import org.eclipse.ui.internal.intro.impl.model.util.ModelUtil;
24 import org.osgi.framework.Bundle;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 /**
31  * An intro container extension. If the content attribute is defined, then it is
32  * assumed that we have XHTML content in an external file. Load content from
33  * external DOM. No need to worry about caching here because this is a transient
34  * model class. It is used and then disregarded from the model.<br>
35  * Just like in a page, the styles and altStyles strings can be a comma
36  * separated list of styles. Handle this by storing styles just like pages.
37  */

38 public class IntroExtensionContent extends AbstractIntroElement {
39
40     protected static final String JavaDoc TAG_CONTAINER_EXTENSION = "extensionContent"; //$NON-NLS-1$
41
protected static final String JavaDoc TAG_CONTAINER_REPLACE = "replacementContent"; //$NON-NLS-1$
42

43     public static final int TYPE_CONTRIBUTION = 0;
44     public static final int TYPE_REPLACEMENT = 1;
45    
46     protected static final String JavaDoc ATT_PATH = "path"; //$NON-NLS-1$
47
protected static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
48
private static final String JavaDoc ATT_STYLE = "style"; //$NON-NLS-1$
49
private static final String JavaDoc ATT_ALT_STYLE = "alt-style"; //$NON-NLS-1$
50
private static final String JavaDoc ATT_CONTENT = "content"; //$NON-NLS-1$
51

52     private static final Element[] EMPTY_ELEMENT_ARRAY = new Element[0];
53
54     private String JavaDoc path;
55     private String JavaDoc content;
56     private String JavaDoc contentFile;
57     private String JavaDoc contentId;
58     private String JavaDoc anchorId;
59
60     private Element element;
61     private String JavaDoc base;
62
63     private Vector JavaDoc styles = new Vector JavaDoc();
64     private Hashtable JavaDoc altStyles = new Hashtable JavaDoc();
65
66     IntroExtensionContent(Element element, Bundle bundle, String JavaDoc base, IConfigurationElement configExtElement) {
67         super(element, bundle);
68         path = getAttribute(element, ATT_PATH);
69         content = getAttribute(element, ATT_CONTENT);
70         anchorId = getAttribute(element, ATT_ID);
71         this.element = element;
72         this.base = base;
73
74         // load and resolve styles, first.
75
init(element, bundle, base);
76
77         // if content is not null we have XHTML extension.
78
if (content != null) {
79             // BASE: since content is being loaded from another XHTML file and
80
// not this xml file, point the base of this page to be relative to
81
// the new xml file location.
82
IPath subBase = ModelUtil.getParentFolderPath(content);
83             String JavaDoc newBase = new Path(base).append(subBase).toString();
84             extractFileAndId(bundle);
85             contentFile = BundleUtil.getResolvedResourceLocation(base, contentFile,
86                 bundle);
87             this.base = newBase;
88         }
89         
90         // Save the mapping between plugin registry id and base/anchor id
91
String JavaDoc contributor = configExtElement.getContributor().getName();
92         ExtensionMap.getInstance().putPluginId(anchorId, contributor);
93     }
94     
95     public String JavaDoc getId() {
96         return anchorId;
97     }
98
99
100     /**
101      * Initialize styles. Take first style in style attribute and make it the
102      * page style. Then put other styles in styles vectors. Make sure to resolve
103      * each style.
104      *
105      * @param element
106      * @param bundle
107      */

108     private void init(Element element, Bundle bundle, String JavaDoc base) {
109         String JavaDoc[] styleValues = getAttributeList(element, ATT_STYLE);
110         if (styleValues != null && styleValues.length > 0) {
111             for (int i = 0; i < styleValues.length; i++) {
112                 String JavaDoc style = styleValues[i];
113                 style = BundleUtil.getResolvedResourceLocation(base, style,
114                     bundle);
115                 addStyle(style);
116             }
117         }
118
119         String JavaDoc[] altStyleValues = getAttributeList(element, ATT_ALT_STYLE);
120         if (altStyleValues != null && altStyleValues.length > 0) {
121             for (int i = 0; i < altStyleValues.length; i++) {
122                 String JavaDoc style = altStyleValues[i];
123                 style = BundleUtil.getResolvedResourceLocation(base, style,
124                     bundle);
125                 addAltStyle(style, bundle);
126             }
127         }
128     }
129
130     /**
131      * Adds the given style to the list. Style is not added if it already exists
132      * in the list.
133      *
134      * @param style
135      */

136     protected void addStyle(String JavaDoc style) {
137         if (styles.contains(style))
138             return;
139         styles.add(style);
140     }
141
142
143     /**
144      * Adds the given style to the list.Style is not added if it already exists
145      * in the list.
146      *
147      * @param altStyle
148      */

149     protected void addAltStyle(String JavaDoc altStyle, Bundle bundle) {
150         if (altStyles.containsKey(altStyle))
151             return;
152         altStyles.put(altStyle, bundle);
153     }
154
155     /**
156      * Returns the extension type; either contribution into an anchor or replacement
157      * of an element.
158      */

159     public int getExtensionType() {
160         return TAG_CONTAINER_REPLACE.equals(element.getNodeName()) ? TYPE_REPLACEMENT : TYPE_CONTRIBUTION;
161     }
162
163     /**
164      * @return Returns the path.
165      */

166     public String JavaDoc getPath() {
167         return path;
168     }
169
170     /*
171      * (non-Javadoc)
172      *
173      * @see org.eclipse.ui.internal.intro.impl.model.IntroElement#getType()
174      */

175     public int getType() {
176         return AbstractIntroElement.CONTAINER_EXTENSION;
177     }
178
179     protected Element[] getChildren() {
180         NodeList JavaDoc nodeList = element.getChildNodes();
181         Vector JavaDoc vector = new Vector JavaDoc();
182         for (int i = 0; i < nodeList.getLength(); i++) {
183             Node JavaDoc node = nodeList.item(i);
184             if (node.getNodeType() == Node.ELEMENT_NODE)
185                 vector.add(node);
186         }
187         Element[] filteredElements = new Element[vector.size()];
188         vector.copyInto(filteredElements);
189         // free DOM model for memory performance.
190
this.element = null;
191         return filteredElements;
192     }
193
194     public boolean isXHTMLContent() {
195         return content != null ? true : false;
196     }
197
198     /**
199      * Returns the elements loaded from the content attribute. This is the content
200      * that should be inserted for the extension. If it is a file, all child elements
201      * of body are returned. If it is a file with an id, only the element with the id
202      * is returned.
203      *
204      * @return the elements to be inserted
205      */

206     public Element[] getElements() {
207         // only applicable when content attribute is specified
208
if (isXHTMLContent()) {
209             IntroContentParser parser = new IntroContentParser(contentFile);
210             Document JavaDoc dom = parser.getDocument();
211             if (dom != null) {
212                 // parser content should be XHTML because defining content here
213
// means that we want XHTML extension.
214
if (parser.hasXHTMLContent()) {
215                     if (contentId != null) {
216                         // id specified, only get that element
217
return new Element[] { ModelUtil.getElementById(dom, contentId) };
218                     }
219                     else {
220                         // no id specified, use the whole body
221
Element extensionBody = ModelUtil.getBodyElement(dom);
222                         return ModelUtil.getElementsByTagName(extensionBody, "*"); //$NON-NLS-1$
223
}
224                 }
225             }
226         }
227         return EMPTY_ELEMENT_ARRAY;
228     }
229
230     /**
231      * @return Returns the altStyle.
232      */

233     protected Hashtable JavaDoc getAltStyles() {
234         return altStyles;
235     }
236
237     /**
238      * @return Returns the style.
239      */

240     protected String JavaDoc[] getStyles() {
241         String JavaDoc[] stylesArray = new String JavaDoc[styles.size()];
242         styles.copyInto(stylesArray);
243         return stylesArray;
244     }
245
246     /**
247      * @return Returns the content.
248      */

249     public String JavaDoc getContent() {
250         return content;
251     }
252
253     public String JavaDoc getBase() {
254         return base;
255     }
256     
257     /**
258      * Extracts the file and id parts of the content attribute. This attribute has two modes -
259      * if you specify a file, it will include the body of that file (minus the body element itself).
260      * If you append an id after the file, only the element with that id will be included. However
261      * we need to know which mode we're in.
262      *
263      * @param bundle the bundle that contributed this extension
264      */

265     private void extractFileAndId(Bundle bundle) {
266         // look for the file
267
IPath resourcePath = new Path(base + content);
268         if (FileLocator.find(bundle, resourcePath, null) != null) {
269             // found it, assume it's a file
270
contentFile = content;
271         }
272         else {
273             // didn't find the file, assume the last segment is an id
274
int lastSlashIndex = content.lastIndexOf('/');
275             if (lastSlashIndex != -1) {
276                 contentFile = content.substring(0, lastSlashIndex);
277                 contentId = content.substring(lastSlashIndex + 1);
278             }
279             else {
280                 // there was no slash, it must be a file
281
contentFile = content;
282             }
283         }
284     }
285 }
286
Popular Tags