All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vectorprint.configuration.generated.parser.ParameterizableParserImpl Maven / Gradle / Ivy

Go to download

This project is about configuration of applications and about parameterization of Objects. This library offers annotations (and annotation processors), parsers, typing, observing changes, serialization, cloning and more when working with settings and/or object parameters. Settings and its features can be declared using an xml format. The library offers syntax support for settings and parameters in a loosely coupled manner. You are not restricted to built in syntax, you can provide your own. At runtime this library tracks keys for which a default is used because they are not found in settings. Also it tracks unused keys. You can stack features for settings such as caching, preparing keys and values, readonlyness, threadsafety, helpsupport, reading / parsing from input. You can easily develop your own features for settings.

There is a newer version: 12.2
Show newest version
/* ParameterizableParserImpl.java */
/* Generated by: ParserGeneratorCC: 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)) {
         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_f() : jj_ntk) {
    case LEFTPAREN:
    case 14:{
      label_1:
      while (true) {
        switch (jj_ntk == -1 ? jj_ntk_f() : 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_f() : 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_f() : 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_f() : 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_f() : 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_f() : 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 ("" != null) return parameterizable;}
    throw new IllegalStateException ("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_f() : 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 ("" != null) return kv;}
    throw new IllegalStateException ("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_f() : 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_f() : 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 ("" != null) return v.toString();}
    throw new IllegalStateException ("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.
   * @param stream input stream
   */
  public ParameterizableParserImpl(final java.io.InputStream stream) {
	  this(stream, null);
  }

  /**
   * Constructor with InputStream and supplied encoding
   * @param stream input stream
   * @param encoding charset to be used
   */
  public ParameterizableParserImpl(final java.io.InputStream stream, final java.nio.charset.Charset encoding) {
   jj_input_stream = new SimpleCharStream(stream, encoding, 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
   * @param stream input stream
   */
  public void ReInit(final java.io.InputStream stream) {
	  ReInit(stream, null);
  }
  /**
   * Reinitialise
   * @param stream input stream
   * @param encoding charset to be used
   */
  public void ReInit(final java.io.InputStream stream, final java.nio.charset.Charset encoding) {
    jj_input_stream.reInit(stream, encoding, 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 InputStream.
   * @param stream char stream
   */
  public ParameterizableParserImpl(final 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
   * @param stream char stream
   */
  public void ReInit(final java.io.Reader stream) {
	if (jj_input_stream == null) {
	  jj_input_stream = new SimpleCharStream(stream, 1, 1);
	} else {
	  jj_input_stream.reInit(stream, 1, 1);
  }
	if (token_source == null) {
 token_source = new ParameterizableParserImplTokenManager(jj_input_stream);
	}

	 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.
   * @param tm Token manager to use
   */
  public ParameterizableParserImpl(final 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
   * @param tm Token manager to use
   */
  public void ReInit(final 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(final int kind) throws ParseException {
    final Token oldToken = token;
    if (token.next != null)
      token = token.next;
    else {
      token.next = token_source.getNextToken();
      token = token.next;
    }
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }


  /**
   * @return the next Token.
   */
  public final Token getNextToken() {
   if (token.next != null)
     token = token.next;
   else
     token = token.next = token_source.getNextToken();
	 jj_ntk = -1;
	 jj_gen++;
	 return token;
  }

  /**
   * @param index index to be retrieved
   * @return the specific Token.
   */
  public final Token getToken(final int index) {
    Token t = token;
    for (int i = 0; i < index; i++) {
      if (t.next == null)
        t.next = token_source.getNextToken();
      t = t.next;
    }
    return t;
  }

  private int jj_ntk_f() {
    jj_nt = token.next;
    if (jj_nt == null) {
      token.next = token_source.getNextToken();
      jj_ntk = token.next.kind;
      return jj_ntk;
    }
    jj_ntk = jj_nt.kind;
    return jj_ntk;
  }

  private java.util.List jj_expentries = new java.util.ArrayList<>();
  private int[] jj_expentry;
  private int jj_kind = -1;

  /**
   * Generate ParseException.
   * @return new Exception object. Never null
   */
  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<false.
   */
  public final boolean trace_enabled() {
    return false;
  }

  /** Enable tracing. */
  public final void enable_tracing() {}

  /** Disable tracing. */
  public final void disable_tracing() {}

    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy