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 password datatype. */
021: public final class PasswordField 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", "20"));
027: int lMinLength = Integer.parseInt(pContext.getString(
028: "TemplateProperties.MinLength", "8"));
029: String lEmptyString = pContext.getString(
030: "TemplateProperties.EmptyString", "");
031: String lConcealedString = pContext.getString(
032: "TemplateProperties.ConcealedString", "********");
033: String lDatatypeName = pContext
034: .getMandatoryString("DatatypeName");
035:
036: pOutputWriter.println("import java.io.Serializable;");
037: pOutputWriter
038: .println("import com.metaboss.enterprise.datatypes.DataType;");
039: pOutputWriter
040: .println("import com.metaboss.enterprise.datatypes.DataTypeTranslationMetadata;");
041: pOutputWriter
042: .println("import com.metaboss.enterprise.datatypes.DataTypeValidationException;");
043: pOutputWriter
044: .println("import com.metaboss.enterprise.datatypes.DefaultDataTypeTranslationMetadataImplementation;");
045: pOutputWriter
046: .println("import com.metaboss.enterprise.datatypes.DataTypeInstanceCreationFailureException;");
047: pOutputWriter
048: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;");
049: pOutputWriter
050: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;");
051: pOutputWriter
052: .println("import com.metaboss.enterprise.datatypes.sqltranslation.SqlVarcharTranslator;");
053: pOutputWriter
054: .println("import com.metaboss.enterprise.datatypes.primitivetranslation.PrimitiveStringTranslator;");
055: pOutputWriter
056: .println("import com.metaboss.enterprise.datatypes.xmltranslation.XmlTextTranslator;");
057:
058: pOutputWriter.println("/** "
059: + pContext.getString("DatatypeDescription",
060: "No description provided") + " */");
061: pOutputWriter.println("public final class " + lDatatypeName
062: + " implements DataType, Serializable");
063: pOutputWriter.println("{");
064: pOutputWriter
065: .println(" // Static storage for translation metadata. Create once and return many times");
066: pOutputWriter
067: .println(" private static DataTypeTranslationMetadata sTranslationMetadata = null;");
068: pOutputWriter
069: .println(" // Static storage for the empty string constant.");
070: pOutputWriter
071: .println(" private static final String sEmptyString = \""
072: + lEmptyString + "\";");
073: pOutputWriter
074: .println(" // Static storage for the concealed string constant.");
075: pOutputWriter
076: .println(" private static final String sConcealedString = \""
077: + lConcealedString + "\";");
078: pOutputWriter
079: .println(" // State is stored here : 0 - normal, 1 - empty, 2 - concealed");
080: pOutputWriter.println(" private byte mState = 0;");
081: pOutputWriter.println(" // Actual value stored here");
082: pOutputWriter.println(" private String mValue;");
083:
084: pOutputWriter.println(" static");
085: pOutputWriter.println(" {");
086: pOutputWriter
087: .println(" SqlVarcharTranslator lSqlTranslator = new SqlVarcharTranslator()");
088: pOutputWriter.println(" {");
089: pOutputWriter
090: .println(" public String getSqlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
091: pOutputWriter.println(" {");
092: pOutputWriter.println(" " + lDatatypeName
093: + " lInstance = (" + lDatatypeName
094: + ")pDataTypeInstance;");
095: pOutputWriter
096: .println(" if (lInstance.isConcealed())");
097: pOutputWriter
098: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
099: pOutputWriter
100: .println(" if (lInstance.isEmpty())");
101: pOutputWriter
102: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
103: pOutputWriter
104: .println(" return lInstance.mValue;");
105: pOutputWriter.println(" }");
106: pOutputWriter
107: .println(" public DataType createFromSqlValue(String pSqlValue) throws DataTypeInstanceCreationFailureException");
108: pOutputWriter.println(" {");
109: pOutputWriter.println(" if (pSqlValue == null)");
110: pOutputWriter
111: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Sql value is null.\");");
112: pOutputWriter.println(" " + lDatatypeName
113: + " lValue = new " + lDatatypeName + "();");
114: pOutputWriter
115: .println(" lValue.mValue = pSqlValue;");
116: pOutputWriter.println(" return lValue;");
117: pOutputWriter.println(" }");
118: pOutputWriter.println(" };");
119: pOutputWriter
120: .println(" PrimitiveStringTranslator lPrimitiveTranslator = new PrimitiveStringTranslator()");
121: pOutputWriter.println(" {");
122: pOutputWriter
123: .println(" public String getPrimitiveValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
124: pOutputWriter.println(" {");
125: pOutputWriter.println(" " + lDatatypeName
126: + " lInstance = (" + lDatatypeName
127: + ")pDataTypeInstance;");
128: pOutputWriter
129: .println(" if (lInstance.isConcealed())");
130: pOutputWriter
131: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
132: pOutputWriter
133: .println(" if (lInstance.isEmpty())");
134: pOutputWriter
135: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
136: pOutputWriter
137: .println(" return lInstance.mValue;");
138: pOutputWriter.println(" }");
139: pOutputWriter
140: .println(" public DataType createFromPrimitiveValue(String pPrimitiveValue) throws DataTypeInstanceCreationFailureException");
141: pOutputWriter.println(" {");
142: pOutputWriter
143: .println(" if (pPrimitiveValue == null)");
144: pOutputWriter
145: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Primitive value is null.\");");
146: pOutputWriter.println(" " + lDatatypeName
147: + " lValue = new " + lDatatypeName + "();");
148: pOutputWriter
149: .println(" lValue.mValue = pPrimitiveValue;");
150: pOutputWriter.println(" return lValue;");
151: pOutputWriter.println(" }");
152: pOutputWriter.println(" };");
153: pOutputWriter
154: .println(" XmlTextTranslator lXmlTranslator = new XmlTextTranslator()");
155: pOutputWriter.println(" {");
156: pOutputWriter
157: .println(" public String getXmlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
158: pOutputWriter.println(" {");
159: pOutputWriter.println(" " + lDatatypeName
160: + " lInstance = (" + lDatatypeName
161: + ")pDataTypeInstance;");
162: pOutputWriter
163: .println(" if (lInstance.isConcealed())");
164: pOutputWriter
165: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
166: pOutputWriter
167: .println(" if (lInstance.isEmpty())");
168: pOutputWriter
169: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
170: pOutputWriter
171: .println(" return lInstance.mValue;");
172: pOutputWriter.println(" }");
173: pOutputWriter
174: .println(" public DataType createFromXmlValue(String pXmlValue) throws DataTypeInstanceCreationFailureException");
175: pOutputWriter.println(" {");
176: pOutputWriter.println(" if (pXmlValue == null)");
177: pOutputWriter
178: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Xml value is null.\");");
179: pOutputWriter.println(" " + lDatatypeName
180: + " lValue = new " + lDatatypeName + "();");
181: pOutputWriter
182: .println(" lValue.mValue = pXmlValue;");
183: pOutputWriter.println(" return lValue;");
184: pOutputWriter.println(" }");
185: pOutputWriter.println(" };");
186: pOutputWriter
187: .println(" java.util.Properties lProperties = new java.util.Properties();");
188: if (lMinLength > 0)
189: pOutputWriter
190: .println(" lProperties.setProperty(\"minsize\",\""
191: + lMinLength + "\");");
192: pOutputWriter
193: .println(" lProperties.setProperty(\"maxsize\",\""
194: + lMaxLength + "\");");
195: pOutputWriter
196: .println(" sTranslationMetadata = new DefaultDataTypeTranslationMetadataImplementation(DataTypeTranslationMetadata.SQL_VARCHAR, lProperties, lSqlTranslator, DataTypeTranslationMetadata.PRIMITIVE_STRING, null, lPrimitiveTranslator, DataTypeTranslationMetadata.XML_TEXT, null, lXmlTranslator);");
197: pOutputWriter.println(" }");
198:
199: pOutputWriter
200: .println(" /** Returns this datatype's translation metadata.");
201: pOutputWriter
202: .println(" * See {@link com.metaboss.enterprise.datatypes.DataType DataType} for more details */");
203: pOutputWriter
204: .println(" public static DataTypeTranslationMetadata getTranslationMetadata()");
205: pOutputWriter.println(" {");
206: pOutputWriter.println(" return sTranslationMetadata;");
207: pOutputWriter.println(" }");
208:
209: pOutputWriter
210: .println(" /** Creates an instance of the datatype carrying an empty value */");
211: pOutputWriter.println(" public static " + lDatatypeName
212: + " createEmpty()");
213: pOutputWriter.println(" {");
214: pOutputWriter.println(" " + lDatatypeName
215: + " lValue = new " + lDatatypeName + "();");
216: pOutputWriter.println(" lValue.mState = 1;");
217: pOutputWriter.println(" return lValue;");
218: pOutputWriter.println(" }");
219:
220: pOutputWriter
221: .println(" /** Creates an instance of the datatype carrying concealed value */");
222: pOutputWriter.println(" public static " + lDatatypeName
223: + " createConcealed()");
224: pOutputWriter.println(" {");
225: pOutputWriter.println(" " + lDatatypeName
226: + " lValue = new " + lDatatypeName + "();");
227: pOutputWriter.println(" lValue.mState = 2;");
228: pOutputWriter.println(" return lValue;");
229: pOutputWriter.println(" }");
230:
231: pOutputWriter
232: .println(" /** Creates an instance of the datatype from given readable value */");
233: pOutputWriter
234: .println(" public static "
235: + lDatatypeName
236: + " createFromString( String pSourceString) throws DataTypeValidationException");
237: pOutputWriter.println(" {");
238: pOutputWriter.println(" // Perform validation");
239: pOutputWriter.println(" validateString(pSourceString);");
240: pOutputWriter.println(" // Create instance");
241: pOutputWriter
242: .println(" if (pSourceString.equals(sEmptyString))");
243: pOutputWriter.println(" return createEmpty();");
244: pOutputWriter
245: .println(" if (pSourceString.equals(sConcealedString))");
246: pOutputWriter.println(" return createConcealed();");
247: pOutputWriter.println(" // Create instance");
248: pOutputWriter.println(" " + lDatatypeName
249: + " lValue = new " + lDatatypeName + "();");
250: pOutputWriter.println(" lValue.mValue = pSourceString;");
251: pOutputWriter.println(" return lValue;");
252: pOutputWriter.println(" }");
253:
254: pOutputWriter
255: .println(" /** Validates proposed readable value */");
256: pOutputWriter
257: .println(" public static void validateString( String pProposedString) throws DataTypeValidationException");
258: pOutputWriter.println(" {");
259: pOutputWriter.println(" if (pProposedString == null)");
260: pOutputWriter
261: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
262: + lDatatypeName
263: + ". Use createEmpty() method to create an empty "
264: + lDatatypeName + "\");");
265: pOutputWriter
266: .println(" if (pProposedString.equals(sEmptyString))");
267: pOutputWriter.println(" return; // Done validation");
268: pOutputWriter
269: .println(" if (pProposedString.equals(sConcealedString))");
270: pOutputWriter
271: .println(" return; // Done validation");
272: pOutputWriter
273: .println(" int lLength = pProposedString.length();");
274: pOutputWriter.println(" if (lLength > " + lMaxLength
275: + ")");
276: pOutputWriter
277: .println(" throw new DataTypeValidationException(\"String is too long for "
278: + lDatatypeName
279: + ". Expected max length is "
280: + lMaxLength + " characters.\");");
281: if (lMinLength > 0) {
282: pOutputWriter.println(" if (lLength < " + lMinLength
283: + ")");
284: pOutputWriter
285: .println(" throw new DataTypeValidationException(\"String is too short for "
286: + lDatatypeName
287: + ". Expected min length is "
288: + lMinLength
289: + " characters.\");");
290: }
291: pOutputWriter.println(" }");
292:
293: pOutputWriter
294: .println(" /** Returns the value inside this instance of "
295: + lDatatypeName + " as readable string */");
296: pOutputWriter.println(" public String toString()");
297: pOutputWriter.println(" {");
298: pOutputWriter.println(" if (isEmpty())");
299: pOutputWriter.println(" return sEmptyString;");
300: pOutputWriter.println(" if (isConcealed())");
301: pOutputWriter.println(" return sConcealedString;");
302: pOutputWriter.println(" return mValue;");
303: pOutputWriter.println(" }");
304:
305: pOutputWriter
306: .println(" /** Indicates whether some other object is \"equal to\" this one");
307: pOutputWriter
308: .println(" * @return true if this object is the same as the obj argument; false otherwise. */");
309: pOutputWriter
310: .println(" public boolean equals(Object pOtherObject)");
311: pOutputWriter.println(" {");
312: pOutputWriter.println(" if (pOtherObject == null)");
313: pOutputWriter.println(" return false;");
314: pOutputWriter.println(" if ((pOtherObject instanceof "
315: + lDatatypeName + ") == false)");
316: pOutputWriter.println(" return false;");
317: pOutputWriter.println(" if (((" + lDatatypeName
318: + ")pOtherObject).isConcealed() || isConcealed())");
319: pOutputWriter
320: .println(" return false; // One of the instances is concealed");
321: pOutputWriter.println(" if (((" + lDatatypeName
322: + ")pOtherObject).isEmpty() && isEmpty())");
323: pOutputWriter
324: .println(" return true; // Both instances are empty.");
325: pOutputWriter.println(" if (((" + lDatatypeName
326: + ")pOtherObject).isEmpty() != isEmpty())");
327: pOutputWriter
328: .println(" return false; // One of the instances is empty, the other one is not");
329: pOutputWriter.println(" return ((" + lDatatypeName
330: + ")pOtherObject).mValue.equals(mValue);");
331: pOutputWriter.println(" }");
332:
333: pOutputWriter
334: .println(" /** Returns a hash code value for the object. This method is supported for the benefit of hashtables");
335: pOutputWriter
336: .println(" * @return instance's hash code or zero if this instance is empty or concealed */");
337: pOutputWriter.println(" public int hashCode()");
338: pOutputWriter.println(" {");
339: pOutputWriter
340: .println(" if (isEmpty() || isConcealed())");
341: pOutputWriter.println(" return 0;");
342: pOutputWriter.println(" return mValue.hashCode();");
343: pOutputWriter.println(" }");
344:
345: pOutputWriter
346: .println(" /** Returns true if the instance contains an empty value */");
347: pOutputWriter.println(" public boolean isEmpty()");
348: pOutputWriter.println(" {");
349: pOutputWriter.println(" return mState == 1;");
350: pOutputWriter.println(" }");
351:
352: pOutputWriter
353: .println(" /** Returns true if the instance contains concealed value */");
354: pOutputWriter.println(" public boolean isConcealed()");
355: pOutputWriter.println(" {");
356: pOutputWriter.println(" return mState == 2;");
357: pOutputWriter.println(" }");
358:
359: pOutputWriter
360: .println(" // Private access. External callers must use one of the static creators");
361: pOutputWriter.println(" private " + lDatatypeName + "()");
362: pOutputWriter.println(" {");
363: pOutputWriter.println(" }");
364: pOutputWriter.println("}");
365: }
366: }
|