##### Coverage Report - org.jbehave.core.model.TableTransformers

Classes in this File | Line Coverage | Branch Coverage | Complexity
TableTransformers |
100% | 10/10
100% | 2/2
| 2.6;2.6
TableTransformers$FromLandscape |
96% | 28/29
83% | 10/12
| 2.6;2.6
TableTransformers$TableTransformer |
N/A | N/A
N/A | N/A
| 2.6;2.6

1 |  |
     package org.jbehave.core.model;
2 |  |
3 |  |
     import java.util.ArrayList;
4 |  |
     import java.util.HashMap;
5 |  |
     import java.util.LinkedHashMap;
6 |  |
     import java.util.List;
7 |  |
     import java.util.Map;
8 |  |
     import java.util.Properties;
9 |  |
10 |  |
     import static java.lang.Boolean.parseBoolean;
11 |  |
12 |  |
     /**
13 |  |
      * <p>
14 |  |
      * Facade responsible for transforming table string representations. It allows
15 |  |
      * the registration of several {@link TableTransformer} instances by name.
16 |  |
      * </p>
17 |  |
      * <p>
18 |  |
      * Some Transformers are provided out-of-the-box:
19 |  |
      * <ul>
20 |  |
      * <li>{@link TableTransformers.FromLandscape FromLandscape}: registered under
21 |  |
      * name {@link TableTransformers#FROM_LANDSCAPE}</li>
22 |  |
      * </ul>
23 |  |
      * </p>
24 |  |
      */
25 |  |
     public class TableTransformers {
26 |  |
27 |  |
         public static final String FROM_LANDSCAPE = "FROM_LANDSCAPE";
28 | 1874 |
         private final Map<String, TableTransformer> transformers = new HashMap<String, TableTransformer>();
29 |  |
30 | 1874 |
         public TableTransformers() {
31 | 1874 |
             useTransformer(FROM_LANDSCAPE, new FromLandscape());
32 | 1874 |
         }
33 |  |
34 |  |
         public String transform(String transformerName, String tableAsString, Properties properties) {
35 | 5 |
             TableTransformer transformer = transformers.get(transformerName);
36 | 5 |
             if (transformer != null) {
37 | 4 |
                 return transformer.transform(tableAsString, properties);
38 |  |
             }
39 | 1 |
             return tableAsString;
40 |  |
         }
41 |  |
42 |  |
         public void useTransformer(String name, TableTransformer transformer) {
43 | 1876 |
             transformers.put(name, transformer);
44 | 1876 |
         }
45 |  |
46 |  |
         public interface TableTransformer {
47 |  |
             String transform(String tableAsString, Properties properties);
48 |  |
         }
49 |  |
50 | 1874 |
         public static class FromLandscape implements TableTransformer {
51 |  |
52 |  |
             private static final String ROW_SEPARATOR = "\n";
53 |  |
54 |  |
             public String transform(String tableAsString, Properties properties) {
55 | 2 |
                 boolean trim = parseBoolean(properties.getProperty("trim", "true"));
56 | 2 |
                 String ignorableSeparator = properties.getProperty("ignorableSeparator", "|--");
57 | 2 |
                 String headerSeparator = properties.getProperty("headerSeparator", "|");
58 | 2 |
                 String valueSeparator = properties.getProperty("valueSeparator", "|");
59 | 2 |
                 Map<String, List<String>> data = new LinkedHashMap<String, List<String>>();
60 | 2 |
                 String[] rowsAsString = tableAsString.split(ROW_SEPARATOR);
61 | 6 |
                 for (int row = 0; row < rowsAsString.length; row++) {
62 | 4 |
                     String rowAsString = rowsAsString[row];
63 | 4 |
                     if (rowAsString.startsWith(ignorableSeparator) || rowAsString.length() == 0) {
64 |  |
                         // skip ignorable or empty lines
65 | 0 |
                         continue;
66 |  |
                     } else {
67 | 4 |
                         List<String> values = TableUtils.parseRow(rowAsString, valueSeparator, trim);
68 | 4 |
                         String header = values.get(0);
69 | 4 |
                         List<String> rowValues = new ArrayList<String>(values);
70 | 4 |
                         rowValues.remove(0);
71 | 4 |
                         data.put(header, rowValues);
72 |  |
                     }
73 |  |
                 }
74 | 2 |
                 StringBuffer sb = new StringBuffer();
75 | 2 |
                 int numberOfRows = 1;
76 | 2 |
                 sb.append(headerSeparator);
77 | 2 |
                 for (String header : data.keySet()) {
78 | 4 |
                     sb.append(header).append(headerSeparator);
79 | 4 |
                     numberOfRows = data.get(header).size();
80 |  |
                 }
81 | 2 |
                 sb.append(ROW_SEPARATOR);
82 | 6 |
                 for (int r = 0; r < numberOfRows; r++) {
83 | 4 |
                     sb.append(valueSeparator);
84 | 4 |
                     for (List<String> rows : data.values()) {
85 | 8 |
                         sb.append(rows.get(r)).append(valueSeparator);
86 |  |
                     }
87 | 4 |
                     sb.append(ROW_SEPARATOR);
88 |  |
                 }
89 | 2 |
                 return sb.toString();
90 |  |
             }
91 |  |
92 |  |
         }
93 |  |
94 |  |
     }