Example usage for org.apache.commons.lang3.text StrTokenizer reset

List of usage examples for org.apache.commons.lang3.text StrTokenizer reset

Introduction

In this page you can find the example usage for org.apache.commons.lang3.text StrTokenizer reset.

Prototype

public StrTokenizer reset(final char[] input) 

Source Link

Document

Reset this tokenizer, giving it a new input string to parse.

Usage

From source file:com.mgmtp.perfload.perfalyzer.util.PerfAlyzerUtils.java

public static Map<String, String> readAggregatedMap(final File executionsFile, final Charset charset)
        throws IOException {
    final StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(';');

    Map<String, String> result = newHashMapWithExpectedSize(11);

    List<String> lines = Files.readLines(executionsFile, charset);
    String[] headers = null;//from  w  w w. j a  va2 s  .c o m

    for (String line : lines) {
        tokenizer.reset(line);
        String[] tokens = tokenizer.getTokenArray();

        if (headers == null) {
            headers = tokens;
        } else {

            String[] data = tokenizer.getTokenArray();

            String operation = data[0];
            for (int i = 1; i < headers.length; ++i) {
                result.put(operation + "." + headers[i], data[i]);
            }
        }
    }

    return result;
}

From source file:com.mgmtp.perfload.perfalyzer.util.PerfAlyzerUtils.java

/**
 * Reads a semicolon-delimited CSV file into a list. Each line in the result list will be
 * another list of {@link Number} objects. The file is expected to have two numberic columns
 * which are parsed using the specified number format.
 * /*from  w ww  .  j  a  va 2  s.  c  o m*/
 * @param file
 *            the file
 * @param charset
 *            the character set to read the file
 * @param numberFormat
 *            the number format for parsing the column values
 * @return the immutable result list
 */
public static List<SeriesPoint> readDataFile(final File file, final Charset charset,
        final NumberFormat numberFormat) throws IOException {
    final StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(';');

    try (BufferedReader br = newReader(file, charset)) {
        boolean headerLine = true;
        List<SeriesPoint> result = newArrayListWithExpectedSize(200);

        for (String line; (line = br.readLine()) != null;) {
            try {
                if (headerLine) {
                    headerLine = false;
                } else {
                    tokenizer.reset(line);
                    String[] tokens = tokenizer.getTokenArray();
                    double x = numberFormat.parse(tokens[0]).doubleValue();
                    double y = numberFormat.parse(tokens[1]).doubleValue();

                    if (!result.isEmpty()) {
                        // additional point for histogram
                        SeriesPoint previousPoint = getLast(result);
                        result.add(new SeriesPoint(x, previousPoint.getY()));
                    }
                    tokenizer.reset(line);
                    result.add(new SeriesPoint(x, y));
                }
            } catch (ParseException ex) {
                throw new IOException("Error parsing number in file: " + file, ex);
            }
        }

        int size = result.size();
        if (size > 2) {
            // additional point at end for histogram
            SeriesPoint nextToLast = result.get(size - 3);
            SeriesPoint last = result.get(size - 1);
            double dX = last.getX().doubleValue() - nextToLast.getX().doubleValue();
            result.add(new SeriesPoint(last.getX().doubleValue() + dX, last.getY()));
        }
        return ImmutableList.copyOf(result);
    }
}

From source file:com.mgmtp.perfload.perfalyzer.util.PerfAlyzerUtils.java

/**
 * Reads a semicolon-delimited CSV file into a map of lists series values. Values for each
 * column are return as list of lists in the map, the key being the column header.
 * /* w  w w  .j  ava  2  s .c  o m*/
 * @param file
 *            the file
 * @param charset
 *            the character set to read the file
 * @param numberFormat
 *            the number format for formatting the column values
 * @param columnNames
 *            the columns to consider
 * 
 * @return an immutable map of lists of series values
 */
public static Map<String, List<SeriesPoint>> readDataFile(final File file, final Charset charset,
        final NumberFormat numberFormat, final Set<String> columnNames) throws IOException {
    final StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(';');

    return readLines(file, charset, new LineProcessor<Map<String, List<SeriesPoint>>>() {
        private String[] headers;
        private final Map<String, List<SeriesPoint>> result = newHashMapWithExpectedSize(4);
        private int colCount;

        @Override
        public boolean processLine(final String line) throws IOException {
            try {
                tokenizer.reset(line);
                String[] tokens = tokenizer.getTokenArray();

                if (headers == null) {
                    headers = tokens;
                    colCount = tokens.length;
                } else {
                    Integer counter = Integer.valueOf(tokens[0]);
                    for (int i = 1; i < colCount; ++i) {
                        String header = headers[i];
                        if (columnNames.contains(header)) {
                            List<SeriesPoint> colValues = result.get(header);
                            if (colValues == null) {
                                colValues = newArrayListWithExpectedSize(50);
                                result.put(header, colValues);
                            }
                            colValues.add(new SeriesPoint(counter, numberFormat.parse(tokens[i])));
                        }
                    }
                }
                return true;
            } catch (ParseException ex) {
                throw new IOException("Error parsing number in file: " + file, ex);
            }
        }

        @Override
        public Map<String, List<SeriesPoint>> getResult() {
            return ImmutableMap.copyOf(result);
        }
    });
}

