KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > context > ContextsBuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.help.internal.context;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.help.*;
17 public class ContextsBuilder {
18     protected PluginContexts contexts;
19     private String JavaDoc definingPluginID;
20     private String JavaDoc pluginID;
21     /**
22      * Contexts Builder Constructor.
23      */

24     public ContextsBuilder(PluginContexts pluginContexts) {
25         this.contexts = pluginContexts;
26     }
27     public void build(RelatedTopic relatedTopic) {
28         // set the href on the related topic
29
String JavaDoc href = relatedTopic.getHref();
30         if (href == null)
31             relatedTopic.setHref(""); //$NON-NLS-1$
32
else {
33             if (!href.equals("") // no empty link //$NON-NLS-1$
34
&& !href.startsWith("/") // no help url //$NON-NLS-1$
35
&& href.indexOf(':') == -1) // no other protocols
36
{
37                 relatedTopic.setHref("/" + definingPluginID + "/" + href); //$NON-NLS-1$ //$NON-NLS-2$
38
}
39         }
40     }
41     public void build(Context context) {
42         context.setPluginID(pluginID);
43         // if context with same Id exists, merge them
44
Context existingContext = (Context) contexts.get(context.getShortId());
45         if (existingContext != null) {
46             mergeContexts(existingContext, context);
47         } else {
48             contexts.put(context.getShortId(), context);
49         }
50     }
51     public void build(ContextsFile contextsFile) {
52         this.pluginID = contextsFile.getPluginID();
53         this.definingPluginID = contextsFile.getDefiningPluginID();
54         ContextsFileParser parser = new ContextsFileParser(this);
55         parser.parse(contextsFile);
56     }
57     public void build(List JavaDoc pluginContextsFiles) {
58         for (Iterator JavaDoc contextFilesIt = pluginContextsFiles.iterator(); contextFilesIt
59                 .hasNext();) {
60             ContextsFile contextsFile = (ContextsFile) contextFilesIt.next();
61             contextsFile.build(this);
62         }
63     }
64     /**
65      * Merges Text and Links from new Context into an existing Context
66      */

67     private void mergeContexts(Context existingContext, Context newContext) {
68         // Merge Text
69
if (newContext.getStyledText() != null) {
70             if (existingContext.getStyledText() != null) {
71                 existingContext.setStyledText(existingContext.getStyledText()
72                         + "\n" //$NON-NLS-1$
73
+ newContext.getStyledText());
74             } else {
75                 existingContext.setStyledText(newContext.getStyledText());
76             }
77         }
78         // Merge Related Links
79
existingContext.getChildren().addAll(newContext.getChildren());
80         removeDuplicateLinks(existingContext);
81     }
82     /**
83      * Filters out the duplicate related topics in a Context
84      */

85     private void removeDuplicateLinks(Context context) {
86         List JavaDoc links = context.getChildren();
87         if (links == null || links.size() <= 0)
88             return;
89         List JavaDoc filtered = new ArrayList JavaDoc();
90         for (Iterator JavaDoc it = links.iterator(); it.hasNext();) {
91             IHelpResource topic1 = (IHelpResource) it.next();
92             if (!isValidTopic(topic1))
93                 continue;
94             boolean dup = false;
95             for (int j = 0; j < filtered.size(); j++) {
96                 IHelpResource topic2 = (IHelpResource) filtered.get(j);
97                 if (!isValidTopic(topic2))
98                     continue;
99                 if (equalTopics(topic1, topic2)) {
100                     dup = true;
101                     break;
102                 }
103             }
104             if (!dup)
105                 filtered.add(topic1);
106         }
107         context.setChildren(filtered);
108     }
109     /**
110      * Checks if topic labels and href are not null and not empty strings
111      */

112     private boolean isValidTopic(IHelpResource topic) {
113         return topic != null && topic.getHref() != null
114                 && !"".equals(topic.getHref()) && topic.getLabel() != null //$NON-NLS-1$
115
&& !"".equals(topic.getLabel()); //$NON-NLS-1$
116
}
117     /**
118      * Check if two context topic are the same. They are considered the same if
119      * both labels and href are equal
120      */

121     private boolean equalTopics(IHelpResource topic1, IHelpResource topic2) {
122         return topic1.getHref().equals(topic2.getHref())
123                 && topic1.getLabel().equals(topic2.getLabel());
124     }
125 }
126
Popular Tags