Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1111d76c7f69a246bbca37a66f8138a77e1d991b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.ws.internal.common;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.jst.ws.internal.plugin.WebServicePlugin;


public class StatusObjectHandler
{
  // Copyright
  public static final String copyright = "(c) Copyright IBM Corporation 2000, 2002.";
  private static final int MAX_DETAILS = 16;

  public static IStatus generateTreeStatus (IStatus status) 
  	{
    //
    // There could be lots of Throwables, CoreExceptions,
    // WrappedExceptions and IStatus objects beneath the
    // given status. Find them.
    //
    LinkedList statusList = new LinkedList();
    appendStatus(MAX_DETAILS, statusList, status, status.getSeverity());
    if (statusList.size() > 0)
    {
      IStatus newStatus = (IStatus)statusList.removeFirst();
      if (statusList.size() > 0)
      {
        IStatus[] secondaryStatusArray = new IStatus[statusList.size()];
        int n = 0;
        Iterator i = statusList.iterator();
        while (i.hasNext())
        {
          secondaryStatusArray[n++] = (IStatus)i.next();
        }
        newStatus = new MultiStatus(status.getPlugin(),status.getCode(),secondaryStatusArray,status.getMessage(),status.getException());
      }
      return newStatus;
    }
    else 
    	return null;
  }

  //
  // Appends zero or more IStatus objects to the list of IStatus objects
  // based upon an IStatus.
  //
  private static int appendStatus ( int max, List statusList, IStatus status, int severity )
  {
    if (max > 0 && status != null)
    {
      //
      // Append an IStatus object for the given status' message.
      //
      if (status.getMessage() != null)
      {
        max = append(statusList,max,severity,status.getPlugin(),status.getCode(),status.getMessage(),status.getException());
      }
      //
      // Append the IStatus' Throwable, if any.
      //
      max = appendThrowable(max,statusList,status.getException(),severity);
      //
      // If the IStatus is a multi status, append the children.
      //
      IStatus[] children = status.getChildren();
      for (int i=0; i<children.length && max>0; i++)
      {
        max = appendStatus(max,statusList,children[i],severity);
      }
    }
    return max;
  }

  //
  // Appends zero or more IStatus objects to the list of IStatus objects
  // based upon a Throwable. WrappedExceptions and CoreExceptions are
  // given special treatment.
  //
  private static int appendThrowable ( int max, List statusList, Throwable throwable, int severity )
  {
    if (max > 0 && throwable != null)
    {
      //
      // Append an IStatus object for the exception's message.
      //
      if (throwable.getLocalizedMessage() != null)
      {
        max = append(statusList,max,severity,WebServicePlugin.ID,0,throwable.getLocalizedMessage(),throwable);
      }
      //
      // If the exception is a WrappedException, append the wrapped Exception.
      //
      if (throwable instanceof WrappedException)
      {
        WrappedException wexc = (WrappedException)throwable;
        max = appendThrowable(max,statusList,wexc.exception(),severity);
      }
      //
      // If the exception is a CoreException, append the enclosed IStatus.
      //
      if (throwable instanceof CoreException)
      {
        CoreException cexc = (CoreException)throwable;
        max = appendStatus(max,statusList,cexc.getStatus(),severity);
      }
    }
    return max;
  }

  //
  // Creates and appends an IStatus to the list of IStatus objects.
  //
  private static int append ( List statusList, int max, int severity, String id, int code, String message, Throwable throwable )
  {
    if (max > 0)
    {
      IStatus status = new Status(severity,id,code,message,throwable);
      statusList.add(status);
      max--;
    }
    return max;
  }
}

Back to the top