From source file:com.mgmtp.jfunk.core.util.CsvDataProcessor.java

private List<Column> initColumns(final StrTokenizer st, final String headerLine) {
    st.reset(headerLine);

    String[] headers = st.getTokenArray();
    List<Column> columns = newArrayListWithCapacity(headers.length);
    for (String header : headers) {
        columns.add(new Column(header));
    }/*from   w  w  w .j  a  v  a 2s . c om*/
    return columns;
}

From source file:com.mgmtp.perfload.perfalyzer.reporting.email.EmailReporter.java

private List<? extends List<String>> loadData(final File file) throws IOException {
    try (BufferedReader br = newReader(file, Charsets.UTF_8)) {
        List<List<String>> rows = newArrayList();
        StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
        tokenizer.setDelimiterChar(DELIMITER);

        for (String line; (line = br.readLine()) != null;) {
            tokenizer.reset(line);
            List<String> tokenList = tokenizer.getTokenList();
            rows.add(tokenList);//from ww w  . j av a2s .co m
        }

        return rows;
    }
}

From source file:com.mgmtp.perfload.perfalyzer.util.MarkersReader.java

public List<Marker> readMarkers() throws IOException {
    Map<String, Marker> markers = new LinkedHashMap<>();

    StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(DELIMITER);

    try (FileInputStream fis = new FileInputStream(inputFile)) {

        for (Scanner scanner = new Scanner(fis.getChannel()); scanner.hasNext();) {
            String line = scanner.nextLine();
            if (line.startsWith("#")) {
                continue;
            }/*w w w.j  a  v  a 2 s  . c  o  m*/

            tokenizer.reset(line);

            List<String> tokenList = tokenizer.getTokenList();

            if (MARKER.equals(tokenList.get(COL_MARKER))) {
                // no whitespace allowed in marker in order to avoid issues in HTML
                String markerName = tokenList.get(COL_MARKER_NAME).replaceAll("\\s+", "_");
                String markerType = tokenList.get(COL_MARKER_TYPE);
                long timeMillis = Long.parseLong(tokenList.get(COL_TIMESTAMP));

                switch (markerType) {
                case MARKER_LEFT: {
                    Marker marker = new Marker(markerName);
                    markers.put(markerName, marker);
                    marker.setLeftMillis(timeMillis);
                    break;
                }
                case MARKER_RIGHT: {
                    Marker marker = markers.get(markerName);
                    marker.setRightMillis(timeMillis);
                    break;
                }
                default:
                    throw new IllegalStateException("Invalid marker type: " + markerType);
                }
            }
        }

        return markers.values().stream().map(marker -> {
            marker.calculateDateTimeFields(testStart);
            return marker;
        }).collect(toList());
    }
}

From source file:com.mgmtp.perfload.core.common.config.XmlConfigReader.java

private ListMultimap<ProcessKey, LoadProfileEvent> readLoadProfileEvents(final Element testplan)
        throws IOException {
    ListMultimap<ProcessKey, LoadProfileEvent> eventsByProcess = ArrayListMultimap.create();
    String loadProfile = testplan.elementTextTrim("loadProfile");

    // relative to testplan
    File loadProfileConfigFile = new File(new File(testplanFile.getParentFile(), "loadprofiles"), loadProfile);

    try (BufferedReader br = new BufferedReader(
            new InputStreamReader(new FileInputStream(loadProfileConfigFile), "UTF-8"))) {
        StrTokenizer st = StrTokenizer.getCSVInstance();
        st.setDelimiterChar(';');

        for (String line = null; (line = br.readLine()) != null;) {
            // ignore line that are blank, commented out, or represent markers
            if (isBlank(line) || startsWith(line, "#") || MARKER_PATTERN.matcher(line).matches()) {
                continue;
            }//from   ww  w  .  jav  a2  s .c  o  m

            st.reset(line);
            String[] tokens = st.getTokenArray();

            long startTime = Long.parseLong(tokens[0]);
            String operation = tokens[1];
            String target = tokens[2];
            int daemonId = Integer.parseInt(tokens[3]);
            int processId = Integer.parseInt(tokens[4]);

            eventsByProcess.put(new ProcessKey(daemonId, processId),
                    new LoadProfileEvent(startTime, operation, target, daemonId, processId));
        }
    }

    return eventsByProcess;
}

From source file:com.mgmtp.jfunk.core.util.CsvDataProcessor.java

/**
 * Processes the specified CSV file. For every line but the header line (which is required), the
 * specified command is executed.// ww  w  .ja va 2s.  com
 * 
 * @param reader
 *            the reader for loading the CSV data
 * @param delimiter
 *            the column separator
 * @param quoteChar
 *            the quote character ('\0' for no quoting)
 * @param command
 *            the command (i. e. a Groovy closure if used in a Groovy script) to be executed for
 *            every processed line
 */
