/** * Returns the Primitive object at the given location. */ private static Primitive getPrimitive(Segment segment, int field, int rep, int component, int subcomponent) throws HL7Exception { Type type = segment.getField(field, rep); return getPrimitive(type, component, subcomponent); }
/** * Returns the Primitive object at the given location. */ private static Primitive getPrimitive(Segment segment, int field, int rep, int component, int subcomponent) throws HL7Exception { Type type = segment.getField(field, rep); return getPrimitive(type, component, subcomponent); }
/** * Attempts to extract a Primitive from the given type. If it's a composite, drills down through * first components until a primitive is reached. */ private static Primitive getPrimitive(Type type) { if (type instanceof Primitive) { return (Primitive) type; } if (type instanceof Composite) { try { return getPrimitive(((Composite) type).getComponent(0)); } catch (HL7Exception e) { throw new RuntimeException("Internal error: HL7Exception thrown on Composite.getComponent(0)."); } } return getPrimitive(((Varies) type).getData()); }
/** * Attempts to extract a Primitive from the given type. If it's a composite, drills down through * first components until a primitive is reached. */ private static Primitive getPrimitive(Type type) { if (type instanceof Primitive) { return (Primitive) type; } if (type instanceof Composite) { try { return getPrimitive(((Composite) type).getComponent(0)); } catch (HL7Exception e) { throw new RuntimeException("Internal error: HL7Exception thrown on Composite.getComponent(0)."); } } return getPrimitive(((Varies) type).getData()); }
/** * Returns the string value of the Primitive at the given location. * * @param segment the segment from which to get the primitive * @param field the field number (indexed from 1) * @param rep the field repetition (indexed from 0) * @param component the component number (indexed from 1, use 1 for primitive field) * @param subcomponent the subcomponent number (indexed from 1, use 1 for primitive component) * @return string value of the Primitive at the given location * @throws HL7Exception if the primitive could not be obtained */ public static String get(Segment segment, int field, int rep, int component, int subcomponent) throws HL7Exception { if (segment == null) { throw new NullPointerException("segment may not be null"); } if (rep < 0) { throw new IllegalArgumentException("rep must not be negative"); } if (component < 1) { throw new IllegalArgumentException( "component must not be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } if (subcomponent < 1) { throw new IllegalArgumentException( "subcomponent must not be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } Primitive prim = getPrimitive(segment, field, rep, component, subcomponent); return prim.getValue(); }
/** * Sets the string value of the Primitive at the given location. * * @param segment the segment from which to get the primitive * @param field the field number (indexed from 1) * @param rep the field repetition (indexed from 0) * @param component the component number (indexed from 1, use 1 for primitive field) * @param subcomponent the subcomponent number (indexed from 1, use 1 for primitive component) * @param value value to be set * @throws HL7Exception if the value could not be set */ public static void set(Segment segment, int field, int rep, int component, int subcomponent, String value) throws HL7Exception { if (segment == null) { throw new NullPointerException("segment may not be null"); } if (rep < 0) { throw new IllegalArgumentException("rep must not be negative"); } if (component < 1) { throw new IllegalArgumentException( "component must be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } if (subcomponent < 1) { throw new IllegalArgumentException( "subcomponent must be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } Primitive prim = getPrimitive(segment, field, rep, component, subcomponent); prim.setValue(value); }
/** * Returns the string value of the Primitive at the given location. * * @param segment the segment from which to get the primitive * @param field the field number (indexed from 1) * @param rep the field repetition (indexed from 0) * @param component the component number (indexed from 1, use 1 for primitive field) * @param subcomponent the subcomponent number (indexed from 1, use 1 for primitive component) * @return string value of the Primitive at the given location * @throws HL7Exception if the primitive could not be obtained */ public static String get(Segment segment, int field, int rep, int component, int subcomponent) throws HL7Exception { if (segment == null) { throw new NullPointerException("segment may not be null"); } if (rep < 0) { throw new IllegalArgumentException("rep must not be negative"); } if (component < 1) { throw new IllegalArgumentException( "component must not be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } if (subcomponent < 1) { throw new IllegalArgumentException( "subcomponent must not be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } Primitive prim = getPrimitive(segment, field, rep, component, subcomponent); return prim.getValue(); }
/** * Sets the string value of the Primitive at the given location. * * @param segment the segment from which to get the primitive * @param field the field number (indexed from 1) * @param rep the field repetition (indexed from 0) * @param component the component number (indexed from 1, use 1 for primitive field) * @param subcomponent the subcomponent number (indexed from 1, use 1 for primitive component) * @param value value to be set * @throws HL7Exception if the value could not be set */ public static void set(Segment segment, int field, int rep, int component, int subcomponent, String value) throws HL7Exception { if (segment == null) { throw new NullPointerException("segment may not be null"); } if (rep < 0) { throw new IllegalArgumentException("rep must not be negative"); } if (component < 1) { throw new IllegalArgumentException( "component must be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } if (subcomponent < 1) { throw new IllegalArgumentException( "subcomponent must be 1 or more (note that this parameter is 1-indexed, not 0-indexed)"); } Primitive prim = getPrimitive(segment, field, rep, component, subcomponent); prim.setValue(value); }
/** * Copies data from the "from" Type into the "to" Type. Either Type may be * a Primitive, Composite, or Varies. If a Varies is provided, the operation is * performed on the result of calling its getData() method. A Primitive may be * copied into a Composite, in which case the value is copied into the first * component of the Composite. A Composite may be copied into a Primitive, * in which case the first component is copied. Given Composites with different * numbers of components, the first components are copied, up to the length * of the smaller one. * * @param from type to copy from * @param to type to copy to * @throws DataTypeException if the types are not compatible */ public static void copy(Type from, Type to) throws DataTypeException { for (int i = 1; i <= Terser.numComponents(from); i++) { for (int j = 1; j <= Terser.numSubComponents(from, i); j++) { String val = Terser.getPrimitive(from, i, j).getValue(); Terser.getPrimitive(to, i, j).setValue(val); } } }
/** * Copies data from the "from" Type into the "to" Type. Either Type may be * a Primitive, Composite, or Varies. If a Varies is provided, the operation is * performed on the result of calling its getData() method. A Primitive may be * copied into a Composite, in which case the value is copied into the first * component of the Composite. A Composite may be copied into a Primitive, * in which case the first component is copied. Given Composites with different * numbers of components, the first components are copied, up to the length * of the smaller one. * * @param from type to copy from * @param to type to copy to * @throws DataTypeException if the types are not compatible */ public static void copy(Type from, Type to) throws DataTypeException { for (int i = 1; i <= Terser.numComponents(from); i++) { for (int j = 1; j <= Terser.numSubComponents(from, i); j++) { String val = Terser.getPrimitive(from, i, j).getValue(); Terser.getPrimitive(to, i, j).setValue(val); } } }
/** * Tests table values for ID, IS, and CE types. An empty list is returned for all other types or * if the table name or number is missing. */ protected void testTypeAgainstTable(Type type, AbstractComponent<?> profile, String profileID, List<HL7Exception> exList) { if (profile.getTable() != null && (type.getName().equals("IS") || type.getName().equals("ID"))) { String codeSystem = String.format("HL7%1$4s", profile.getTable()).replace(" ", "0"); String value = ((Primitive) type).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } else if (type.getName().equals("CE")) { String value = Terser.getPrimitive(type, 1, 1).getValue(); String codeSystem = Terser.getPrimitive(type, 3, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); value = Terser.getPrimitive(type, 4, 1).getValue(); codeSystem = Terser.getPrimitive(type, 6, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } }
/** * Tests table values for ID, IS, and CE types. An empty list is returned for all other types or * if the table name or number is missing. */ protected void testTypeAgainstTable(Type type, AbstractComponent<?> profile, String profileID, List<HL7Exception> exList) { if (profile.getTable() != null && (type.getName().equals("IS") || type.getName().equals("ID"))) { String codeSystem = String.format("HL7%1$4s", profile.getTable()).replace(" ", "0"); String value = ((Primitive) type).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } else if (type.getName().equals("CE")) { String value = Terser.getPrimitive(type, 1, 1).getValue(); String codeSystem = Terser.getPrimitive(type, 3, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); value = Terser.getPrimitive(type, 4, 1).getValue(); codeSystem = Terser.getPrimitive(type, 6, 1).getValue(); addTableTestResult(profileID, codeSystem, value, exList); } }
/** * Encodes the given Type, using the given encoding characters. * It is assumed that the Type represents a complete field rather than a component. */ public static String encode(Type source, EncodingCharacters encodingChars) { StringBuffer field = new StringBuffer(); for (int i = 1; i <= Terser.numComponents(source); i++) { StringBuffer comp = new StringBuffer(); for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { Primitive p = Terser.getPrimitive(source, i, j); comp.append(encodePrimitive(p, encodingChars)); comp.append(encodingChars.getSubcomponentSeparator()); } field.append(stripExtraDelimiters(comp.toString(), encodingChars.getSubcomponentSeparator())); field.append(encodingChars.getComponentSeparator()); } return stripExtraDelimiters(field.toString(), encodingChars.getComponentSeparator()); //return encode(source, encodingChars, false); }
/** * Encodes the given Type, using the given encoding characters. * It is assumed that the Type represents a complete field rather than a component. */ public static String encode(Type source, EncodingCharacters encodingChars) { StringBuffer field = new StringBuffer(); for (int i = 1; i <= Terser.numComponents(source); i++) { StringBuffer comp = new StringBuffer(); for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { Primitive p = Terser.getPrimitive(source, i, j); comp.append(encodePrimitive(p, encodingChars)); comp.append(encodingChars.getSubcomponentSeparator()); } field.append(stripExtraDelimiters(comp.toString(), encodingChars.getSubcomponentSeparator())); field.append(encodingChars.getComponentSeparator()); } return stripExtraDelimiters(field.toString(), encodingChars.getComponentSeparator()); //return encode(source, encodingChars, false); }
StringBuilder comp = new StringBuilder(); for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { Primitive p = Terser.getPrimitive(source, i, j); comp.append(encodePrimitive(p, parserConfig.getEscaping(), encodingChars)); comp.append(encodingChars.getSubcomponentSeparator());
/** * Fills a field with values from an unparsed string representing the field. * @param destinationField the field Type * @param data the field string (including all components and subcomponents; not including field delimiters) * @param encodingCharacters the encoding characters used in the message */ public void parse(Type destinationField, String data, EncodingCharacters encodingCharacters) throws HL7Exception { String[] components = split(data, String.valueOf(encodingCharacters.getComponentSeparator())); for (int i = 0; i < components.length; i++) { String[] subcomponents = split(components[i], String.valueOf(encodingCharacters.getSubcomponentSeparator())); for (int j = 0; j < subcomponents.length; j++) { String val = subcomponents[j]; if (val != null) { val = Escape.unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, i+1, j+1).setValue(val); } } }
/** * Fills a field with values from an unparsed string representing the field. * @param destinationField the field Type * @param data the field string (including all components and subcomponents; not including field delimiters) * @param encodingCharacters the encoding characters used in the message */ public void parse(Type destinationField, String data, EncodingCharacters encodingCharacters) throws HL7Exception { String[] components = split(data, String.valueOf(encodingCharacters.getComponentSeparator())); for (int i = 0; i < components.length; i++) { String[] subcomponents = split(components[i], String.valueOf(encodingCharacters.getSubcomponentSeparator())); for (int j = 0; j < subcomponents.length; j++) { String val = subcomponents[j]; if (val != null) { val = Escape.unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, i+1, j+1).setValue(val); } } }
return getPrimitive(sub);
/** * Fills a field with values from an unparsed string representing the field. * * @param destinationField * the field Type * @param data * the field string (including all components and subcomponents; * not including field delimiters) * @param encodingCharacters * the encoding characters used in the message */ @Override public void parse(Type destinationField, String data, EncodingCharacters encodingCharacters) throws HL7Exception { String[] components = split(data, String.valueOf(encodingCharacters.getComponentSeparator())); for (int i = 0; i < components.length; i++) { String[] subcomponents = split(components[i], String.valueOf(encodingCharacters.getSubcomponentSeparator())); for (int j = 0; j < subcomponents.length; j++) { String val = subcomponents[j]; if (val != null) { val = getParserConfiguration().getEscaping().unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, i + 1, j + 1).setValue(val); } } }
/** * Fills a field with values from an unparsed string representing the field. * * @param destinationField * the field Type * @param data * the field string (including all components and subcomponents; * not including field delimiters) * @param encodingCharacters * the encoding characters used in the message */ @Override public void parse(Type destinationField, String data, EncodingCharacters encodingCharacters) throws HL7Exception { String[] components = split(data, String.valueOf(encodingCharacters.getComponentSeparator())); for (int i = 0; i < components.length; i++) { String[] subcomponents = split(components[i], String.valueOf(encodingCharacters.getSubcomponentSeparator())); for (int j = 0; j < subcomponents.length; j++) { String val = subcomponents[j]; if (val != null) { val = getParserConfiguration().getEscaping().unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, i + 1, j + 1).setValue(val); } } }