hzsk-corpus-services  1.0
TemplatingCSV2JSON.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package de.uni_hamburg.corpora.conversion;
7 
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Properties;
12 //import com.google.refine.expr.ParsingException;
13 //import org.openrefine;
14 //import com.google.refine.templating.Template;
15 
20 public class TemplatingCSV2JSON {
21 
22  String csvasString;
23  String createdJson;
24  String templateString;
25  String prefixString;
26  String suffixString;
27  String separatorString;
28  /*
29 
30  */
31  Template template;
32 
33  public TemplatingCSV2JSON() {
34 
35  }
36 
37  public String templatingcsv2json(String templateStr, String prefix, String suffix, String separator) throws IOException {
38  templateString = templateStr;
39  prefixString = prefix;
40  suffixString = suffix;
41  separatorString = separator;
42 
43  //try {
44  template = parse(templateString);
45  // } catch (ParsingException e) {
46  // throw new IOException("Missing or bad template", e);
47  // }
48 
49  //writer.toString();
50  //writer.write(_prefix);
51  return createdJson;
52  }
53 
54  public Template parse(String s) {
55  List<Fragment> fragments = new ArrayList<Fragment>();
56 
57  int start = 0, current = 0;
58  while (current < s.length() - 1) {
59  char c = s.charAt(current);
60  char c2 = s.charAt(current + 1);
61  if (c == '\\') {
62  if (c2 == '\\' || c2 == '{' || c2 == '$') {
63  fragments.add(new StaticFragment(s.substring(start, current).concat(Character.toString(c2))));
64  start = current += 2;
65  } else {
66  // Invalid escape - just leave it in the template
67  current += 1;
68  }
69  continue;
70  }
71 
72  if (c == '$' && c2 == '{') {
73  int closeBrace = s.indexOf('}', current + 2);
74  if (closeBrace > current + 1) {
75  String columnName = s.substring(current + 2, closeBrace);
76 
77  if (current > start) {
78  fragments.add(new StaticFragment(s.substring(start, current)));
79  }
80  start = current = closeBrace + 1;
81 
82  /*fragments.add(
83  new DynamicFragment(
84  new FieldAccessorExpr(
85  new FieldAccessorExpr(
86  new VariableExpr("cells"),
87  columnName),
88  "value")));
89 */
90  continue;
91  }
92  } else if (c == '{' && c2 == '{') {
93  int closeBrace = s.indexOf('}', current + 2);
94  if (closeBrace > current + 1 && closeBrace < s.length() - 1 && s.charAt(closeBrace + 1) == '}') {
95  String expression = s.substring(current + 2, closeBrace);
96 
97  if (current > start) {
98  fragments.add(new StaticFragment(s.substring(start, current)));
99  }
100  start = current = closeBrace + 2;
101 
102  fragments.add(
103  new DynamicFragment(
104  parseEvaluable(expression)));
105 
106  continue;
107  }
108  }
109 
110  current++;
111  }
112 
113  if (start < s.length()) {
114  fragments.add(new StaticFragment(s.substring(start)));
115  }
116 
117  return new Template(fragments);
118  }
119 
120  public class Template {
121 
122  protected String _prefix;
123  protected String _suffix;
124  protected String _separator;
125 
126  protected List<Fragment> _fragments;
127 
128  public Template(List<Fragment> fragments) {
129  _fragments = fragments;
130  }
131 
132  }
133 
134  public class Fragment {
135 
136  }
137 
138  class StaticFragment extends Fragment {
139 
140  final public String text;
141 
142  public StaticFragment(String text) {
143  this.text = text;
144  }
145  }
146 
147  class DynamicFragment extends Fragment {
148 
149  final public Evaluable eval;
150 
151  public DynamicFragment(Evaluable eval) {
152  this.eval = eval;
153  }
154  }
155 
159  public interface Evaluable {
160 
167  public Object evaluate(Properties bindings);
168  }
169 
179  public Evaluable parseEvaluable(String s) {
180  String language = "grel";
181 
182  int colon = s.indexOf(':');
183  if (colon >= 0) {
184  language = s.substring(0, colon).toLowerCase();
185  if ("gel".equals(language)) {
186  language = "grel";
187  }
188  }
189 
190  /* LanguageInfo info = s_languages.get(language.toLowerCase());
191  if (info != null) {
192  return info.parser.parse(s.substring(colon + 1));
193  } else {
194  return parseGREL(s);
195  }*/
196  return null;
197  }
198 
199 }
String templatingcsv2json(String templateStr, String prefix, String suffix, String separator)