View Javadoc

1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.io;
9   
10  import org.codehaus.metaclass.model.Attribute;
11  import org.codehaus.metaclass.model.ClassDescriptor;
12  import org.codehaus.metaclass.model.FieldDescriptor;
13  import org.codehaus.metaclass.model.MethodDescriptor;
14  import org.codehaus.metaclass.model.ParameterDescriptor;
15  import org.xml.sax.ContentHandler;
16  import org.xml.sax.SAXException;
17  import org.xml.sax.helpers.AttributesImpl;
18  
19  /***
20   * Utility class that serializes a ClassDescriptor object
21   * to a SAX2 compliant ContentHandler.
22   *
23   * @author Peter Donald
24   * @version $Revision: 1.10 $ $Date: 2003/11/27 08:09:53 $
25   */
26  public class SAXMetaClassSerializer
27  {
28      /***
29       * Constant for CDATA type in attributes.
30       */
31      static final String CDATA_TYPE = "CDATA";
32  
33      /***
34       * Constant for empty namespace in attributes.
35       */
36      static final String EMPTY_NAMESPACE = "";
37  
38      /***
39       * Serialize the ClassDescriptor to as a Document to the
40       * specified ContentHandler.
41       *
42       * @param handler the handler
43       * @param descriptor the ClassDescriptor
44       * @throws SAXException if error during serilization
45       */
46      public void serialize( final ContentHandler handler,
47                             final ClassDescriptor descriptor )
48          throws SAXException
49      {
50          handler.startDocument();
51          serializeClass( handler, descriptor );
52          handler.endDocument();
53      }
54  
55      /***
56       * Serialize the ClassDescriptor to as an Element.
57       *
58       * @param handler the handler
59       * @param descriptor the ClassDescriptor
60       * @throws SAXException if error during serilization
61       */
62      void serializeClass( final ContentHandler handler,
63                           final ClassDescriptor descriptor )
64          throws SAXException
65      {
66          final AttributesImpl atts = new AttributesImpl();
67          add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, descriptor.getName() );
68          add( atts, MetaClassIOXml.VERSION_ATTRIBUTE, MetaClassIOXml.VERSION );
69          start( handler, MetaClassIOXml.CLASS_ELEMENT, atts );
70          serializeFields( handler, descriptor.getFields() );
71          serializeMethods( handler, descriptor.getMethods() );
72          serializeAttributes( handler, descriptor.getAttributes() );
73          end( handler, MetaClassIOXml.CLASS_ELEMENT );
74      }
75  
76      /***
77       * Serialize Fields.
78       *
79       * @param handler the handler
80       * @param descriptors the descriptors
81       * @throws SAXException if error during serilization
82       */
83      void serializeFields( final ContentHandler handler,
84                            final FieldDescriptor[] descriptors )
85          throws SAXException
86      {
87          if( 0 == descriptors.length )
88          {
89              return;
90          }
91          start( handler, MetaClassIOXml.FIELDS_ELEMENT );
92          for( int i = 0; i < descriptors.length; i++ )
93          {
94              serializeField( handler, descriptors[ i ] );
95          }
96          end( handler, MetaClassIOXml.FIELDS_ELEMENT );
97      }
98  
99      /***
100      * Serialize Field.
101      *
102      * @param handler the handler
103      * @param descriptor the descriptor
104      * @throws SAXException if error during serilization
105      */
106     void serializeField( final ContentHandler handler,
107                          final FieldDescriptor descriptor )
108         throws SAXException
109     {
110         final AttributesImpl atts = new AttributesImpl();
111         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, descriptor.getName() );
112         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, descriptor.getType() );
113         start( handler, MetaClassIOXml.FIELD_ELEMENT, atts );
114         serializeAttributes( handler, descriptor.getAttributes() );
115         end( handler, MetaClassIOXml.FIELD_ELEMENT );
116     }
117 
118     /***
119      * Serialize Method.
120      *
121      * @param handler the ContentHandler
122      * @param descriptors the descriptors
123      * @throws SAXException if error during serilization
124      */
125     void serializeMethods( final ContentHandler handler,
126                            final MethodDescriptor[] descriptors )
127         throws SAXException
128     {
129         if( 0 == descriptors.length )
130         {
131             return;
132         }
133         start( handler, MetaClassIOXml.METHODS_ELEMENT );
134         for( int i = 0; i < descriptors.length; i++ )
135         {
136             serializeMethod( handler, descriptors[ i ] );
137         }
138         end( handler, MetaClassIOXml.METHODS_ELEMENT );
139     }
140 
141     /***
142      * Serialize Method.
143      *
144      * @param handler the ContentHandler
145      * @param descriptor the descriptor
146      * @throws SAXException if error during serilization
147      */
148     void serializeMethod( final ContentHandler handler,
149                           final MethodDescriptor descriptor )
150         throws SAXException
151     {
152         final AttributesImpl atts = new AttributesImpl();
153         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, descriptor.getName() );
154         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, descriptor.getReturnType() );
155         start( handler, MetaClassIOXml.METHOD_ELEMENT, atts );
156         serializeParameters( handler, descriptor.getParameters() );
157         serializeAttributes( handler, descriptor.getAttributes() );
158         end( handler, MetaClassIOXml.METHOD_ELEMENT );
159     }
160 
161     /***
162      * Serialize methods parameters.
163      *
164      * @param handler the handler
165      * @param parameters the parameters
166      * @throws SAXException if error during serilization
167      */
168     void serializeParameters( final ContentHandler handler,
169                               final ParameterDescriptor[] parameters )
170         throws SAXException
171     {
172         if( 0 == parameters.length )
173         {
174             return;
175         }
176         start( handler, MetaClassIOXml.PARAMETERS_ELEMENT );
177         for( int i = 0; i < parameters.length; i++ )
178         {
179             serializeParameter( handler, parameters[ i ] );
180         }
181         end( handler, MetaClassIOXml.PARAMETERS_ELEMENT );
182     }
183 
184     /***
185      * Serialize method parameter.
186      *
187      * @param handler the handler
188      * @param parameter the parameter
189      * @throws SAXException if error during serilization
190      */
191     void serializeParameter( final ContentHandler handler,
192                              final ParameterDescriptor parameter )
193         throws SAXException
194     {
195         final AttributesImpl atts = new AttributesImpl();
196         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, parameter.getName() );
197         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, parameter.getType() );
198         start( handler, MetaClassIOXml.PARAMETER_ELEMENT, atts );
199         end( handler, MetaClassIOXml.PARAMETER_ELEMENT );
200     }
201 
202     /***
203      * Serialize attributes.
204      *
205      * @param handler the handler
206      * @param attributes the attributes
207      * @throws SAXException if error during serilization
208      */
209     void serializeAttributes( final ContentHandler handler,
210                               final Attribute[] attributes )
211         throws SAXException
212     {
213         if( 0 == attributes.length )
214         {
215             return;
216         }
217         start( handler, MetaClassIOXml.ATTRIBUTES_ELEMENT );
218         for( int i = 0; i < attributes.length; i++ )
219         {
220             serializeAttribute( handler, attributes[ i ] );
221         }
222         end( handler, MetaClassIOXml.ATTRIBUTES_ELEMENT );
223     }
224 
225     /***
226      * Serialize an attribute.
227      *
228      * @param handler the handler
229      * @param attribute the attribute
230      * @throws SAXException if error during serilization
231      */
232     void serializeAttribute( final ContentHandler handler,
233                              final Attribute attribute )
234         throws SAXException
235     {
236         final AttributesImpl atts = new AttributesImpl();
237         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, attribute.getName() );
238 
239         start( handler, MetaClassIOXml.ATTRIBUTE_ELEMENT, atts );
240 
241         text( handler, attribute.getValue() );
242         serializeAttributeParams( handler, attribute );
243 
244         end( handler, MetaClassIOXml.ATTRIBUTE_ELEMENT );
245     }
246 
247     /***
248      * Serialize an attributes parameters.
249      *
250      * @param handler the handler
251      * @param attribute the attribute
252      * @throws SAXException if error during serilization
253      */
254     void serializeAttributeParams( final ContentHandler handler,
255                                    final Attribute attribute )
256         throws SAXException
257     {
258         final String[] names = attribute.getParameterNames();
259         for( int i = 0; i < names.length; i++ )
260         {
261             final String name = names[ i ];
262             final String value = attribute.getParameter( name );
263             serializeAttributeParam( handler, name, value );
264         }
265     }
266 
267     /***
268      * Serialize a single attribute parameter.
269      *
270      * @param handler the handler
271      * @param name the name of parameter
272      * @param value the value of parameter
273      * @throws SAXException if error during serilization
274      */
275     void serializeAttributeParam( final ContentHandler handler,
276                                   final String name,
277                                   final String value )
278         throws SAXException
279     {
280         final AttributesImpl atts = new AttributesImpl();
281         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, name );
282         add( atts, MetaClassIOXml.VALUE_ATTRIBUTE, value );
283         start( handler, MetaClassIOXml.PARAM_ELEMENT, atts );
284         end( handler, MetaClassIOXml.PARAM_ELEMENT );
285     }
286 
287     /***
288      * Helper metho to add an attribute to a set.
289      *
290      * @param atts the attributes
291      * @param name the attribute name
292      * @param value the attribute value
293      */
294     void add( final AttributesImpl atts,
295               final String name,
296               final String value )
297     {
298         atts.addAttribute( EMPTY_NAMESPACE, name, name, CDATA_TYPE, value );
299     }
300 
301     /***
302      * Helper method to output a start element.
303      *
304      * @param handler the handler
305      * @param name the element name
306      * @throws SAXException if error during serilization
307      */
308     void start( final ContentHandler handler,
309                 final String name )
310         throws SAXException
311     {
312         start( handler, name, new AttributesImpl() );
313     }
314 
315     /***
316      * Helper method to output a start element.
317      *
318      * @param handler the handler
319      * @param name the element name
320      * @param atts the attributes
321      * @throws SAXException if error during serilization
322      */
323     void start( final ContentHandler handler,
324                 final String name,
325                 final AttributesImpl atts )
326         throws SAXException
327     {
328         handler.startElement( EMPTY_NAMESPACE, name, name, atts );
329     }
330 
331     /***
332      * Helper method to output some text.
333      *
334      * @param handler the handler
335      * @param text the text
336      * @throws SAXException if error during serilization
337      */
338     void text( final ContentHandler handler,
339                final String text )
340         throws SAXException
341     {
342         if( null != text )
343         {
344             final char[] ch = text.toCharArray();
345             handler.characters( ch, 0, ch.length );
346         }
347     }
348 
349     /***
350      * Helper method to output an end element.
351      *
352      * @param handler the handler
353      * @param name the element name
354      * @throws SAXException if error during serilization
355      */
356     void end( final ContentHandler handler,
357               final String name )
358         throws SAXException
359     {
360         handler.endElement( EMPTY_NAMESPACE, name, name );
361     }
362 }