KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > UATopicExtension


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

9
10 package org.eclipse.help.internal.xhtml;
11
12 import org.eclipse.core.runtime.FileLocator;
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.core.runtime.Path;
15 import org.osgi.framework.Bundle;
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element JavaDoc;
18
19
20 public class UATopicExtension extends AbstractUAElement {
21
22     protected static final String JavaDoc TAG_TOPIC_EXTENSION = "topicExtension"; //$NON-NLS-1$
23

24     protected static final String JavaDoc ATT_PATH = "path"; //$NON-NLS-1$
25
private static final String JavaDoc ATT_CONTENT = "content"; //$NON-NLS-1$
26

27     private static final Element JavaDoc[] EMPTY_ELEMENT_ARRAY = new Element JavaDoc[0];
28
29     private String JavaDoc path;
30     private String JavaDoc contentFile;
31     private String JavaDoc contentId;
32     private Element JavaDoc element;
33
34     UATopicExtension(Element JavaDoc element, Bundle bundle) {
35         super(element, bundle);
36         path = getAttribute(element, ATT_PATH);
37         extractFileAndId(getAttribute(element, ATT_CONTENT), bundle);
38         contentFile = BundleUtil.getResolvedResourceLocation(contentFile, bundle, false);
39         this.element = element;
40     }
41
42     /**
43      * @return Returns the path.
44      */

45     public String JavaDoc getPath() {
46         return path;
47     }
48
49     /**
50      * Returns the elements loaded from the content attribute. This is the content
51      * that should be inserted for the extension. If it is a file, all child elements
52      * of body are returned. If it is a file with an id, only the element with the id
53      * is returned.
54      *
55      * @return the elements to be inserted
56      */

57     public Element JavaDoc[] getElements() {
58         UAContentParser parser = new UAContentParser(contentFile);
59         Document JavaDoc dom = parser.getDocument();
60         if (dom != null) {
61             if (contentId != null) {
62                 // id specified, only get that element
63
return new Element JavaDoc[] { dom.getElementById(contentId) };
64             }
65             else {
66                 // no id specified, use the whole body
67
Element JavaDoc extensionBody = DOMUtil.getBodyElement(dom);
68                 return DOMUtil.getElementsByTagName(extensionBody, "*"); //$NON-NLS-1$
69
}
70         }
71         return EMPTY_ELEMENT_ARRAY;
72     }
73
74     public Element JavaDoc getElement() {
75         return element;
76     }
77
78     /**
79      * Extracts the file and id parts of the content attribute. This attribute has two modes -
80      * if you specify a file, it will include the body of that file (minus the body element itself).
81      * If you append an id after the file, only the element with that id will be included. However
82      * we need to know which mode we're in.
83      *
84      * @param content the content attribute value
85      * @param bundle the bundle that contributed this extension
86      */

87     private void extractFileAndId(String JavaDoc content, Bundle bundle) {
88         // look for the file first
89
IPath resourcePath = new Path(content);
90         if (FileLocator.find(bundle, resourcePath, null) != null) {
91             // found it, it's a file with no id
92
contentFile = content;
93         }
94         else {
95             // didn't find the file, assume the last segment is an id
96
int lastSlashIndex = content.lastIndexOf('/');
97             if (lastSlashIndex != -1) {
98                 contentFile = content.substring(0, lastSlashIndex);
99                 contentId = content.substring(lastSlashIndex + 1);
100             }
101             else {
102                 // there was no slash, it must be a file
103
contentFile = content;
104             }
105         }
106     }
107 }
108
Popular Tags