1 11 package org.eclipse.help.internal.toc; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.help.internal.HelpPlugin; 19 20 public class TocBuilder { 21 22 protected Collection contributedTocFiles; 24 25 protected Collection unprocessedTocFiles; 27 28 protected List unprocessedTocs; 31 32 35 public TocBuilder() { 36 unprocessedTocFiles = new ArrayList (); 37 unprocessedTocs = new ArrayList (); 38 } 39 40 public List getBuiltTocs() { 41 List tocCol = new ArrayList (contributedTocFiles.size()); 43 for (Iterator it = contributedTocFiles.iterator(); it.hasNext();) { 44 TocFile tocFile = (TocFile) it.next(); 45 Toc toc = tocFile.getToc(); 46 if (toc != null && toc.getTocFile().isPrimary() && !isIntegrated(toc)) 47 tocCol.add((toc)); 48 } 49 return tocCol; 50 } 51 52 54 public void build(Collection contributedTocFiles) { 55 this.contributedTocFiles = contributedTocFiles; 56 unprocessedTocFiles.addAll(contributedTocFiles); 57 while (!unprocessedTocFiles.isEmpty()) { 60 TocFile tocFile = (TocFile) unprocessedTocFiles.iterator().next(); 61 tocFile.build(this); 62 } 63 67 int remaining = unprocessedTocs.size(); 69 for (int i = 0; i < remaining; i++) { 70 Toc toc = (Toc) unprocessedTocs.get(i); 71 buildToc(toc); 72 } 73 } 74 75 public void buildTocFile(TocFile tocFile) { 76 try { 77 unprocessedTocFiles.remove(tocFile); 78 TocFileParser parser = new TocFileParser(this); 79 parser.parse(tocFile); 80 } catch (Exception e) { 81 String msg = "Error occurred processing file " + tocFile.getHref() + "."; HelpPlugin.logError(msg, e); 83 } 84 } 85 86 public void buildAnchor(Anchor anchor) { 87 anchor.getTocFile().addAnchor(anchor); 89 } 90 91 public void buildLink(Link link) { 92 String linkedToc = link.getToc(); 94 TocFile includedTocFile = getTocFile(linkedToc); 95 if (includedTocFile == null) 96 return; 97 Toc toc = includedTocFile.getToc(); 98 if (toc == null) 99 return; 100 link.addChild(toc); 102 } 103 104 public void buildTopic(Topic topic) { 105 } 107 108 public void buildToc(Toc toc) { 109 String href = toc.getLink_to(); 111 if (href == null || href.equals("")) return; 113 TocFile targetTocFile = getTocFile(href); 114 if (targetTocFile == null) 115 return; 116 Anchor anchor = targetTocFile.getAnchor(href); 117 if (anchor == null) { 118 unprocessedTocs.add(toc); 119 return; 120 } 121 anchor.addChild(toc); 123 } 124 125 private TocFile getTocFile(String href) { 126 String plugin = HrefUtil.getPluginIDFromHref(href); 127 if (plugin == null) 128 return null; 129 String path = HrefUtil.getResourcePathFromHref(href); 130 if (path == null) 131 return null; 132 TocFile tocFile = null; 133 for (Iterator it = contributedTocFiles.iterator(); it.hasNext();) { 134 tocFile = (TocFile) it.next(); 135 if (tocFile.getPluginID().equals(plugin) && tocFile.getHref().equals(path)) 136 break; 137 tocFile = null; 138 } 139 if (tocFile == null) 140 return null; 141 if (unprocessedTocFiles.contains(tocFile)) 142 buildTocFile(tocFile); 143 return tocFile; 144 } 145 146 149 private boolean isIntegrated(TocNode element) { 150 for (Iterator it = element.getParents().iterator(); it.hasNext();) { 152 TocNode parent = (TocNode) it.next(); 153 if (parent instanceof Toc && ((Toc) parent).getTocFile().isPrimary()) { 154 return true; 155 } else if (isIntegrated(parent)) { 156 return true; 157 } 158 } 159 return false; 161 } 162 } 163 | Popular Tags |