1   package eu.fbk.knowledgestore.tool;
2   
3   import java.util.List;
4   import java.util.Map;
5   import java.util.Properties;
6   
7   import com.google.common.base.Preconditions;
8   import com.google.common.base.Strings;
9   import com.google.common.collect.Maps;
10  
11  import org.openrdf.model.Value;
12  import org.openrdf.query.BindingSet;
13  import org.openrdf.query.impl.MapBindingSet;
14  
15  import eu.fbk.knowledgestore.data.Data;
16  import eu.fbk.rdfpro.util.Namespaces;
17  import eu.fbk.rdfpro.util.Statements;
18  
19  final class TestUtil {
20  
21      // Properties manipulation
22  
23      public static <T> T read(final Properties properties, final String name, final Class<T> type) {
24          final T value = read(properties, name, type, null);
25          if (value == null) {
26              throw new IllegalArgumentException("No value for mandatory property '" + name + "'");
27          }
28          return value;
29      }
30  
31      public static <T> T read(final Properties properties, final String name, final Class<T> type,
32              final T defaultValue) {
33          final String value = properties.getProperty(name);
34          if (value == null) {
35              return defaultValue;
36          }
37          try {
38              return Data.convert(value, type);
39          } catch (final Throwable ex) {
40              throw new IllegalArgumentException("Invalid value for property '" + name + "': "
41                      + value + " (" + ex.getMessage() + ")");
42          }
43      }
44  
45      public static void expand(final Properties targetProperties, final Properties defaultProperties) {
46          for (final Map.Entry<Object, Object> entry : defaultProperties.entrySet()) {
47              final String name = (String) entry.getKey();
48              if (!targetProperties.containsKey(name)) {
49                  final String value = (String) entry.getValue();
50                  targetProperties.setProperty(name, value);
51              }
52          }
53      }
54  
55      public static Map<String, Properties> split(final Properties properties) {
56          final Map<String, Properties> map = Maps.newLinkedHashMap();
57          for (final Object key : properties.keySet()) {
58              final String keyString = key.toString();
59              final int index = keyString.indexOf(".");
60              if (index > 0) {
61                  final String name = keyString.substring(0, index);
62                  final String property = keyString.substring(index + 1);
63                  final String value = properties.getProperty(keyString);
64                  Properties subProperties = map.get(name);
65                  if (subProperties == null) {
66                      subProperties = new Properties();
67                      map.put(name, subProperties);
68                  }
69                  subProperties.setProperty(property, value);
70              }
71          }
72          return map;
73      }
74  
75      // TSV and RDF manipulation
76  
77      public static BindingSet decode(final List<String> variables, final String line) {
78          final String[] tokens = line.split("\t");
79          Preconditions.checkArgument(tokens.length == variables.size(), "Wrong number of values ("
80                  + tokens.length + " found, " + variables.size() + " expected) in line: " + line);
81          try {
82              final MapBindingSet bindings = new MapBindingSet();
83              for (int i = 0; i < tokens.length; ++i) {
84                  String token = tokens[i];
85                  if (!Strings.isNullOrEmpty(token)) {
86                      final char ch = token.charAt(0);
87                      token = ch == '\'' || ch == '"' || ch == '<' || ch == '_' ? token : "\""
88                              + token + "\"";
89                      final Value value = Statements.parseValue(token, Namespaces.DEFAULT);
90                      bindings.addBinding(variables.get(i), value);
91                  }
92              }
93              return bindings;
94          } catch (final Throwable ex) {
95              throw new IllegalArgumentException("Could not parse variable values.\nVariables: "
96                      + variables + "\nLine: " + line, ex);
97          }
98      }
99  
100     public static String encode(final List<String> variables, final BindingSet bindings) {
101         final StringBuilder builder = new StringBuilder();
102         for (int i = 0; i < variables.size(); ++i) {
103             if (i > 0) {
104                 builder.append('\t');
105             }
106             final Value value = bindings.getValue(variables.get(i));
107             builder.append(format(value));
108         }
109         return builder.toString();
110     }
111 
112     public static String format(final Iterable<String> variables, final BindingSet bindings,
113             final String separator) {
114         final StringBuilder builder = new StringBuilder();
115         for (final String variable : variables) {
116             final Value value = bindings.getValue(variable);
117             if (value != null) {
118                 builder.append(builder.length() == 0 ? "" : separator);
119                 builder.append(variable);
120                 builder.append('=');
121                 builder.append(format(value));
122             }
123         }
124         return builder.toString();
125     }
126 
127     public static String format(final Value value) {
128         // Emit literal without lang / datatype for easier consumption in analysis tools
129         if (value instanceof Value) { // was instanceof Resource
130             return Statements.formatValue(value, null);
131         } else if (value != null) {
132             return value.stringValue();
133         } else {
134             return "";
135         }
136     }
137 
138 }