1 17 package org.eclipse.emf.importer.util; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.MultiStatus; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.emf.common.util.WrappedException; 25 import org.eclipse.emf.ecore.plugin.EcorePlugin; 26 import org.eclipse.emf.ecore.resource.ResourceSet; 27 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 28 import org.eclipse.emf.importer.ImporterPlugin; 29 30 36 public class ImporterUtil 37 { 38 public static final int ACTION_DEFAULT = 0; 39 public static final int ACTION_MESSAGE_NONE = 1; public static final int ACTION_MESSAGE_SET = 2; public static final int ACTION_MESSAGE_SET_TYPED = 4; public static final int ACTION_MESSAGE_SET_ERROR = 8; public static final int ACTION_DIALOG_NONE = 1 << 8; public static final int ACTION_DIALOG_SHOW_IF_HAS_CHILD = 2 << 8; public static final int ACTION_DIALOG_SHOW = 4 << 8; public static final int ACTION_DIALOG_SHOW_ERROR = 8 << 8; 48 private static final int ACTION_MESSAGE_MASK = 0x00F; private static final int ACTION_DIALOG_MASK = 0xF00; 51 public static class DecodedAction 52 { 53 public int message; 54 public int dialog; 55 } 56 57 public static DecodedAction decodeAction(int actionCode) 58 { 59 DecodedAction decodedAction = new DecodedAction(); 60 61 decodedAction.message = actionCode & ACTION_MESSAGE_MASK; 62 if (ACTION_MESSAGE_SET_ERROR == (decodedAction.message & ACTION_MESSAGE_SET_ERROR)) 63 { 64 decodedAction.message = ACTION_MESSAGE_SET_ERROR; 65 } 66 else if (ACTION_MESSAGE_SET_TYPED == (decodedAction.message & ACTION_MESSAGE_SET_TYPED)) 67 { 68 decodedAction.message = ACTION_MESSAGE_SET_TYPED; 69 } 70 else if (ACTION_MESSAGE_SET == (decodedAction.message & ACTION_MESSAGE_SET)) 71 { 72 decodedAction.message = ACTION_MESSAGE_SET; 73 } 74 else if (ACTION_MESSAGE_NONE == (decodedAction.message & ACTION_MESSAGE_NONE)) 75 { 76 decodedAction.message = ACTION_MESSAGE_NONE; 77 } 78 else 79 { 80 decodedAction.message = ACTION_DEFAULT; 81 } 82 83 decodedAction.dialog = actionCode & ACTION_DIALOG_MASK; 84 if (ACTION_DIALOG_SHOW_ERROR == (decodedAction.dialog & ACTION_DIALOG_SHOW_ERROR)) 85 { 86 decodedAction.dialog = ACTION_DIALOG_SHOW_ERROR; 87 } 88 else if(ACTION_DIALOG_SHOW == (decodedAction.dialog & ACTION_DIALOG_SHOW)) 89 { 90 decodedAction.dialog = ACTION_DIALOG_SHOW; 91 } 92 else if(ACTION_DIALOG_SHOW_IF_HAS_CHILD == (decodedAction.dialog & ACTION_DIALOG_SHOW_IF_HAS_CHILD)) 93 { 94 decodedAction.dialog = ACTION_DIALOG_SHOW_IF_HAS_CHILD; 95 } 96 else if(ACTION_DIALOG_NONE == (decodedAction.dialog & ACTION_DIALOG_NONE)) 97 { 98 decodedAction.dialog = ACTION_DIALOG_NONE; 99 } 100 else 101 { 102 decodedAction.dialog = ACTION_DEFAULT; 103 } 104 105 return decodedAction; 106 } 107 108 public static int computeActionCode(IStatus status) 109 { 110 if (ImporterPlugin.ID.equals(status.getPlugin())) 111 { 112 int actionCode = status.getCode(); 113 if (status.isMultiStatus()) 114 { 115 IStatus[] children = status.getChildren(); 116 for (int i = 0; i < children.length; i++) 117 { 118 actionCode |= computeActionCode(children[i]); 119 } 120 } 121 return actionCode; 122 } 123 else 124 { 125 return ImporterUtil.ACTION_DEFAULT; 126 } 127 } 128 129 130 public static class MergedStatus extends MultiStatus 131 { 132 public MergedStatus(IStatus baseStatus) 133 { 134 super(baseStatus.getPlugin(), 135 baseStatus.getCode(), 136 baseStatus.getChildren(), 137 baseStatus.getMessage(), 138 baseStatus.getException()); 139 140 setSeverity(baseStatus.getSeverity()); 141 } 142 143 public void setPlugin(String pluginId) 144 { 145 super.setPlugin(pluginId); 146 } 147 148 public void setCode(int code) 149 { 150 super.setCode(code); 151 } 152 } 153 154 public static MultiStatus mergeStatus(IStatus baseStatus, IStatus statusToBeMerged) 155 { 156 if (baseStatus == null) 157 { 158 if (statusToBeMerged == null) 159 { 160 return null; 161 } 162 else 163 { 164 return new MergedStatus(statusToBeMerged); 165 } 166 } 167 168 MultiStatus multiStatus = null; 169 if (baseStatus instanceof MultiStatus) 170 { 171 multiStatus = ((MultiStatus)baseStatus); 172 } 173 else 174 { 175 multiStatus = new MergedStatus(baseStatus); 176 } 177 178 if (statusToBeMerged != null) 179 { 180 multiStatus.merge(statusToBeMerged); 181 } 182 return multiStatus; 183 } 184 185 193 public static IStatus createStatus(IStatus baseStatus, String pluginID, int code) 194 { 195 MergedStatus mergedStatus = new MergedStatus(baseStatus); 196 mergedStatus.setPlugin(pluginID); 197 mergedStatus.setCode(code); 198 return mergedStatus; 199 } 200 201 public static IStatus createErrorStatus(Throwable throwable, boolean showErrorDialog) 202 { 203 while (true) 204 { 205 Throwable cause = 206 throwable instanceof WrappedException ? ((WrappedException)throwable).exception() : 207 throwable.getCause() != null ? throwable.getCause() : 208 null; 209 210 if (cause != null && cause != throwable) 211 { 212 throwable = cause; 213 } 214 else 215 { 216 break; 217 } 218 } 219 220 IStatus status = null; 221 if (throwable instanceof CoreException) 222 { 223 IStatus originalStatus = ((CoreException)throwable).getStatus(); 224 if (originalStatus != null && originalStatus.getSeverity() == IStatus.ERROR) 225 { 226 status = originalStatus; 227 } 228 } 229 230 if (status == null) 231 { 232 String message = throwable.getLocalizedMessage(); 233 if (message == null) 234 { 235 message = throwable.getMessage(); 236 } 237 if (message == null) 238 { 239 String exceptionName = throwable.getClass().getName(); 240 int index = exceptionName.lastIndexOf('.'); 241 if (index >= 0) 242 { 243 exceptionName = exceptionName.substring(index+1); 244 } 245 message = ImporterPlugin.INSTANCE.getString("_UI_GenericException_message", new Object []{exceptionName}); 246 } 247 248 status = new Status(IStatus.ERROR, 249 ImporterPlugin.ID, showErrorDialog ? ACTION_DIALOG_SHOW_ERROR : ACTION_DEFAULT, 250 message, throwable); 251 } 252 253 return status; 254 } 255 256 public static ResourceSet createResourceSet() 257 { 258 ResourceSet result = new ResourceSetImpl(); 259 result.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap()); 260 return result; 261 } 262 263 public static String validPluginID(String base) 264 { 265 StringBuffer sb = new StringBuffer (base); 266 for (int i = sb.length() - 1; i >= 0; i--) 267 { 268 char c = sb.charAt(i); 269 if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '.') 270 { 271 } 273 else if (c == ' ') 274 { 275 sb.deleteCharAt(i); 276 } 277 else if (c == '-') 278 { 279 sb.setCharAt(i, '_'); 280 } 281 } 282 return sb.toString(); 283 } 284 } 285 | Popular Tags |