1 11 package org.eclipse.help.internal.dynamic; 12 13 import org.eclipse.help.internal.Include; 14 import org.eclipse.help.internal.UAElement; 15 16 20 public class IncludeHandler extends ProcessorHandler { 21 22 private IncludeResolver resolver; 23 private DocumentReader reader; 24 private String locale; 25 26 30 public IncludeHandler(DocumentReader reader, String locale) { 31 this.reader = reader; 32 this.locale = locale; 33 } 34 35 public short handle(UAElement element, String id) { 36 if (element instanceof Include) { 37 String path = ((Include)element).getPath(); 38 if (path != null && path.length() > 0) { 39 String bundleId = getBundleId(path); 40 String relativePath = getRelativePath(path); 41 String elementId = getElementId(path); 42 if (bundleId != null && relativePath != null && elementId != null) { 43 resolveInclude(bundleId, relativePath, elementId, element, locale); 44 } 45 } 46 else { 47 element.getParentElement().removeChild(element); 49 } 50 return HANDLED_SKIP; 51 } 52 return UNHANDLED; 53 } 54 55 59 private void resolveInclude(String bundleId, String relativePath, String elementId, UAElement element, String locale) { 60 if (resolver == null) { 61 resolver = new IncludeResolver(getProcessor(), reader, locale); 62 } 63 UAElement parent = element.getParentElement(); 64 if (parent != null) { 65 try { 66 UAElement nodeToInclude = resolver.resolve(bundleId, relativePath, elementId); 67 parent.insertBefore(nodeToInclude, element); 68 parent.removeChild(element); 69 } 70 catch (Throwable t) { 71 parent.removeChild(element); 73 } 74 } 75 } 76 77 80 private String getBundleId(String path) { 81 if (path.charAt(0) == '/') { 82 int index = path.indexOf('/', 1); 83 if (index > 1) { 84 return path.substring(1, index); 85 } 86 } 87 else { 88 int index = path.indexOf('/'); 90 if (index != -1) { 91 return path.substring(0, index); 92 } 93 } 94 return null; 95 } 96 97 100 private String getRelativePath(String path) { 101 int startIndex = path.indexOf('/', 1); 102 int endIndex = path.indexOf('#'); 103 if (endIndex == -1) { 104 endIndex = path.lastIndexOf('/'); 106 } 107 if (startIndex != -1 && endIndex > startIndex + 1) { 108 return path.substring(startIndex + 1, endIndex); 109 } 110 return null; 111 } 112 113 116 private String getElementId(String path) { 117 int index = path.indexOf('#'); 118 if (index == -1) { 119 index = path.lastIndexOf('/'); 121 } 122 if (index != -1 && index < path.length() - 1) { 123 return path.substring(index + 1); 124 } 125 return null; 126 } 127 } 128 | Popular Tags |