1 11 package org.eclipse.help.internal.dynamic; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import javax.xml.parsers.ParserConfigurationException ; 19 20 import org.eclipse.help.HelpSystem; 21 import org.eclipse.help.internal.HelpPlugin; 22 import org.eclipse.help.internal.UAElement; 23 import org.eclipse.help.internal.extension.ContentExtension; 24 import org.eclipse.help.internal.extension.ContentExtensionManager; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Node ; 27 import org.xml.sax.SAXException ; 28 29 34 public class ExtensionResolver { 35 36 private static final String ELEMENT_BODY = "body"; private static final String ATTRIBUTE_ID = "id"; 39 private DocumentProcessor processor; 40 private DocumentReader reader; 41 private String locale; 42 private ContentExtensionManager manager; 43 44 48 public ExtensionResolver(DocumentProcessor processor, DocumentReader reader, String locale) { 49 this.processor = processor; 50 this.reader = reader; 51 this.locale = locale; 52 } 53 54 57 public Node [] resolveExtension(String path, int type) { 58 if (manager == null) { 59 manager = HelpPlugin.getContentExtensionManager(); 60 } 61 ContentExtension[] extensions = manager.getExtensions(path, type, locale); 62 List list = new ArrayList (); 63 for (int i=0;i<extensions.length;++i) { 64 String content = extensions[i].getContent(); 65 try { 66 Node [] nodes = getContent(content); 67 for (int j=0;j<nodes.length;++j) { 68 list.add(nodes[j]); 69 } 70 } 71 catch (Throwable t) { 72 } 74 } 75 return (Node [])list.toArray(new Node [list.size()]); 76 } 77 78 82 private Node [] getContent(String content) throws IOException , SAXException , ParserConfigurationException { 83 String bundleId = null; 84 String relativePath = null; 85 String nodeId = null; 86 87 int bundleStart = 0; 88 if (content.charAt(0) == '/') { 90 bundleStart = 1; 91 } 92 int bundleEnd = content.indexOf('/', bundleStart + 1); 93 if (bundleEnd > bundleStart) { 94 bundleId = content.substring(bundleStart, bundleEnd); 95 int pathStart = bundleEnd + 1; 96 int pathEnd = content.indexOf('#', pathStart + 1); 97 if (pathEnd == -1) { 98 int lastSlash = content.lastIndexOf('/'); 100 int secondLastSlash = content.lastIndexOf('/', lastSlash - 1); 101 if (secondLastSlash != -1 && lastSlash > secondLastSlash) { 102 String secondLastToken = content.substring(secondLastSlash + 1, lastSlash); 103 if (secondLastToken.indexOf('.') != -1) { 104 pathEnd = lastSlash; 105 } 106 else { 107 pathEnd = content.length(); 108 } 109 } 110 else { 111 pathEnd = content.length(); 112 } 113 } 114 relativePath = content.substring(pathStart, pathEnd); 115 if (pathEnd < content.length()) { 116 nodeId = content.substring(pathEnd + 1); 117 } 118 } 119 120 if (bundleId != null && relativePath != null) { 121 return getContent(bundleId, relativePath, nodeId); 122 } 123 return null; 124 } 125 126 129 private Node [] getContent(String bundleId, String relativePath, String nodeId) throws IOException , SAXException , ParserConfigurationException { 130 String href = '/' + bundleId + '/' + relativePath; 131 InputStream in = HelpSystem.getHelpContent(href, locale); 132 try { 133 if (nodeId != null) { 134 Element element = findElement(in, nodeId); 135 processor.process(new UAElement(element), href); 136 return new Node [] { element }; 137 } 138 Element body = findBody(in); 139 List children = new ArrayList (); 140 Node node = body.getFirstChild(); 141 while (node != null) { 142 if (node.getNodeType() == Node.ELEMENT_NODE) { 143 processor.process(new UAElement((Element)node), href); 144 } 145 children.add(node); 146 node = node.getNextSibling(); 147 } 148 return (Node [])children.toArray(new Node [children.size()]); 149 } 150 finally { 151 try { 152 in.close(); 153 } 154 catch (IOException e) {} 155 } 156 } 157 158 162 private Element findElement(InputStream in, String elementId) throws IOException , SAXException , ParserConfigurationException { 163 return findElement(reader.read(in).element, elementId); 164 } 165 166 170 private Element findElement(Element element, String elementId) { 171 String id = element.getAttribute(ATTRIBUTE_ID); 172 if (id != null && id.equals(elementId)) { 173 return element; 174 } 175 Node node = element.getFirstChild(); 176 while (node != null) { 177 if (node.getNodeType() == Node.ELEMENT_NODE) { 178 element = findElement((Element)node, elementId); 179 if (element != null) { 180 return element; 181 } 182 } 183 node = node.getNextSibling(); 184 } 185 return null; 186 } 187 188 191 private Element findBody(InputStream in) throws IOException , SAXException , ParserConfigurationException { 192 return findBody(reader.read(in).element); 193 } 194 195 198 private Element findBody(Element element) { 199 if (ELEMENT_BODY.equals(element.getNodeName())) { 200 return element; 201 } 202 Node node = element.getFirstChild(); 203 while (node != null) { 204 if (node.getNodeType() == Node.ELEMENT_NODE) { 205 Element body = findBody((Element)node); 206 if (body != null) { 207 return body; 208 } 209 } 210 node = node.getNextSibling(); 211 } 212 return null; 213 } 214 } 215 | Popular Tags |