1 11 package org.eclipse.help.internal.dynamic; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 16 import javax.xml.parsers.ParserConfigurationException ; 17 18 import org.eclipse.help.HelpSystem; 19 import org.eclipse.help.IUAElement; 20 import org.eclipse.help.internal.UAElement; 21 import org.xml.sax.SAXException ; 22 23 27 public class IncludeResolver { 28 29 private static final String ATTRIBUTE_ID = "id"; 31 private DocumentProcessor processor; 32 private DocumentReader reader; 33 private String locale; 34 35 public IncludeResolver(DocumentProcessor processor, DocumentReader reader, String locale) { 36 this.processor = processor; 37 this.reader = reader; 38 this.locale = locale; 39 } 40 41 44 public UAElement resolve(String bundleId, String relativePath, String elementId) throws IOException , SAXException , ParserConfigurationException { 45 String href = '/' + bundleId + '/' + relativePath; 46 InputStream in = HelpSystem.getHelpContent(href, locale); 47 try { 48 UAElement element = findElement(in, elementId); 49 processor.process(element, href); 50 return element; 51 } 52 finally { 53 try { 54 in.close(); 55 } 56 catch (IOException e) {} 57 } 58 } 59 60 63 private UAElement findElement(InputStream in, String elementId) throws IOException , SAXException , ParserConfigurationException { 64 UAElement element = reader.read(in); 65 return findElement(element, elementId); 66 } 67 68 71 private UAElement findElement(UAElement element, String elementId) { 72 String id = element.getAttribute(ATTRIBUTE_ID); 73 if (id != null && id.equals(elementId)) { 74 return element; 75 } 76 IUAElement[] children = element.getChildren(); 77 for (int i=0;i<children.length;++i) { 78 UAElement result = findElement((UAElement)children[i], elementId); 79 if (result != null) { 80 return result; 81 } 82 } 83 return null; 84 } 85 } 86 | Popular Tags |