01: package org.apache.ojb.broker.util;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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.2.2 2005/12/21 22:27:47 tomdz Exp $
27: * @since OJB 1.0.4
28: */
29: public class XmlHelper {
30:
31: /** End-of-line string used in serialized XML. */
32: public static final String XML_EOL = SystemUtils.LINE_SEPARATOR;
33:
34: /**
35: * Returns an XML-string with serialized configuration attributes.
36: * Used when serializing {@link org.apache.ojb.broker.metadata.AttributeContainer} attributes.
37: * @param prefix the line prefix (ie indent) or null for no prefix
38: * @param attributeProperties the properties object holding attributes to be serialized
39: * (null-safe)
40: * @return XML-string with serialized configuration attributes (never null)
41: */
42: public static String getSerializedAttributes(final String prefix,
43: final Properties attributeProperties) {
44: final StringBuffer buf = new StringBuffer();
45: appendSerializedAttributes(buf, prefix, attributeProperties);
46: return buf.toString();
47: }
48:
49: /**
50: * Appends an XML-string with serialized configuration attributes to the specified buffer.
51: * Used when serializing {@link org.apache.ojb.broker.metadata.AttributeContainer} attributes.
52: * @param buf the string buffer to append to
53: * @param prefix the line prefix (ie indent) or null for no prefix
54: * @param attributeProperties the properties object holding attributes to be serialized
55: * (null-safe)
56: */
57: public static void appendSerializedAttributes(
58: final StringBuffer buf, final String prefix,
59: final Properties attributeProperties) {
60: if (attributeProperties != null) {
61: final Enumeration keys = attributeProperties.keys();
62: while (keys.hasMoreElements()) {
63: final String key = (String) keys.nextElement();
64: final String value = attributeProperties
65: .getProperty(key);
66: if (prefix != null) {
67: buf.append(prefix);
68: }
69: buf.append("<attribute attribute-name=\"").append(key);
70: buf.append("\" attribute-value=\"").append(value);
71: buf.append("\"/>");
72: buf.append(XML_EOL);
73: }
74: }
75: }
76:
77: }
|