/* Generated By:JavaCC: Do not edit this line. ParameterizableParserImpl.java */
package com.vectorprint.configuration.generated.parser;
import com.vectorprint.ArrayHelper;
import com.vectorprint.configuration.EnhancedMap;
import com.vectorprint.configuration.annotation.SettingsAnnotationProcessor;
import com.vectorprint.configuration.annotation.SettingsAnnotationProcessorImpl;
import com.vectorprint.configuration.binding.parameters.AbstractParameterizableBinding;
import com.vectorprint.configuration.binding.parameters.ParameterizableSerializer;
import com.vectorprint.configuration.binding.parameters.ParamBindingHelper;
import com.vectorprint.configuration.binding.parameters.ParameterHelper;
import com.vectorprint.configuration.parameters.Parameterizable;
import com.vectorprint.configuration.parameters.Parameter;
import com.vectorprint.VectorPrintRuntimeException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.io.Writer;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
/**
* A parser for Parameterizable objects and / or Configurable in the form
class(param1=v1|v2,param2=v3) separators ("," or ")") can be escaped, example configuration:
*
*
Shape(bordercolor=#663322,color=#11aa55,borderwidth=2,SHAPE=bezier,fill=true,background=false,points=60|20|90|20|75|40,close=false)
*
*
* Annotations will be processed, {@link SettingsAnnotationProcessor#initSettings(Object,EnhancedMap)} will be called on the class and the object.
* Parameterizables and Parameters will be initialized and default values for parameters will initialized from settings, see
* {@link ParameterHelper#findKey(java.lang.String, java.lang.Class, com.vectorprint.configuration.EnhancedMap, com.vectorprint.configuration.binding.parameters.ParameterHelper.SUFFIX)}
*/
public class ParameterizableParserImpl extends AbstractParameterizableBinding> implements ParameterizableParserImplConstants {
private Parameterizable parameterizable = null;
private ParamBindingHelper bindingHelper;
private final SettingsAnnotationProcessor sap = new SettingsAnnotationProcessorImpl();
@Override
public void setBindingHelper(ParamBindingHelper bindingHelper) {
this.bindingHelper = bindingHelper ;
}
@Override
public Parameterizable parseParameterizable() {
try {
return parseAsObject();
} catch(Exception e) {
throw new VectorPrintRuntimeException(e);
}
}
@Override
public TYPE parseAsParameterValue(String valueToParse, Parameter parameter) {
ParameterizableParserImpl parser = new ParameterizableParserImpl(new StringReader(parameter.getKey()+"="+valueToParse));
parser.token_source.SwitchTo(ParameterizableParserImplTokenManager.PARAMMODE);
List kv;
try {
kv = parser.keyValue();
} catch (ParseException ex) {
throw new VectorPrintRuntimeException(ex);
}
kv.remove(0);
return (TYPE)convert(kv, parameter);
}
@Override
public void initParameter(Parameter parameter, List values) {
if (values != null) {
convertAndSet(parameter, values, false);
}
}
protected TYPE convert(List values, Parameter parameter) {
if (values.isEmpty()) {
return null;
}
if (parameter.getValueClass().isArray()) {
if (String[].class.equals(parameter.getValueClass())) {
return (TYPE) ArrayHelper.toArray(values);
} else {
Serializable o = (Serializable) bindingHelper.convert(ArrayHelper.toArray(values), parameter.getValueClass());
return (TYPE) o;
}
} else {
if (values.size()>1) {
throw new VectorPrintRuntimeException(String.format("unable to store %s in %s, it does not support multiple values",values,parameter));
}
if (String.class.equals(parameter.getValueClass())) {
return (TYPE) values.get(0);
} else {
Serializable o = (Serializable) bindingHelper.convert(values.get(0), parameter.getValueClass());
return (TYPE) o;
}
}
}
protected void convertAndSet(Parameter parameter, List values, boolean setDefault) {
Serializable o = convert(values, parameter);
bindingHelper.setValueOrDefault(parameter, o, setDefault);
}
@Override
public void serialize(Parameterizable parameterizable, Writer writer) throws IOException {
writer.write(toConfig(parameterizable).toString());
}
/**
* returns a String in the form simpleClassName(key=value,key2=v1|v2|v3,key3=value)
*
* @param parameterizable
* @return
*/
private StringBuilder toConfig(Parameterizable parameterizable) {
StringBuilder sb = new StringBuilder(10 + parameterizable.getParameters().size() * 15);
sb.append(parameterizable.getClass().getSimpleName());
if (!parameterizable.getParameters().isEmpty()) {
int offset = sb.length();
toConfig(parameterizable.getParameters().values(),sb);
if (sb.length() > offset) {
sb.insert(offset, ParameterizableParserImplConstants.tokenImage[ParameterizableParserImplConstants.LEFTPAREN].substring(1, 2))
.append(ParameterizableParserImplConstants.tokenImage[ParameterizableParserImplConstants.RIGHTPAREN].substring(1, 2));
}
}
return sb;
}
/**
* returns a String in the form key=value,key2=v1|v2|v3,key3=value. Only includes parameters when
* {@link ParameterizableSerializer#include(Parameter)} is true.
*
* @param parameters
* @param sb
* @return
*/
private void toConfig(Collection parameters, StringBuilder sb) {
if (!parameters.isEmpty()) {
boolean del = false;
for (Parameter p : parameters) {
if (include(p)) {
toConfig(p,sb);
sb.append(ParameterizableParserImplConstants.tokenImage[ParameterizableParserImplConstants.KOMMA].substring(1, 2));
del = true;
}
}
if (del) {
sb.deleteCharAt(sb.length() - 1);
}
}
}
/**
* returns a String in the form key=value or key2=v1|v2|v3.
*
* @param p
* @param sb
* @return
*/
private void toConfig(Parameter p, StringBuilder sb) {
sb.append(p.getKey()).append(ParameterizableParserImplConstants.tokenImage[ParameterizableParserImplConstants.EQ].substring(1, 2))
.append(bindingHelper.serializeValue(bindingHelper.getValueToSerialize(p, false)));
}
/** stuf like this should be parsed
Shape(bordercolor=#663322,color=#11aa55,borderwidth=2,SHAPE=bezier,fill=true,background=false,points=60|20|90|20|75|40,close=false)
*/
final public Parameterizable parseAsObject() throws ParseException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
Token t = null; List kv = null;
t = jj_consume_token(IDENTIFIER);
String pkg = getPackageName();
EnhancedMap settings = getSettings();
Class c = (pkg!=null) ? Class.forName(pkg + "." + t.image) : Class.forName(t.image);
if (!Parameterizable.class.isAssignableFrom(c)) {
{if (true) throw new VectorPrintRuntimeException(String.format("%s is not a %s",c.getName(),Parameterizable.class.getName()));}
}
if (settings != null) {
// init static settings
sap.initSettings(c,settings);
}
parameterizable = (Parameterizable)c.newInstance();
sap.initSettings(parameterizable,settings);
initParameterizable(parameterizable);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case LEFTPAREN:
case 14:
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 14:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
jj_consume_token(14);
}
jj_consume_token(LEFTPAREN);
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 14:
;
break;
default:
jj_la1[1] = jj_gen;
break label_2;
}
jj_consume_token(14);
}
kv = keyValue();
checkKey(parameterizable,kv.get(0));
Parameter p = parameterizable.getParameters().get(kv.get(0));
kv.remove(0);
initParameter(p,kv);
label_3:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case KOMMA:
;
break;
default:
jj_la1[2] = jj_gen;
break label_3;
}
jj_consume_token(KOMMA);
label_4:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 14:
;
break;
default:
jj_la1[3] = jj_gen;
break label_4;
}
jj_consume_token(14);
}
kv = keyValue();
checkKey(parameterizable,kv.get(0));
p = parameterizable.getParameters().get(kv.get(0));
kv.remove(0);
initParameter(p,kv);
}
label_5:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 14:
;
break;
default:
jj_la1[4] = jj_gen;
break label_5;
}
jj_consume_token(14);
}
jj_consume_token(RIGHTPAREN);
label_6:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 14:
;
break;
default:
jj_la1[5] = jj_gen;
break label_6;
}
jj_consume_token(14);
}
break;
default:
jj_la1[6] = jj_gen;
;
}
for (Parameter pa : parameterizable.getParameters().values()) {
String key = ParameterHelper.findKey(pa.getKey(), parameterizable.getClass(), getSettings(),ParameterHelper.SUFFIX.set_default);
if (key!=null) {
Serializable values = parseAsParameterValue(getSettings().getProperty(key), pa);
bindingHelper.setValueOrDefault(pa, values, true);
}
key = ParameterHelper.findKey(pa.getKey(), parameterizable.getClass(), getSettings(), ParameterHelper.SUFFIX.set_value);
if (key!=null) {
Serializable values = parseAsParameterValue(getSettings().getProperty(key), pa);
bindingHelper.setValueOrDefault(pa, values, true);
}
}
{if (true) return parameterizable;}
throw new Error("Missing return statement in function");
}
final public List keyValue() throws ParseException {
List kv = new ArrayList(2); Token t = null; String v = null;
t = jj_consume_token(KEY);
kv.add(t.image);
jj_consume_token(EQ);
v = value();
kv.add(v);
label_7:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PIPE:
;
break;
default:
jj_la1[7] = jj_gen;
break label_7;
}
t = jj_consume_token(PIPE);
v = value();
kv.add(v);
}
{if (true) return kv;}
throw new Error("Missing return statement in function");
}
final public String value() throws ParseException {
StringBuilder v = new StringBuilder();Token t = null;
label_8:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ESC_RIGHTPAREN:
case ESC_KOMMA:
case ESC_PIPE:
case VALUE:
;
break;
default:
jj_la1[8] = jj_gen;
break label_8;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ESC_KOMMA:
jj_consume_token(ESC_KOMMA);
v.append(',');
break;
case ESC_RIGHTPAREN:
jj_consume_token(ESC_RIGHTPAREN);
v.append(')');
break;
case ESC_PIPE:
jj_consume_token(ESC_PIPE);
v.append('|');
break;
case VALUE:
t = jj_consume_token(VALUE);
v.append(t.image);
break;
default:
jj_la1[9] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
{if (true) return v.toString();}
throw new Error("Missing return statement in function");
}
/** Generated Token Manager. */
public ParameterizableParserImplTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
public Token token;
/** Next token. */
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[10];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x4000,0x4000,0x200,0x4000,0x4000,0x4000,0x4002,0x800,0x3500,0x3500,};
}
/** Constructor with InputStream. */
public ParameterizableParserImpl(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public ParameterizableParserImpl(java.io.InputStream stream, String encoding) {
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new ParameterizableParserImplTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 10; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 10; i++) jj_la1[i] = -1;
}
/** Constructor. */
public ParameterizableParserImpl(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new ParameterizableParserImplTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 10; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 10; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public ParameterizableParserImpl(ParameterizableParserImplTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 10; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(ParameterizableParserImplTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 10; i++) jj_la1[i] = -1;
}
private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
/** Get the next Token. */
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}
/** Get the specific Token. */
final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}
private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}
private java.util.List jj_expentries = new java.util.ArrayList();
private int[] jj_expentry;
private int jj_kind = -1;
/** Generate ParseException. */
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[15];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 10; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<