1 11 package org.eclipse.help.internal.extension; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtensionRegistry; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.help.AbstractContentExtensionProvider; 25 import org.eclipse.help.IContentExtension; 26 import org.eclipse.help.internal.HelpPlugin; 27 import org.eclipse.help.internal.UAElementFactory; 28 29 33 public class ContentExtensionManager { 34 35 private static final String EXTENSION_POINT_ID_CONTENT_EXTENSION = HelpPlugin.PLUGIN_ID + ".contentExtension"; private static final String ELEMENT_NAME_CONTENT_EXTENSION_PROVIDER = "contentExtensionProvider"; private static final String ATTRIBUTE_NAME_CLASS = "class"; private static final ContentExtension[] EMPTY_ARRAY = new ContentExtension[0]; 39 40 private AbstractContentExtensionProvider[] contentExtensionProviders; 41 private Map extensionsByPath; 42 private Map replacesByPath; 43 44 47 public ContentExtension[] getExtensions(String locale) { 48 if (extensionsByPath == null) { 49 loadExtensions(locale); 50 } 51 List extensions = new ArrayList (); 52 Iterator iter = extensionsByPath.values().iterator(); 53 while (iter.hasNext()) { 54 extensions.addAll((Collection )iter.next()); 55 } 56 iter = replacesByPath.values().iterator(); 57 while (iter.hasNext()) { 58 extensions.addAll((Collection )iter.next()); 59 } 60 return (ContentExtension[])extensions.toArray(new ContentExtension[extensions.size()]); 61 } 62 63 66 public ContentExtension[] getExtensions(String path, int type, String locale) { 67 if (extensionsByPath == null) { 68 loadExtensions(locale); 69 } 70 Map map = (type == ContentExtension.CONTRIBUTION ? extensionsByPath : replacesByPath); 71 List extensions = (List )map.get(path); 72 if (extensions != null) { 73 return (ContentExtension[])extensions.toArray(new ContentExtension[extensions.size()]); 74 } 75 return EMPTY_ARRAY; 76 } 77 78 82 public void clearCache() { 83 if (extensionsByPath != null) { 84 extensionsByPath.clear(); 85 } 86 if (replacesByPath != null) { 87 replacesByPath.clear(); 88 } 89 } 90 91 95 private void loadExtensions(String locale) { 96 extensionsByPath = new HashMap (); 97 replacesByPath = new HashMap (); 98 contentExtensionProviders = getContentExtensionProviders(); 99 for (int i=0;i<contentExtensionProviders.length;++i) { 100 IContentExtension[] extensions = contentExtensionProviders[i].getContentExtensions(locale); 101 for (int j=0;j<extensions.length;++j) { 102 ContentExtension extension = (extensions[j] instanceof ContentExtension ? (ContentExtension)extensions[j] : (ContentExtension)UAElementFactory.newElement(extensions[j])); 103 String content = extension.getContent(); 104 String path = extension.getPath(); 105 if (content != null && path != null) { 106 int type = extension.getType(); 107 Map map = (type == IContentExtension.CONTRIBUTION ? extensionsByPath : replacesByPath); 108 content = normalizePath(content); 109 path = normalizePath(path); 110 extension.setContent(content); 111 extension.setPath(path); 112 List list = (List )map.get(path); 113 if (list == null) { 114 list = new ArrayList (); 115 map.put(path, list); 116 } 117 list.add(extension); 118 } 119 } 120 } 121 } 122 123 126 private AbstractContentExtensionProvider[] getContentExtensionProviders() { 127 if (contentExtensionProviders == null) { 128 List providers = new ArrayList (); 129 IExtensionRegistry registry = Platform.getExtensionRegistry(); 130 IConfigurationElement[] elements = registry.getConfigurationElementsFor(EXTENSION_POINT_ID_CONTENT_EXTENSION); 131 for (int i=0;i<elements.length;++i) { 132 IConfigurationElement elem = elements[i]; 133 if (elem.getName().equals(ELEMENT_NAME_CONTENT_EXTENSION_PROVIDER)) { 134 try { 135 AbstractContentExtensionProvider provider = (AbstractContentExtensionProvider)elem.createExecutableExtension(ATTRIBUTE_NAME_CLASS); 136 providers.add(provider); 137 } 138 catch (CoreException e) { 139 String msg = "Error instantiating user assistance content extension provider class \"" + elem.getAttribute(ATTRIBUTE_NAME_CLASS) + '"'; HelpPlugin.logError(msg, e); 142 } 143 } 144 } 145 contentExtensionProviders = (AbstractContentExtensionProvider[])providers.toArray(new AbstractContentExtensionProvider[providers.size()]); 146 } 147 return contentExtensionProviders; 148 } 149 150 155 private String normalizePath(String path) { 156 int bundleStart, bundleEnd; 157 int pathStart, pathEnd; 158 int elementStart, elementEnd; 159 160 bundleStart = path.charAt(0) == '/' ? 1 : 0; 161 bundleEnd = path.indexOf('/', bundleStart); 162 163 pathStart = bundleEnd + 1; 164 pathEnd = path.indexOf('#', pathStart); 165 if (pathEnd == -1) { 166 int lastSlash = path.lastIndexOf('/'); 167 if (lastSlash > 0) { 168 int secondLastSlash = path.lastIndexOf('/', lastSlash - 1); 169 if (secondLastSlash != -1) { 170 String secondLastToken = path.substring(secondLastSlash, lastSlash); 171 boolean hasDot = (secondLastToken.indexOf('.') != -1); 172 if (hasDot) { 173 pathEnd = lastSlash; 174 } 175 } 176 } 177 if (pathEnd == -1) { 178 pathEnd = path.length(); 179 } 180 } 181 182 elementStart = Math.min(pathEnd + 1, path.length()); 183 elementEnd = path.length(); 184 185 if (bundleEnd > bundleStart && pathStart > bundleEnd && pathEnd > pathStart && elementStart >= pathEnd && elementEnd >= elementStart) { 186 String bundleId = path.substring(bundleStart, bundleEnd); 187 String relativePath = path.substring(pathStart, pathEnd); 188 String elementId = path.substring(elementStart, elementEnd); 189 path = '/' + bundleId + '/' + relativePath; 190 if (elementId.length() > 0) { 191 path += '#' + elementId; 192 } 193 } 194 return path; 195 } 196 } 197 | Popular Tags |