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.CobolUsage;
15  import com.legstar.coxb.ICobolComplexBinding;
16  import com.legstar.coxb.ICobolOctetStreamBinding;
17  import com.legstar.coxb.CobolElementVisitor;
18  import com.legstar.coxb.common.CBinding;
19  import com.legstar.coxb.host.HostException;
20  import com.legstar.coxb.util.PictureUtil;
21  
22  /**
23   * This class implements the behavior of a binary cobol element bound to
24   * a JAXB byte array property.
25   * COBOL elements could be PIC X which were explicitly changed to OCTET_STREAM_ITEM
26   * or COBOL special types such as INDEX or POINTERS that we would not be able to
27   * map to anything meaningful in Java.
28   *
29   * @author Fady Moussallam
30   * 
31   */
32  public class COctetStreamBinding extends CBinding
33  implements ICobolOctetStreamBinding {
34  
35      /** The current value for this element. */
36      private byte[] mValue =  null;
37  
38      /**
39       * Constructor for a cobol element to java binding.
40       * 
41       * @param bindingName the identifier for this binding
42       * @param jaxbName the name of the bound java property
43       * @param jaxbType the type of the bound java property
44       * @param cobolAnnotations the cobol annotations for this element
45       * @param parentBinding a reference to the parent binding
46       */
47      public COctetStreamBinding(
48              final String bindingName,
49              final String jaxbName,
50              final Class < ? > jaxbType,
51              final CobolElement cobolAnnotations,
52              final ICobolComplexBinding parentBinding) {
53          super(bindingName, jaxbName, jaxbType, cobolAnnotations, parentBinding);
54      }
55  
56      /** {@inheritDoc} */
57      public void accept(final CobolElementVisitor cev)
58      throws HostException {
59          cev.visit(this);
60      }
61  
62      /** {@inheritDoc} */
63      public byte[] getByteArrayValue() throws HostException {
64          return mValue;
65      }
66  
67      /** {@inheritDoc} */
68      public void setByteArrayValue(final byte[] value)
69      throws HostException {
70          mValue = value;
71      }
72  
73      /** {@inheritDoc} */
74      public int calcByteLength() {
75          return calcOctetStreamByteLength(getPicture(), getUsage());
76      }
77  
78      /**
79       * Calculates the host byte length for a PIC X(n).
80       * @param picture the picture clause
81       * @param usage the usage clause
82       * @return the host byte length for a PIC X(n)
83       */
84      public static int calcOctetStreamByteLength(
85              final String picture, final String usage) {
86          if (usage.equals(CobolUsage.INDEX)) {
87              return 4;
88          } else if (usage.equals(CobolUsage.POINTER)) {
89              return 4;
90          } else if (usage.equals(CobolUsage.PROCEDURE_POINTER)) {
91              return 8;
92          } else if (usage.equals(CobolUsage.FUNCTION_POINTER)) {
93              return 4;
94          }
95          return PictureUtil.getSymbolsNumber('X', picture);
96      }
97  
98      /** {@inheritDoc} */
99      public Object getObjectValue(
100             final Class < ? > type) throws HostException {
101         if (type.equals(byte[].class)) {
102             return mValue;
103         } else {
104             throw new HostException("Attempt to get binding " + getBindingName()
105                     + " as an incompatible type " + type);
106         }
107     }
108 
109     /** {@inheritDoc} */
110     public void setObjectValue(final Object value) throws HostException {
111         if (value == null) {
112             mValue = null;
113             return;
114         }
115         if (value instanceof byte[]) {
116             mValue = (byte[]) value;
117         } else {
118             throw new HostException("Attempt to set binding " + getBindingName()
119                     + " from an incompatible value " + value);
120         }
121     }
122 
123     /** {@inheritDoc} */
124     public boolean isSet() {
125         return (mValue != null);
126     }
127 }