public void processFile(final Reader reader, final String delimiter, final char quoteChar,
        final Runnable command) {
    try {
        List<String> inputLines = CharStreams.readLines(reader);

        StrTokenizer st = StrTokenizer.getCSVInstance();
        st.setDelimiterString(delimiter);
        if (quoteChar != '\0') {
            st.setQuoteChar(quoteChar);
        } else {
            st.setQuoteMatcher(StrMatcher.noneMatcher());
        }

        // extract header
        String headerLine = inputLines.remove(0);
        List<Column> columns = initColumns(st, headerLine);
        for (String line : inputLines) {
            st.reset(line);
            String[] colArray = st.getTokenArray();
            int len = colArray.length;
            checkState(len == columns.size(),
                    "Mismatch between number of header columns and number of line columns.");

            DataSource dataSource = dataSourceProvider.get();
            Configuration config = configProvider.get();
            for (int i = 0; i < len; ++i) {
                String value = StringUtils.trimToEmpty(colArray[i]);

                String dataSetKey = columns.get(i).dataSetKey;
                String key = columns.get(i).key;
                if (dataSetKey != null) {
                    if ("<auto>".equals(value)) {
                        dataSource.resetFixedValue(dataSetKey, key);
                    } else {
                        log.debug("Setting data set entry for " + this + " to value=" + value);
                        dataSource.setFixedValue(dataSetKey, key, value);
                    }
                } else {
                    log.debug("Setting property for " + this + " to value=" + value);
                    config.put(key, value);
                }
            }

            command.run();
        }
    } catch (IOException ex) {
        throw new JFunkException("Error processing CSV data", ex);
    }
}

From source file:com.mgmtp.perfload.perfalyzer.util.BinnedFilesMerger.java

public void mergeFiles() throws IOException {
    if (!inputDir.isDirectory()) {
        throw new IllegalArgumentException("The input File must be a directory");
    }//from w  ww. j a  va 2s  .  c  o  m

    StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(DELIMITER);
    Map<String, FileChannel> destChannels = newHashMap();
    List<OutputStream> outputStreams = newArrayList();
    File[] filesInInputDirectory = inputDir.listFiles();

    try {
        for (File file : filesInInputDirectory) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                for (Scanner scanner = new Scanner(fis.getChannel(), Charsets.UTF_8.name()); scanner
                        .hasNext();) {
                    String line = scanner.nextLine();
                    tokenizer.reset(line);

                    List<String> tokenList = tokenizer.getTokenList();
                    String key = tokenList.get(sortCriteriaColumn);
                    FileChannel destChannel = destChannels.get(key);
                    if (destChannel == null) {
                        FileOutputStream fos = new FileOutputStream(
                                new File(outputDir, FILE_TYPE + "_" + key + ".out"));
                        outputStreams.add(fos);
                        destChannel = fos.getChannel();
                        destChannels.put(key, destChannel);

                        //Write the Header...... Has to be improved
                        IoUtilities.writeLineToChannel(destChannel, getHeader(), Charsets.UTF_8);
                    }

                    StrBuilder outputLine = new StrBuilder();
                    for (String s : tokenList) {
                        StrBuilderUtils.appendEscapedAndQuoted(outputLine, DELIMITER, s);
                    }
                    IoUtilities.writeLineToChannel(destChannel, outputLine.toString(), Charsets.UTF_8);
                }
            } finally {
                closeQuietly(fis);
            }
        }
    } finally {
        outputStreams.forEach(IOUtils::closeQuietly);
    }

}

From source file:com.mgmtp.perfload.perfalyzer.PerfAlyzer.java

private void extractFilesForMarkers() {
    if (!markers.isEmpty()) {
        StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
        tokenizer.setDelimiterChar(';');

        listPerfAlyzerFiles(normalizedDir).stream().filter(perfAlyzerFile -> {
            // GC logs cannot split up here and need to explicitly handle markers later.
            // Load profiles contains the markers themselves and thus need to be filtered out as well.
            String fileName = perfAlyzerFile.getFile().getName();
            return !fileName.contains("gclog") & !fileName.contains("[loadprofile]");
        }).forEach(perfAlyzerFile -> markers.forEach(marker -> {
            try {
                PerfAlyzerFile markerFile = perfAlyzerFile.copy();
                markerFile.setMarker(marker.getName());

                Path destPath = normalizedDir.toPath().resolve(markerFile.getFile().toPath());
                WritableByteChannel destChannel = newByteChannel(destPath, CREATE, WRITE);

                Path srcPath = normalizedDir.toPath().resolve(perfAlyzerFile.getFile().toPath());
                NioUtils.lines(srcPath, UTF_8).filter(s -> {
                    tokenizer.reset(s);
                    long timestamp = Long.parseLong(tokenizer.nextToken());
                    return marker.getLeftMillis() <= timestamp && marker.getRightMillis() > timestamp;
                }).forEach(s -> writeLineToChannel(destChannel, s, UTF_8));
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }//from w w  w.  j  av  a2  s  .co  m
        }));
    }
}