1 22 package org.jboss.services.deployment.metadata; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 27 import org.jboss.xb.binding.GenericObjectModelFactory; 28 import org.jboss.xb.binding.UnmarshallingContext; 29 import org.xml.sax.Attributes ; 30 31 40 public class ConfigInfoBinding 41 implements GenericObjectModelFactory 42 { 43 44 46 public Object newRoot(Object root, UnmarshallingContext ctx, String namespaceURI, 47 String localName, Attributes attrs) 48 { 49 final ConfigInfo ci; 50 if(root == null) 51 { 52 root = ci = new ConfigInfo(); 53 } 54 else 55 { 56 ci = (ConfigInfo) root; 57 } 58 59 if(attrs.getLength() > 0) 60 { 61 for(int i = 0; i < attrs.getLength(); ++i) 62 { 63 if (attrs.getLocalName(i).equals("copydir")) 64 { 65 ci.setCopydir(attrs.getValue(i)); 66 } 67 else if (attrs.getLocalName(i).equals("template")) 68 { 69 ci.setTemplate(attrs.getValue(i)); 70 } 71 else if (attrs.getLocalName(i).equals("extension")) 72 { 73 ci.setExtension(attrs.getValue(i)); 74 } 75 } 76 } 77 return root; 78 } 79 80 public Object completeRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName) 81 { 82 return root; 83 } 84 85 87 public Object newChild(Object parent, UnmarshallingContext ctx, String namespaceURI, 88 String localName, Attributes attrs) 89 { 90 Object child = null; 91 92 if (parent instanceof ConfigInfo) 93 { 94 if("property".equals(localName)) 95 { 96 PropertyInfo pi = new PropertyInfo(); 97 child = pi; 98 99 if(attrs.getLength() > 0) 100 { 101 for(int i = 0; i < attrs.getLength(); ++i) 102 { 103 if(attrs.getLocalName(i).equals("name")) 104 { 105 pi.setName(attrs.getValue(i)); 106 } 107 else if (attrs.getLocalName(i).equals("type")) 108 { 109 pi.setType(attrs.getValue(i)); 110 } 111 else if (attrs.getLocalName(i).equals("optional")) 112 { 113 pi.setOptional(Boolean.valueOf(attrs.getValue(i)).booleanValue()); 114 } 115 } 116 } 117 if (pi.getName() == null) 118 throw new RuntimeException ( 119 "Missing attribute 'name' for <property> element"); 120 121 String type = pi.getType(); 123 if (type == null) 124 pi.setType("java.lang.String"); 125 } 126 else if ("template".equals(localName)) 127 { 128 TemplateInfo ti = new TemplateInfo(); 129 child = ti; 130 131 if(attrs.getLength() > 0) 132 { 133 for(int i = 0; i < attrs.getLength(); ++i) 134 { 135 if(attrs.getLocalName(i).equals("input")) 136 { 137 ti.setInput(attrs.getValue(i)); 138 } 139 else if (attrs.getLocalName(i).equals("output")) 140 { 141 ti.setOutput(attrs.getValue(i)); 142 } 143 } 144 } 145 if (ti.getInput() == null || ti.getOutput() == null) 147 throw new RuntimeException ( 148 "Both 'input' and 'output' attribute must be set for a <template> element"); 149 } 150 } 151 return child; 152 } 153 154 public void addChild(Object parent, Object child, UnmarshallingContext ctx, 155 String namespaceURI, String localName) 156 { 157 if(parent instanceof ConfigInfo) 158 { 159 final ConfigInfo ci = (ConfigInfo)parent; 160 if(child instanceof PropertyInfo) 161 { 162 ci.addPropertyInfo((PropertyInfo)child); 163 } 164 else if(child instanceof TemplateInfo) 165 { 166 ci.addTemplateInfo((TemplateInfo)child); 167 } 168 } 169 } 170 171 public void setValue(Object o, UnmarshallingContext ctx, String namespaceURI, 172 String localName, String value) 173 { 174 if(o instanceof ConfigInfo) 175 { 176 final ConfigInfo ci = (ConfigInfo)o; 177 if("description".equals(localName)) 178 { 179 ci.setDescription(value); 180 } 181 } 182 else if(o instanceof PropertyInfo) 183 { 184 PropertyInfo pi = (PropertyInfo)o; 185 if("description".equals(localName)) 186 { 187 pi.setDescription(value); 188 } 189 else if("default-value".equals(localName)) 190 { 191 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 192 Class clazz = null; 193 try { 194 clazz = cl.loadClass(pi.getType()); 195 } 196 catch (ClassNotFoundException e) { 197 throw new RuntimeException ("Class not found for property '" + pi.getName() + 198 "' of type '" + pi.getType() + "'"); 199 } 200 PropertyEditor peditor = PropertyEditorManager.findEditor(clazz); 201 202 if (peditor != null) { 203 peditor.setAsText(value); 204 pi.setDefaultValue(peditor.getValue()); 205 } 206 else 207 throw new RuntimeException ("Property editor not found for property '" + pi.getName() + 208 "' of type '" + pi.getType() + "'"); 209 } 210 } 211 } 212 } 213 | Popular Tags |