001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.util;
022:
023: import java.util.Properties;
024:
025: import org.jibx.runtime.JiBXException;
026: import org.jibx.runtime.impl.MarshallingContext;
027: import org.jibx.runtime.impl.UnmarshallingContext;
028:
029: /**
030: * Customer JiBX unmarshaller for unmarshalling a {@link java.util.Properties} object.
031: *
032: * @author Paul Ferraro
033: * @since 1.0
034: */
035: public class PropertiesMapper extends AbstractMapper<Properties> {
036: private static final String ELEMENT = "property"; //$NON-NLS-1$
037: private static final String ATTRIBUTE = "name"; //$NON-NLS-1$
038:
039: /**
040: * Constructs a new PropertiesMapper.
041: */
042: public PropertiesMapper() {
043: super (Properties.class);
044: }
045:
046: /**
047: * Constructs a new PropertiesMapper.
048: * @param uri
049: * @param index
050: * @param name
051: */
052: public PropertiesMapper(String uri, int index, String name) {
053: super (Properties.class, uri, index, name);
054: }
055:
056: /**
057: * @see net.sf.hajdbc.util.AbstractMapper#marshal(java.lang.Object, org.jibx.runtime.impl.MarshallingContext)
058: */
059: @Override
060: protected void marshal(Properties properties,
061: MarshallingContext context) throws JiBXException {
062: if (properties != null) {
063: if (this .name != null) {
064: context.startTag(this .index, this .name);
065: }
066:
067: for (Object key : properties.keySet()) {
068: String name = (String) key;
069:
070: context.startTagAttributes(this .index, ELEMENT)
071: .attribute(this .index, ATTRIBUTE, name)
072: .closeStartContent().content(
073: properties.getProperty(name)).endTag(
074: this .index, ELEMENT);
075: }
076:
077: if (this .name != null) {
078: context.endTag(this .index, this .name);
079: }
080: }
081: }
082:
083: /**
084: * @see net.sf.hajdbc.util.AbstractMapper#unmarshal(java.lang.Object, org.jibx.runtime.impl.UnmarshallingContext)
085: */
086: @Override
087: protected Properties unmarshal(Properties existingProperties,
088: UnmarshallingContext context) throws JiBXException {
089: Properties properties = (existingProperties != null) ? existingProperties
090: : new Properties();
091:
092: if (this .name != null) {
093: context.parsePastStartTag(this .uri, this .name);
094: }
095:
096: while (context.isAt(this .uri, ELEMENT)) {
097: String name = context.attributeText(this .uri, ATTRIBUTE);
098:
099: context.parsePastStartTag(this .uri, ELEMENT);
100:
101: String value = context.parseContentText();
102:
103: properties.put(name, value);
104:
105: context.parsePastEndTag(this.uri, ELEMENT);
106: }
107:
108: if (this.name != null) {
109: context.parsePastEndTag(this.uri, this.name);
110: }
111:
112: return properties;
113: }
114: }
|