View Javadoc

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      public void serialize( final Configuration configuration,
54                             final ContentHandler handler )
55          throws SAXException
56      {
57          handler.startDocument();
58          serializeElement( configuration, handler );
59          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      void serializeElement( final Configuration configuration,
71                             final ContentHandler handler )
72          throws SAXException
73      {
74          final AttributesImpl attributes = serializeAttributes( configuration );
75  
76          final String name = configuration.getName();
77          handler.startElement( EMPTY_NAMESPACE, name, name, attributes );
78  
79          String value = configuration.getValue( null );
80          if( null == value )
81          {
82              final Configuration[] children = configuration.getChildren();
83              for( int i = 0; i < children.length; i++ )
84              {
85                  serializeElement( children[ i ], handler );
86              }
87          }
88          else
89          {
90              /*if ( needsEscaping( value ) )
91              {
92                 value = CDATA_PREFIX + value + CDATA_POSTFIX;
93              }
94              */
95              handler.characters( value.toCharArray(), 0, value.length() );
96          }
97  
98          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     AttributesImpl serializeAttributes( final Configuration configuration )
108     {
109         final AttributesImpl attributes = new AttributesImpl();
110         final String[] names = configuration.getAttributeNames();
111         for( int i = 0; i < names.length; i++ )
112         {
113             final String name = names[ i ];
114             final String value = configuration.getAttribute( name, "" );
115             attributes.addAttribute( EMPTY_NAMESPACE, name, name,
116                                      CDATA_TYPE, value );
117         }
118         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 }