property annotations: first version (inherited properties are not working yet) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 11:52:38 +0200
branchv_0
changeset 212d154d6012cbe
parent 211 b5148f646278
child 213 39d154429f7a
property annotations: first version (inherited properties are not working yet)
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
java/sql-dk/src/info/globalcode/sql/dk/configuration/PropertyDeclaration.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sat Aug 15 11:07:50 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sat Aug 15 11:52:38 2015 +0200
     1.3 @@ -25,8 +25,11 @@
     1.4  import info.globalcode.sql.dk.configuration.FormatterDefinition;
     1.5  import info.globalcode.sql.dk.configuration.Properties;
     1.6  import info.globalcode.sql.dk.configuration.Property;
     1.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     1.8 +import info.globalcode.sql.dk.configuration.PropertyDeclarations;
     1.9  import info.globalcode.sql.dk.configuration.TunnelDefinition;
    1.10  import info.globalcode.sql.dk.formatting.ColumnsHeader;
    1.11 +import info.globalcode.sql.dk.formatting.CommonProperties;
    1.12  import info.globalcode.sql.dk.formatting.FakeSqlArray;
    1.13  import info.globalcode.sql.dk.formatting.Formatter;
    1.14  import info.globalcode.sql.dk.formatting.FormatterContext;
    1.15 @@ -184,23 +187,52 @@
    1.16  	}
    1.17  
    1.18  	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
    1.19 -		ColumnsHeader header = constructHeader(
    1.20 -				new HeaderField("name", SQLType.VARCHAR),
    1.21 -				new HeaderField("type", SQLType.VARCHAR),
    1.22 -				new HeaderField("default", SQLType.VARCHAR),
    1.23 -				new HeaderField("description", SQLType.VARCHAR)
    1.24 -		);
    1.25 -		List<Object[]> data = new ArrayList<>();
    1.26 +		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
    1.27 +		try {
    1.28  
    1.29 -		data.add(new Object[]{"TODO", "a", "b", "c"});
    1.30 -		data.add(new Object[]{"TODO", "a", "b", "c"});
    1.31 -		data.add(new Object[]{"TODO", "a", "b", "c"});
    1.32 -		data.add(new Object[]{"TODO", "a", "b", "c"});
    1.33 +			ColumnsHeader header = constructHeader(
    1.34 +					new HeaderField("name", SQLType.VARCHAR),
    1.35 +					new HeaderField("type", SQLType.VARCHAR),
    1.36 +					new HeaderField("default", SQLType.VARCHAR),
    1.37 +					new HeaderField("description", SQLType.VARCHAR)
    1.38 +			);
    1.39 +			List<Object[]> data = new ArrayList<>();
    1.40  
    1.41 -		List<Parameter> parameters = new ArrayList<>();
    1.42 -		parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
    1.43 +			Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
    1.44  
    1.45 -		printTable(formatter, header, "-- formatter properties", parameters, data);
    1.46 +			// TOOD: formatterClass.getDeclaredAnnotation(PropertyDeclarations.class); and separate inherited properties
    1.47 +			// Repeated PropertyDeclaration wrapped in PropertyDeclarations
    1.48 +			PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
    1.49 +			if (properties == null) {
    1.50 +				log.log(Level.WARNING, "Formatter „{0}“  has no declared properties", formatterName);
    1.51 +			} else {
    1.52 +				for (PropertyDeclaration p : properties.value()) {
    1.53 +					data.add(propertyDeclarationToRow(p));
    1.54 +				}
    1.55 +			}
    1.56 +
    1.57 +			// Single PropertyDeclaration
    1.58 +			PropertyDeclaration property = formatterClass.getAnnotation(PropertyDeclaration.class);
    1.59 +			if (property != null) {
    1.60 +				data.add(propertyDeclarationToRow(property));
    1.61 +			}
    1.62 +
    1.63 +			List<Parameter> parameters = new ArrayList<>();
    1.64 +			parameters.add(new NamedParameter("formatter", formatterName, SQLType.VARCHAR));
    1.65 +
    1.66 +			printTable(formatter, header, "-- formatter properties", parameters, data);
    1.67 +		} catch (ClassNotFoundException e) {
    1.68 +			throw new ConfigurationException("Unable to find class " + fd.getClassName() + " of formatter" + fd.getName(), e);
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	private static Object[] propertyDeclarationToRow(PropertyDeclaration p) {
    1.73 +		return new Object[]{
    1.74 +			p.name(),
    1.75 +			CommonProperties.getSimpleTypeName(p.type()),
    1.76 +			p.defaultValue(),
    1.77 +			p.description()
    1.78 +		};
    1.79  	}
    1.80  
    1.81  	private void listTypes() throws FormatterException, ConfigurationException {
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/PropertyDeclaration.java	Sat Aug 15 11:07:50 2015 +0200
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/PropertyDeclaration.java	Sat Aug 15 11:52:38 2015 +0200
     2.3 @@ -44,7 +44,7 @@
     2.4  	 * @return data type of the value: String, numbers, Boolean or Enum
     2.5  	 */
     2.6  	Class type();
     2.7 -
     2.8 +	
     2.9  	/**
    2.10  	 * @return documentation for the users
    2.11  	 */
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java	Sat Aug 15 11:07:50 2015 +0200
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/CommonProperties.java	Sat Aug 15 11:52:38 2015 +0200
     3.3 @@ -17,12 +17,34 @@
     3.4   */
     3.5  package info.globalcode.sql.dk.formatting;
     3.6  
     3.7 +import java.util.Collections;
     3.8 +import java.util.HashMap;
     3.9 +import java.util.Map;
    3.10 +
    3.11  /**
    3.12   *
    3.13   * @author Ing. František Kučera (frantovo.cz)
    3.14   */
    3.15  public class CommonProperties {
    3.16  
    3.17 +	private static final Map<Class, String> TYPE_SIMPLE_NAMES;
    3.18 +
    3.19 +	static {
    3.20 +		Map<Class, String> m = new HashMap<>();
    3.21 +		m.put(Boolean.class, "boolean");
    3.22 +		m.put(String.class, "String");
    3.23 +		m.put(Character.class, "char");
    3.24 +		m.put(Integer.class, "int");
    3.25 +		m.put(Long.class, "long");
    3.26 +		m.put(Double.class, "double");
    3.27 +		TYPE_SIMPLE_NAMES = Collections.unmodifiableMap(m);
    3.28 +	}
    3.29 +
    3.30 +	public static String getSimpleTypeName(Class type) {
    3.31 +		String name = TYPE_SIMPLE_NAMES.get(type);
    3.32 +		return name == null ? type.getName() : name;
    3.33 +	}
    3.34 +
    3.35  	public static final String COLORFUL = "color";
    3.36 -	public static final String COLORFUL_DESCRIPTION = "whether the output should be printed in color (escape sequences)";
    3.37 +	public static final String COLORFUL_DESCRIPTION = "whether the output should be printed in color (ANSI Escape Sequences)";
    3.38  }