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 */
021package net.sf.hajdbc.util;
022
023import java.util.Properties;
024
025import org.jibx.runtime.JiBXException;
026import org.jibx.runtime.impl.MarshallingContext;
027import 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 */
035public class PropertiesMapper extends AbstractMapper<Properties>
036{
037        private static final String ELEMENT = "property"; //$NON-NLS-1$
038        private static final String ATTRIBUTE = "name"; //$NON-NLS-1$
039
040        /**
041         * Constructs a new PropertiesMapper.
042         */
043        public PropertiesMapper()
044        {
045                super(Properties.class);
046        }
047
048        /**
049         * Constructs a new PropertiesMapper.
050         * @param uri
051         * @param index
052         * @param name
053         */
054        public PropertiesMapper(String uri, int index, String name)
055        {
056                super(Properties.class, uri, index, name);
057        }
058
059        /**
060         * @see net.sf.hajdbc.util.AbstractMapper#marshal(java.lang.Object, org.jibx.runtime.impl.MarshallingContext)
061         */
062        @Override
063        protected void marshal(Properties properties, MarshallingContext context) throws JiBXException
064        {
065                if (properties != null)
066                {
067                        if (this.name != null)
068                        {
069                                context.startTag(this.index, this.name);
070                        }
071                        
072                        for (Object key: properties.keySet())
073                        {
074                                String name = (String) key;
075                                
076                                context.startTagAttributes(this.index, ELEMENT).attribute(this.index, ATTRIBUTE, name).closeStartContent().content(properties.getProperty(name)).endTag(this.index, ELEMENT);
077                        }
078
079                        if (this.name != null)
080                        {
081                                context.endTag(this.index, this.name);
082                        }
083                }
084        }
085
086        /**
087         * @see net.sf.hajdbc.util.AbstractMapper#unmarshal(java.lang.Object, org.jibx.runtime.impl.UnmarshallingContext)
088         */
089        @Override
090        protected Properties unmarshal(Properties existingProperties, UnmarshallingContext context) throws JiBXException
091        {
092                Properties properties = (existingProperties != null) ? existingProperties : new Properties();
093                
094                if (this.name != null)
095                {
096                        context.parsePastStartTag(this.uri, this.name);
097                }
098                
099                while (context.isAt(this.uri, ELEMENT))
100                {
101                        String name = context.attributeText(this.uri, ATTRIBUTE);
102                        
103                        context.parsePastStartTag(this.uri, ELEMENT);
104                        
105                        String value = context.parseContentText();
106                        
107                        properties.put(name, value);
108                        
109                        context.parsePastEndTag(this.uri, ELEMENT);
110                }
111                
112                if (this.name != null)
113                {
114                        context.parsePastEndTag(this.uri, this.name);
115                }
116                
117                return properties;
118        }
119}