1 package org.apache.ojb.broker.util;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 import org.apache.commons.lang.SystemUtils;
19
20 import java.util.Properties;
21 import java.util.Enumeration;
22
23 /**
24 * Simple helper class with static methods for common XML-handling tasks.
25 *
26 * @version CVS $Id: XmlHelper.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
27 * @since OJB 1.0.4
28 */
29 public class XmlHelper
30 {
31
32 /** End-of-line string used in serialized XML. */
33 public static final String XML_EOL = SystemUtils.LINE_SEPARATOR;
34
35 /**
36 * Returns an XML-string with serialized configuration attributes.
37 * Used when serializing {@link org.apache.ojb.broker.metadata.AttributeContainer} attributes.
38 * @param prefix the line prefix (ie indent) or null for no prefix
39 * @param attributeProperties the properties object holding attributes to be serialized
40 * (null-safe)
41 * @return XML-string with serialized configuration attributes (never null)
42 */
43 public static String getSerializedAttributes(final String prefix,
44 final Properties attributeProperties)
45 {
46 final StringBuffer buf = new StringBuffer();
47 appendSerializedAttributes(buf, prefix, attributeProperties);
48 return buf.toString();
49 }
50
51 /**
52 * Appends an XML-string with serialized configuration attributes to the specified buffer.
53 * Used when serializing {@link org.apache.ojb.broker.metadata.AttributeContainer} attributes.
54 * @param buf the string buffer to append to
55 * @param prefix the line prefix (ie indent) or null for no prefix
56 * @param attributeProperties the properties object holding attributes to be serialized
57 * (null-safe)
58 */
59 public static void appendSerializedAttributes(final StringBuffer buf,
60 final String prefix,
61 final Properties attributeProperties)
62 {
63 if (attributeProperties != null)
64 {
65 final Enumeration keys = attributeProperties.keys();
66 while (keys.hasMoreElements())
67 {
68 final String key = (String) keys.nextElement();
69 final String value = attributeProperties.getProperty( key );
70 if (prefix != null)
71 {
72 buf.append(prefix);
73 }
74 buf.append("<attribute attribute-name=\"").append(key);
75 buf.append("\" attribute-value=\"" ).append(value);
76 buf.append("\"/>");
77 buf.append(XML_EOL);
78 }
79 }
80 }
81
82 }