/** * Converts a colour from #rrggbb to Java Color object. * * @param rgb String e.g. #ff00ee * @return Colour * @throws IllegalArgumentException If colour string is not valid */ protected Color convertRgb(String rgb) throws IllegalArgumentException { // Get colour parameter Matcher m = REGEX_RGB.matcher(rgb); if (!m.matches()) { throw new IllegalArgumentException( "MathML invalid colour '" + rgb + "'; expected #rrggbb (lower-case)"); } return new Color( Integer.parseInt(m.group(1), 16), Integer.parseInt(m.group(2), 16), Integer.parseInt(m.group(3), 16)); }
/** 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; }