KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > util > PropertiesMapper


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.util;
22
23 import java.util.Properties JavaDoc;
24
25 import org.jibx.runtime.IAliasable;
26 import org.jibx.runtime.IMarshaller;
27 import org.jibx.runtime.IMarshallingContext;
28 import org.jibx.runtime.IUnmarshaller;
29 import org.jibx.runtime.IUnmarshallingContext;
30 import org.jibx.runtime.JiBXException;
31 import org.jibx.runtime.impl.MarshallingContext;
32 import org.jibx.runtime.impl.UnmarshallingContext;
33
34 /**
35  * Customer JiBX unmarshaller for unmarshalling a {@link java.util.Properties} object.
36  *
37  * @author Paul Ferraro
38  * @since 1.0
39  */

40 public class PropertiesMapper implements IUnmarshaller, IMarshaller, IAliasable
41 {
42     private static final String JavaDoc ELEMENT = "property";
43     private static final String JavaDoc ATTRIBUTE = "name";
44     
45     private String JavaDoc uri;
46     private String JavaDoc name;
47     private int index;
48     
49     /**
50      * Constructs a new PropertiesMapper.
51      */

52     public PropertiesMapper()
53     {
54     }
55     
56     /**
57      * Constructs a new PropertiesMapper.
58      * @param uri
59      * @param index
60      * @param name
61      */

62     public PropertiesMapper(String JavaDoc uri, int index, String JavaDoc name)
63     {
64         this.uri = uri;
65         this.index = index;
66         this.name = name;
67     }
68     
69     /**
70      * @see org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)
71      */

72     public boolean isPresent(IUnmarshallingContext context) throws JiBXException
73     {
74         return context.isAt(this.uri, this.name);
75     }
76
77     /**
78      * @see org.jibx.runtime.IUnmarshaller#unmarshal(java.lang.Object, org.jibx.runtime.IUnmarshallingContext)
79      */

80     public Object JavaDoc unmarshal(Object JavaDoc object, IUnmarshallingContext ctx) throws JiBXException
81     {
82         UnmarshallingContext context = UnmarshallingContext.class.cast(ctx);
83         
84         Properties JavaDoc properties = Properties JavaDoc.class.cast(object);
85         
86         if (properties == null)
87         {
88             properties = new Properties JavaDoc();
89         }
90         
91         if (this.name != null)
92         {
93             context.parsePastStartTag(this.uri, this.name);
94         }
95         
96         while (context.isAt(this.uri, ELEMENT))
97         {
98             String JavaDoc name = context.attributeText(this.uri, ATTRIBUTE);
99             
100             context.parsePastStartTag(this.uri, ELEMENT);
101             
102             String JavaDoc value = context.parseContentText();
103             
104             properties.put(name, value);
105             
106             context.parsePastEndTag(this.uri, ELEMENT);
107         }
108         
109         if (this.name != null)
110         {
111             context.parsePastEndTag(this.uri, this.name);
112         }
113         
114         return properties;
115     }
116
117     /**
118      * @see org.jibx.runtime.IMarshaller#isExtension(int)
119      */

120     public boolean isExtension(int index)
121     {
122         return false;
123     }
124
125     /**
126      * @see org.jibx.runtime.IMarshaller#marshal(java.lang.Object, org.jibx.runtime.IMarshallingContext)
127      */

128     public void marshal(Object JavaDoc object, IMarshallingContext ctx) throws JiBXException
129     {
130         Properties JavaDoc properties = Properties JavaDoc.class.cast(object);
131         
132         if (properties != null)
133         {
134             MarshallingContext context = MarshallingContext.class.cast(ctx);
135             
136             if (this.name != null)
137             {
138                 context.startTag(this.index, this.name);
139             }
140             
141             for (String JavaDoc name: Collections.cast(properties.keySet(), String JavaDoc.class))
142             {
143                 context.startTagAttributes(this.index, ELEMENT).attribute(this.index, ATTRIBUTE, name).closeStartContent().content(properties.getProperty(name)).endTag(this.index, ELEMENT);
144             }
145
146             if (this.name != null)
147             {
148                 context.endTag(this.index, this.name);
149             }
150         }
151     }
152 }
153
Popular Tags