1 7 package org.jboss.webservice.deployment; 8 9 11 import java.io.PrintWriter ; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import javax.xml.namespace.QName ; 18 19 import org.jboss.axis.utils.LinkedHashMap; 20 import org.jboss.webservice.util.DOMUtils; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.NodeList ; 23 24 68 public class BeanXMLMetaData 69 { 70 private Map elementDescMap = new LinkedHashMap(); 71 72 private List elementOrderList = new ArrayList (); 73 74 private BeanXMLMetaData() 76 { 77 } 78 79 82 public static BeanXMLMetaData parse(Element metaData) 83 { 84 BeanXMLMetaData beanMetaData = new BeanXMLMetaData(); 85 86 if (metaData != null) 87 { 88 ArrayList qnames = new ArrayList (); 89 90 NodeList nlistElementDesc = metaData.getElementsByTagName("elementDesc"); 92 for (int i = 0; i < nlistElementDesc.getLength(); i++) 93 { 94 Element elDesc = (Element )nlistElementDesc.item(i); 95 String fieldName = DOMUtils.getAttributeValue(elDesc, "fieldName"); 96 97 QName xmlName = DOMUtils.getAttributeValueAsQName(elDesc, "xmlName"); 98 if (xmlName == null) 99 xmlName = new QName ("", fieldName); 100 101 QName xmlType = DOMUtils.getAttributeValueAsQName(elDesc, "xmlType"); 102 if (xmlType != null) 103 { 104 if (qnames.contains(xmlType)) 105 throw new IllegalStateException ("Duplicate type mapping definition: " + xmlType); 106 107 qnames.add(xmlType); 108 } 109 110 ElementMetaData elMetaData = new ElementMetaData(fieldName, xmlName, xmlType); 111 beanMetaData.elementDescMap.put(fieldName, elMetaData); 112 113 boolean asAttribute = DOMUtils.getAttributeValueAsBoolean(elDesc, "asAttr"); 114 elMetaData.setAsAttribute(asAttribute); 115 116 boolean asContent = DOMUtils.getAttributeValueAsBoolean(elDesc, "asContent"); 117 elMetaData.setAsContent(asContent); 118 119 Integer minOccurs = DOMUtils.getAttributeValueAsInteger(elDesc, "minOccurs"); 120 elMetaData.setMinOccurs(minOccurs); 121 122 QName itemXmlType = DOMUtils.getAttributeValueAsQName(elDesc, "itemXmlType"); 123 elMetaData.setItemXmlType(itemXmlType); 124 } 125 126 Element orderEl = DOMUtils.getFirstChildElement(metaData, "elementOrder"); 128 if (orderEl != null) 129 { 130 NodeList nlistElementOrder = orderEl.getElementsByTagName("element"); 133 for (int i = 0; i < nlistElementOrder.getLength(); i++) 134 { 135 Element el = (Element )nlistElementOrder.item(i); 136 String fieldName = el.getAttribute("name"); 137 beanMetaData.elementOrderList.add(fieldName); 138 } 139 } 140 } 141 142 return beanMetaData; 143 } 144 145 public Iterator getElementMetaData() 146 { 147 return elementDescMap.values().iterator(); 148 } 149 150 public Iterator getElementOrder() 151 { 152 return elementOrderList.iterator(); 153 } 154 155 public void serializeAsXML(PrintWriter out) 156 { 157 String pad = " "; 158 out.println(pad + "<typeDesc>"); 159 Iterator itElDesc = elementDescMap.values().iterator(); 160 while (itElDesc.hasNext()) 161 { 162 ElementMetaData elMetaData = (ElementMetaData)itElDesc.next(); 163 String prefixedXmlName = elMetaData.getPrefixedXmlName(); 164 out.print(pad + pad + "<elementDesc fieldName='" + elMetaData.getFieldName() + "' xmlName='" + prefixedXmlName + "'"); 165 166 if (elMetaData.getXmlType() != null) 167 { 168 String prefixedXmlType = elMetaData.getPrefixedXmlType(); 169 out.print(" xmlType='" + prefixedXmlType + "'"); 170 } 171 172 if (elMetaData.isAsAttribute()) 173 out.print(" asAttr='true'"); 174 175 if (elMetaData.isAsContent()) 176 out.print(" asContent='true'"); 177 178 if (elMetaData.getMinOccurs() != null) 179 out.print(" minOccurs='" + elMetaData.getMinOccurs() + "'"); 180 181 if (elMetaData.getItemXmlType() != null) 182 out.print(" itemXmlType='" + elMetaData.getPrefixedItemXmlType() + "'"); 183 184 out.println("/>"); 185 } 186 187 if (elementOrderList.size() > 0) 188 { 189 out.println(pad + pad + "<elementOrder>"); 190 Iterator itElOrder = elementOrderList.iterator(); 191 while (itElOrder.hasNext()) 192 { 193 String fieldName = (String )itElOrder.next(); 194 out.println(pad + pad + pad + "<element name='" + fieldName + "'/>"); 195 } 196 out.println(pad + pad + "</elementOrder>"); 197 } 198 out.println(pad + "</typeDesc>"); 199 } 200 201 204 public static class ElementMetaData 205 { 206 207 private String fieldName; 208 209 private QName xmlName; 210 211 private QName xmlType; 212 213 private QName itemXmlType; 214 215 private boolean asAttribute; 216 217 private boolean asContent; 218 219 private Integer minOccurs; 220 221 public ElementMetaData(String fieldName, QName xmlName, QName xmlType) 222 { 223 this.fieldName = fieldName; 224 this.xmlName = xmlName; 225 this.xmlType = xmlType; 226 } 227 228 public String getFieldName() 229 { 230 return fieldName; 231 } 232 233 public QName getXmlName() 234 { 235 return xmlName; 236 } 237 238 public String getPrefixedXmlName() 239 { 240 String retStr = null; 241 if (xmlName != null) 242 { 243 retStr = xmlName.getLocalPart(); 244 if (xmlName.getPrefix().length() > 0) 245 retStr = xmlName.getPrefix() + ":" + retStr; 246 } 247 return retStr; 248 } 249 250 public QName getXmlType() 251 { 252 return xmlType; 253 } 254 255 public String getPrefixedXmlType() 256 { 257 String retStr = null; 258 if (xmlType != null) 259 { 260 retStr = xmlType.getLocalPart(); 261 if (xmlType.getPrefix().length() > 0) 262 retStr = xmlType.getPrefix() + ":" + retStr; 263 } 264 return retStr; 265 } 266 267 public boolean isAsAttribute() 268 { 269 return asAttribute; 270 } 271 272 public void setAsAttribute(boolean asAttribute) 273 { 274 this.asAttribute = asAttribute; 275 } 276 277 public boolean isAsContent() 278 { 279 return asContent; 280 } 281 282 public void setAsContent(boolean asContent) 283 { 284 this.asContent = asContent; 285 } 286 287 public Integer getMinOccurs() 288 { 289 return minOccurs; 290 } 291 292 public void setMinOccurs(Integer minOccurs) 293 { 294 this.minOccurs = minOccurs; 295 } 296 297 public QName getItemXmlType() 298 { 299 return itemXmlType; 300 } 301 302 public String getPrefixedItemXmlType() 303 { 304 String retStr = null; 305 if (itemXmlType != null) 306 { 307 retStr = itemXmlType.getLocalPart(); 308 if (itemXmlType.getPrefix().length() > 0) 309 retStr = itemXmlType.getPrefix() + ":" + retStr; 310 } 311 return retStr; 312 } 313 314 317 public void setItemXmlType(QName itemXmlType) 318 { 319 this.itemXmlType = itemXmlType; 320 } 321 } 322 } 323 | Popular Tags |