1 21 package net.sf.hajdbc.util; 22 23 import java.util.Properties ; 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 40 public class PropertiesMapper implements IUnmarshaller, IMarshaller, IAliasable 41 { 42 private static final String ELEMENT = "property"; 43 private static final String ATTRIBUTE = "name"; 44 45 private String uri; 46 private String name; 47 private int index; 48 49 52 public PropertiesMapper() 53 { 54 } 55 56 62 public PropertiesMapper(String uri, int index, String name) 63 { 64 this.uri = uri; 65 this.index = index; 66 this.name = name; 67 } 68 69 72 public boolean isPresent(IUnmarshallingContext context) throws JiBXException 73 { 74 return context.isAt(this.uri, this.name); 75 } 76 77 80 public Object unmarshal(Object object, IUnmarshallingContext ctx) throws JiBXException 81 { 82 UnmarshallingContext context = UnmarshallingContext.class.cast(ctx); 83 84 Properties properties = Properties .class.cast(object); 85 86 if (properties == null) 87 { 88 properties = new Properties (); 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 name = context.attributeText(this.uri, ATTRIBUTE); 99 100 context.parsePastStartTag(this.uri, ELEMENT); 101 102 String 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 120 public boolean isExtension(int index) 121 { 122 return false; 123 } 124 125 128 public void marshal(Object object, IMarshallingContext ctx) throws JiBXException 129 { 130 Properties properties = Properties .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 name: Collections.cast(properties.keySet(), String .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 |