0001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
0002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
0003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
0005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
0008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
0012: // POSSIBILITY OF SUCH DAMAGE.
0013: //
0014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
0015: package designlibrary.core;
0016:
0017: import java.io.PrintWriter;
0018: import java.util.ArrayList;
0019: import java.util.HashMap;
0020: import java.util.HashSet;
0021: import java.util.Properties;
0022:
0023: import com.metaboss.enterprise.bs.BSException;
0024: import com.metaboss.javatemplate.JavaTemplate;
0025: import com.metaboss.javatemplate.JavaTemplateContext;
0026: import com.metaboss.javatemplate.JavaTemplateException;
0027: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
0028: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
0029: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDatatypeStylesheet;
0030: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDocumentationStylesheet;
0031: import com.metaboss.util.StringUtils;
0032:
0033: /** The basic enumerated value datatype. */
0034: public final class EnumerableValueField implements JavaTemplate {
0035: // This class keeps the enumerable value metadata
0036: private class ValueMetadata {
0037: public String Name;
0038: public String Description;
0039: public String VariableName;
0040: }
0041:
0042: // This class keeps the category metadata
0043: private class CategoryMetadata {
0044: public String Name;
0045: public String Description;
0046: public String PartOfIdentifierName;
0047: public ValueMetadata[] CategoryValues;
0048: }
0049:
0050: // Holds details for a jargon
0051: private class JargonMetadata {
0052: public String Name;
0053: public String Description;
0054: public String PartOfIdentifierName;
0055: public String EmptyString;
0056: public String ConcealedString;
0057: public String[] JargonValueNames;
0058: }
0059:
0060: /* This method is called to perform the template merge with the values supplied in context */
0061: public void mergeTemplate(PrintWriter pOutputWriter,
0062: JavaTemplateContext pContext) throws JavaTemplateException {
0063: // Access mandatory elements of the context
0064: DataType lDatatype = (DataType) pContext
0065: .getMandatoryObject("DataType");
0066: STDocumentationStylesheet lDocumentationStylesheet = null;
0067: STDatatypeStylesheet lDataTypeStylesheet = null;
0068: try {
0069: lDocumentationStylesheet = CodeGenerationStylesheetAccessor
0070: .getDocumentationStylesheet();
0071: lDataTypeStylesheet = CodeGenerationStylesheetAccessor
0072: .getDatatypeStylesheet(lDatatype);
0073: } catch (BSException e) {
0074: throw new JavaTemplateException(
0075: "Unable to obtain Datatype stylesheet. DataTypeRef:"
0076: + lDatatype.getRef(), e);
0077: }
0078: String lDatatypeName = lDataTypeStylesheet.getNormalisedName();
0079: Properties lTypetemplateProperties = lDataTypeStylesheet
0080: .getTypetemplateProperties();
0081: String lEmptyString = lTypetemplateProperties.getProperty(
0082: "EmptyString", "");
0083: String lConcealedString = lTypetemplateProperties.getProperty(
0084: "ConcealedString", "xxxxxx");
0085:
0086: int lMaxValueLength = 1;
0087: boolean lZeroBasedValues = true;
0088:
0089: // Work with values
0090: HashMap lValuesMap = new HashMap(); // ValueMetadata indexed by value name
0091: ValueMetadata[] lValues; // Array of value metadatas as they where specified important for looking up by index
0092: {
0093: HashSet lUsedNames = new HashSet();
0094: HashSet lUsedVariableNames = new HashSet();
0095: ArrayList lValuesArray = new ArrayList();
0096: for (int i = 0;; i++) {
0097: String lName = lTypetemplateProperties.getProperty(
0098: "Value[" + i + "].Name", null);
0099: if (lName == null) {
0100: if (i == 0) {
0101: lZeroBasedValues = false;
0102: continue; // Relax about zero-element - use it if it is there and continue if it is not there
0103: }
0104: break; //no more names
0105: }
0106: if (!lUsedNames.add(lName))
0107: throw new JavaTemplateException("Property Value["
0108: + i + "].Name contains duplicate value '"
0109: + lName + "'. DataTypeRef:"
0110: + lDatatype.getRef());
0111: ValueMetadata lMetadata = new ValueMetadata();
0112: lMetadata.Name = lName;
0113: // Work on the name for variable
0114: lMetadata.VariableName = StringUtils
0115: .suggestJavaIdentifier(lMetadata.Name);
0116: if (lMetadata.VariableName == null
0117: || lMetadata.VariableName.length() == 0)
0118: throw new JavaTemplateException(
0119: "Unable to create Java identifier from the given Name '"
0120: + lMetadata.Name + "' DataTypeRef:"
0121: + lDatatype.getRef());
0122: lMetadata.VariableName = lMetadata.VariableName
0123: .toUpperCase();
0124: // Work on description
0125: lMetadata.Description = lTypetemplateProperties
0126: .getProperty("Value[" + i + "].Description",
0127: null);
0128: if (lMetadata.Description == null
0129: || lMetadata.Description.trim().length() == 0)
0130: lMetadata.Description = lDocumentationStylesheet
0131: .getMissingDescriptionHtmlText();
0132: // Add to collections
0133: lValuesMap.put(lName, lMetadata);
0134: lValuesArray.add(lMetadata);
0135: // Accumulate maximum value length
0136: int lValueLength = lName.length();
0137: if (lValueLength > lMaxValueLength)
0138: lMaxValueLength = lValueLength;
0139: }
0140: lValues = (ValueMetadata[]) lValuesArray
0141: .toArray(new ValueMetadata[lValuesArray.size()]);
0142: }
0143:
0144: // Work with categories
0145: CategoryMetadata[] lCategories = new CategoryMetadata[0];
0146: {
0147: HashSet lUsedNames = new HashSet();
0148: ArrayList lCategoriesArray = new ArrayList();
0149: for (int i = 0;; i++) {
0150: String lName = lTypetemplateProperties.getProperty(
0151: "Category[" + i + "].Name", null);
0152: if (lName == null) {
0153: if (i == 0)
0154: continue; // Relax about zero-element - use it if it is there and continue if it is not there
0155: break; //no more names
0156: }
0157: if (!lUsedNames.add(lName))
0158: throw new JavaTemplateException(
0159: "Property Category["
0160: + i
0161: + "].Name contains duplicate value '"
0162: + lName + "'.");
0163: CategoryMetadata lMetadata = new CategoryMetadata();
0164: lMetadata.Name = lName;
0165: lMetadata.Description = lTypetemplateProperties
0166: .getProperty("Category[" + i + "].Description",
0167: null);
0168: if (lMetadata.Description == null
0169: || lMetadata.Description.trim().length() == 0)
0170: lMetadata.Description = lDocumentationStylesheet
0171: .getMissingDescriptionHtmlText();
0172: lMetadata.PartOfIdentifierName = StringUtils
0173: .suggestName(lName, true, false);
0174: // Watch for clashes with isEmpty() and isConcealed() methods
0175: if (lMetadata.PartOfIdentifierName.equals("Empty")
0176: || lMetadata.PartOfIdentifierName
0177: .equals("Concealed"))
0178: throw new JavaTemplateException(
0179: "Category name "
0180: + lName
0181: + " can not be used. This name is reserved for generated constant or method names.");
0182: ArrayList lCategoryValues = new ArrayList();
0183: for (int j = 0;; j++) {
0184: String lCategoryValueName = lTypetemplateProperties
0185: .getProperty("Category[" + i
0186: + "].ValueName[" + j + "]", null);
0187: if (lCategoryValueName == null) {
0188: if (j == 0)
0189: continue; // Relax about zero-element - use it if it is there and continue if it is not there
0190: break; //no more names
0191: }
0192: ValueMetadata lValueMetadata = (ValueMetadata) lValuesMap
0193: .get(lCategoryValueName);
0194: if (lValueMetadata == null)
0195: throw new JavaTemplateException(
0196: "Undefined value '"
0197: + lCategoryValueName
0198: + "' listed in " + lName
0199: + " category.");
0200: lCategoryValues.add(lValueMetadata);
0201: }
0202: lMetadata.CategoryValues = (ValueMetadata[]) lCategoryValues
0203: .toArray(new ValueMetadata[lCategoryValues
0204: .size()]);
0205: lCategoriesArray.add(lMetadata);
0206: }
0207: lCategories = (CategoryMetadata[]) lCategoriesArray
0208: .toArray(new CategoryMetadata[lCategoriesArray
0209: .size()]);
0210: }
0211: // Work with jargons
0212: JargonMetadata[] lJargons; // Array of jargon metadatas as they where specified
0213: {
0214: HashSet lUsedNames = new HashSet();
0215: ArrayList lJargonsArray = new ArrayList();
0216: for (int i = 0;; i++) {
0217: String lName = lTypetemplateProperties.getProperty(
0218: "Jargon[" + i + "].Name", null);
0219: if (lName == null) {
0220: if (i == 0)
0221: continue; // Relax about zero-element - use it if it is there and continue if it is not there
0222: break; //no more names
0223: }
0224: if (!lUsedNames.add(lName))
0225: throw new JavaTemplateException("Property Jargon["
0226: + i + "].Name contains duplicate value '"
0227: + lName + "'.");
0228: JargonMetadata lMetadata = new JargonMetadata();
0229: lMetadata.Name = lName;
0230: lMetadata.PartOfIdentifierName = StringUtils
0231: .suggestName(lName, true, false);
0232: lMetadata.Description = lTypetemplateProperties
0233: .getProperty("Jargon[" + i + "].Description",
0234: null);
0235: if (lMetadata.Description == null
0236: || lMetadata.Description.trim().length() == 0)
0237: lMetadata.Description = lDocumentationStylesheet
0238: .getMissingDescriptionHtmlText();
0239: lMetadata.EmptyString = lTypetemplateProperties
0240: .getProperty("Jargon[" + i + "].EmptyString",
0241: lEmptyString);
0242: lMetadata.ConcealedString = lTypetemplateProperties
0243: .getProperty("Jargon[" + i
0244: + "].ConcealedString", lConcealedString);
0245: HashSet lUsedJargonValueNames = new HashSet();
0246: lMetadata.JargonValueNames = new String[lValues.length];
0247: for (int j = 0; j < lValues.length; j++) {
0248: String lJargonValueName_PropertyName = "Jargon["
0249: + i
0250: + "].Value["
0251: + Integer.toString(lZeroBasedValues ? (j)
0252: : (j + 1)) + "].Name";
0253: String lJargonValueName = lMetadata.JargonValueNames[j] = lTypetemplateProperties
0254: .getProperty(lJargonValueName_PropertyName,
0255: lValues[j].Name);
0256: if (!lUsedJargonValueNames.add(lJargonValueName))
0257: throw new JavaTemplateException("Property "
0258: + lJargonValueName_PropertyName
0259: + " contains duplicate value '"
0260: + lJargonValueName + "'.");
0261: }
0262: lJargonsArray.add(lMetadata);
0263: }
0264: lJargons = (JargonMetadata[]) lJargonsArray
0265: .toArray(new JargonMetadata[lJargonsArray.size()]);
0266: }
0267: boolean lComparable = Boolean.valueOf(
0268: lTypetemplateProperties.getProperty("Comparable",
0269: "false")).booleanValue();
0270:
0271: pOutputWriter.println(lDocumentationStylesheet
0272: .getCopyrightCommentInJava());
0273: pOutputWriter.println("package "
0274: + lDataTypeStylesheet.getPackageName() + ";");
0275: pOutputWriter.println();
0276: pOutputWriter.println("import java.util.Locale;");
0277: pOutputWriter.println("import java.io.Serializable;");
0278: pOutputWriter
0279: .println("import com.metaboss.util.ResourceUtils;");
0280: pOutputWriter
0281: .println("import com.metaboss.enterprise.datatypes.DataType;");
0282: pOutputWriter
0283: .println("import com.metaboss.enterprise.datatypes.DataTypeTranslationMetadata;");
0284: pOutputWriter
0285: .println("import com.metaboss.enterprise.datatypes.DataTypeValidationException;");
0286: pOutputWriter
0287: .println("import com.metaboss.enterprise.datatypes.DefaultDataTypeTranslationMetadataImplementation;");
0288: pOutputWriter
0289: .println("import com.metaboss.enterprise.datatypes.DataTypeInstanceCreationFailureException;");
0290: pOutputWriter
0291: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;");
0292: pOutputWriter
0293: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;");
0294: pOutputWriter
0295: .println("import com.metaboss.enterprise.datatypes.sqltranslation.SqlVarcharTranslator;");
0296: pOutputWriter
0297: .println("import com.metaboss.enterprise.datatypes.primitivetranslation.PrimitiveStringTranslator;");
0298: pOutputWriter
0299: .println("import com.metaboss.enterprise.datatypes.xmltranslation.XmlTextTranslator;");
0300:
0301: String lDescriptionHtmlText = lDataTypeStylesheet
0302: .getDescriptionHtmlText();
0303: pOutputWriter.println("/** " + lDescriptionHtmlText
0304: + (lDescriptionHtmlText.endsWith(".") ? "" : "."));
0305: pOutputWriter
0306: .println(" * <p>This datatype is an enumerable type, which means that it has a finite set of valid values.");
0307: pOutputWriter
0308: .println(" * Each valid value is represented by public member constant, which must be used as concrete instance of this type.");
0309: pOutputWriter
0310: .println(" * This datatype has following characteristics:</p>");
0311: pOutputWriter.println(" * <p><table border=\"0\">");
0312: pOutputWriter
0313: .println(" * <tr><td>Empty Value:</td><td> </td><td>'"
0314: + lEmptyString + "'</td></tr>");
0315: pOutputWriter
0316: .println(" * <tr><td>Concealed Value:</td><td> </td><td>'"
0317: + lConcealedString + "'</td></tr>");
0318: pOutputWriter
0319: .println(" * <tr><td>Is Comparable:</td><td> </td><td>"
0320: + (lComparable ? "yes" : "no") + "</td></tr>");
0321: pOutputWriter.println(" * </table></p>");
0322:
0323: if (lJargons.length > 0) {
0324: pOutputWriter
0325: .println(" * <p><b>Additional jargons are:</b>");
0326: pOutputWriter.println(" * <table border=\"1\">");
0327: pOutputWriter
0328: .println(" * <tr><th>Jargon Name</th><th>Jargon Description</th></tr>");
0329: for (int i = 0; i < lJargons.length; i++)
0330: pOutputWriter.println("<tr><td>" + lJargons[i].Name
0331: + "</td><td>" + lJargons[i].Description
0332: + "</td></tr>");
0333: pOutputWriter.println(" * </table></p>");
0334: }
0335: pOutputWriter.println(" * <p><b>Valid values"
0336: + ((lComparable) ? "(<i> in ascending order</i>)" : "")
0337: + " are:</b>");
0338: pOutputWriter.println(" * <table border=\"1\">");
0339: pOutputWriter.print(" * <tr><th>Value</th>");
0340: // Optional jargons
0341: for (int i = 0; i < lJargons.length; i++)
0342: pOutputWriter.print("<th>" + lJargons[i].Name
0343: + " Jargon Value</th>");
0344: pOutputWriter
0345: .println("<th>Member Variable</th><th>Value Description</th></tr>");
0346: // Write out table rows
0347: for (int i = 0; i < lValues.length; i++) {
0348: ValueMetadata lValueMetadata = lValues[i];
0349: pOutputWriter.print(" * <tr><td>" + lValueMetadata.Name
0350: + "</td>");
0351: // Optional jargons
0352: for (int j = 0; j < lJargons.length; j++)
0353: pOutputWriter.print("<td>"
0354: + lJargons[j].JargonValueNames[i] + "</td>");
0355: pOutputWriter.println("<td><a href=\"#"
0356: + lValueMetadata.VariableName + "\">"
0357: + lValueMetadata.VariableName + "</a></td><td>"
0358: + lValueMetadata.Description + "</td></tr>");
0359: }
0360: pOutputWriter.println(" * </table></p>");
0361: if (lCategories.length > 0) {
0362: pOutputWriter
0363: .println(" * <p><b>Value categories are:</b>");
0364: pOutputWriter
0365: .println(" * <p><table border=\"1\"><caption><b>Value categories</b></caption>");
0366: pOutputWriter
0367: .println(" * <tr><th>Category Name</th><th>Testing Method</th><th>Category Description</th><th>Category Values</th></tr>");
0368: for (int i = 0; i < lCategories.length; i++) {
0369: CategoryMetadata lCategoryMetadata = lCategories[i];
0370: // Build up value names belonging to category
0371: String lCategoryValues = null;
0372: if (lCategoryMetadata.CategoryValues.length > 0) {
0373: StringBuffer lBuffer = new StringBuffer();
0374: for (int j = 0; j < lCategoryMetadata.CategoryValues.length; j++) {
0375: if (j > 0)
0376: lBuffer.append(" , ");
0377: lBuffer
0378: .append(lCategoryMetadata.CategoryValues[j].Name);
0379: }
0380: lCategoryValues = lBuffer.toString();
0381: } else
0382: lCategoryValues = "<font color=\"red\">missing in the model</font>";
0383: pOutputWriter.println(" * <tr><td>"
0384: + lCategoryMetadata.Name
0385: + "</td><td><a href=\"#is"
0386: + lCategoryMetadata.PartOfIdentifierName
0387: + "()\">is"
0388: + lCategoryMetadata.PartOfIdentifierName
0389: + "()</a></td><td>"
0390: + lCategoryMetadata.Description + "</td><td>"
0391: + lCategoryValues + "</td></tr>");
0392: }
0393: }
0394: pOutputWriter.println(" * </table></p>");
0395: pOutputWriter.println(" */");
0396: pOutputWriter.println("public final class " + lDatatypeName
0397: + " implements DataType, Serializable"
0398: + (lComparable ? ",Comparable" : ""));
0399: pOutputWriter.println("{");
0400: pOutputWriter
0401: .println(" // Static storage for translation metadata. Create once and return many times");
0402: pOutputWriter
0403: .println(" private static DataTypeTranslationMetadata sTranslationMetadata = null;");
0404: pOutputWriter
0405: .println(" // Static storage for the empty string constant.");
0406: pOutputWriter
0407: .println(" private static final String sEmptyString = \""
0408: + lEmptyString + "\";");
0409: pOutputWriter
0410: .println(" // Static storage for the concealed string constant.");
0411: pOutputWriter
0412: .println(" private static final String sConcealedString = \""
0413: + lConcealedString + "\";");
0414: pOutputWriter
0415: .println(" // Static storage for all valid string constants except empty and concealed");
0416: if (lValues.length > 0) {
0417: pOutputWriter
0418: .print(" private static final String[] sAllStrings = new String[] {");
0419: // Write out public final values
0420: {
0421: for (int i = 0; i < lValues.length; i++) {
0422: ValueMetadata lValueMetadata = lValues[i];
0423: pOutputWriter.print("\"" + lValueMetadata.Name
0424: + "\"");
0425: if (i < (lValues.length - 1))
0426: pOutputWriter.print(",");
0427: else
0428: pOutputWriter.println("};");
0429: }
0430: }
0431: } else {
0432: pOutputWriter
0433: .print(" private static final String[] sAllStrings = new String[0];");
0434: }
0435: pOutputWriter
0436: .println(" // Static storage for all description string constants except empty and concealed");
0437: pOutputWriter
0438: .println(" // This storage is for default locale only, Descriptions will be loaded dynamically for all other locales");
0439: // Write out public final values
0440: if (lValues.length > 0) {
0441: pOutputWriter
0442: .print(" private static final String[] sAllDescriptions = new String[] {");
0443: for (int i = 0; i < lValues.length; i++) {
0444: ValueMetadata lValueMetadata = lValues[i];
0445: pOutputWriter
0446: .print("ResourceUtils.getClassResourceString("
0447: + lDatatypeName + ".class,\"Value["
0448: + Integer.toString(i + 1)
0449: + "].Description\",\"\")");
0450: if (i < (lValues.length - 1))
0451: pOutputWriter.print(",");
0452: else
0453: pOutputWriter.println("};");
0454: }
0455: } else {
0456: pOutputWriter
0457: .print(" private static final String[] sAllDescriptions = new String[0];");
0458: }
0459:
0460: // Generate string constants for alternative jargons
0461: for (int i = 0; i < lJargons.length; i++) {
0462: JargonMetadata lJargonMetadata = lJargons[i];
0463: pOutputWriter
0464: .println(" // Static storage for the empty string constant in "
0465: + lJargonMetadata.Name + " jargon.");
0466: pOutputWriter
0467: .println(" private static final String sEmpty"
0468: + lJargonMetadata.PartOfIdentifierName
0469: + "String = \""
0470: + lJargonMetadata.EmptyString + "\";");
0471: pOutputWriter
0472: .println(" // Static storage for the concealed string constant in "
0473: + lJargonMetadata.Name + " jargon.");
0474: pOutputWriter
0475: .println(" private static final String sConcealed"
0476: + lJargonMetadata.PartOfIdentifierName
0477: + "String = \""
0478: + lJargonMetadata.ConcealedString + "\";");
0479: pOutputWriter
0480: .println(" // Static storage for all valid string constants in "
0481: + lJargonMetadata.Name
0482: + " jargon except empty and concealed");
0483: if (lValues.length > 0) {
0484: // Write out public final values
0485: pOutputWriter
0486: .print(" private static final String[] sAll"
0487: + lJargonMetadata.PartOfIdentifierName
0488: + "Strings = new String[] {");
0489: for (int j = 0; j < lValues.length; j++) {
0490: pOutputWriter.print("\""
0491: + lJargonMetadata.JargonValueNames[j]
0492: + "\"");
0493: if (j < (lValues.length - 1))
0494: pOutputWriter.print(",");
0495: else
0496: pOutputWriter.println("};");
0497: }
0498: } else {
0499: pOutputWriter
0500: .print(" private static final String[] sAll"
0501: + lJargonMetadata.PartOfIdentifierName
0502: + "Strings = new String[0];");
0503: }
0504: }
0505: pOutputWriter
0506: .println(" // Special constant representing empty value");
0507: pOutputWriter.println(" private static final "
0508: + lDatatypeName + " sEmptyInstance = new "
0509: + lDatatypeName + "((byte)1, sEmptyString"
0510: + ((lComparable) ? (", " + Integer.toString(-2)) : "")
0511: + ");");
0512: pOutputWriter
0513: .println(" // Special value to carry concealed value around");
0514: pOutputWriter.println(" private static final "
0515: + lDatatypeName + " sConcealedInstance = new "
0516: + lDatatypeName + "((byte)2, sConcealedString"
0517: + ((lComparable) ? (", " + Integer.toString(-1)) : "")
0518: + ");");
0519: pOutputWriter
0520: .println(" // State is stored here : 0 - normal, 1 - empty, 2 - concealed");
0521: pOutputWriter.println(" private byte mState = 0;");
0522: pOutputWriter.println(" // Actual value stored here");
0523: pOutputWriter.println(" private String mValue;");
0524: if (lComparable)
0525: pOutputWriter.println(" private int mComparableWeight;");
0526: // Write out public final values
0527: for (int i = 0; i < lValues.length; i++) {
0528: ValueMetadata lValueMetadata = lValues[i];
0529: pOutputWriter
0530: .println(" /** Constant, which represents '"
0531: + lValueMetadata.Name
0532: + "' value (one of the possible values of "
0533: + lDatatypeName + ").");
0534: pOutputWriter.println(" * Value description is : <i> "
0535: + lValueMetadata.Description + " </i> */");
0536: pOutputWriter.println(" public static final "
0537: + lDatatypeName
0538: + " "
0539: + lValueMetadata.VariableName
0540: + " = new "
0541: + lDatatypeName
0542: + "((byte)0, \""
0543: + lValueMetadata.Name
0544: + "\""
0545: + ((lComparable) ? (", " + Integer.toString(i))
0546: : "") + ");");
0547: }
0548: // Write out array of these values
0549: pOutputWriter
0550: .println(" // Static storage for all values except empty and concealed");
0551: if (lValues.length > 0) {
0552: pOutputWriter.print(" private static final "
0553: + lDatatypeName + "[] sAllValues = new "
0554: + lDatatypeName + "[] {");
0555: for (int i = 0; i < lValues.length; i++) {
0556: ValueMetadata lValueMetadata = lValues[i];
0557: pOutputWriter.print(lValueMetadata.VariableName);
0558: if (i < (lValues.length - 1))
0559: pOutputWriter.print(",");
0560: else
0561: pOutputWriter.println("};");
0562: }
0563: } else {
0564: pOutputWriter.print(" private static final "
0565: + lDatatypeName + "[] sAllValues = new "
0566: + lDatatypeName + "[0];");
0567: }
0568: pOutputWriter.println(" static");
0569: pOutputWriter.println(" {");
0570: pOutputWriter
0571: .println(" SqlVarcharTranslator lSqlTranslator = new SqlVarcharTranslator()");
0572: pOutputWriter.println(" {");
0573: pOutputWriter
0574: .println(" public String getSqlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
0575: pOutputWriter.println(" {");
0576: pOutputWriter.println(" " + lDatatypeName
0577: + " lInstance = (" + lDatatypeName
0578: + ")pDataTypeInstance;");
0579: pOutputWriter
0580: .println(" if (lInstance.isConcealed())");
0581: pOutputWriter
0582: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
0583: pOutputWriter
0584: .println(" if (lInstance.isEmpty())");
0585: pOutputWriter
0586: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
0587: pOutputWriter
0588: .println(" return lInstance.mValue;");
0589: pOutputWriter.println(" }");
0590: pOutputWriter
0591: .println(" public DataType createFromSqlValue(String pSqlValue) throws DataTypeInstanceCreationFailureException");
0592: pOutputWriter.println(" {");
0593: pOutputWriter.println(" if (pSqlValue == null)");
0594: pOutputWriter
0595: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Sql value is null.\");");
0596: {
0597: for (int i = 0; i < lValues.length; i++) {
0598: ValueMetadata lValueMetadata = lValues[i];
0599: pOutputWriter
0600: .println(" if (pSqlValue.equals("
0601: + lValueMetadata.VariableName
0602: + ".mValue))");
0603: pOutputWriter.println(" return "
0604: + lValueMetadata.VariableName + ";");
0605: }
0606: }
0607: pOutputWriter
0608: .println(" throw new DataTypeInstanceCreationFailureException(\"'\" + pSqlValue + \"' is not a legal value for "
0609: + lDatatypeName + ".\");");
0610: pOutputWriter.println(" }");
0611: pOutputWriter.println(" };");
0612: pOutputWriter
0613: .println(" PrimitiveStringTranslator lPrimitiveTranslator = new PrimitiveStringTranslator()");
0614: pOutputWriter.println(" {");
0615: pOutputWriter
0616: .println(" public String getPrimitiveValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
0617: pOutputWriter.println(" {");
0618: pOutputWriter.println(" " + lDatatypeName
0619: + " lInstance = (" + lDatatypeName
0620: + ")pDataTypeInstance;");
0621: pOutputWriter
0622: .println(" if (lInstance.isConcealed())");
0623: pOutputWriter
0624: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
0625: pOutputWriter
0626: .println(" if (lInstance.isEmpty())");
0627: pOutputWriter
0628: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
0629: pOutputWriter
0630: .println(" return lInstance.mValue;");
0631: pOutputWriter.println(" }");
0632: pOutputWriter
0633: .println(" public DataType createFromPrimitiveValue(String pPrimitiveValue) throws DataTypeInstanceCreationFailureException");
0634: pOutputWriter.println(" {");
0635: pOutputWriter
0636: .println(" if (pPrimitiveValue == null)");
0637: pOutputWriter
0638: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Primitive value is null.\");");
0639: {
0640: for (int i = 0; i < lValues.length; i++) {
0641: ValueMetadata lValueMetadata = lValues[i];
0642: pOutputWriter
0643: .println(" if (pPrimitiveValue.equals("
0644: + lValueMetadata.VariableName
0645: + ".mValue))");
0646: pOutputWriter.println(" return "
0647: + lValueMetadata.VariableName + ";");
0648: }
0649: }
0650: pOutputWriter
0651: .println(" throw new DataTypeInstanceCreationFailureException(\"'\" + pPrimitiveValue + \"' is not a legal value for "
0652: + lDatatypeName + ".\");");
0653: pOutputWriter.println(" }");
0654: pOutputWriter.println(" };");
0655: pOutputWriter
0656: .println(" XmlTextTranslator lXmlTranslator = new XmlTextTranslator()");
0657: pOutputWriter.println(" {");
0658: pOutputWriter
0659: .println(" public String getXmlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
0660: pOutputWriter.println(" {");
0661: pOutputWriter.println(" " + lDatatypeName
0662: + " lInstance = (" + lDatatypeName
0663: + ")pDataTypeInstance;");
0664: pOutputWriter
0665: .println(" if (lInstance.isConcealed())");
0666: pOutputWriter
0667: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
0668: pOutputWriter
0669: .println(" if (lInstance.isEmpty())");
0670: pOutputWriter
0671: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
0672: pOutputWriter
0673: .println(" return lInstance.mValue;");
0674: pOutputWriter.println(" }");
0675: pOutputWriter
0676: .println(" public DataType createFromXmlValue(String pXmlValue) throws DataTypeInstanceCreationFailureException");
0677: pOutputWriter.println(" {");
0678: pOutputWriter.println(" if (pXmlValue == null)");
0679: pOutputWriter
0680: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Xml value is null.\");");
0681: {
0682: for (int i = 0; i < lValues.length; i++) {
0683: ValueMetadata lValueMetadata = lValues[i];
0684: pOutputWriter
0685: .println(" if (pXmlValue.equals("
0686: + lValueMetadata.VariableName
0687: + ".mValue))");
0688: pOutputWriter.println(" return "
0689: + lValueMetadata.VariableName + ";");
0690: }
0691: }
0692: pOutputWriter
0693: .println(" throw new DataTypeInstanceCreationFailureException(\"'\" + pXmlValue + \"' is not a legal value for "
0694: + lDatatypeName + ".\");");
0695: pOutputWriter.println(" }");
0696: pOutputWriter.println(" };");
0697: pOutputWriter
0698: .println(" java.util.Properties lProperties = new java.util.Properties();");
0699: pOutputWriter
0700: .println(" lProperties.setProperty(\"maxsize\",\""
0701: + lMaxValueLength + "\");");
0702: pOutputWriter
0703: .println(" sTranslationMetadata = new DefaultDataTypeTranslationMetadataImplementation(DataTypeTranslationMetadata.SQL_VARCHAR, lProperties, lSqlTranslator, DataTypeTranslationMetadata.PRIMITIVE_STRING, null, lPrimitiveTranslator, DataTypeTranslationMetadata.XML_TEXT, null, lXmlTranslator);");
0704: pOutputWriter.println(" }");
0705:
0706: pOutputWriter
0707: .println(" /** Returns this datatype's translation metadata.");
0708: pOutputWriter
0709: .println(" * See {@link com.metaboss.enterprise.datatypes.DataType DataType} for more details */");
0710: pOutputWriter
0711: .println(" public static DataTypeTranslationMetadata getTranslationMetadata()");
0712: pOutputWriter.println(" {");
0713: pOutputWriter.println(" return sTranslationMetadata;");
0714: pOutputWriter.println(" }");
0715:
0716: pOutputWriter
0717: .println(" /** Returns string used to represent empty value in a toString() / createFromString() methods */");
0718: pOutputWriter
0719: .println(" public static String getEmptyString()");
0720: pOutputWriter.println(" {");
0721: pOutputWriter.println(" return sEmptyString;");
0722: pOutputWriter.println(" }");
0723:
0724: pOutputWriter
0725: .println(" /** Returns string used to represent concealed value in a toString() / createFromString() methods */");
0726: pOutputWriter
0727: .println(" public static String getConcealedString()");
0728: pOutputWriter.println(" {");
0729: pOutputWriter.println(" return sConcealedString;");
0730: pOutputWriter.println(" }");
0731:
0732: pOutputWriter
0733: .println(" /** Returns array of all valid value objects, except empty and concealed. */");
0734: pOutputWriter.println(" public static " + lDatatypeName
0735: + "[] getAllValues()");
0736: pOutputWriter.println(" {");
0737: pOutputWriter.println(" return sAllValues;");
0738: pOutputWriter.println(" }");
0739:
0740: pOutputWriter
0741: .println(" /** Returns array of all valid value strings in a toString() / createFromString() methods, except empty and concealed. */");
0742: pOutputWriter
0743: .println(" public static String[] getAllStrings()");
0744: pOutputWriter.println(" {");
0745: pOutputWriter.println(" return sAllStrings;");
0746: pOutputWriter.println(" }");
0747:
0748: pOutputWriter
0749: .println(" /** Returns array of all valid value descriptions, except descriptions for empty and concealed.");
0750: pOutputWriter
0751: .println(" * Descriptions are returned in the same order as values returned from getAllStrings() method.");
0752: pOutputWriter
0753: .println(" * Note that this method uses default locale for description texts. Use getAllLocalisedDescriptions() to");
0754: pOutputWriter
0755: .println(" * obtain description texts for the specific locale */");
0756: pOutputWriter
0757: .println(" public static String[] getAllDescriptions()");
0758: pOutputWriter.println(" {");
0759: pOutputWriter.println(" return sAllDescriptions;");
0760: pOutputWriter.println(" }");
0761:
0762: pOutputWriter
0763: .println(" /** Returns array of all valid value descriptions, except descriptions for empty and concealed.");
0764: pOutputWriter
0765: .println(" * Descriptions are returned in the same order as values returned from getAllStrings() method.");
0766: pOutputWriter
0767: .println(" * Note that this method uses specified locale for description texts. Use getAllDescriptions() to");
0768: pOutputWriter
0769: .println(" * obtain description texts for the default locale.");
0770: pOutputWriter
0771: .println(" * @param pDesiredLocale the locale to use for sourcing localised description strings */");
0772: pOutputWriter
0773: .println(" public static String[] getAllLocalisedDescriptions(Locale pDesiredLocale)");
0774: pOutputWriter.println(" {");
0775: if (lValues.length > 0) {
0776: // Write out public final values
0777: pOutputWriter.print(" return new String[] {");
0778: for (int i = 0; i < lValues.length; i++) {
0779: ValueMetadata lValueMetadata = lValues[i];
0780: pOutputWriter
0781: .print("ResourceUtils.getLocalisedClassResourceString(pDesiredLocale, "
0782: + lDatatypeName
0783: + ".class,\"Value["
0784: + Integer.toString(i + 1)
0785: + "].Description\",\"\")");
0786: if (i < (lValues.length - 1))
0787: pOutputWriter.print(",");
0788: else
0789: pOutputWriter.println("};");
0790: }
0791: } else {
0792: pOutputWriter.print(" return new String[0];");
0793: }
0794: pOutputWriter.println(" }");
0795: // Generate string constants for alternative jargons
0796: for (int i = 0; i < lJargons.length; i++) {
0797: JargonMetadata lJargonMetadata = lJargons[i];
0798: pOutputWriter
0799: .println(" /** Returns string used to represent empty value in a toString() / createFromString() methods in "
0800: + lJargonMetadata.Name + " jargon. */");
0801: pOutputWriter
0802: .println(" public static String getEmpty"
0803: + lJargonMetadata.PartOfIdentifierName
0804: + "String()");
0805: pOutputWriter.println(" {");
0806: pOutputWriter.println(" return sEmpty"
0807: + lJargonMetadata.PartOfIdentifierName + "String;");
0808: pOutputWriter.println(" }");
0809:
0810: pOutputWriter
0811: .println(" /** Returns string used to represent concealed value in a toString() / createFromString() methods in "
0812: + lJargonMetadata.Name + " jargon. */");
0813: pOutputWriter
0814: .println(" public static String getConcealed"
0815: + lJargonMetadata.PartOfIdentifierName
0816: + "String()");
0817: pOutputWriter.println(" {");
0818: pOutputWriter.println(" return sConcealed"
0819: + lJargonMetadata.PartOfIdentifierName + "String;");
0820: pOutputWriter.println(" }");
0821:
0822: pOutputWriter
0823: .println(" /** Returns array of all valid value strings in a toString() / createFromString() methods in "
0824: + lJargonMetadata.Name
0825: + " jargon, except empty and concealed. */");
0826: pOutputWriter.println(" public static String[] getAll"
0827: + lJargonMetadata.PartOfIdentifierName
0828: + "Strings()");
0829: pOutputWriter.println(" {");
0830: pOutputWriter
0831: .println(" return sAll"
0832: + lJargonMetadata.PartOfIdentifierName
0833: + "Strings;");
0834: pOutputWriter.println(" }");
0835: }
0836:
0837: pOutputWriter
0838: .println(" /** Creates an instance of the datatype carrying an empty value */");
0839: pOutputWriter.println(" public static " + lDatatypeName
0840: + " createEmpty()");
0841: pOutputWriter.println(" {");
0842: pOutputWriter.println(" return sEmptyInstance;");
0843: pOutputWriter.println(" }");
0844:
0845: pOutputWriter
0846: .println(" /** Creates an instance of the datatype carrying concealed value */");
0847: pOutputWriter.println(" public static " + lDatatypeName
0848: + " createConcealed()");
0849: pOutputWriter.println(" {");
0850: pOutputWriter.println(" return sConcealedInstance;");
0851: pOutputWriter.println(" }");
0852:
0853: pOutputWriter
0854: .println(" /** Creates an instance of the datatype from given readable value */");
0855: pOutputWriter
0856: .println(" public static "
0857: + lDatatypeName
0858: + " createFromString( String pSourceString) throws DataTypeValidationException");
0859: pOutputWriter.println(" {");
0860: pOutputWriter
0861: .println(" // No need to specially validate - we will either return appropriate constant or throw exception at the end");
0862: pOutputWriter.println(" if (pSourceString == null)");
0863: pOutputWriter
0864: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
0865: + lDatatypeName
0866: + ". Use createEmpty() method to create an empty "
0867: + lDatatypeName + "\");");
0868: pOutputWriter
0869: .println(" if (pSourceString.equals(sEmptyString))");
0870: pOutputWriter.println(" return createEmpty();");
0871: pOutputWriter
0872: .println(" if (pSourceString.equals(sConcealedString))");
0873: pOutputWriter.println(" return createConcealed();");
0874: // Write out individual creations
0875: for (int i = 0; i < lValues.length; i++) {
0876: ValueMetadata lValueMetadata = lValues[i];
0877: pOutputWriter.println(" if (pSourceString.equals( "
0878: + lValueMetadata.VariableName + ".mValue))");
0879: pOutputWriter.println(" return "
0880: + lValueMetadata.VariableName + ";");
0881: }
0882: pOutputWriter
0883: .println(" throw new DataTypeValidationException(\"'\" + pSourceString + \"' is not a legal value for "
0884: + lDatatypeName + ".\");");
0885: pOutputWriter.println(" }");
0886:
0887: pOutputWriter
0888: .println(" /** Validates proposed readable string */");
0889: pOutputWriter
0890: .println(" public static void validateString( String pProposedString) throws DataTypeValidationException");
0891: pOutputWriter.println(" {");
0892: pOutputWriter.println(" if (pProposedString == null)");
0893: pOutputWriter
0894: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
0895: + lDatatypeName
0896: + ". Use createEmpty() method to create an empty "
0897: + lDatatypeName + "\");");
0898: pOutputWriter
0899: .println(" if (pProposedString.equals(sEmptyString))");
0900: pOutputWriter.println(" return; // Done validation");
0901: pOutputWriter
0902: .println(" if (pProposedString.equals(sConcealedString))");
0903: pOutputWriter
0904: .println(" return; // Done validation");
0905: // Write out individual creations
0906: for (int i = 0; i < lValues.length; i++) {
0907: ValueMetadata lValueMetadata = lValues[i];
0908: pOutputWriter
0909: .println(" if (pProposedString.equals( "
0910: + lValueMetadata.VariableName + ".mValue))");
0911: pOutputWriter
0912: .println(" return; // Done validation");
0913: }
0914: pOutputWriter
0915: .println(" throw new DataTypeValidationException(\"'\" + pProposedString + \"' is not a legal value for "
0916: + lDatatypeName + ".\");");
0917: pOutputWriter.println(" }");
0918:
0919: pOutputWriter
0920: .println(" /** Returns the value inside this instance of "
0921: + lDatatypeName + " as readable string.");
0922: pOutputWriter
0923: .println(" * Produced string is always the same as in getAsString() method. */");
0924: pOutputWriter.println(" public String toString()");
0925: pOutputWriter.println(" {");
0926: pOutputWriter.println(" return getAsString();");
0927: pOutputWriter.println(" }");
0928:
0929: pOutputWriter
0930: .println(" /** Returns the value inside this instance of "
0931: + lDatatypeName + " as readable string */");
0932: pOutputWriter.println(" public String getAsString()");
0933: pOutputWriter.println(" {");
0934: pOutputWriter.println(" if (isEmpty())");
0935: pOutputWriter.println(" return sEmptyString;");
0936: pOutputWriter.println(" if (isConcealed())");
0937: pOutputWriter.println(" return sConcealedString;");
0938: pOutputWriter.println(" return mValue;");
0939: pOutputWriter.println(" }");
0940:
0941: pOutputWriter
0942: .println(" /** Indicates whether some other object is \"equal to\" this one");
0943: pOutputWriter
0944: .println(" * @return true if this object is the same as the obj argument; false otherwise. */");
0945: pOutputWriter
0946: .println(" public boolean equals(Object pOtherObject)");
0947: pOutputWriter.println(" {");
0948: pOutputWriter.println(" if (pOtherObject == null)");
0949: pOutputWriter.println(" return false;");
0950: pOutputWriter.println(" if ((pOtherObject instanceof "
0951: + lDatatypeName + ") == false)");
0952: pOutputWriter.println(" return false;");
0953: pOutputWriter.println(" if (((" + lDatatypeName
0954: + ")pOtherObject).isConcealed() || isConcealed())");
0955: pOutputWriter
0956: .println(" return false; // One of the instances is concealed");
0957: pOutputWriter.println(" if (((" + lDatatypeName
0958: + ")pOtherObject).isEmpty() && isEmpty())");
0959: pOutputWriter
0960: .println(" return true; // Both instances are empty.");
0961: pOutputWriter.println(" if (((" + lDatatypeName
0962: + ")pOtherObject).isEmpty() != isEmpty())");
0963: pOutputWriter
0964: .println(" return false; // One of the instances is empty, the other one is not");
0965: pOutputWriter.println(" return ((" + lDatatypeName
0966: + ")pOtherObject).mValue.equals(mValue);");
0967: pOutputWriter.println(" }");
0968: if (lComparable) {
0969: pOutputWriter
0970: .println(" /** Compares this object with the specified object for order.");
0971: pOutputWriter
0972: .println(" * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object");
0973: pOutputWriter
0974: .println(" * @exception java.lang.ClassCastException - if the specified object's type prevents it from being compared to this Object. */");
0975: pOutputWriter
0976: .println(" public int compareTo(Object pOtherObject)");
0977: pOutputWriter.println(" {");
0978: pOutputWriter.println(" if (pOtherObject == null)");
0979: pOutputWriter
0980: .println(" return 1; // Anything is greater than null");
0981: pOutputWriter
0982: .println(" int lOtherObjectComparableWeight = (("
0983: + lDatatypeName
0984: + ")pOtherObject).mComparableWeight;");
0985: pOutputWriter
0986: .println(" if (mComparableWeight < lOtherObjectComparableWeight)");
0987: pOutputWriter.println(" return -1;");
0988: pOutputWriter
0989: .println(" if (mComparableWeight > lOtherObjectComparableWeight)");
0990: pOutputWriter.println(" return 1;");
0991: pOutputWriter.println(" return 0;");
0992: pOutputWriter.println(" }");
0993: }
0994:
0995: pOutputWriter
0996: .println(" /** Returns a hash code value for the object. This method is supported for the benefit of hashtables");
0997: pOutputWriter
0998: .println(" * @return instance's hash code or zero if this instance is empty or concealed */");
0999: pOutputWriter.println(" public int hashCode()");
1000: pOutputWriter.println(" {");
1001: pOutputWriter
1002: .println(" if (isEmpty() || isConcealed())");
1003: pOutputWriter.println(" return 0;");
1004: pOutputWriter.println(" return mValue.hashCode();");
1005: pOutputWriter.println(" }");
1006:
1007: pOutputWriter
1008: .println(" /** Returns true if the instance contains an empty value */");
1009: pOutputWriter.println(" public boolean isEmpty()");
1010: pOutputWriter.println(" {");
1011: pOutputWriter.println(" return mState == 1;");
1012: pOutputWriter.println(" }");
1013:
1014: pOutputWriter
1015: .println(" /** Returns true if the instance contains concealed value */");
1016: pOutputWriter.println(" public boolean isConcealed()");
1017: pOutputWriter.println(" {");
1018: pOutputWriter.println(" return mState == 2;");
1019: pOutputWriter.println(" }");
1020:
1021: // Generate methods for custom categories
1022: for (int i = 0; i < lCategories.length; i++) {
1023: CategoryMetadata lCategoryMetadata = lCategories[i];
1024: pOutputWriter
1025: .println(" /** Returns true if the instance belongs to the '"
1026: + lCategoryMetadata.Name + "' category.");
1027: pOutputWriter
1028: .println(" * Category description is : <i> "
1029: + lCategoryMetadata.Description
1030: + " </i> */");
1031: pOutputWriter.println(" public boolean is"
1032: + lCategoryMetadata.PartOfIdentifierName + "()");
1033: pOutputWriter.println(" {");
1034: if (lCategoryMetadata.CategoryValues.length > 0) {
1035: for (int j = 0; j < lCategoryMetadata.CategoryValues.length; j++) {
1036: if (j == 0)
1037: pOutputWriter.print(" return ");
1038: else {
1039: pOutputWriter.println(" ||");
1040: pOutputWriter.print(" ");
1041: }
1042: pOutputWriter
1043: .print("(this == "
1044: + lCategoryMetadata.CategoryValues[j].VariableName
1045: + ")");
1046: }
1047: pOutputWriter.println(";");
1048: } else {
1049: pOutputWriter.println(" return false;");
1050: }
1051: pOutputWriter.println(" }");
1052: }
1053:
1054: // Generate methods for alternative jargons
1055: for (int i = 0; i < lJargons.length; i++) {
1056: JargonMetadata lJargonMetadata = lJargons[i];
1057:
1058: // Generate creator method.
1059: pOutputWriter
1060: .println(" /** Creates an instance of the datatype from given readable string in "
1061: + lJargonMetadata.Name + " jargon */");
1062: pOutputWriter
1063: .println(" public static "
1064: + lDatatypeName
1065: + " createFrom"
1066: + lJargonMetadata.PartOfIdentifierName
1067: + "String( String pSourceString) throws DataTypeValidationException");
1068: pOutputWriter.println(" {");
1069: pOutputWriter
1070: .println(" // No need to specially validate - we will either return appropriate constant or throw exception at the end");
1071: pOutputWriter.println(" if (pSourceString == null)");
1072: pOutputWriter
1073: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
1074: + lDatatypeName
1075: + ". Use createEmpty() method to create an empty "
1076: + lDatatypeName + "\");");
1077: pOutputWriter
1078: .println(" if (pSourceString.equals(sEmpty"
1079: + lJargonMetadata.PartOfIdentifierName
1080: + "String))");
1081: pOutputWriter.println(" return createEmpty();");
1082: pOutputWriter
1083: .println(" if (pSourceString.equals(sConcealed"
1084: + lJargonMetadata.PartOfIdentifierName
1085: + "String))");
1086: pOutputWriter
1087: .println(" return createConcealed();");
1088: // Write out individual creations
1089: for (int j = 0; j < lValues.length; j++) {
1090: pOutputWriter
1091: .println(" if (pSourceString.equals(\""
1092: + lJargonMetadata.JargonValueNames[j]
1093: + "\"))");
1094: pOutputWriter.println(" return "
1095: + lValues[j].VariableName + ";");
1096: }
1097: pOutputWriter
1098: .println(" throw new DataTypeValidationException(\"'\" + pSourceString + \"' is not a legal value for "
1099: + lDatatypeName
1100: + " in "
1101: + lJargonMetadata.Name + " jargon.\");");
1102: pOutputWriter.println(" }");
1103:
1104: // Generate validator method.
1105: pOutputWriter
1106: .println(" /** Validates proposed readable string in "
1107: + lJargonMetadata.Name + " jargon */");
1108: pOutputWriter
1109: .println(" public static void validate"
1110: + lJargonMetadata.PartOfIdentifierName
1111: + "String( String pProposedString) throws DataTypeValidationException");
1112: pOutputWriter.println(" {");
1113: pOutputWriter
1114: .println(" if (pProposedString == null)");
1115: pOutputWriter
1116: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
1117: + lDatatypeName
1118: + ". Use createEmpty() method to create an empty "
1119: + lDatatypeName + "\");");
1120: pOutputWriter
1121: .println(" if (pProposedString.equals(sEmpty"
1122: + lJargonMetadata.PartOfIdentifierName
1123: + "String))");
1124: pOutputWriter
1125: .println(" return; // Done validation");
1126: pOutputWriter
1127: .println(" if (pProposedString.equals(sConcealed"
1128: + lJargonMetadata.PartOfIdentifierName
1129: + "String))");
1130: pOutputWriter
1131: .println(" return; // Done validation");
1132: // Write out individual creations
1133: for (int j = 0; j < lValues.length; j++) {
1134: pOutputWriter
1135: .println(" if (pProposedString.equals(\""
1136: + lJargonMetadata.JargonValueNames[j]
1137: + "\"))");
1138: pOutputWriter
1139: .println(" return; // Done validation");
1140: }
1141: pOutputWriter
1142: .println(" throw new DataTypeValidationException(\"'\" + pProposedString + \"' is not a legal value for "
1143: + lDatatypeName
1144: + " in "
1145: + lJargonMetadata.Name + " jargon.\");");
1146: pOutputWriter.println(" }");
1147:
1148: // Generate getter method. using to<JargonName>String() to higlight that it is an alternative to toString() nothing elese
1149: pOutputWriter
1150: .println(" /** Returns the value inside this instance of "
1151: + lDatatypeName
1152: + " as readable string in "
1153: + lJargonMetadata.Name + " jargon */");
1154: pOutputWriter
1155: .println(" public String getAs"
1156: + lJargonMetadata.PartOfIdentifierName
1157: + "String()");
1158: pOutputWriter.println(" {");
1159: pOutputWriter.println(" if (isEmpty())");
1160: pOutputWriter.println(" return sEmpty"
1161: + lJargonMetadata.PartOfIdentifierName + "String;");
1162: pOutputWriter.println(" if (isConcealed())");
1163: pOutputWriter.println(" return sConcealed"
1164: + lJargonMetadata.PartOfIdentifierName + "String;");
1165: // Write out other values
1166: for (int j = 0; j < lValues.length; j++) {
1167: pOutputWriter.println(" if (mValue.equals("
1168: + lValues[j].VariableName + ".mValue))");
1169: pOutputWriter.println(" return \""
1170: + lJargonMetadata.JargonValueNames[j] + "\";");
1171: }
1172: pOutputWriter
1173: .println(" throw new com.metaboss.enterprise.datatypes.DataTypeRuntimeException(\"Unexpected program condition. Detected illegal internal state of the enumerable type instance.\");");
1174: pOutputWriter.println(" }");
1175: }
1176:
1177: pOutputWriter
1178: .println(" // Private access. External callers must use one of the static creators");
1179: pOutputWriter.println(" private " + lDatatypeName
1180: + "( byte pState, String pValue"
1181: + ((lComparable) ? ", int pComparableWeight" : "")
1182: + ")");
1183: pOutputWriter.println(" {");
1184: pOutputWriter.println(" mState = pState;");
1185: pOutputWriter.println(" mValue = pValue;");
1186: if (lComparable)
1187: pOutputWriter
1188: .println(" mComparableWeight = pComparableWeight;");
1189: pOutputWriter.println(" }");
1190: pOutputWriter.println("}");
1191: }
1192: }
|