1 17 package org.apache.ws.jaxme.generator.types; 18 19 import org.apache.ws.jaxme.generator.sg.AtomicTypeSG; 20 import org.apache.ws.jaxme.generator.sg.SGFactory; 21 import org.apache.ws.jaxme.generator.sg.SGlet; 22 import org.apache.ws.jaxme.generator.sg.SchemaSG; 23 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG; 24 import org.apache.ws.jaxme.js.DirectAccessible; 25 import org.apache.ws.jaxme.js.JavaMethod; 26 import org.apache.ws.jaxme.js.JavaQName; 27 import org.apache.ws.jaxme.js.JavaQNameImpl; 28 import org.apache.ws.jaxme.js.JavaSource; 29 import org.apache.ws.jaxme.js.LocalJavaField; 30 import org.apache.ws.jaxme.js.TypedValue; 31 import org.apache.ws.jaxme.js.impl.TypedValueImpl; 32 import org.apache.ws.jaxme.xs.XSType; 33 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException; 34 import org.xml.sax.SAXException ; 35 36 37 40 public class StringSG extends AtomicTypeSGImpl { 41 43 public StringSG(SGFactory pFactory, SchemaSG pSchema, XSType pType) throws SAXException { 44 super(pFactory, pSchema, pType); 45 } 46 47 49 public final static JavaQName STRING_TYPE = JavaQNameImpl.getInstance(String .class); 50 protected String getDatatypeName() { return "String"; } 51 protected JavaQName getDatatypeType() { return STRING_TYPE; } 52 53 public JavaQName getRuntimeType(SimpleTypeSG pController) { return STRING_TYPE; } 54 55 public TypedValue getCastFromString(SimpleTypeSG pController, String pValue) { 56 return new TypedValueImpl(JavaSource.getQuoted(pValue), STRING_TYPE); 57 } 58 59 public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, Object pData) { 60 return new TypedValueImpl(pValue, STRING_TYPE); 61 } 62 63 public TypedValue getCastToString(SimpleTypeSG pController, Object pValue, DirectAccessible pData) { 64 return new TypedValueImpl(pValue, STRING_TYPE); 65 } 66 67 public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 68 LocalJavaField f = pMethod.newJavaField(STRING_TYPE); 69 f.addLine(pValue); 70 pMethod.addIf(f, " != null"); 71 pSGlet.generate(pMethod, pValue); 72 pMethod.addEndIf(); 73 } 74 75 public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object pValue, SGlet pSGlet) throws SAXException { 76 pSGlet.generate(pMethod, pValue); 77 } 78 79 public boolean isCausingParseConversionEvent(SimpleTypeSG pController) { 80 final AtomicTypeSG atomicType = pController.getAtomicType(); 81 84 return atomicType.getLength() != null 85 || atomicType.getMinLength() != null 86 || atomicType.getMaxLength() != null; 87 } 88 89 public void addValidation(SimpleTypeSG pController, JavaMethod pMethod, DirectAccessible pValue) 90 throws SAXException { 91 94 final AtomicTypeSG atomicType = pController.getAtomicType(); 95 Long length = atomicType.getLength(); 96 Long maxLength = atomicType.getMaxLength(); 97 Long minLength = atomicType.getMinLength(); 98 99 if (minLength != null && minLength.longValue() < 0) { 100 throw new LocSAXException("Negative value for minLength detected: " + minLength, getLocator()); 101 } 102 if (maxLength != null) { 103 if (maxLength.longValue() < 0) { 104 throw new LocSAXException("Negative value for maxLength detected: " + maxLength, getLocator()); 105 } 106 if (minLength != null) { 107 if (maxLength.longValue() < minLength.longValue()) { 108 throw new LocSAXException("MaxLength value of " + maxLength + " is smaller than minLength value of " + minLength, 109 getLocator()); 110 } 111 if (maxLength.longValue() == minLength.longValue()) { 112 length = maxLength; 113 } 114 } 115 } 116 if (length != null) { 117 if (length.longValue() < 0) { 118 throw new LocSAXException("Negative value for length detected: " + length, getLocator()); 119 } 120 if (maxLength != null) { 121 if (maxLength.longValue() < length.longValue()) { 122 throw new LocSAXException("MaxLength value of " + maxLength + " is smaller than length value of " + length, 123 getLocator()); 124 } 125 maxLength = null; } 127 if (minLength != null) { 128 if (minLength.longValue() > length.longValue()) { 129 throw new LocSAXException("MinLength value of " + minLength + " is larger than length value of " + length, 130 getLocator()); 131 } 132 minLength = null; } 134 } 135 136 if (length != null || maxLength != null || minLength != null) { 137 if (pValue.isNullable()) { 138 pMethod.addIf(pValue, " != null"); 139 } 140 if (maxLength != null) { 141 pMethod.addIf(pValue, ".length()", " > ", maxLength); 142 pMethod.addThrowNew(IllegalArgumentException .class, 143 JavaSource.getQuoted("Length of " + maxLength + " characters exceeded: "), 144 " + ", pValue); 145 pMethod.addEndIf(); 146 } 147 if (minLength != null) { 148 pMethod.addIf(pValue, ".length()", " < ", minLength); 149 pMethod.addThrowNew(IllegalArgumentException .class, 150 JavaSource.getQuoted("Length of " + minLength + " characters exceeded: "), 151 " + ", pValue); 152 pMethod.addEndIf(); 153 } 154 if (length != null) { 155 pMethod.addIf(pValue, ".length()", " != ", length); 156 pMethod.addThrowNew(IllegalArgumentException .class, 157 JavaSource.getQuoted("Length of " + length + " characters not matched: "), 158 " + ", pValue); 159 pMethod.addEndIf(); 160 } 161 if (pValue.isNullable()) { 162 pMethod.addEndIf(); 163 } 164 } 165 } 166 } 167 | Popular Tags |