1 11 package org.eclipse.help.internal.context; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.eclipse.help.*; 17 public class ContextsBuilder { 18 protected PluginContexts contexts; 19 private String definingPluginID; 20 private String pluginID; 21 24 public ContextsBuilder(PluginContexts pluginContexts) { 25 this.contexts = pluginContexts; 26 } 27 public void build(RelatedTopic relatedTopic) { 28 String href = relatedTopic.getHref(); 30 if (href == null) 31 relatedTopic.setHref(""); else { 33 if (!href.equals("") && !href.startsWith("/") && href.indexOf(':') == -1) { 37 relatedTopic.setHref("/" + definingPluginID + "/" + href); } 39 } 40 } 41 public void build(Context context) { 42 context.setPluginID(pluginID); 43 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 pluginContextsFiles) { 58 for (Iterator contextFilesIt = pluginContextsFiles.iterator(); contextFilesIt 59 .hasNext();) { 60 ContextsFile contextsFile = (ContextsFile) contextFilesIt.next(); 61 contextsFile.build(this); 62 } 63 } 64 67 private void mergeContexts(Context existingContext, Context newContext) { 68 if (newContext.getStyledText() != null) { 70 if (existingContext.getStyledText() != null) { 71 existingContext.setStyledText(existingContext.getStyledText() 72 + "\n" + newContext.getStyledText()); 74 } else { 75 existingContext.setStyledText(newContext.getStyledText()); 76 } 77 } 78 existingContext.getChildren().addAll(newContext.getChildren()); 80 removeDuplicateLinks(existingContext); 81 } 82 85 private void removeDuplicateLinks(Context context) { 86 List links = context.getChildren(); 87 if (links == null || links.size() <= 0) 88 return; 89 List filtered = new ArrayList (); 90 for (Iterator 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 112 private boolean isValidTopic(IHelpResource topic) { 113 return topic != null && topic.getHref() != null 114 && !"".equals(topic.getHref()) && topic.getLabel() != null && !"".equals(topic.getLabel()); } 117 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 |