コード例 #1
0
ファイル: TDTEngine.java プロジェクト: max90727/fosstrak
  /**
   * Adds additional entries to the extraparams hashmap by processing various rules defined in the
   * TDT definition files. Typically used for string processing functions, lookup in tables,
   * calculation of check digits etc.
   */
  private void processRules(Map<String, String> extraparams, Rule tdtrule) {
    String tdtfunction = tdtrule.getFunction();
    int openbracket = tdtfunction.indexOf("(");
    assert openbracket != -1;
    String params = tdtfunction.substring(openbracket + 1, tdtfunction.length() - 1);
    String rulename = tdtfunction.substring(0, openbracket);
    String[] parameter = params.split(",");
    String newfieldname = tdtrule.getNewFieldName();
    // System.out.println(tdtfunction + " " + parameter[0] + " " + extraparams.get(parameter[0]));
    /**
     * Stores in the hashmap extraparams the value obtained from a lookup in a specified XML table.
     *
     * <p>The first parameter is the given value already known. This is denoted as $1 in the
     * corresponding XPath expression
     *
     * <p>The second parameter is the string filename of the table which must be present in the
     * auxiliary subdirectory
     *
     * <p>The third parameter is the column in which the supplied input value should be sought
     *
     * <p>The fourth parameter is the column whose value should be read for the corresponding row,
     * in order to obtain the result of the lookup.
     *
     * <p>The rule in the definition file may contain an XPath expression and a URL where the table
     * may be obtained.
     */
    if (rulename.equals("TABLELOOKUP")) {
      // parameter[0] is given value
      // parameter[1] is table
      // parameter[2] is input column supplied
      // parameter[3] is output column required
      assert parameter.length == 4 : "incorrect number of parameters to tablelookup " + params;
      if (parameter[1].equals("tdt64bitcpi")) {
        String s = extraparams.get(parameter[0]);
        assert s != null : tdtfunction + " when " + parameter[0] + " is null";
        String t = gs1cpi.get(s);
        assert t != null : "gs1cpi[" + s + "] is null";
        assert newfieldname != null;
        extraparams.put(newfieldname, t);
        // extraparams.put(newfieldname, gs1cpi.get(extraparams.get(parameter[0])));
      } else { // JPB! the following is untested
        String tdtxpath = tdtrule.getTableXPath();
        String tdttableurl = tdtrule.getTableURL();
        String tdtxpathsub = tdtxpath.replaceAll("\\$1", extraparams.get(parameter[0]));
        extraparams.put(newfieldname, xpathlookup("ManagerTranslation.xml", tdtxpathsub));
      }
    }

    /**
     * Stores the length of the specified string under the new fieldname specified by the
     * corresponding rule of the definition file.
     */
    if (rulename.equals("LENGTH")) {
      assert extraparams.get(parameter[0]) != null
          : tdtfunction + " when " + parameter[0] + " is null";
      if (extraparams.get(parameter[0]) != null) {
        extraparams.put(newfieldname, Integer.toString(extraparams.get(parameter[0]).length()));
      }
    }

    /**
     * Stores a GS1 check digit in the extraparams hashmap, keyed under the new fieldname specified
     * by the corresponding rule of the definition file.
     */
    if (rulename.equals("GS1CHECKSUM")) {
      assert extraparams.get(parameter[0]) != null
          : tdtfunction + " when " + parameter[0] + " is null";
      if (extraparams.get(parameter[0]) != null) {
        extraparams.put(newfieldname, gs1checksum(extraparams.get(parameter[0])));
      }
    }

    /**
     * Obtains a substring of the string provided as the first parameter. If only a single second
     * parameter is specified, then this is considered as the start index and all characters from
     * the start index onwards are stored in the extraparams hashmap under the key named
     * 'newfieldname' in the corresponding rule of the definition file. If a second and third
     * parameter are specified, then the second parameter is the start index and the third is the
     * length of characters required. A substring consisting characters from the start index up to
     * the required length of characters is stored in the extraparams hashmap, keyed under the new
     * fieldname specified by the corresponding rule of the defintion file.
     */
    if (rulename.equals("SUBSTR")) {
      assert extraparams.get(parameter[0]) != null
          : tdtfunction + " when " + parameter[0] + " is null";
      if (parameter.length == 2) {
        if (extraparams.get(parameter[0]) != null) {
          int start = getIntValue(parameter[1], extraparams);
          if (start >= 0) {
            extraparams.put(newfieldname, extraparams.get(parameter[0]).substring(start));
          }
        }
      }
      if (parameter.length
          == 3) { // need to check that this variation is correct - c.f. Perl substr
        assert extraparams.get(parameter[0]) != null
            : tdtfunction + " when " + parameter[0] + " is null";
        if (extraparams.get(parameter[0]) != null) {
          int start = getIntValue(parameter[1], extraparams);
          int end = getIntValue(parameter[2], extraparams);
          if ((start >= 0) && (end >= 0)) {
            extraparams.put(
                newfieldname, extraparams.get(parameter[0]).substring(start, start + end));
          }
        }
      }
    }

    /**
     * Concatenates specified string parameters together. Literal values must be enclosed within
     * single or double quotes or consist of unquoted digits. Other unquoted strings are considered
     * as fieldnames and the corresponding value from the extraparams hashmap are inserted. The
     * result of the concatenation (and substitution) of the strings is stored as a new entry in the
     * extraparams hashmap, keyed under the new fieldname specified by the rule.
     */
    if (rulename.equals("CONCAT")) {
      StringBuilder buffer = new StringBuilder();
      for (int p1 = 0; p1 < parameter.length; p1++) {
        Matcher matcher = Pattern.compile("\"(.*?)\"|'(.*?)'|[0-9]").matcher(parameter[p1]);
        if (matcher.matches()) {
          buffer.append(parameter[p1]);
        } else {
          assert extraparams.get(parameter[p1]) != null
              : tdtfunction + " when " + parameter[p1] + " is null";
          if (extraparams.get(parameter[p1]) != null) {
            buffer.append(extraparams.get(parameter[p1]));
          }
        }
      }
      extraparams.put(newfieldname, buffer.toString());
    }
  }
