Clover coverage report - DNA Site - 1.1
Coverage timestamp: Sun May 2 2004 15:33:21 BST
file stats: LOC: 135   Methods: 3
NCLOC: 53   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
SAXConfigurationSerializer.java 100% 100% 100% 100%
coverage
 1   
 /*
 2   
  * Copyright (C) The DNA Group. All rights reserved.
 3   
  *
 4   
  * This software is published under the terms of the DNA
 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.dna.impl;
 9   
 
 10   
 import org.codehaus.dna.Configuration;
 11   
 import org.xml.sax.ContentHandler;
 12   
 import org.xml.sax.SAXException;
 13   
 import org.xml.sax.helpers.AttributesImpl;
 14   
 
 15   
 /**
 16   
  * Utility class that serializes a Configuration object
 17   
  * to a SAX2 compliant ContentHandler.
 18   
  *
 19   
  * @author Peter Donald
 20   
  * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
 21   
  */
 22   
 public class SAXConfigurationSerializer
 23   
 {
 24   
     /**
 25   
      * Constant for CDATA type in attributes.
 26   
      */
 27   
     private static final String CDATA_TYPE = "CDATA";
 28   
 
 29   
     /**
 30   
      * Constant for empty namespace in attributes.
 31   
      */
 32   
     private static final String EMPTY_NAMESPACE = "";
 33   
 
 34   
     /**
 35   
      * Constant for start of CDATA content sections.
 36   
      */
 37   
     //private static final String CDATA_PREFIX = "<![CDATA[";
 38   
 
 39   
     /**
 40   
      * Constant for end of CDATA content sections.
 41   
      */
 42   
     //private static final String CDATA_POSTFIX = "]]>";
 43   
 
 44   
     /**
 45   
      * Serialize the configuration to as a Document to the
 46   
      * specified ContentHandler. The serialization writes
 47   
      * out an Element for each configuration object.
 48   
      *
 49   
      * @param handler the ContentHandler to write Configuration out to
 50   
      * @param configuration the Configuration
 51   
      * @throws SAXException if the handler throws an exception
 52   
      */
 53  8
     public void serialize( final Configuration configuration,
 54   
                            final ContentHandler handler )
 55   
         throws SAXException
 56   
     {
 57  8
         handler.startDocument();
 58  8
         serializeElement( configuration, handler );
 59  8
         handler.endDocument();
 60   
     }
 61   
 
 62   
     /**
 63   
      * Serialize the configuration as an Element to
 64   
      * specified ContentHandler.
 65   
      *
 66   
      * @param handler the ContentHandler to write Configuration out to
 67   
      * @param configuration the Configuration
 68   
      * @throws SAXException if the handler throws an exception
 69   
      */
 70  32
     void serializeElement( final Configuration configuration,
 71   
                            final ContentHandler handler )
 72   
         throws SAXException
 73   
     {
 74  32
         final AttributesImpl attributes = serializeAttributes( configuration );
 75   
 
 76  32
         final String name = configuration.getName();
 77  32
         handler.startElement( EMPTY_NAMESPACE, name, name, attributes );
 78   
 
 79  32
         String value = configuration.getValue( null );
 80  32
         if( null == value )
 81   
         {
 82  24
             final Configuration[] children = configuration.getChildren();
 83  24
             for( int i = 0; i < children.length; i++ )
 84   
             {
 85  8
                 serializeElement( children[ i ], handler );
 86   
             }
 87   
         }
 88   
         else
 89   
         {
 90   
             /*if ( needsEscaping( value ) )
 91   
             {
 92   
                value = CDATA_PREFIX + value + CDATA_POSTFIX;
 93   
             }
 94   
             */
 95  8
             handler.characters( value.toCharArray(), 0, value.length() );
 96   
         }
 97   
 
 98  32
         handler.endElement( EMPTY_NAMESPACE, name, name );
 99   
     }
 100   
 
 101   
     /**
 102   
      * Serialize Configuration attributes to an AttributesImpl instance.
 103   
      *
 104   
      * @param configuration the configuration
 105   
      * @return the AttributesImpl instance
 106   
      */
 107  12
     AttributesImpl serializeAttributes( final Configuration configuration )
 108   
     {
 109  12
         final AttributesImpl attributes = new AttributesImpl();
 110  12
         final String[] names = configuration.getAttributeNames();
 111  12
         for( int i = 0; i < names.length; i++ )
 112   
         {
 113  4
             final String name = names[ i ];
 114  4
             final String value = configuration.getAttribute( name, "" );
 115  4
             attributes.addAttribute( EMPTY_NAMESPACE, name, name,
 116   
                                      CDATA_TYPE, value );
 117   
         }
 118  12
         return attributes;
 119   
     }
 120   
 
 121   
     /**
 122   
      * Determine whether the specified value string needs to
 123   
      * be escaped in a CDATA section to produce valid XML.
 124   
      *
 125   
      * @param value the string value
 126   
      * @return true if value needs escaping, false otherwise
 127   
      */
 128   
     /*
 129   
     boolean needsEscaping( final String value )
 130   
     {
 131   
        return false;
 132   
     }
 133   
     */
 134   
 }
 135