KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > util > ImporterUtil


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * ImporterUtil.java,v 1.1 2005/05/16 14:19:18 marcelop Exp
16  */

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 /**
31  * Utility methods and classes. This class cannot import UI code because it is used on headless
32  * scenarios.
33  *
34  * @since 2.1.0
35  */

36 public class ImporterUtil
37 {
38   public static final int ACTION_DEFAULT = 0;
39   public static final int ACTION_MESSAGE_NONE = 1; //0000 0000 0001
40
public static final int ACTION_MESSAGE_SET = 2; //0000 0000 0010
41
public static final int ACTION_MESSAGE_SET_TYPED = 4; //0000 0000 0100
42
public static final int ACTION_MESSAGE_SET_ERROR = 8; //0000 0000 1000
43
public static final int ACTION_DIALOG_NONE = 1 << 8; //0001 0000 0000
44
public static final int ACTION_DIALOG_SHOW_IF_HAS_CHILD = 2 << 8; //0010 0000 0000
45
public static final int ACTION_DIALOG_SHOW = 4 << 8; //0100 0000 0000
46
public static final int ACTION_DIALOG_SHOW_ERROR = 8 << 8; //1000 0000 0000
47

48   private static final int ACTION_MESSAGE_MASK = 0x00F; //0000 0000 1111
49
private static final int ACTION_DIALOG_MASK = 0xF00; //1111 0000 0000
50

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 JavaDoc 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   /**
186    * Creates a new status based on the specified status, setting a new pluginID and
187    * code.
188    * @param baseStatus
189    * @param pluginID
190    * @param code
191    * @return IStatus
192    */

193   public static IStatus createStatus(IStatus baseStatus, String JavaDoc 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 JavaDoc throwable, boolean showErrorDialog)
202   {
203     while (true)
204     {
205       Throwable JavaDoc 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 JavaDoc message = throwable.getLocalizedMessage();
233       if (message == null)
234       {
235         message = throwable.getMessage();
236       }
237       if (message == null)
238       {
239         String JavaDoc 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 JavaDoc[]{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 JavaDoc validPluginID(String JavaDoc base)
264   {
265     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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         //do nothing
272
}
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