Skip to main content
summaryrefslogtreecommitdiffstats
blob: a15eb540ac90a780a4553d804fa2170f3d008b10 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
 * 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.consumption.common;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.ws.internal.common.AnyFilter;
import org.eclipse.jst.ws.internal.consumption.ConsumptionMessages;
import org.eclipse.jst.ws.internal.consumption.plugin.WebServiceConsumptionPlugin;
import org.eclipse.osgi.util.NLS;


/**
* This is a kind of Filter that accepts only objects
* which indicate a Java resource. In particular, any
* {@link org.eclipse.core.resources.IResource} whose
* pathname, or any object whose string representation as
* returned by <code>toString()</code>, ends in
* "<code>.java</code>" or "<code>.class</code>" will be accepted.
* All other objects will be rejected.
*/
public class JavaResourceFilter extends AnyFilter
{

  /**
  * A <code>JavaResourceFilter</code> constructed with this value will
  * accept "<code>.java</code>", but not "<code>.class</code>", objects.
  */
  public static final byte JAVA_FILES = (byte)1;

  /**
  * A <code>JavaResourceFilter</code> constructed with this value will
  * accept "<code>.class</code>", but not "<code>.java</code>", objects.
  */
  public static final byte CLASS_FILES = (byte)2;

  /**
  * A <code>JavaResourceFilter</code> constructed with this value will
  * accept both "<code>.java</code>" and "<code>.class</code>" objects.
  */
  public static final byte ALL_FILES = (byte)255;

  private byte extensions_;

  /**
  * Constructs a new <code>JavaResourceFilter</code> that filters
  * "<code>.java</code>" and "<code>.class</code>" objects.
  * Equivalent to <code>JavaResourceFilter(ALL_FILES)</code>.
  */
  public JavaResourceFilter ()
  {
    this(ALL_FILES);
  }

  /**
  * Constructs a new <code>JavaResourceFilter</code> that filters
  * "<code>.java</code>" objects, "<code>.class</code>" objects,
  * or both if the value of <code>extensions</code> is
  * {@link #JAVA_FILES}, {@link #CLASS_FILES} or {@link #ALL_FILES}
  * respectively.
  * @param extensions One of
  * {@link #JAVA_FILES}, {@link #CLASS_FILES} or {@link #ALL_FILES}.
  */
  public JavaResourceFilter ( byte extensions )
  {
    extensions_ = extensions;
  }

  /**
  * Returns the locale-specific name of this filter.
  * @return The locale-specific name of this filter.
  */
  public String getName ()
  {
    return ConsumptionMessages.JAVA_FILTER_NAME;
  }

  /**
  * Returns the locale-specific description of this filter.
  * @return The locale-specific description of this filter.
  */
  public String getDescription ()
  {
    return ConsumptionMessages.JAVA_FILTER_DESC;
  }

  /**
  * Returns an {@link org.eclipse.core.runtime.IStatus}
  * describing the <code>Filter</code>'s assessment of
  * the given <code>object</code>.
  * @param object The object to filter.
  * @return An {@link org.eclipse.core.runtime.IStatus}
  * indicating whether or not if the given <code>object</code>
  * represents a Java resource.
  */
  public IStatus statusOf ( Object object )
  {
    if (object == null)
    {
      return new Status(
        IStatus.ERROR,
        WebServiceConsumptionPlugin.ID,
        0,
        ConsumptionMessages.FILTER_MSG_ERROR_NULL_OBJECT,
        null
      );
    }

    String name = null;

    if (object instanceof IResource)
    {
      IResource resource = (IResource)object;
      name = resource.getFullPath().toString();
      if (resource.getType() != IResource.FILE)
      {
        return new Status(
          IStatus.ERROR,
          WebServiceConsumptionPlugin.ID,
          0,
          NLS.bind(ConsumptionMessages.FILTER_MSG_ERROR_NOT_FILE,new Object[] {name}),
          null
        );
      }
    }

    if (name == null)
    {
      name = object.toString();
    }

    if (!acceptsName(name))
    {
      return new Status(
        IStatus.ERROR,
        WebServiceConsumptionPlugin.ID,
        0,
        NLS.bind(ConsumptionMessages.JAVA_FILTER_MSG_ERROR_WRONG_EXTENSION,new Object[] {name}),
        null
      );
    }

    return new Status(IStatus.OK,WebServiceConsumptionPlugin.ID,0,"",null);
  }

  //
  // Checks if the given name is acceptable based upon its extension
  // and the value of the extensions_ bitmask.
  //
  private boolean acceptsName ( String name )
  {
    return ( 
      (name.endsWith(".java") && ((extensions_ & JAVA_FILES) != 0)) ||
      (name.endsWith(".class") && ((extensions_ & CLASS_FILES) != 0))
    );
  }
}

Back to the top