View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2015 LegSem.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the GNU Lesser Public License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7    * 
8    * Contributors:
9    *     LegSem - initial API and implementation
10   ******************************************************************************/
11  package com.legstar.coxb;
12  
13  /**
14   * These are the COBOL usage attribute that are derived from the original COBOL
15   * statement and then propagated as attributes in XML Schema and java
16   * annototations.
17   * 
18   * @author Fady Moussallam
19   * 
20   */
21  public final class CobolUsage {
22  
23      /**
24       * Utility class.
25       */
26      private CobolUsage() {
27  
28      }
29  
30      /** An character string. */
31      public static final String DISPLAY = "DISPLAY";
32      /** An double byte character string. */
33      public static final String DISPLAY_1 = "DISPLAY-1";
34      /** A UTF16-BE character string. */
35      public static final String NATIONAL = "NATIONAL";
36      /** A binary numeric. */
37      public static final String BINARY = "BINARY";
38      /** A native binary numeric. */
39      public static final String COMP_5 = "COMP-5";
40      /** A packed numeric. */
41      public static final String PACKED_DECIMAL = "COMP-3";
42      /** A single float. */
43      public static final String COMP_1 = "COMP-1";
44      /** A double float. */
45      public static final String COMP_2 = "COMP-2";
46      /** An index. */
47      public static final String INDEX = "INDEX";
48      /** A pointer. */
49      public static final String POINTER = "POINTER";
50      /** A pointer to a procedure. */
51      public static final String PROCEDURE_POINTER = "PROCEDURE-POINTER";
52      /** A pointer to a function. */
53      public static final String FUNCTION_POINTER = "FUNCTION-POINTER";
54  
55      /** Usable as java enumeration. */
56      public enum Usage {
57          /** COMP. */
58          BINARY,
59          /** COMP-1. */
60          SINGLEFLOAT,
61          /** COMP-2. */
62          DOUBLEFLOAT,
63          /** COMP-3. */
64          PACKEDDECIMAL,
65          /** COMP-5. */
66          NATIVEBINARY,
67          /** DISPLAY. */
68          DISPLAY,
69          /** DBCS. */
70          DISPLAY1,
71          /** INDEX. */
72          INDEX,
73          /** UTF-16. */
74          NATIONAL,
75          /** Pointer. */
76          POINTER,
77          /** Procedure pointer. */
78          PROCEDUREPOINTER,
79          /** Function pointer. */
80          FUNCTIONPOINTER
81      };
82  
83      /**
84       * @param usage the java enumeration
85       * @return the Cobol usage clause (as a COBOL string)
86       */
87      public static String getCobolUsage(Usage usage) {
88          if (usage == null) {
89              return null;
90          }
91          switch (usage) {
92          case BINARY:
93              return BINARY;
94          case SINGLEFLOAT:
95              return COMP_1;
96          case DOUBLEFLOAT:
97              return COMP_2;
98          case PACKEDDECIMAL:
99              return PACKED_DECIMAL;
100         case NATIVEBINARY:
101             return COMP_5;
102         case DISPLAY:
103             return DISPLAY;
104         case DISPLAY1:
105             return DISPLAY_1;
106         case INDEX:
107             return INDEX;
108         case NATIONAL:
109             return NATIONAL;
110         case POINTER:
111             return POINTER;
112         case PROCEDUREPOINTER:
113             return PROCEDURE_POINTER;
114         case FUNCTIONPOINTER:
115             return FUNCTION_POINTER;
116         default:
117             throw new IllegalArgumentException("Unknown Enum usage: " + usage);
118 
119         }
120     }
121 
122     /**
123      * Get the java enumeration corresponding to a COBOL usage string.
124      * 
125      * @param cobolUsage the COBOL usage string
126      * @return the java enumeration
127      */
128     public static Usage getUsage(String cobolUsage) {
129         if (cobolUsage == null) {
130             return null;
131         }
132         if (cobolUsage.equals(BINARY)) {
133             return Usage.BINARY;
134         } else if (cobolUsage.equals("COMP")) {
135             return Usage.BINARY;
136         } else if (cobolUsage.equals("COMPUTATIONAL")) {
137             return Usage.BINARY;
138         } else if (cobolUsage.equals("COMP-4")) {
139             return Usage.BINARY;
140         } else if (cobolUsage.equals("COMPUTATIONAL-4")) {
141             return Usage.BINARY;
142         } else if (cobolUsage.equals(COMP_1)) {
143             return Usage.SINGLEFLOAT;
144         } else if (cobolUsage.equals(COMP_2)) {
145             return Usage.DOUBLEFLOAT;
146         } else if (cobolUsage.equals(PACKED_DECIMAL)) {
147             return Usage.PACKEDDECIMAL;
148         } else if (cobolUsage.equals("PACKED-DECIMAL")) {
149             return Usage.PACKEDDECIMAL;
150         } else if (cobolUsage.equals("COMPUTATIONAL-3")) {
151             return Usage.PACKEDDECIMAL;
152         } else if (cobolUsage.equals(COMP_5)) {
153             return Usage.NATIVEBINARY;
154         } else if (cobolUsage.equals("COMPUTATIONAL-5")) {
155             return Usage.NATIVEBINARY;
156         } else if (cobolUsage.equals(DISPLAY)) {
157             return Usage.DISPLAY;
158         } else if (cobolUsage.equals(DISPLAY_1)) {
159             return Usage.DISPLAY1;
160         } else if (cobolUsage.equals(INDEX)) {
161             return Usage.INDEX;
162         } else if (cobolUsage.equals(NATIONAL)) {
163             return Usage.NATIONAL;
164         } else if (cobolUsage.equals(POINTER)) {
165             return Usage.POINTER;
166         } else if (cobolUsage.equals(PROCEDURE_POINTER)) {
167             return Usage.PROCEDUREPOINTER;
168         } else if (cobolUsage.equals(FUNCTION_POINTER)) {
169             return Usage.FUNCTIONPOINTER;
170         } else {
171             throw new IllegalArgumentException("Unknown COBOL usage: "
172                     + cobolUsage);
173         }
174 
175     }
176 
177 }