/** * The convert method translates a String input to a specified outbound level of the same coding * scheme. For example, the input string value may be a tag-encoding URI and the outbound level * specified by string outboundlevel may be BINARY, in which case the return value is a binary * representation expressed as a string. * * @param input the identifier to be converted. * @param inputParameters additional parameters which need to be provided because they cannot * always be determined from the input value alone. Examples include the taglength, * companyprefixlength and filter values. * @param outputLevel the outbound level required for the ouput. Permitted values include BINARY, * TAG_ENCODING, PURE_IDENTITY, LEGACY and ONS_HOSTNAME. * @return the identifier converted to the output level. */ public String convert( String input, Map<String, String> inputParameters, LevelTypeList outputLevel) { TagLengthList tagLength = null; if (inputParameters.containsKey("taglength")) { // in principle, the user should provide a // TagLengthList object in the parameter list. String s = inputParameters.get("taglength"); tagLength = TagLengthList.valueOf(s); } PrefixMatch match = findPrefixMatch(input, tagLength); return convertLevel(match.getScheme(), match.getLevel(), input, inputParameters, outputLevel); }
/** * Returns the value of a specified fieldname from the specified hashmap and returns an integer * value or throws an exception if the value is not an integer */ private int getIntValue(String fieldname, Map<String, String> extraparams) { Matcher checkint = Pattern.compile("^\\d+$").matcher(fieldname); int rv; if (checkint.matches()) { rv = Integer.parseInt(fieldname); } else { if (extraparams.containsKey(fieldname)) { rv = Integer.parseInt(extraparams.get(fieldname)); } else { rv = -1; throw new TDTException( "No integer value for " + fieldname + " can be found - check extraparams"); } } return rv; }
private void processTxt(Node operation) { List<Node> targets = getChildNodes(operation, "target"); List<Node> optionNodes = getChildNodes(operation, "opt"); List<Node> separatorNode = getChildNodes(operation, "separator"); if (targets.isEmpty() || optionNodes.isEmpty()) { return; } String defaultSeparator = "="; String globalSeparator = defaultSeparator; if (!separatorNode.isEmpty()) { globalSeparator = separatorNode.get(0).getTextContent(); if (globalSeparator.length() != 1) { globalSeparator = defaultSeparator; } } Map<String, String> options = new HashMap<String, String>(); Map<String, String> processedOptions = new HashMap<String, String>(); for (int i = 0; i < optionNodes.size(); i++) { Node option = optionNodes.get(i); String name = option.getAttributes().getNamedItem("name").getNodeValue(); String value = option.getTextContent(); if (options.containsKey(name)) { options.remove(name); } options.put(name, value); } for (int t = 0; t < targets.size(); t++) { File target = new File(absolutePath(targets.get(t).getTextContent())); File tmpFile = new File(Utils.timestamp()); BufferedWriter bw = null; BufferedReader br = null; try { Node separatorAttr = targets.get(t).getAttributes().getNamedItem("separator"); String separator = (separatorAttr == null) ? globalSeparator : separatorAttr.getNodeValue(); if (separator.length() != 1) { separator = globalSeparator; } bw = new BufferedWriter(new FileWriter(tmpFile)); if (target.exists()) { br = new BufferedReader(new FileReader(target)); for (String line; (line = br.readLine()) != null; ) { String[] parts = line.split(separator); if (parts.length < 2) { bw.write(line); bw.newLine(); continue; } String optName = parts[0].trim(); if (options.containsKey(optName)) { String optValue = options.get(optName); bw.write(optName + " " + separator + " " + optValue); bw.newLine(); processedOptions.put(optName, optValue); options.remove(optName); } else if (processedOptions.containsKey(optName)) { bw.write(optName + " " + separator + " " + processedOptions.get(optName)); bw.newLine(); } else { bw.write(line); bw.newLine(); } } br.close(); } for (Map.Entry<String, String> entry : options.entrySet()) { bw.write(entry.getKey() + " " + separator + " " + entry.getValue()); bw.newLine(); } bw.close(); FileUtils.copyFile(tmpFile, target); FileUtils.forceDelete(tmpFile); } catch (IOException ex) { Utils.onError(new Error.WriteTxtConfig(target.getPath())); } } }