001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import org.codehaus.dna.Configuration;
011: import org.xml.sax.ContentHandler;
012: import org.xml.sax.SAXException;
013: import org.xml.sax.helpers.AttributesImpl;
014:
015: /**
016: * Utility class that serializes a Configuration object
017: * to a SAX2 compliant ContentHandler.
018: *
019: * @author Peter Donald
020: * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
021: */
022: public class SAXConfigurationSerializer {
023: /**
024: * Constant for CDATA type in attributes.
025: */
026: private static final String CDATA_TYPE = "CDATA";
027:
028: /**
029: * Constant for empty namespace in attributes.
030: */
031: private static final String EMPTY_NAMESPACE = "";
032:
033: /**
034: * Constant for start of CDATA content sections.
035: */
036: //private static final String CDATA_PREFIX = "<![CDATA[";
037: /**
038: * Constant for end of CDATA content sections.
039: */
040: //private static final String CDATA_POSTFIX = "]]>";
041: /**
042: * Serialize the configuration to as a Document to the
043: * specified ContentHandler. The serialization writes
044: * out an Element for each configuration object.
045: *
046: * @param handler the ContentHandler to write Configuration out to
047: * @param configuration the Configuration
048: * @throws SAXException if the handler throws an exception
049: */
050: public void serialize(final Configuration configuration,
051: final ContentHandler handler) throws SAXException {
052: handler.startDocument();
053: serializeElement(configuration, handler);
054: handler.endDocument();
055: }
056:
057: /**
058: * Serialize the configuration as an Element to
059: * specified ContentHandler.
060: *
061: * @param handler the ContentHandler to write Configuration out to
062: * @param configuration the Configuration
063: * @throws SAXException if the handler throws an exception
064: */
065: void serializeElement(final Configuration configuration,
066: final ContentHandler handler) throws SAXException {
067: final AttributesImpl attributes = serializeAttributes(configuration);
068:
069: final String name = configuration.getName();
070: handler.startElement(EMPTY_NAMESPACE, name, name, attributes);
071:
072: String value = configuration.getValue(null);
073: if (null == value) {
074: final Configuration[] children = configuration
075: .getChildren();
076: for (int i = 0; i < children.length; i++) {
077: serializeElement(children[i], handler);
078: }
079: } else {
080: /*if ( needsEscaping( value ) )
081: {
082: value = CDATA_PREFIX + value + CDATA_POSTFIX;
083: }
084: */
085: handler.characters(value.toCharArray(), 0, value.length());
086: }
087:
088: handler.endElement(EMPTY_NAMESPACE, name, name);
089: }
090:
091: /**
092: * Serialize Configuration attributes to an AttributesImpl instance.
093: *
094: * @param configuration the configuration
095: * @return the AttributesImpl instance
096: */
097: AttributesImpl serializeAttributes(final Configuration configuration) {
098: final AttributesImpl attributes = new AttributesImpl();
099: final String[] names = configuration.getAttributeNames();
100: for (int i = 0; i < names.length; i++) {
101: final String name = names[i];
102: final String value = configuration.getAttribute(name, "");
103: attributes.addAttribute(EMPTY_NAMESPACE, name, name,
104: CDATA_TYPE, value);
105: }
106: return attributes;
107: }
108:
109: /**
110: * Determine whether the specified value string needs to
111: * be escaped in a CDATA section to produce valid XML.
112: *
113: * @param value the string value
114: * @return true if value needs escaping, false otherwise
115: */
116: /*
117: boolean needsEscaping( final String value )
118: {
119: return false;
120: }
121: */
122: }
|