/*
* Copyright 2014 Eediom Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.araqne.logdb.query.parser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.araqne.logdb.AbstractQueryCommandParser;
import org.araqne.logdb.QueryCommand;
import org.araqne.logdb.QueryContext;
import org.araqne.logdb.QueryErrorMessage;
import org.araqne.logdb.QueryParseException;
import org.araqne.logdb.query.command.ToJson;
public class ToJsonParser extends AbstractQueryCommandParser {
public ToJsonParser() {
setDescriptions("Convert specified fields to json string.", "주어진 필드 값들을 json 포맷의 텍스트로 변환합니다.");
setOptions("output", OPTIONAL, "Specify output field name. Default value is '_json'",
"json 포맷으로 변환한 결과물이 저장될 필드를 지정합니다. 지정하지 않을 경우 _json 필드에 저장합니다.");
}
@Override
public String getCommandName() {
return "tojson";
}
@Override
public Map<String, QueryErrorMessage> getErrorMessages() {
Map<String, QueryErrorMessage> m = new HashMap<String, QueryErrorMessage>();
m.put("22300", new QueryErrorMessage("missing-field", "필드가 없습니다."));
return m;
}
@Override
public QueryCommand parse(QueryContext context, String commandString) {
if (commandString.trim().endsWith(","))
// throw new QueryParseException("missing-field",
// commandString.length());
throw new QueryParseException("22300", getCommandName().length() + 1, commandString.length() - 1, null);
ParseResult r = QueryTokenizer.parseOptions(context, commandString, getCommandName().length(), Arrays.asList("output"),
getFunctionRegistry());
@SuppressWarnings("unchecked")
Map<String, String> options = (Map<String, String>) r.value;
String output = options.get("output");
if (output == null)
output = "_json";
QueryTokens tokens = QueryTokenizer.tokenize(commandString.substring(r.next));
List<String> fields = new ArrayList<String>();
List<QueryToken> queryFields = tokens.subtokens(0, tokens.size());
for (QueryToken token : queryFields) {
if (!token.token.contains(",")) {
fields.add(token.token.trim());
continue;
}
StringTokenizer tok = new StringTokenizer(token.token, ",");
while (tok.hasMoreTokens())
fields.add(tok.nextToken().trim());
}
return new ToJson(fields, output);
}
}