Clover coverage report - MetaClass - 1.1
Coverage timestamp: Tue Apr 27 2004 10:46:24 EST
file stats: LOC: 363   Methods: 17
NCLOC: 197   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
SAXMetaClassSerializer.java 100% 100% 100% 100%
coverage
 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  4
     public void serialize( final ContentHandler handler,
 47   
                            final ClassDescriptor descriptor )
 48   
         throws SAXException
 49   
     {
 50  4
         handler.startDocument();
 51  4
         serializeClass( handler, descriptor );
 52  4
         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  6
     void serializeClass( final ContentHandler handler,
 63   
                          final ClassDescriptor descriptor )
 64   
         throws SAXException
 65   
     {
 66  6
         final AttributesImpl atts = new AttributesImpl();
 67  6
         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, descriptor.getName() );
 68  6
         add( atts, MetaClassIOXml.VERSION_ATTRIBUTE, MetaClassIOXml.VERSION );
 69  6
         start( handler, MetaClassIOXml.CLASS_ELEMENT, atts );
 70  6
         serializeFields( handler, descriptor.getFields() );
 71  6
         serializeMethods( handler, descriptor.getMethods() );
 72  6
         serializeAttributes( handler, descriptor.getAttributes() );
 73  6
         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  8
     void serializeFields( final ContentHandler handler,
 84   
                           final FieldDescriptor[] descriptors )
 85   
         throws SAXException
 86   
     {
 87  8
         if( 0 == descriptors.length )
 88   
         {
 89  6
             return;
 90   
         }
 91  2
         start( handler, MetaClassIOXml.FIELDS_ELEMENT );
 92  2
         for( int i = 0; i < descriptors.length; i++ )
 93   
         {
 94  2
             serializeField( handler, descriptors[ i ] );
 95   
         }
 96  2
         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  2
     void serializeField( final ContentHandler handler,
 107   
                          final FieldDescriptor descriptor )
 108   
         throws SAXException
 109   
     {
 110  2
         final AttributesImpl atts = new AttributesImpl();
 111  2
         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, descriptor.getName() );
 112  2
         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, descriptor.getType() );
 113  2
         start( handler, MetaClassIOXml.FIELD_ELEMENT, atts );
 114  2
         serializeAttributes( handler, descriptor.getAttributes() );
 115  2
         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  8
     void serializeMethods( final ContentHandler handler,
 126   
                            final MethodDescriptor[] descriptors )
 127   
         throws SAXException
 128   
     {
 129  8
         if( 0 == descriptors.length )
 130   
         {
 131  6
             return;
 132   
         }
 133  2
         start( handler, MetaClassIOXml.METHODS_ELEMENT );
 134  2
         for( int i = 0; i < descriptors.length; i++ )
 135   
         {
 136  2
             serializeMethod( handler, descriptors[ i ] );
 137   
         }
 138  2
         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  2
     void serializeMethod( final ContentHandler handler,
 149   
                           final MethodDescriptor descriptor )
 150   
         throws SAXException
 151   
     {
 152  2
         final AttributesImpl atts = new AttributesImpl();
 153  2
         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, descriptor.getName() );
 154  2
         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, descriptor.getReturnType() );
 155  2
         start( handler, MetaClassIOXml.METHOD_ELEMENT, atts );
 156  2
         serializeParameters( handler, descriptor.getParameters() );
 157  2
         serializeAttributes( handler, descriptor.getAttributes() );
 158  2
         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  4
     void serializeParameters( final ContentHandler handler,
 169   
                               final ParameterDescriptor[] parameters )
 170   
         throws SAXException
 171   
     {
 172  4
         if( 0 == parameters.length )
 173   
         {
 174  2
             return;
 175   
         }
 176  2
         start( handler, MetaClassIOXml.PARAMETERS_ELEMENT );
 177  2
         for( int i = 0; i < parameters.length; i++ )
 178   
         {
 179  2
             serializeParameter( handler, parameters[ i ] );
 180   
         }
 181  2
         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  2
     void serializeParameter( final ContentHandler handler,
 192   
                              final ParameterDescriptor parameter )
 193   
         throws SAXException
 194   
     {
 195  2
         final AttributesImpl atts = new AttributesImpl();
 196  2
         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, parameter.getName() );
 197  2
         add( atts, MetaClassIOXml.TYPE_ATTRIBUTE, parameter.getType() );
 198  2
         start( handler, MetaClassIOXml.PARAMETER_ELEMENT, atts );
 199  2
         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  12
     void serializeAttributes( final ContentHandler handler,
 210   
                               final Attribute[] attributes )
 211   
         throws SAXException
 212   
     {
 213  12
         if( 0 == attributes.length )
 214   
         {
 215  8
             return;
 216   
         }
 217  4
         start( handler, MetaClassIOXml.ATTRIBUTES_ELEMENT );
 218  4
         for( int i = 0; i < attributes.length; i++ )
 219   
         {
 220  4
             serializeAttribute( handler, attributes[ i ] );
 221   
         }
 222  4
         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  4
     void serializeAttribute( final ContentHandler handler,
 233   
                              final Attribute attribute )
 234   
         throws SAXException
 235   
     {
 236  4
         final AttributesImpl atts = new AttributesImpl();
 237  4
         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, attribute.getName() );
 238   
 
 239  4
         start( handler, MetaClassIOXml.ATTRIBUTE_ELEMENT, atts );
 240   
 
 241  4
         text( handler, attribute.getValue() );
 242  4
         serializeAttributeParams( handler, attribute );
 243   
 
 244  4
         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  6
     void serializeAttributeParams( final ContentHandler handler,
 255   
                                    final Attribute attribute )
 256   
         throws SAXException
 257   
     {
 258  6
         final String[] names = attribute.getParameterNames();
 259  6
         for( int i = 0; i < names.length; i++ )
 260   
         {
 261  2
             final String name = names[ i ];
 262  2
             final String value = attribute.getParameter( name );
 263  2
             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  2
     void serializeAttributeParam( final ContentHandler handler,
 276   
                                   final String name,
 277   
                                   final String value )
 278   
         throws SAXException
 279   
     {
 280  2
         final AttributesImpl atts = new AttributesImpl();
 281  2
         add( atts, MetaClassIOXml.NAME_ATTRIBUTE, name );
 282  2
         add( atts, MetaClassIOXml.VALUE_ATTRIBUTE, value );
 283  2
         start( handler, MetaClassIOXml.PARAM_ELEMENT, atts );
 284  2
         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  32
     void add( final AttributesImpl atts,
 295   
               final String name,
 296   
               final String value )
 297   
     {
 298  32
         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  10
     void start( final ContentHandler handler,
 309   
                 final String name )
 310   
         throws SAXException
 311   
     {
 312  10
         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  28
     void start( final ContentHandler handler,
 324   
                 final String name,
 325   
                 final AttributesImpl atts )
 326   
         throws SAXException
 327   
     {
 328  28
         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  6
     void text( final ContentHandler handler,
 339   
                final String text )
 340   
         throws SAXException
 341   
     {
 342  6
         if( null != text )
 343   
         {
 344  2
             final char[] ch = text.toCharArray();
 345  2
             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  28
     void end( final ContentHandler handler,
 357   
               final String name )
 358   
         throws SAXException
 359   
     {
 360  28
         handler.endElement( EMPTY_NAMESPACE, name, name );
 361   
     }
 362   
 }
 363