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.impl;
12  
13  import com.legstar.coxb.CobolElement;
14  import com.legstar.coxb.CobolType;
15  import com.legstar.coxb.ICobolComplexBinding;
16  import com.legstar.coxb.ICobolStringBinding;
17  import com.legstar.coxb.CobolElementVisitor;
18  import com.legstar.coxb.host.HostException;
19  import com.legstar.coxb.util.PictureUtil;
20  
21  /**
22   * This class implements the behavior of a string cobol element bound to
23   * a JAXB String property.
24   * Numerous COBOL data types map to a String, namely:
25   * - Alphabetic PIC A(n)
26   * - Alphanumeric PIC X(n)
27   * - Alphanumeric edited PIC X/A
28   * - External floating point
29   * - Numeric edited
30   *
31   * @author Fady Moussallam
32   * 
33   */
34  public class CStringBinding extends AbstractAlphaNumericBinding
35  implements ICobolStringBinding {
36  
37      /**
38       * Constructor for a cobol element to java binding.
39       * 
40       * @param bindingName the identifier for this binding
41       * @param jaxbName the name of the bound java property
42       * @param jaxbType the type of the bound java property
43       * @param cobolAnnotations the cobol annotations for this element
44       * @param parentBinding a reference to the parent binding
45       */
46      public CStringBinding(
47              final String bindingName,
48              final String jaxbName,
49              final Class < ? > jaxbType,
50              final CobolElement cobolAnnotations,
51              final ICobolComplexBinding parentBinding) {
52          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
53      }
54  
55      /** {@inheritDoc} */
56      public void accept(final CobolElementVisitor cev)
57      throws HostException {
58          cev.visit(this);
59      }
60  
61      /** {@inheritDoc} */
62      public int calcByteLength() {
63          return calcStringByteLength(getPicture(), getCobolType());
64      }
65  
66      /**
67       * Calculates the host byte length for a variety of COBOL data types.
68       * @param picture the picture clause
69       * @param cobolType the original COBOL data type
70       * @return the host byte length
71       */
72      public static int calcStringByteLength(
73              final String picture, final CobolType cobolType) {
74          switch (cobolType) {
75          case ALPHABETIC_ITEM:
76              return PictureUtil.getSymbolsNumber(
77                      new char[] {'A'}, picture);
78          case ALPHANUMERIC_ITEM:
79              return PictureUtil.getSymbolsNumber(
80                      new char[] {'A', 'X', '9'}, picture);
81          case ALPHANUMERIC_EDITED_ITEM:
82              return PictureUtil.getSymbolsNumber(
83                      new char[] {'A', 'X', '9', 'B', '0', '/'}, picture);
84          case EXTERNAL_FLOATING_ITEM:
85              return PictureUtil.getSymbolsNumber(
86                      new char[] {'+', '-', '9', '.', 'E'}, picture);
87          case NUMERIC_EDITED_ITEM:
88              int count = 0;
89              String strippedPicture = picture;
90              /* In addition, there might be a CR or DB
91               * There might be a CR of DB in the picture. Process them before to
92               * avoid processing the 'B' character twice.
93               */
94              int idx = picture.indexOf("CR");
95              if (idx > -1) {
96                  // CR should exist at most once
97                  count += 2;
98                  strippedPicture = strippedPicture.replaceFirst("CR", "");
99              }
100             idx = picture.indexOf("DB");
101             if (idx > -1) {
102                 // DB should exist at most once
103                 count += 2;
104                 strippedPicture = strippedPicture.replaceFirst("DB", "");
105             }
106 
107             /* TODO the currency sign should not be hardcoded */
108             count += PictureUtil.getSymbolsNumber(new char[] { 'B', 'Z', '9',
109                     '0', ',', '.', '-', '+', '/', '*', '$' }, strippedPicture);
110 
111             return count;
112         default:
113             return PictureUtil.getSymbolsNumber(new char[] { 'X' }, picture);
114         }
115     }
116 
117 }