com.vectorprint.configuration.parser.ObjectParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of VectorPrintConfig Show documentation
Show all versions of VectorPrintConfig Show documentation
A library for configuration of applications and parameterization of objects. Settings can be provided in a configfile (file, url, stream), as arguments and programmatically, object parameters can be set using annotations or code. Both provide data type support, serialization, cloning, a help mechanism.
/* Generated By:JavaCC: Do not edit this line. ObjectParser.java */
package com.vectorprint.configuration.parser;
import com.vectorprint.configuration.Configurable;
import com.vectorprint.configuration.EnhancedMap;
import com.vectorprint.configuration.annotation.SettingsAnnotationProcessor;
import com.vectorprint.configuration.annotation.SettingsAnnotationProcessorImpl;
import com.vectorprint.configuration.parameters.Parameterizable;
import com.vectorprint.configuration.parameters.ParameterizableImpl;
import com.vectorprint.configuration.parameters.annotation.ParamAnnotationProcessor;
import com.vectorprint.configuration.parameters.annotation.ParamAnnotationProcessorImpl;
import com.vectorprint.VectorPrintRuntimeException;
import java.util.Map;
import java.util.HashMap;
/**
* A parser for objects that may be Parameterizable 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)
*
*
* For Parameterizable objects parameter annotations will be processed and setup will be called.
* When settings is an instance of EnhancedMap {@link SettingsAnnotationProcessor#initSettings(Object,EnhancedMap)} will be called
*/
public class ObjectParser implements ObjectParserConstants {
public static final ParamAnnotationProcessor processor = new ParamAnnotationProcessorImpl();
public static final SettingsAnnotationProcessor sap = new SettingsAnnotationProcessorImpl();
private void checkKey(Object st, String key) throws ParseException {
if (st instanceof Parameterizable && !((Parameterizable)st).getParameters().containsKey(key)) {
throw new ParseException(String.format("wrong paramter(s) for %s: %s; parameters allowed are: %s",
st.getClass().getSimpleName(), key, ((Parameterizable)st).getParameters()));
}
}
/**
* @param pkg the name of the package without trailing dot or null. When pkg is null a full className is expected for the object
*/
public S parse(String pkg, Map settings, Class clazz)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, ParseException {
Object o = parseAsObject(pkg,settings);
if (clazz.isAssignableFrom(o.getClass())) {
return (S)o;
} else {
throw new ParseException(String.format("%s is not a %s", o.getClass().getName(), clazz.getName()));
}
}
/** 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)
* @param pkg the name of the package without trailing dot or null. When pkg is null a full className is expected for the object
*/
final public Object parseAsObject(String pkg, Map settings) throws ParseException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
Token t = null; Map params = new HashMap(5); String[] kv = null;
t = jj_consume_token(IDENTIFIER);
Object st = (pkg!=null)? Class.forName(pkg + "." + t.image).newInstance() : Class.forName(t.image).newInstance();
if (settings instanceof EnhancedMap) {
sap.initSettings(st,(EnhancedMap)settings);
}
if (st instanceof Configurable) {
((Configurable)st).initSettings(settings);
}
if (st instanceof Parameterizable && !(st instanceof ParameterizableImpl)) {
try {
processor.initParameters((Parameterizable) st);
} catch (Exception ex) {
{if (true) throw new VectorPrintRuntimeException(ex);}
}
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case LEFTPAREN:
case 12:
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 12:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
jj_consume_token(12);
}
jj_consume_token(LEFTPAREN);
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 12:
;
break;
default:
jj_la1[1] = jj_gen;
break label_2;
}
jj_consume_token(12);
}
kv = keyValue();
checkKey(st,kv[0]); params.put(kv[0],kv[1]);
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 12:
;
break;
default:
jj_la1[3] = jj_gen;
break label_4;
}
jj_consume_token(12);
}
kv = keyValue();
checkKey(st,kv[0]); params.put(kv[0],kv[1]);
}
label_5:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 12:
;
break;
default:
jj_la1[4] = jj_gen;
break label_5;
}
jj_consume_token(12);
}
jj_consume_token(RIGHTPAREN);
label_6:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 12:
;
break;
default:
jj_la1[5] = jj_gen;
break label_6;
}
jj_consume_token(12);
}
break;
default:
jj_la1[6] = jj_gen;
;
}
if (st instanceof Parameterizable) {
((Parameterizable)st).setup(params,settings);
}
{if (true) return st;}
throw new Error("Missing return statement in function");
}
final public String[] keyValue() throws ParseException {
String[] kv = new String[2]; Token t = null; StringBuilder v = new StringBuilder();
t = jj_consume_token(KEY);
kv[0] = t.image;
jj_consume_token(EQ);
label_7:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ESC_RIGHTPAREN:
case ESC_KOMMA:
case VALUE:
;
break;
default:
jj_la1[7] = jj_gen;
break label_7;
}
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 VALUE:
t = jj_consume_token(VALUE);
v.append(t.image);
break;
default:
jj_la1[8] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
kv[1]=v.toString();{if (true) return kv;}
throw new Error("Missing return statement in function");
}
/** Generated Token Manager. */
public ObjectParserTokenManager 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[9];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x1000,0x1000,0x200,0x1000,0x1000,0x1000,0x1002,0xd00,0xd00,};
}
/** Constructor with InputStream. */
public ObjectParser(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public ObjectParser(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 ObjectParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; 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 < 9; i++) jj_la1[i] = -1;
}
/** Constructor. */
public ObjectParser(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new ObjectParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; 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 < 9; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public ObjectParser(ObjectParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(ObjectParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 9; 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[13];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 9; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<