コード例 #2
0
ファイル: TDTEngine.java プロジェクト: max90727/fosstrak
  /** convert from a particular scheme / level */
  private String convertLevel(
      Scheme tdtscheme,
      Level tdtlevel,
      String input,
      Map<String, String> inputParameters,
      LevelTypeList outboundlevel) {

    String outboundstring;
    Map<String, String> extraparams =
        //	    new NoisyMap
        (new HashMap<String, String>(inputParameters));

    // get the scheme's option key, which is the name of a
    // parameter whose value is matched to the option key of the
    // level.

    String optionkey = tdtscheme.getOptionKey();
    String optionValue = extraparams.get(optionkey);
    // the name of a parameter which allows the appropriate option
    // to be selected

    // now consider the various options within the scheme and
    // level for each option element inside the level, check
    // whether the pattern attribute matches as a regular
    // expression

    String matchingOptionKey = null;
    Option matchingOption = null;
    Matcher prefixMatcher = null;
    for (Enumeration e = tdtlevel.enumerateOption(); e.hasMoreElements(); ) {
      Option opt = (Option) e.nextElement();
      if (optionValue == null || optionValue.equals(opt.getOptionKey())) {
        // possible match

        Matcher matcher = Pattern.compile(opt.getPattern()).matcher(input);
        if (matcher.matches()) {
          if (prefixMatcher != null) throw new TDTException("Multiple patterns matched");
          prefixMatcher = matcher;
          matchingOptionKey = opt.getOptionKey();
          matchingOption = opt;
        }
      }
    }
    if (prefixMatcher == null) throw new TDTException("No patterns matched");

    optionValue = matchingOptionKey;

    for (Enumeration e = matchingOption.enumerateField(); e.hasMoreElements(); ) {
      Field field = (Field) e.nextElement();
      int seq = field.getSeq();

      String strfieldname = field.getName();
      int fieldlength = field.getLength();
      String strfieldvalue = prefixMatcher.group(seq);
      // System.out.println("   processing field " + strfieldname + " = '" + strfieldvalue + "'");

      if (field.getCompaction() == null) {
        // if compaction is null, treat field as an integer

        if (field.getCharacterSet() != null) { // if the character set is specified
          Matcher charsetmatcher =
              Pattern.compile("^" + field.getCharacterSet() + "$").matcher(strfieldvalue);
          if (!charsetmatcher.matches()) {
            throw new TDTException(
                "field "
                    + strfieldname
                    + " ("
                    + strfieldvalue
                    + ") does not conform to the allowed character set ("
                    + field.getCharacterSet()
                    + ") ");
          }
        }

        BigInteger bigvalue = null;

        if (tdtlevel.getType() == LevelTypeList.BINARY) { // if the input was BINARY
          bigvalue = new BigInteger(strfieldvalue, 2);
          extraparams.put(strfieldname, bigvalue.toString());
        } else {
          if (field.getDecimalMinimum() != null || field.getDecimalMaximum() != null)
            bigvalue = new BigInteger(strfieldvalue);
          extraparams.put(strfieldname, strfieldvalue);
        }

        if (field.getDecimalMinimum() != null) { // if the decimal minimum is specified
          BigInteger bigmin = new BigInteger(field.getDecimalMinimum());

          if (bigvalue.compareTo(bigmin)
              == -1) { // throw an exception if the field value is less than the decimal minimum
            throw new TDTException(
                "field "
                    + strfieldname
                    + " ("
                    + bigvalue
                    + ") is less than DecimalMinimum ("
                    + field.getDecimalMinimum()
                    + ") allowed");
          }
        }

        if (field.getDecimalMaximum() != null) { // if the decimal maximum is specified
          BigInteger bigmax = new BigInteger(field.getDecimalMaximum());

          if (bigvalue.compareTo(bigmax)
              == 1) { // throw an excpetion if the field value is greater than the decimal maximum
            throw new TDTException(
                "field "
                    + strfieldname
                    + " ("
                    + bigvalue
                    + ") is greater than DecimalMaximum ("
                    + field.getDecimalMaximum()
                    + ") allowed");
          }
        }

        // after extracting the field, it may be necessary to pad it.

        padField(extraparams, field);

      } else {
        // compaction is specified - interpret binary as a string value using a truncated byte per
        // character

        CompactionMethodList compaction = field.getCompaction();
        PadDirectionList padDir = field.getPadDir();
        String padchar = field.getPadChar();
        String s;
        if (compaction == CompactionMethodList.VALUE_5)
          // "5-bit"
          s = bin2uppercasefive(strfieldvalue);
        else if (compaction == CompactionMethodList.VALUE_4)
          // 6-bit
          s = bin2alphanumsix(strfieldvalue);
        else if (compaction == CompactionMethodList.VALUE_3)
          // 7-bit
          s = bin2asciiseven(strfieldvalue);
        else if (compaction == CompactionMethodList.VALUE_2)
          // 8-bit
          s = bin2bytestring(strfieldvalue);
        else throw new Error("unsupported compaction method " + compaction);
        extraparams.put(strfieldname, stripPadChar(s, padDir, padchar));
      }
    } // for each field;

    /**
     * the EXTRACT rules are performed after parsing the input, in order to determine additional
     * fields that are to be derived from the fields obtained by the pattern match process
     */
    int seq = 0;
    for (Enumeration e = tdtlevel.enumerateRule(); e.hasMoreElements(); ) {
      Rule tdtrule = (Rule) e.nextElement();
      if (tdtrule.getType() == ModeList.EXTRACT) {
        assert seq < tdtrule.getSeq() : "Rule out of sequence order";
        seq = tdtrule.getSeq();
        processRules(extraparams, tdtrule);
      }
    }

    /**
     * Now we need to consider the corresponding output level and output option. The scheme must
     * remain the same, as must the value of optionkey (to select the corresponding option element
     * nested within the required outbound level)
     */
    Level tdtoutlevel = findLevel(tdtscheme, outboundlevel);
    Option tdtoutoption = findOption(tdtoutlevel, optionValue);

    /**
     * the FORMAT rules are performed before formatting the output, in order to determine additional
     * fields that are required for preparation of the outbound format
     */
    seq = 0;
    for (Enumeration e = tdtoutlevel.enumerateRule(); e.hasMoreElements(); ) {
      Rule tdtrule = (Rule) e.nextElement();
      if (tdtrule.getType() == ModeList.FORMAT) {
        assert seq < tdtrule.getSeq() : "Rule out of sequence order";
        seq = tdtrule.getSeq();
        processRules(extraparams, tdtrule);
      }
    }

    /**
     * Now we need to ensure that all fields required for the outbound grammar are suitably padded
     * etc. processPadding takes care of firstly padding the non-binary fields if padChar and
     * padDir, length are specified then (if necessary) converting to binary and padding the binary
     * representation to the left with zeros if the bit string is has fewer bits than the bitLength
     * attribute specifies. N.B. TDTv1.1 will be more specific about bit-level padding rather than
     * assuming that it is always to the left with the zero bit.
     */

    // System.out.println(" prior to processPadding, " + extraparams);
    for (Enumeration e = tdtoutoption.enumerateField(); e.hasMoreElements(); ) {
      Field field = (Field) e.nextElement();
      // processPadding(extraparams, field, outboundlevel, tdtoutoption);

      padField(extraparams, field);
      if (outboundlevel == LevelTypeList.BINARY) binaryPadding(extraparams, field);
    }

    /**
     * Construct the output from the specified grammar (in ABNF format) together with the field
     * values stored in inputparams
     */
    outboundstring = buildGrammar(tdtoutoption.getGrammar(), extraparams);

    // System.out.println("final extraparams = " + extraparams);
    // System.out.println("returned " + outboundstring);
    return outboundstring;
  }