001    package org.apache.ojb.broker.util;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.commons.lang.SystemUtils;
019    
020    import java.util.Properties;
021    import java.util.Enumeration;
022    
023    /**
024     * Simple helper class with static methods for common XML-handling tasks.
025     *
026     * @version CVS $Id: XmlHelper.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
027     * @since OJB 1.0.4
028     */
029    public class XmlHelper
030    {
031    
032        /** End-of-line string used in serialized XML. */
033        public static final String XML_EOL = SystemUtils.LINE_SEPARATOR;
034    
035        /**
036         * Returns an XML-string with serialized configuration attributes.
037         * Used when serializing {@link org.apache.ojb.broker.metadata.AttributeContainer} attributes.
038         * @param prefix the line prefix (ie indent) or null for no prefix
039         * @param attributeProperties the properties object holding attributes to be serialized
040         * (null-safe)
041         * @return XML-string with serialized configuration attributes (never null)
042         */
043        public static String getSerializedAttributes(final String prefix,
044                                                     final Properties attributeProperties)
045        {
046            final StringBuffer buf = new StringBuffer();
047            appendSerializedAttributes(buf, prefix, attributeProperties);
048            return buf.toString();
049        }
050    
051        /**
052         * Appends an XML-string with serialized configuration attributes to the specified buffer.
053         * Used when serializing {@link org.apache.ojb.broker.metadata.AttributeContainer} attributes.
054         * @param buf the string buffer to append to
055         * @param prefix the line prefix (ie indent) or null for no prefix
056         * @param attributeProperties the properties object holding attributes to be serialized
057         * (null-safe)
058         */
059        public static void appendSerializedAttributes(final StringBuffer buf,
060                                                      final String prefix,
061                                                      final Properties attributeProperties)
062        {
063            if (attributeProperties != null)
064            {
065                final Enumeration keys = attributeProperties.keys();
066                while (keys.hasMoreElements())
067                {
068                    final String key = (String) keys.nextElement();
069                    final String value = attributeProperties.getProperty( key );
070                    if (prefix != null)
071                    {
072                        buf.append(prefix);
073                    }
074                    buf.append("<attribute attribute-name=\"").append(key);
075                    buf.append("\" attribute-value=\"" ).append(value);
076                    buf.append("\"/>");
077                    buf.append(XML_EOL);
078                }
079            }
080        }
081    
082    }