protected List<ValidationException> testField(
      Type type, SegmentType.Field profile, boolean escape) {
    List<ValidationException> exList = new ArrayList<>();

    UsageInfo usage = new UsageInfo(profile);

    // account for MSH 1 & 2 which aren't escaped
    String encoded = null;
    if (!escape && Primitive.class.isAssignableFrom(type.getClass()))
      encoded = ((Primitive) type).getValue();

    exList.addAll(testType(type, profile.getDatatype(), usage, encoded, false));

    // test children
    if (validateChildren) {
      if (profile.getComponents().size() > 0 && !usage.disallowed()) {
        if (Composite.class.isAssignableFrom(type.getClass())) {
          Composite comp = (Composite) type;
          int i = 1;
          boolean nullContext = false;
          for (SegmentType.Field.Component component : profile.getComponents()) {
            try {
              SegmentType.Field.Component component2;
              if (nullContext) {
                component2 = new SegmentType.Field.Component();
                try {
                  BeanUtils.copyProperties(component2, component);
                } catch (InvocationTargetException | IllegalAccessException e) {
                  // nop
                }
                component2.setUsage("NULL");
              } else {
                component2 = component;
                if ((i == 1)
                    && profile.isNullable()
                    && PipeParser.encode(comp.getComponent(0), this.enc).equals("\"\"")) {
                  nullContext = true;
                }
              }

              exList.addAll(testComponent(comp.getComponent(i - 1), component2));
            } catch (DataTypeException de) {
              profileNotHL7Compliant(exList, COMPONENT_TYPE_MISMATCH, type.getName(), i);
            }
            ++i;
          }
          exList.addAll(checkUndefinedComponents(comp, profile.getComponents().size()));
        } else {
          profileNotHL7Compliant(exList, WRONG_FIELD_TYPE, type.getClass().getName());
        }
      }
    }
    return exList;
  }
  /**
   * Tests a Type against the corresponding section of a profile.
   *
   * @param encoded optional encoded form of type (if you want to specify this -- if null, default
   *     pipe-encoded form is used to check length and constant val)
   */
  protected List<ValidationException> testType(
      Type type, String dataType, UsageInfo usage, String encoded, boolean testUsage) {
    ArrayList<ValidationException> exList = new ArrayList<>();
    if (encoded == null) encoded = PipeParser.encode(type, this.enc);

    if (testUsage) {
      testUsage(exList, encoded, usage);
    }

    if (!usage.disallowed() && !encoded.isEmpty()) {
      // check datatype
      if ((type instanceof ca.uhn.hl7v2.model.v231.datatype.TSComponentOne
              || type instanceof ca.uhn.hl7v2.model.v24.datatype.TSComponentOne)
          && !dataType.equals("ST")) {
        profileNotHL7Compliant(exList, HL7_DATATYPE_MISMATCH, type.getName(), dataType);
      } else if (!(type instanceof TSComponentOne) && !type.getName().contains(dataType)) {
        profileViolatedWhen(
            !(type.getClass().getSimpleName().equals("Varies")
                || type.getClass().getSimpleName().equals("QIP")),
            exList,
            HL7_DATATYPE_MISMATCH,
            type.getName(),
            dataType);
      }

      // check length
      profileViolatedWhen(
          encoded.length() > usage.length,
          exList,
          LENGTH_EXCEEDED,
          usage.name,
          encoded.length(),
          usage.length);

      // check constant value
      if (usage.constantValue != null && usage.constantValue.length() > 0) {
        profileViolatedWhen(
            !encoded.equals(usage.constantValue),
            exList,
            WRONG_CONSTANT_VALUE,
            encoded,
            usage.constantValue);
      }

      // TODO : check against table, or do we need this check?
      // Gazelle checks code system and issues a WARNING if a check fails
    }

    return exList;
  }
  protected List<ValidationException> testComponent(
      Type type, SegmentType.Field.Component profile) {
    List<ValidationException> exList = new ArrayList<>();
    UsageInfo usage = new UsageInfo(profile);
    exList.addAll(testType(type, profile.getDatatype(), usage, null));

    // test children
    try {
      if (profile.getSubComponents().size() > 0 && !usage.disallowed() && !isEmpty(type)) {
        if (Composite.class.isAssignableFrom(type.getClass())) {
          Composite comp = (Composite) type;

          if (validateChildren) {
            int i = 1;
            for (SegmentType.Field.Component.SubComponent subComponent :
                profile.getSubComponents()) {
              UsageInfo scUsage = new UsageInfo(subComponent);
              try {
                Type sub = comp.getComponent(i - 1);
                exList.addAll(testType(sub, subComponent.getDatatype(), scUsage, null));
              } catch (DataTypeException de) {
                profileNotHL7Compliant(exList, SUBCOMPONENT_TYPE_MISMATCH, type.getName(), i);
              }
              ++i;
            }
          }

          exList.addAll(checkUndefinedComponents(comp, profile.getSubComponents().size()));
        } else {
          profileViolatedWhen(true, exList, WRONG_COMPONENT_TYPE, type.getClass().getName());
        }
      }
    } catch (HL7Exception e) {
      exList.add(new ValidationException(e));
    }

    return exList;
  }