Пример #1
0
 public String replaceTokens(String text, Map<String, String> replacements) {
   boolean hasReplaced = false;
   Pattern pattern = Pattern.compile("\\[(.+?)\\]");
   Matcher matcher = pattern.matcher(text);
   StringBuffer buffer = new StringBuffer();
   while (matcher.find()) {
     String replacement = replacements.get(matcher.group(1));
     if (replacement != null) {
       matcher.appendReplacement(buffer, "");
       buffer.append(replacement);
       hasReplaced = true;
     }
   }
   matcher.appendTail(buffer);
   String strLine2 = buffer.toString();
   if (hasReplaced) System.out.println(">>> replaced text : " + text + " => " + strLine2);
   return strLine2;
 }
Пример #2
0
  /**
   * wrapped main method within instance context. Called from contructor
   *
   * @param args
   */
  void doMain(String[] args) { // here we are at nonstatic context

    // System.out.println("args: "+args);
    CmdLineParser parser = new CmdLineParser();
    CmdLineParser.Option textOpt = parser.addStringOption('K', "Key");
    CmdLineParser.Option countersOpt = parser.addStringOption('C', "Counterdef");
    CmdLineParser.Option timestampOpt = parser.addStringOption('T', "Timestamp");
    CmdLineParser.Option ipAddressOpt = parser.addStringOption('I', "IPAddress");
    // Text replacements
    Map<String, String> textReplacements = new HashMap<String, String>();

    try {
      parser.parse(args);
    } catch (Exception e) {
      e.printStackTrace();
    }
    Vector textOpts = parser.getOptionValues(textOpt);
    for (Object s : textOpts) {
      String[] pair = ((String) s).split("=");
      if (pair.length == 2) { // protect against wrong param
        System.out.println("Text replacement option: " + pair[0] + ":" + pair[1]);
        textReplacements.put(pair[0], pair[1]);
      }
    }
    //
    // Address replacements
    //
    Map<String, String> ipAddressRepl = new HashMap<String, String>();

    try {
      parser.parse(args);
    } catch (Exception e) {
      e.printStackTrace();
    }
    Vector ipOpts = parser.getOptionValues(ipAddressOpt);
    for (Object s : ipOpts) {
      String[] pair = ((String) s).split("=");
      String ipv4bin = convertIPtoBin(pair[1]);
      System.out.println("ipaddr: " + pair[0] + ":" + pair[1] + " => " + ipv4bin);
      ipAddressRepl.put(pair[0], ipv4bin);
    }
    //
    //
    //
    String argTimestamp = (String) parser.getOptionValue(timestampOpt);
    if (argTimestamp != null) {
      System.out.println("Timestamp: " + argTimestamp);
    }

    tsGen = new TimestampGen(argTimestamp); // should be instantiated anyway to avoid nullpointers.

    String[] fileArgs = parser.getRemainingArgs();

    if (fileArgs.length < 2) {
      usage();
      return;
    }

    for (String s : fileArgs) {
      System.out.println("Other: " + s);
    }

    String argSeagullScenInputFile = fileArgs[0];
    String argSeagullScenOutputFile = fileArgs[1];

    System.out.println(
        "seagull scenario files '"
            + argSeagullScenInputFile
            + "' => '"
            + argSeagullScenOutputFile
            + "'");
    // String str_date="2013/02/19 22:57:05 +0000";

    try {
      // Open the file that is the first
      // command line parameter

      FileInputStream inStream = new FileInputStream(argSeagullScenInputFile);
      DataInputStream in = new DataInputStream(inStream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));

      FileWriter outStream = new FileWriter(argSeagullScenOutputFile);
      BufferedWriter out = new BufferedWriter(outStream);

      String strLine;
      // Read File Line By Line
      // <avp name="Event-Timestamp" value="xxxxxxx"> </avp>

      System.out.println("About to read lines");
      while ((strLine = br.readLine()) != null) {
        // replace Event-Timestamp value
        strLine = tsGen.replaceTimestamp(strLine);
        // replace text  textReplacements
        strLine = replaceTokens(strLine, textReplacements);
        strLine = replaceTokens(strLine, ipAddressRepl);
        strLine = replaceCounterdef(strLine);
        out.write(strLine + "\n");
        //
        // System.out.println (strLine);
      }
      // Close the input stream
      in.close();
      out.close();
    } catch (Exception e) { // Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
    /////////////////////////////////////////////

  }