1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import javax.xml.namespace.QName ; 26 27 import org.apache.ws.jaxme.WildcardAttribute; 28 import org.apache.ws.jaxme.generator.sg.AttributeSG; 29 import org.apache.ws.jaxme.generator.sg.PropertySG; 30 import org.apache.ws.jaxme.js.DirectAccessible; 31 import org.apache.ws.jaxme.js.JavaClassInitializer; 32 import org.apache.ws.jaxme.js.JavaComment; 33 import org.apache.ws.jaxme.js.JavaField; 34 import org.apache.ws.jaxme.js.JavaMethod; 35 import org.apache.ws.jaxme.js.JavaQName; 36 import org.apache.ws.jaxme.js.JavaQNameImpl; 37 import org.apache.ws.jaxme.js.JavaSource; 38 import org.apache.ws.jaxme.js.LocalJavaField; 39 import org.apache.ws.jaxme.js.Parameter; 40 import org.apache.ws.jaxme.xs.XSWildcard; 41 import org.apache.ws.jaxme.xs.xml.XsAnyURI; 42 import org.apache.ws.jaxme.xs.xml.XsNamespaceList; 43 import org.apache.ws.jaxme.xs.xml.XsSchemaHeader; 44 import org.xml.sax.SAXException ; 45 46 47 51 public class AnyAttributePropertySG extends JAXBPropertySG { 52 private final XsNamespaceList namespaceList; 53 private final XsSchemaHeader schemaHeader; 54 55 protected AnyAttributePropertySG(AttributeSG pAttribute, XSWildcard pWildcard) { 56 super("anyAttribute", pAttribute.getSchema(), pWildcard, null, null); 57 namespaceList = pWildcard.getNamespaceList(); 58 schemaHeader = pWildcard.getSchemaHeader(); 59 } 60 61 protected String getTargetNamespace() { 62 String targetNamespace = schemaHeader == null ? "" : schemaHeader.getTargetNamespace().toString(); 63 if (targetNamespace == null) { 64 targetNamespace = ""; 65 } 66 return targetNamespace; 67 } 68 69 public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException { 70 if (pSource.isInterface()) { 71 return null; 72 } 73 JavaQName runtimeType = JavaQNameImpl.getInstance(Map .class); 74 JavaField jf = pSource.newJavaField(pController.getXMLFieldName(), runtimeType, JavaSource.PRIVATE); 75 jf.setFinal(true); 76 jf.addLine("new ", HashMap .class, "()"); 77 return jf; 78 } 79 80 protected JavaField getNamespaces(PropertySG pController, JavaSource pSource) throws SAXException { 81 if (namespaceList.isAny() || namespaceList.isOther()) { 82 return null; 83 } 84 String namespaces = pController.getXMLFieldName() + "Namespaces"; 85 JavaField jf = pSource.newJavaField(namespaces, Set .class); 86 jf.setStatic(true); 87 jf.setFinal(true); 88 JavaClassInitializer jci = pSource.newJavaClassInitializer(); 89 LocalJavaField set = jci.newJavaField(Set .class); 90 set.addLine("new ", HashSet .class, "()"); 91 XsAnyURI[] uris = namespaceList.getUris(); 92 for (int i = 0; i < uris.length; i++) { 93 jci.addLine(set, ".add(", JavaSource.getQuoted(uris[i].toString()), ");"); 94 } 95 jci.addLine(jf, " = ", Collections .class, ".unmodifiableSet(", set, ");"); 96 return jf; 97 } 98 99 protected void getValidNamespaceCheck(PropertySG pController, JavaMethod pMethod, Parameter pName) 100 throws SAXException { 101 if (namespaceList.isAny()) { 102 } else if (namespaceList.isOther()) { 104 String targetNamespace = getTargetNamespace(); 105 pMethod.addIf(JavaSource.getQuoted(targetNamespace), ".equals(", pName, ".getNamespaceURI())"); 106 pMethod.addThrowNew(IllegalArgumentException .class, 107 JavaSource.getQuoted("The namespace of the pName argument must not match the target namespace '" + 108 targetNamespace + "'.")); 109 pMethod.addEndIf(); 110 } else { 111 String targetNamespace = getTargetNamespace(); 112 String namespaces = pController.getXMLFieldName() + "Namespaces"; 113 pMethod.addIf("!", namespaces, ".contains(", pName, ".getNamespaceURI())"); 114 pMethod.addThrowNew(IllegalArgumentException .class, 115 JavaSource.getQuoted("The namespace of the pName argument must not match the target namespace '" + 116 targetNamespace + "'.")); 117 pMethod.addEndIf(); 118 } 119 } 120 121 public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 122 JavaMethod jm = pSource.newJavaMethod(pController.getXMLGetMethodName(), String .class, JavaSource.PUBLIC); 123 JavaComment jc = jm.newComment(); 124 jc.addLine("<p>Returns the value of the 'anyAttribute' named <code>pName</code>.</p>"); 125 jc.addLine("@return Attribute value or null, if the attribute is not set."); 126 jc.addLine("@throws NullPointerException The <code>pName</code> argument is null."); 127 if (!namespaceList.isAny()) { 128 jc.addLine("@throws IllegalArgumentException The namespace <code>pName.getNamespaceURI()</code> is invalid."); 129 } 130 Parameter pName = jm.addParam(QName .class, "pName"); 131 if (!pSource.isInterface()) { 132 jm.addIf(pName, " == null"); 133 jm.addThrowNew(NullPointerException .class, JavaSource.getQuoted("The pName argument must not be null.")); 134 jm.addEndIf(); 135 getValidNamespaceCheck(pController, jm, pName); 136 jm.addLine("return (", String .class, ") ", pController.getXMLFieldName(), ".get(", pName, ");"); 137 } 138 return jm; 139 } 140 141 public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 142 JavaMethod jm = pSource.newJavaMethod(pController.getXMLSetMethodName(), void.class, JavaSource.PUBLIC); 143 JavaComment jc = jm.newComment(); 144 jc.addLine("<p>Sets the 'anyAttribute' named <code>pName</code> to the value <code>pValue</code>.</p>"); 145 if (!namespaceList.isAny()) { 146 jc.addLine("@throws IllegalArgumentException The namespace <code>pName.getNamespaceURI()</code> is invalid."); 147 } 148 jc.addLine("@throws NullPointerException Either of the arguments <code>pName</code> or <code>pValue</code> is null."); 149 Parameter pName = jm.addParam(QName .class, "pName"); 150 Parameter pValue = jm.addParam(String .class, "pValue"); 151 if (!pSource.isInterface()) { 152 jm.addIf(pName, " == null"); 153 jm.addThrowNew(NullPointerException .class, JavaSource.getQuoted("The pName argument must not be null.")); 154 jm.addEndIf(); 155 jm.addIf(pValue, " == null"); 156 jm.addThrowNew(NullPointerException .class, JavaSource.getQuoted("The pValue argument must not be null.")); 157 jm.addEndIf(); 158 getValidNamespaceCheck(pController, jm, pName); 159 jm.addLine(pController.getXMLFieldName(), ".put(", pName, ", ", pValue, ");"); 160 } 161 return jm; 162 } 163 164 public JavaMethod getXMLUnsetMethod(PropertySG pController, JavaSource pSource) throws SAXException { 165 JavaMethod jm = pSource.newJavaMethod("un" + pController.getXMLSetMethodName(), boolean.class, JavaSource.PUBLIC); 166 JavaComment jc = jm.newComment(); 167 jc.addLine("<p>Removes the 'anyAttribute' named <code>pName</code>.</p>"); 168 if (!namespaceList.isAny()) { 169 jc.addLine("@throws IllegalArgumentException The namespace <code>pName.getNamespaceURI()</code> is invalid."); 170 } 171 jc.addLine("@throws NullPointerException Either of the arguments <code>pName</code> or <code>pValue</code> is null."); 172 jc.addLine("@return True, if the attribute was set, otherwise false."); 173 Parameter pName = jm.addParam(QName .class, "pName"); 174 if (!pSource.isInterface()) { 175 jm.addIf(pName, " == null"); 176 jm.addThrowNew(NullPointerException .class, JavaSource.getQuoted("The pName argument must not be null.")); 177 jm.addEndIf(); 178 getValidNamespaceCheck(pController, jm, pName); 179 jm.addLine("return ", pController.getXMLFieldName(), ".remove(", pName, ") != null;"); 180 } 181 return jm; 182 } 183 184 public JavaMethod getXMLGetArrayMethod(PropertySG pController, JavaSource pSource) throws SAXException { 185 JavaMethod jm = pSource.newJavaMethod(pController.getXMLGetMethodName() + "Array", WildcardAttribute[].class, JavaSource.PUBLIC); 186 JavaComment jc = jm.newComment(); 187 jc.addLine("<p>Returns the array of 'anyAttributes'.</p>"); 188 LocalJavaField size = jm.newJavaField(int.class); 189 size.addLine(pController.getXMLFieldName(), ".size()"); 190 LocalJavaField result = jm.newJavaField(jm.getType()); 191 result.addLine("new ", WildcardAttribute.class, "[", size, "]"); 192 DirectAccessible iter = jm.addForCollection(new Object []{pController.getXMLFieldName(), ".entrySet()"}); 193 LocalJavaField entry = jm.newJavaField(Map.Entry .class); 194 entry.addLine("(", Map.Entry .class, ") ", iter, ".next()"); 195 if (!pSource.isInterface()) { 196 jm.addLine(result, "[--", size, "] = new ", WildcardAttribute.class, "((", 197 QName .class, ") ", entry, ".getKey(), (", String .class, ") ", 198 entry, ".getValue());"); 199 jm.addEndFor(); 200 jm.addLine("return ", result, ";"); 201 } 202 return jm; 203 } 204 205 public void generate(PropertySG pController, JavaSource pSource) throws SAXException { 206 super.generate(pController, pSource); 207 getXMLUnsetMethod(pController, pSource); 208 getXMLGetArrayMethod(pController, pSource); 209 if (!pSource.isInterface()) { 210 getNamespaces(pController, pSource); 211 } 212 } 213 } 214 | Popular Tags |