1 package net.logAnalyzer.converters; 2 3 import java.lang.reflect.Constructor; 4 5 import net.logAnalyzer.patternParser.PatternParsingException; 6 7 /*** 8 * This class contains the definition of a converter. 9 * 10 * @author Karim REFEYTON 11 * @version 0.1 12 */ 13 public class ConverterDefinition { 14 /*** 15 * Key identifying the converter type. 16 */ 17 private String key; 18 19 /*** 20 * User friendly label for the converter. 21 */ 22 private String label; 23 24 /*** 25 * LAConverter class. 26 */ 27 private Class converterClass; 28 29 /*** 30 * LAConverter has option if <code>true</code>. 31 */ 32 private boolean option; 33 34 /*** 35 * LAConverter is a messageLabel converter if <code>true</code>. 36 */ 37 private boolean message; 38 39 public ConverterDefinition(String key, String classname, boolean option, 40 boolean message, String label) throws ClassNotFoundException { 41 this.key = key; 42 this.label = label; 43 this.converterClass = ClassLoader.getSystemClassLoader().loadClass( 44 classname); 45 this.option = option; 46 this.message = message; 47 } 48 49 public ConverterDefinition(String key, Class converterClass, 50 boolean option, String label) { 51 this.key = key; 52 this.label = label; 53 this.converterClass = converterClass; 54 this.option = option; 55 } 56 57 /*** 58 * Return the definition key. 59 * 60 * @return The definition key. 61 */ 62 public String getKey() { 63 return this.key; 64 } 65 66 /*** 67 * Returns the converter label. 68 * 69 * @return Converter label. 70 */ 71 public String getLabel() { 72 return this.label; 73 } 74 75 /*** 76 * Returns the converter class. 77 * 78 * @return Converter class. 79 */ 80 public Class getConverterClass() { 81 return this.converterClass; 82 } 83 84 /*** 85 * Returns <tt>true</tt> if the converter has options. 86 * 87 * @return <tt>true</tt> if the converter has options; <tt>false</tt> 88 * otherwise. 89 */ 90 public boolean hasOption() { 91 return this.option; 92 } 93 94 /*** 95 * Returns <tt>true</tt> if the converter is a messageLabel converter. 96 * <p> 97 * A messageLabel is a special field which could be extended during parsing 98 * process. 99 * <p> 100 * For example, if you log a Java exception, the exception may be written on 101 * more than one line, so the parsing process has to concat several lines to 102 * get the complete messageLabel. 103 * 104 * @return <tt>true</tt> if the converter is used to parse a messageLabel; 105 * <tt>false</tt> otherwise. 106 */ 107 public boolean isMessage() { 108 return this.message; 109 } 110 111 /*** 112 * Creates a new converter instance. 113 * 114 * @param value 115 * Name of the converter. 116 * @return New instance. 117 * @throws PatternParsingException 118 */ 119 public LAConverter createConverterInstance(String value) 120 throws PatternParsingException { 121 try { 122 Class[] classes = new Class[] { ConverterDefinition.class, 123 String.class }; 124 Constructor constructor = converterClass.getConstructor(classes); 125 Object[] parameters = new Object[] { this, value }; 126 return (LAConverter) constructor.newInstance(parameters); 127 } catch (Exception e) { 128 throw new PatternParsingException(e); 129 } 130 } 131 }