OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [beans/] [decoder/] [AbstractElementHandler.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* gnu.java.beans.decoder.AbstractElementHandler
2
   Copyright (C) 2004 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.java.beans.decoder;
39
 
40
import java.beans.ExceptionListener;
41
 
42
import org.xml.sax.Attributes;
43
 
44
/** ElementHandler manages a Context instance and interacts with
45
 * its parent and child handlers.
46
 *
47
 * @author Robert Schuster
48
 */
49
abstract class AbstractElementHandler implements ElementHandler
50
{
51
  /** The Context instance of this handler. The instance is available after the startElement()
52
   * method was called. Otherwise the handler is marked as failed.
53
   */
54
  private Context context;
55
 
56
  /** The parent handler. */
57
  private ElementHandler parent;
58
 
59
  /** Stores whether this handler is marked as failed. */
60
  private boolean hasFailed;
61
 
62
  /** Stores the character data which is contained in the body of the XML tag. */
63
  private StringBuffer buffer = new StringBuffer();
64
 
65
  /** Stores whether this ElementHandler can have subelements. The information for this is taken from
66
   * javabeans.dtd which can be found here:
67
   * <a href="http://java.sun.com/products/jfc/tsc/articles/persistence3/">Java Persistence Article</a>
68
   */
69
  private boolean allowsSubelements;
70
 
71
  /** Creates a new ElementHandler with the given ElementHandler instance
72
   * as parent.
73
   *
74
   * @param parentHandler The parent handler.
75
   */
76
  protected AbstractElementHandler(ElementHandler parentHandler,
77
                                   boolean allowsSubs)
78
  {
79
    parent = parentHandler;
80
    allowsSubelements = allowsSubs;
81
  }
82
 
83
  /** Evaluates the attributes and creates a Context instance.
84
   * If the creation of the Context instance fails the ElementHandler
85
   * is marked as failed which may affect the parent handler other.
86
   *
87
   * @param attributes Attributes of the XML tag.
88
   */
89
  public final void start(Attributes attributes,
90
                          ExceptionListener exceptionListener)
91
  {
92
    try
93
      {
94
        // lets the subclass create the appropriate Context instance
95
        context = startElement(attributes, exceptionListener);
96
      }
97
    catch (AssemblyException pe)
98
      {
99
        Throwable t = pe.getCause();
100
 
101
        if (t instanceof Exception)
102
          exceptionListener.exceptionThrown((Exception) t);
103
        else
104
          throw new InternalError("Unexpected Throwable type in AssemblerException. Please file a bug report.");
105
 
106
        notifyContextFailed();
107
 
108
        return;
109
      }
110
  }
111
 
112
  /** Analyses the content of the Attributes instance and creates a Context
113
   * object accordingly.
114
   * An AssemblerException is thrown when the Context instance could not
115
   * be created.
116
   *
117
   * @param attributes Attributes of the XML tag.
118
   * @return A Context instance.
119
   * @throws AssemblerException when Context instance could not be created.
120
   */
121
  protected abstract Context startElement(Attributes attributes, ExceptionListener exceptionListener)
122
    throws AssemblyException;
123
 
124
  /** Post-processes the Context.
125
   */
126
  public final void end(ExceptionListener exceptionListener)
127
  {
128
    // skips processing if the handler is marked as failed (because the Context
129
    // is then invalid or may not exist at all)
130
    if (!hasFailed)
131
      {
132
        try
133
          {
134
            // note: the order of operations is very important here
135
            // sends the stored character data to the Context
136
            endElement(buffer.toString());
137
 
138
            // reports to the parent handler if this handler's Context is a
139
            // statement (returning no value BACK to the parent's Context)
140
            if (context.isStatement())
141
              {
142
                // This may create a valid result in the parent's Context
143
                // or let it fail
144
                parent.notifyStatement(exceptionListener);
145
 
146
                // skips any further processing if the parent handler is now marked
147
                // as failed
148
                if (parent.hasFailed())
149
                  return;
150
              }
151
 
152
            // processes the Context and stores the result
153
            putObject(context.getId(), context.endContext(parent.getContext()));
154
 
155
            // transfers the Context's results to the parent's Context
156
            // if it is an expression (rather than a statement)
157
            if (! context.isStatement())
158
              parent.getContext().addParameterObject(context.getResult());
159
          }
160
        catch (AssemblyException pe)
161
          {
162
            // notifies that an exception was thrown in this handler's Context
163
            Throwable t = pe.getCause();
164
 
165
            if (t instanceof Exception)
166
              exceptionListener.exceptionThrown((Exception) t);
167
            else
168
              throw (InternalError) new InternalError("Severe problem while decoding XML data.")
169
                    .initCause(t);
170
 
171
            // marks the handler as failed
172
            notifyContextFailed();
173
          }
174
      }
175
  }
176
 
177
  /** Notifies the handler's Context that its child Context will not return
178
   * a value back. Some Context variants need this information to know when
179
   * a method or a constructor call can be made.
180
   *
181
   * This method is called by a child handler.
182
   */
183
  public void notifyStatement(ExceptionListener exceptionListener)
184
  {
185
    try
186
      {
187
 
188
        // propagates to parent handler first to generate objects
189
        // needed by this Context instance
190
        if(context.isStatement())
191
        {
192
                parent.notifyStatement(exceptionListener);
193
        }
194
 
195
        // Some Context instances do stuff which can fail now. If that
196
        // happens this handler is marked as failed.
197
        context.notifyStatement(parent.getContext());
198
      }
199
    catch (AssemblyException ae)
200
      {
201
        // notifies that an exception was thrown in this handler's Context
202
        Throwable t = ae.getCause();
203
 
204
        if (t instanceof Exception)
205
          exceptionListener.exceptionThrown((Exception) t);
206
        else
207
          throw (InternalError) new InternalError("Severe problem while decoding XML data.")
208
                .initCause(t);
209
 
210
        // marks the handler as failed
211
        notifyContextFailed();
212
      }
213
  }
214
 
215
  /** Marks this and any depending parent handlers as failed. Which means that on their end
216
   * no result is calculated.
217
   *
218
   * When a handler has failed no more handlers are accepted within it.
219
   */
220
  public final void notifyContextFailed()
221
  {
222
    hasFailed = true;
223
 
224
    // marks the parent handler as failed if its Context
225
    // is affected by the failure of this handler's Context
226
    if (parent.getContext().subContextFailed())
227
      parent.notifyContextFailed();
228
  }
229
 
230
  /** Returns whether this handler has failed.
231
   *
232
   * This is used to skip child elements.
233
   *
234
   * @return Whether this handler has failed.
235
   */
236
  public final boolean hasFailed()
237
  {
238
    return hasFailed;
239
  }
240
 
241
  /** Processes the character data when the element ends.
242
   *
243
   * The default implementation does nothing for convenience.
244
   *
245
   * @param characters
246
   * @throws AssemblerException
247
   */
248
  protected void endElement(String characters) throws AssemblyException
249
  {
250
    // XXX: throw an exception when unexpected character data is available?
251
  }
252
 
253
  /** Adds characters from the body of the XML tag to the buffer.
254
   *
255
   * @param ch
256
   * @param start
257
   * @param length
258
   * @throws SAXException
259
   */
260
  public final void characters(char[] ch, int start, int length)
261
  {
262
    // simply appends character data
263
    buffer.append(ch, start, length);
264
  }
265
 
266
  /** Stores an object globally under a unique id. If the id is
267
   * null the object is not stored.
268
   *
269
   * @param objectId
270
   * @param o
271
   */
272
  public void putObject(String objectId, Object o)
273
  {
274
    if (objectId != null)
275
      parent.putObject(objectId, o);
276
  }
277
 
278
  /** Returns a previously stored object. If the id is null the
279
   * result is null, too.
280
   *
281
   * @param objectId
282
   * @return Returns a previously stored object or null.
283
   */
284
  public Object getObject(String objectId) throws AssemblyException
285
  {
286
    return objectId == null ? null : parent.getObject(objectId);
287
  }
288
 
289
  /** Returns the Class instance as if called Class.forName() but
290
   * uses a ClassLoader given by the user.
291
   *
292
   * @param className
293
   * @return
294
   * @throws ClassNotFoundException
295
   */
296
  public Class instantiateClass(String className)
297
    throws ClassNotFoundException
298
  {
299
    return parent.instantiateClass(className);
300
  }
301
 
302
  public final boolean isSubelementAllowed(String subElementName)
303
  {
304
    return allowsSubelements && ! subElementName.equals("java");
305
  }
306
 
307
  public final Context getContext()
308
  {
309
    return context;
310
  }
311
 
312
  public final ElementHandler getParent()
313
  {
314
    return parent;
315
  }
316
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.