1 11 package org.eclipse.ui.internal.intro.impl.model.loader; 12 13 import org.eclipse.core.runtime.IConfigurationElement; 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement; 16 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement; 17 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroIdElement; 18 import org.eclipse.ui.internal.intro.impl.model.util.BundleUtil; 19 import org.eclipse.ui.internal.intro.impl.util.Log; 20 import org.osgi.framework.Bundle; 21 import org.w3c.dom.Element ; 22 23 24 28 public class ModelLoaderUtil { 29 30 37 public static boolean isValidElementName(IConfigurationElement element, 38 String validName) { 39 40 if (element.getName().equals(validName)) 41 return true; 42 return false; 44 } 45 46 56 public static IConfigurationElement validateSingleContribution( 57 IConfigurationElement[] configElements, String logAttribute) { 58 59 int arraySize = configElements.length; 60 if (arraySize == 0) 61 return null; 63 64 IConfigurationElement configElement = configElements[0]; 66 if (Log.logInfo) { 67 String msg = "Loading " + getLogString(configElement, logAttribute); Log.info(msg); 69 } 70 71 if (arraySize != 1) { 72 for (int i = 1; i < arraySize; i++) 74 Log.warning(getLogString(configElements[i], logAttribute) 76 + " ignored due to multiple contributions"); } 78 return configElement; 79 } 80 81 85 public static String getLogString(IConfigurationElement element, 86 String logAttribute) { 87 StringBuffer buffer = new StringBuffer (element.getName()); 88 buffer.append(" element"); if (logAttribute != null) { 90 buffer.append(" with "); buffer.append(logAttribute); 92 buffer.append("=\""); buffer.append(element.getAttribute(logAttribute)); 94 } 95 buffer.append("\" in extension: "); buffer.append(element.getDeclaringExtension() 97 .getExtensionPointUniqueIdentifier()); 98 buffer.append(" in Bundle: "); buffer.append(element.getContributor().getName()); 100 101 102 return buffer.toString(); 103 } 104 105 106 107 108 118 public static Element validateSingleContribution(Bundle bundle, 119 Element[] elements, String logAttribute) { 120 121 int arraySize = elements.length; 122 if (arraySize == 0) 123 return null; 125 126 Element element = elements[0]; 128 if (Log.logInfo) { 129 String msg = "Loading " + getLogString(bundle, element, logAttribute); 131 Log.info(msg); 132 } 133 134 if (arraySize != 1) { 135 for (int i = 1; i < arraySize; i++) { 137 if (Log.logWarning) 138 Log.warning(getLogString(bundle, element, logAttribute) 140 + " ignored due to multiple contributions"); } 142 } 143 return element; 144 } 145 146 150 public static String getLogString(Bundle bundle, Element element, 151 String logAttribute) { 152 StringBuffer buffer = new StringBuffer (element.getNodeName()); 153 buffer.append(" element"); if (logAttribute != null) { 155 buffer.append(" with "); buffer.append(logAttribute); 157 buffer.append("=\""); buffer.append(element.getAttribute(logAttribute)); 159 } 160 buffer.append("\" from xml file in Bundle:"); buffer.append(bundle.getSymbolicName()); 162 163 164 return buffer.toString(); 165 } 166 167 168 169 176 public static Object createClassInstance(String pluginId, String className) { 177 if (pluginId == null || className == null) 179 return null; 180 Bundle bundle = Platform.getBundle(pluginId); 181 if (!BundleUtil.bundleHasValidState(bundle)) 182 return null; 183 184 Class aClass; 185 Object aObject; 186 try { 187 aClass = bundle.loadClass(className); 188 aObject = aClass.newInstance(); 189 return aObject; 190 } catch (Exception e) { 191 Log.error("Intro Could not instantiate: " + className + " in " + pluginId, e); 193 return null; 194 } 195 } 196 197 198 199 206 public static StringBuffer createPathToElementKey( 207 AbstractIntroIdElement element, boolean full) { 208 if (element.getId() == null) 209 return null; 210 StringBuffer buffer = new StringBuffer (element.getId()); 211 AbstractBaseIntroElement parent = (AbstractBaseIntroElement) element 212 .getParent(); 213 while (parent != null 214 && !parent.isOfType(AbstractIntroElement.MODEL_ROOT)) { 215 if (parent.getId() == null) 216 return null; 217 buffer.insert(0, parent.getId() + "."); parent = (AbstractBaseIntroElement) parent.getParent(); 219 } 220 return buffer; 221 } 222 } 223 | Popular Tags |