KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > IntroData


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 package org.eclipse.ui.internal.intro.universal;
12
13 import java.io.IOException JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22
23 import org.eclipse.core.runtime.IConfigurationElement;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.ui.internal.intro.impl.model.loader.IntroContentParser;
26 import org.eclipse.ui.internal.intro.universal.util.BundleUtil;
27 import org.eclipse.ui.internal.intro.universal.util.Log;
28 import org.osgi.framework.Bundle;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36
37
38 public class IntroData {
39     private String JavaDoc productId;
40     private Hashtable JavaDoc pages=new Hashtable JavaDoc();
41     private boolean active;
42     
43     public IntroData(String JavaDoc productId, String JavaDoc fileNameOrData, boolean active) {
44         this.productId = productId;
45         this.active = active;
46         if (fileNameOrData!=null)
47             initialize(fileNameOrData);
48     }
49     
50     public String JavaDoc getProductId() {
51         return productId;
52     }
53     
54     public PageData getPage(String JavaDoc pageId) {
55         return (PageData)pages.get(pageId);
56     }
57
58     public boolean isActive() {
59         return active;
60     }
61
62     private void initialize(String JavaDoc fileNameOrData) {
63         Document JavaDoc doc = parse(fileNameOrData);
64         if (doc == null)
65             return;
66         Element root = doc.getDocumentElement();
67         NodeList JavaDoc pages = root.getChildNodes();
68         for (int i = 0; i < pages.getLength(); i++) {
69             Node JavaDoc node = pages.item(i);
70             if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("page")) { //$NON-NLS-1$
71
loadPage((Element) node);
72             }
73         }
74     }
75
76     private void loadPage(Element page) {
77         PageData pd = new PageData(page);
78         pages.put(pd.getId(), pd);
79     }
80     
81     public void addImplicitContent() {
82         IConfigurationElement [] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.intro.configExtension"); //$NON-NLS-1$
83
for (int i=0; i<elements.length; i++) {
84             IConfigurationElement element = elements[i];
85             if (element.getName().equals("configExtension")) { //$NON-NLS-1$
86
String JavaDoc cid = element.getAttribute("configId"); //$NON-NLS-1$
87
if (cid!=null && cid.equals("org.eclipse.ui.intro.universalConfig")) { //$NON-NLS-1$
88
addCandidate(element);
89                 }
90             }
91         }
92     }
93
94     private void addCandidate(IConfigurationElement element) {
95         String JavaDoc fileName = element.getAttribute("content"); //$NON-NLS-1$
96
if (fileName==null)
97             return;
98         String JavaDoc bundleId = element.getDeclaringExtension().getNamespaceIdentifier();
99         Bundle bundle = Platform.getBundle(bundleId);
100         if (bundle==null)
101             return;
102         String JavaDoc content = BundleUtil.getResolvedResourceLocation("", fileName, //$NON-NLS-1$
103
bundle);
104         IntroContentParser parser = new IntroContentParser(content);
105         Document JavaDoc dom = parser.getDocument();
106         // dom can be null if the content file cannot be found
107
if (dom==null)
108             return;
109         Element root = dom.getDocumentElement();
110         Element extension = null;
111         NodeList JavaDoc children = root.getChildNodes();
112         for (int i=0; i<children.getLength(); i++) {
113             Node JavaDoc child = children.item(i);
114             if (child.getNodeType()==Node.ELEMENT_NODE) {
115                 Element el = (Element)child;
116                 if (el.getNodeName().equalsIgnoreCase("extensionContent")) { //$NON-NLS-1$
117
extension = el;
118                     break;
119                 }
120             }
121         }
122         if (extension==null)
123             return;
124         String JavaDoc id = extension.getAttribute("id"); //$NON-NLS-1$
125
String JavaDoc name = extension.getAttribute("name"); //$NON-NLS-1$
126
String JavaDoc path = extension.getAttribute("path"); //$NON-NLS-1$
127
if (id==null || path==null)
128             return;
129         int at = path.lastIndexOf("/@"); //$NON-NLS-1$
130
if (at == -1)
131             return;
132         if (path.charAt(path.length()-1)!='@')
133             return;
134         String JavaDoc pageId = path.substring(0, at);
135         PageData pd = (PageData)pages.get(pageId);
136         if (pd==null) {
137             pd = new PageData(pageId);
138             pages.put(pageId, pd);
139         }
140         pd.addImplicitExtension(id, name);
141     }
142
143     private Document JavaDoc parse(String JavaDoc fileNameOrData) {
144         Document JavaDoc document = null;
145         try {
146             DocumentBuilderFactory JavaDoc docFactory = DocumentBuilderFactory.newInstance();
147             docFactory.setValidating(false);
148             // if this is not set, Document.getElementsByTagNameNS() will fail.
149
docFactory.setNamespaceAware(true);
150             docFactory.setExpandEntityReferences(false);
151             DocumentBuilder JavaDoc parser = docFactory.newDocumentBuilder();
152
153             if (fileNameOrData.charAt(0)=='<') {
154                 //This is actual content, not the file name
155
StringReader JavaDoc reader = new StringReader JavaDoc(fileNameOrData);
156                 document = parser.parse(new InputSource JavaDoc(reader));
157             }
158             else
159                 document = parser.parse(fileNameOrData);
160             return document;
161
162         } catch (SAXParseException JavaDoc spe) {
163             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("IntroData error in line "); //$NON-NLS-1$
164
buffer.append(spe.getLineNumber());
165             buffer.append(", uri "); //$NON-NLS-1$
166
buffer.append(spe.getSystemId());
167             buffer.append("\n"); //$NON-NLS-1$
168
buffer.append(spe.getMessage());
169
170             // Use the contained exception.
171
Exception JavaDoc x = spe;
172             if (spe.getException() != null)
173                 x = spe.getException();
174             Log.error(buffer.toString(), x);
175
176         } catch (SAXException JavaDoc sxe) {
177             Exception JavaDoc x = sxe;
178             if (sxe.getException() != null)
179                 x = sxe.getException();
180             Log.error(x.getMessage(), x);
181
182         } catch (ParserConfigurationException JavaDoc pce) {
183             // Parser with specified options can't be built
184
Log.error(pce.getMessage(), pce);
185
186         } catch (IOException JavaDoc ioe) {
187             Log.error(ioe.getMessage(), ioe);
188         }
189         return null;
190     }
191     
192     public void write(PrintWriter JavaDoc writer) {
193         writer.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); //$NON-NLS-1$
194
writer.println("<extensions>"); //$NON-NLS-1$
195
for (Enumeration JavaDoc keys = pages.keys(); keys.hasMoreElements();) {
196             String JavaDoc id = (String JavaDoc)keys.nextElement();
197             PageData pd = (PageData)pages.get(id);
198             pd.write(writer, " "); //$NON-NLS-1$
199
}
200         writer.println("</extensions>"); //$NON-NLS-1$
201
}
202 }
203
Popular Tags