001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2001-2003 © Softaris Pty.Ltd. All Rights Reserved.
015: package metalib.common;
016:
017: import java.io.PrintWriter;
018: import com.metaboss.javatemplate.*;
019:
020: /** The basic string datatype. */
021: public final class StringField implements JavaTemplate {
022: /* This method is called to perform the template merge with the values supplied in context */
023: public void mergeTemplate(PrintWriter pOutputWriter,
024: JavaTemplateContext pContext) throws JavaTemplateException {
025: int lMaxLength = Integer.parseInt(pContext.getString(
026: "TemplateProperties.MaxLength", "100"));
027: int lMinLength = Integer.parseInt(pContext.getString(
028: "TemplateProperties.MinLength", "0"));
029: String lEmptyString = pContext.getString(
030: "TemplateProperties.EmptyString", "");
031: String lConcealedString = pContext.getString(
032: "TemplateProperties.ConcealedString", "xxxxxx");
033: boolean lComparable = Boolean.valueOf(
034: pContext.getString("TemplateProperties.Comparable",
035: "true")).booleanValue();
036: String lDatatypeName = pContext
037: .getMandatoryString("DatatypeName");
038:
039: pOutputWriter.println("import java.io.Serializable;");
040: pOutputWriter
041: .println("import com.metaboss.enterprise.datatypes.DataType;");
042: pOutputWriter
043: .println("import com.metaboss.enterprise.datatypes.DataTypeTranslationMetadata;");
044: pOutputWriter
045: .println("import com.metaboss.enterprise.datatypes.DataTypeValidationException;");
046: pOutputWriter
047: .println("import com.metaboss.enterprise.datatypes.DefaultDataTypeTranslationMetadataImplementation;");
048: pOutputWriter
049: .println("import com.metaboss.enterprise.datatypes.DataTypeInstanceCreationFailureException;");
050: pOutputWriter
051: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;");
052: pOutputWriter
053: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;");
054: pOutputWriter
055: .println("import com.metaboss.enterprise.datatypes.sqltranslation.SqlVarcharTranslator;");
056: pOutputWriter
057: .println("import com.metaboss.enterprise.datatypes.primitivetranslation.PrimitiveStringTranslator;");
058: pOutputWriter
059: .println("import com.metaboss.enterprise.datatypes.xmltranslation.XmlTextTranslator;");
060:
061: pOutputWriter.println("/** "
062: + pContext.getString("DatatypeDescription",
063: "No description provided") + " */");
064: if (lComparable) {
065: pOutputWriter.println("public final class " + lDatatypeName
066: + " implements DataType, Serializable, Comparable");
067: } else {
068: pOutputWriter.println("public final class " + lDatatypeName
069: + " implements DataType, Serializable");
070: }
071: pOutputWriter.println("{");
072: pOutputWriter
073: .println(" // Static storage for translation metadata. Create once and return many times");
074: pOutputWriter
075: .println(" private static DataTypeTranslationMetadata sTranslationMetadata = null;");
076: pOutputWriter
077: .println(" // Static storage for the empty string constant.");
078: pOutputWriter
079: .println(" private static final String sEmptyString = \""
080: + lEmptyString + "\";");
081: pOutputWriter
082: .println(" // Static storage for the concealed string constant.");
083: pOutputWriter
084: .println(" private static final String sConcealedString = \""
085: + lConcealedString + "\";");
086: pOutputWriter
087: .println(" // State is stored here : 0 - normal, 1 - empty, 2 - concealed");
088: pOutputWriter.println(" private byte mState = 0;");
089: pOutputWriter.println(" // Actual value stored here");
090: pOutputWriter.println(" private String mValue;");
091:
092: pOutputWriter.println(" static");
093: pOutputWriter.println(" {");
094: pOutputWriter
095: .println(" SqlVarcharTranslator lSqlTranslator = new SqlVarcharTranslator()");
096: pOutputWriter.println(" {");
097: pOutputWriter
098: .println(" public String getSqlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
099: pOutputWriter.println(" {");
100: pOutputWriter.println(" " + lDatatypeName
101: + " lInstance = (" + lDatatypeName
102: + ")pDataTypeInstance;");
103: pOutputWriter
104: .println(" if (lInstance.isConcealed())");
105: pOutputWriter
106: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
107: pOutputWriter
108: .println(" if (lInstance.isEmpty())");
109: pOutputWriter
110: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
111: pOutputWriter
112: .println(" return lInstance.mValue;");
113: pOutputWriter.println(" }");
114: pOutputWriter
115: .println(" public DataType createFromSqlValue(String pSqlValue) throws DataTypeInstanceCreationFailureException");
116: pOutputWriter.println(" {");
117: pOutputWriter.println(" if (pSqlValue == null)");
118: pOutputWriter
119: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Sql value is null.\");");
120: pOutputWriter.println(" " + lDatatypeName
121: + " lValue = new " + lDatatypeName + "();");
122: pOutputWriter
123: .println(" lValue.mValue = pSqlValue;");
124: pOutputWriter.println(" return lValue;");
125: pOutputWriter.println(" }");
126: pOutputWriter.println(" };");
127: pOutputWriter
128: .println(" PrimitiveStringTranslator lPrimitiveTranslator = new PrimitiveStringTranslator()");
129: pOutputWriter.println(" {");
130: pOutputWriter
131: .println(" public String getPrimitiveValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
132: pOutputWriter.println(" {");
133: pOutputWriter.println(" " + lDatatypeName
134: + " lInstance = (" + lDatatypeName
135: + ")pDataTypeInstance;");
136: pOutputWriter
137: .println(" if (lInstance.isConcealed())");
138: pOutputWriter
139: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
140: pOutputWriter
141: .println(" if (lInstance.isEmpty())");
142: pOutputWriter
143: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
144: pOutputWriter
145: .println(" return lInstance.mValue;");
146: pOutputWriter.println(" }");
147: pOutputWriter
148: .println(" public DataType createFromPrimitiveValue(String pPrimitiveValue) throws DataTypeInstanceCreationFailureException");
149: pOutputWriter.println(" {");
150: pOutputWriter
151: .println(" if (pPrimitiveValue == null)");
152: pOutputWriter
153: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Primitive value is null.\");");
154: pOutputWriter.println(" " + lDatatypeName
155: + " lValue = new " + lDatatypeName + "();");
156: pOutputWriter
157: .println(" lValue.mValue = pPrimitiveValue;");
158: pOutputWriter.println(" return lValue;");
159: pOutputWriter.println(" }");
160: pOutputWriter.println(" };");
161: pOutputWriter
162: .println(" XmlTextTranslator lXmlTranslator = new XmlTextTranslator()");
163: pOutputWriter.println(" {");
164: pOutputWriter
165: .println(" public String getXmlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
166: pOutputWriter.println(" {");
167: pOutputWriter.println(" " + lDatatypeName
168: + " lInstance = (" + lDatatypeName
169: + ")pDataTypeInstance;");
170: pOutputWriter
171: .println(" if (lInstance.isConcealed())");
172: pOutputWriter
173: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
174: pOutputWriter
175: .println(" if (lInstance.isEmpty())");
176: pOutputWriter
177: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
178: pOutputWriter
179: .println(" return lInstance.mValue;");
180: pOutputWriter.println(" }");
181: pOutputWriter
182: .println(" public DataType createFromXmlValue(String pXmlValue) throws DataTypeInstanceCreationFailureException");
183: pOutputWriter.println(" {");
184: pOutputWriter.println(" if (pXmlValue == null)");
185: pOutputWriter
186: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Xml value is null.\");");
187: pOutputWriter.println(" " + lDatatypeName
188: + " lValue = new " + lDatatypeName + "();");
189: pOutputWriter
190: .println(" lValue.mValue = pXmlValue;");
191: pOutputWriter.println(" return lValue;");
192: pOutputWriter.println(" }");
193: pOutputWriter.println(" };");
194: pOutputWriter
195: .println(" java.util.Properties lProperties = new java.util.Properties();");
196: if (lMinLength > 0)
197: pOutputWriter
198: .println(" lProperties.setProperty(\"minsize\",\""
199: + lMinLength + "\");");
200: pOutputWriter
201: .println(" lProperties.setProperty(\"maxsize\",\""
202: + lMaxLength + "\");");
203: pOutputWriter
204: .println(" sTranslationMetadata = new DefaultDataTypeTranslationMetadataImplementation(DataTypeTranslationMetadata.SQL_VARCHAR, lProperties, lSqlTranslator, DataTypeTranslationMetadata.PRIMITIVE_STRING, null, lPrimitiveTranslator, DataTypeTranslationMetadata.XML_TEXT, null, lXmlTranslator);");
205: pOutputWriter.println(" }");
206:
207: pOutputWriter
208: .println(" /** Returns this datatype's translation metadata.");
209: pOutputWriter
210: .println(" * See {@link com.metaboss.enterprise.datatypes.DataType DataType} for more details */");
211: pOutputWriter
212: .println(" public static DataTypeTranslationMetadata getTranslationMetadata()");
213: pOutputWriter.println(" {");
214: pOutputWriter.println(" return sTranslationMetadata;");
215: pOutputWriter.println(" }");
216:
217: pOutputWriter
218: .println(" /** Returns string used to represent empty value in a toString() / createFromString() methods */");
219: pOutputWriter
220: .println(" public static String getEmptyString()");
221: pOutputWriter.println(" {");
222: pOutputWriter.println(" return sEmptyString;");
223: pOutputWriter.println(" }");
224:
225: pOutputWriter
226: .println(" /** Returns string used to represent concealed value in a toString() / createFromString() methods */");
227: pOutputWriter
228: .println(" public static String getConcealedString()");
229: pOutputWriter.println(" {");
230: pOutputWriter.println(" return sConcealedString;");
231: pOutputWriter.println(" }");
232:
233: pOutputWriter
234: .println(" /** Returns maximum length of string this datatype will take or return in toString() / createFromString() methods */");
235: pOutputWriter
236: .println(" public static long getMaxStringLength()");
237: pOutputWriter.println(" {");
238: pOutputWriter.println(" return "
239: + Integer.toString(lMaxLength) + "L;");
240: pOutputWriter.println(" }");
241:
242: pOutputWriter
243: .println(" /** Creates an instance of the datatype carrying an empty value */");
244: pOutputWriter.println(" public static " + lDatatypeName
245: + " createEmpty()");
246: pOutputWriter.println(" {");
247: pOutputWriter.println(" " + lDatatypeName
248: + " lValue = new " + lDatatypeName + "();");
249: pOutputWriter.println(" lValue.mState = 1;");
250: pOutputWriter.println(" return lValue;");
251: pOutputWriter.println(" }");
252:
253: pOutputWriter
254: .println(" /** Creates an instance of the datatype carrying concealed value */");
255: pOutputWriter.println(" public static " + lDatatypeName
256: + " createConcealed()");
257: pOutputWriter.println(" {");
258: pOutputWriter.println(" " + lDatatypeName
259: + " lValue = new " + lDatatypeName + "();");
260: pOutputWriter.println(" lValue.mState = 2;");
261: pOutputWriter.println(" return lValue;");
262: pOutputWriter.println(" }");
263:
264: pOutputWriter
265: .println(" /** Creates an instance of the datatype from given readable value */");
266: pOutputWriter
267: .println(" public static "
268: + lDatatypeName
269: + " createFromString( String pSourceString) throws DataTypeValidationException");
270: pOutputWriter.println(" {");
271: pOutputWriter.println(" // Perform validation");
272: pOutputWriter.println(" validateString(pSourceString);");
273: pOutputWriter.println(" // Create instance");
274: pOutputWriter
275: .println(" if (pSourceString.equals(sEmptyString))");
276: pOutputWriter.println(" return createEmpty();");
277: pOutputWriter
278: .println(" if (pSourceString.equals(sConcealedString))");
279: pOutputWriter.println(" return createConcealed();");
280: pOutputWriter.println(" // Create instance");
281: pOutputWriter.println(" " + lDatatypeName
282: + " lValue = new " + lDatatypeName + "();");
283: pOutputWriter.println(" lValue.mValue = pSourceString;");
284: pOutputWriter.println(" return lValue;");
285: pOutputWriter.println(" }");
286:
287: pOutputWriter
288: .println(" /** Validates proposed readable value */");
289: pOutputWriter
290: .println(" public static void validateString( String pProposedString) throws DataTypeValidationException");
291: pOutputWriter.println(" {");
292: pOutputWriter.println(" if (pProposedString == null)");
293: pOutputWriter
294: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
295: + lDatatypeName
296: + ". Use createEmpty() method to create an empty "
297: + lDatatypeName + "\");");
298: pOutputWriter
299: .println(" if (pProposedString.equals(sEmptyString))");
300: pOutputWriter.println(" return; // Done validation");
301: pOutputWriter
302: .println(" if (pProposedString.equals(sConcealedString))");
303: pOutputWriter
304: .println(" return; // Done validation");
305: pOutputWriter
306: .println(" int lLength = pProposedString.length();");
307: pOutputWriter.println(" if (lLength > " + lMaxLength
308: + ")");
309: pOutputWriter
310: .println(" throw new DataTypeValidationException(\"String is too long for "
311: + lDatatypeName
312: + ". Expected max length is "
313: + lMaxLength + " characters.\");");
314: if (lMinLength > 0) {
315: pOutputWriter.println(" if (lLength < " + lMinLength
316: + ")");
317: pOutputWriter
318: .println(" throw new DataTypeValidationException(\"String is too short for "
319: + lDatatypeName
320: + ". Expected min length is "
321: + lMinLength
322: + " characters.\");");
323: }
324: pOutputWriter.println(" }");
325:
326: if (lComparable) {
327: pOutputWriter
328: .println(" /** Compares this instance to another");
329: pOutputWriter
330: .println(" * Two empty instances considered equal. The empty instance is always less than any non-empty one.");
331: pOutputWriter
332: .println(" * Two concealed instances considered equal. The concealed instance is always less than any non-concealed one.");
333: pOutputWriter
334: .println(" * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.");
335: pOutputWriter
336: .println(" * @exception java.lang.ClassCastException if the specified object's type prevents it from being compared to this Object */");
337: pOutputWriter
338: .println(" public int compareTo(Object pOtherObject)");
339: pOutputWriter.println(" {");
340: pOutputWriter.println(" " + lDatatypeName
341: + " lOtherInstance = (" + lDatatypeName
342: + ")pOtherObject;");
343: pOutputWriter
344: .println(" if (lOtherInstance.isEmpty() && isEmpty())");
345: pOutputWriter
346: .println(" return 0; // Both instances are empty and therefore equal.");
347: pOutputWriter.println(" if (isEmpty())");
348: pOutputWriter
349: .println(" return -1; // Anything bigger than empty");
350: pOutputWriter
351: .println(" if (lOtherInstance.isEmpty())");
352: pOutputWriter
353: .println(" return 1; // Anything bigger than empty");
354: pOutputWriter
355: .println(" if (lOtherInstance.isConcealed() && isConcealed())");
356: pOutputWriter
357: .println(" return 0; // Both instances are concealed and therefore equal.");
358: pOutputWriter.println(" if (isConcealed())");
359: pOutputWriter
360: .println(" return -1; // Anything bigger than concealed");
361: pOutputWriter
362: .println(" if (lOtherInstance.isConcealed())");
363: pOutputWriter
364: .println(" return 1; // Anything bigger than concealed");
365: pOutputWriter
366: .println(" return mValue.compareTo(lOtherInstance.mValue);");
367: pOutputWriter.println(" }");
368: }
369: pOutputWriter
370: .println(" /** Returns the value inside this instance of "
371: + lDatatypeName + " as readable string */");
372: pOutputWriter.println(" public String toString()");
373: pOutputWriter.println(" {");
374: pOutputWriter.println(" if (isEmpty())");
375: pOutputWriter.println(" return sEmptyString;");
376: pOutputWriter.println(" if (isConcealed())");
377: pOutputWriter.println(" return sConcealedString;");
378: pOutputWriter.println(" return mValue;");
379: pOutputWriter.println(" }");
380:
381: pOutputWriter
382: .println(" /** Indicates whether some other object is \"equal to\" this one");
383: pOutputWriter
384: .println(" * @return true if this object is the same as the obj argument; false otherwise. */");
385: pOutputWriter
386: .println(" public boolean equals(Object pOtherObject)");
387: pOutputWriter.println(" {");
388: pOutputWriter.println(" if (pOtherObject == null)");
389: pOutputWriter.println(" return false;");
390: pOutputWriter.println(" if ((pOtherObject instanceof "
391: + lDatatypeName + ") == false)");
392: pOutputWriter.println(" return false;");
393: pOutputWriter.println(" if (((" + lDatatypeName
394: + ")pOtherObject).isConcealed() || isConcealed())");
395: pOutputWriter
396: .println(" return false; // One of the instances is concealed");
397: pOutputWriter.println(" if (((" + lDatatypeName
398: + ")pOtherObject).isEmpty() && isEmpty())");
399: pOutputWriter
400: .println(" return true; // Both instances are empty.");
401: pOutputWriter.println(" if (((" + lDatatypeName
402: + ")pOtherObject).isEmpty() != isEmpty())");
403: pOutputWriter
404: .println(" return false; // One of the instances is empty, the other one is not");
405: pOutputWriter.println(" return ((" + lDatatypeName
406: + ")pOtherObject).mValue.equals(mValue);");
407: pOutputWriter.println(" }");
408:
409: pOutputWriter
410: .println(" /** Returns a hash code value for the object. This method is supported for the benefit of hashtables");
411: pOutputWriter
412: .println(" * @return instance's hash code or zero if this instance is empty or concealed */");
413: pOutputWriter.println(" public int hashCode()");
414: pOutputWriter.println(" {");
415: pOutputWriter
416: .println(" if (isEmpty() || isConcealed())");
417: pOutputWriter.println(" return 0;");
418: pOutputWriter.println(" return mValue.hashCode();");
419: pOutputWriter.println(" }");
420:
421: pOutputWriter
422: .println(" /** Returns true if the instance contains an empty value */");
423: pOutputWriter.println(" public boolean isEmpty()");
424: pOutputWriter.println(" {");
425: pOutputWriter.println(" return mState == 1;");
426: pOutputWriter.println(" }");
427:
428: pOutputWriter
429: .println(" /** Returns true if the instance contains concealed value */");
430: pOutputWriter.println(" public boolean isConcealed()");
431: pOutputWriter.println(" {");
432: pOutputWriter.println(" return mState == 2;");
433: pOutputWriter.println(" }");
434:
435: pOutputWriter
436: .println(" // Private access. External callers must use one of the static creators");
437: pOutputWriter.println(" private " + lDatatypeName + "()");
438: pOutputWriter.println(" {");
439: pOutputWriter.println(" }");
440: pOutputWriter.println("}");
441: }
442: }
|