1 11 package org.eclipse.help.internal.toc; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.ListIterator ; 17 18 import org.eclipse.help.IToc; 19 import org.eclipse.help.ITopic; 20 import org.eclipse.help.internal.model.ITopicElement; 21 import org.xml.sax.Attributes ; 22 23 27 public class Topic extends TocNode implements ITopic, ITopicElement { 28 private String href; 29 private String label; 30 private ITopic[] topicArray; 31 34 protected Topic(TocFile tocFile, Attributes attrs) { 35 if (attrs == null) 36 return; 37 href = attrs.getValue("href"); if (href != null && href.length() > 0) 39 href = HrefUtil.normalizeHref(tocFile.getPluginID(), href); 40 label = attrs.getValue("label"); if (label == null) { 42 throw new RuntimeException ("topic label==null"); } 44 tocFile.getToc().registerTopic(this); 45 addFilters(attrs); 46 } 47 50 public final void build(TocBuilder builder) { 51 builder.buildTopic(this); 52 } 53 public String getHref() { 54 return href; 55 } 56 public String getLabel() { 57 return label; 58 } 59 67 public ITopic[] getSubtopics() { 68 if (topicArray == null) { 69 List topics = getChildTopics(); 70 topicArray = new ITopic[topics.size()]; 72 topics.toArray(topicArray); 73 } 74 return topicArray; 75 } 76 77 void setLabel(String label) { 78 this.label = label; 79 } 80 81 void setHref(String href) { 82 this.href = href; 83 } 84 90 public ITopic[] getPathInToc(IToc toc) { 91 List ancestors = getTopicPathInToc(toc, this); 92 if (ancestors == null) { 93 return null; 94 } 95 return (ITopic[]) ancestors.toArray(new ITopic[ancestors.size()]); 96 } 97 98 105 static List getTopicPathInToc(IToc toc, Topic topic) { 106 List topicParents = new ArrayList (topic.getParents()); 107 for (ListIterator it = topicParents.listIterator(); it.hasNext();) { 108 TocNode tocNode = (TocNode) it.next(); 109 if (!(tocNode instanceof Topic)) { 110 if (tocNode == toc) { 112 List ancestors = new ArrayList (); 114 ancestors.add(topic); 115 return ancestors; 116 } 117 List grandParents = tocNode.getParents(); 120 it.remove(); 121 for (Iterator it2 = grandParents.iterator(); it2.hasNext();) { 122 it.add(it2.next()); 123 it.previous(); 124 } 125 } 126 } 127 128 for (Iterator it = topicParents.iterator(); it.hasNext();) { 129 List a = getTopicPathInToc(toc, (Topic) it.next()); 131 if (a != null) { 132 a.add(topic); 134 return a; 135 } 136 } 137 138 return null; 139 } 140 } 141 | Popular Tags |