author | František Kučera <franta-hg@frantovo.cz> |
Sun, 22 Dec 2013 22:02:44 +0100 | |
branch | v_0 |
changeset 33 | 04db6ccd6c48 |
parent 30 | b7ea47b2d4ca |
child 34 | 9335cf31c0f2 |
permissions | -rw-r--r-- |
26 | 1 |
/** |
2 |
* SQL-DK |
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package info.globalcode.sql.dk.configuration; |
|
19 |
||
30
b7ea47b2d4ca
class for xmlns constants
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
20 |
import static info.globalcode.sql.dk.Xmlns.CONFIGURATION; |
26 | 21 |
import info.globalcode.sql.dk.DKException; |
22 |
import info.globalcode.sql.dk.formatting.Formatter; |
|
23 |
import info.globalcode.sql.dk.formatting.FormatterContext; |
|
24 |
import java.lang.reflect.Constructor; |
|
25 |
import java.lang.reflect.InvocationTargetException; |
|
26 |
import javax.xml.bind.annotation.XmlElement; |
|
27 |
||
28 |
/** |
|
29 |
* |
|
30 |
* @author Ing. František Kučera (frantovo.cz) |
|
31 |
*/ |
|
32 |
public class FormatterDefinition implements NameIdentified { |
|
33 |
||
34 |
private String name; |
|
35 |
private String className; |
|
36 |
||
29
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
37 |
public FormatterDefinition() { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
38 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
39 |
|
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
40 |
public FormatterDefinition(String name, String className) { |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
41 |
this.name = name; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
42 |
this.className = className; |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
43 |
} |
d66858b4b563
more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents:
26
diff
changeset
|
44 |
|
30
b7ea47b2d4ca
class for xmlns constants
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
45 |
@XmlElement(name = "name", namespace = CONFIGURATION) |
26 | 46 |
@Override |
47 |
public String getName() { |
|
48 |
return name; |
|
49 |
} |
|
50 |
||
51 |
public void setName(String name) { |
|
52 |
this.name = name; |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Filter's class. Must implement the |
|
57 |
* <code>info.globalcode.sql.dk.formatting.Formatter</code> interface. |
|
58 |
* Subclassing the |
|
59 |
* <code>info.globalcode.sql.dk.formatting.AbstractFormatter</code> is strongly recommended. |
|
60 |
* The constructor must accept one parameter: |
|
61 |
* <code>info.globalcode.sql.dk.formatting.FormatterContext</code> |
|
62 |
* |
|
63 |
* @return fully qualified class name |
|
64 |
*/ |
|
30
b7ea47b2d4ca
class for xmlns constants
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
65 |
@XmlElement(name = "class", namespace = CONFIGURATION) |
26 | 66 |
public String getClassName() { |
67 |
return className; |
|
68 |
} |
|
69 |
||
70 |
public void setClassName(String className) { |
|
71 |
this.className = className; |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* @param context |
|
76 |
* @return |
|
77 |
* @throws DKException |
|
78 |
*/ |
|
79 |
public Formatter getInstance(FormatterContext context) throws DKException { |
|
80 |
try { |
|
81 |
Constructor constructor = Class.forName(className).getConstructor(context.getClass()); |
|
82 |
||
83 |
Object instance = constructor.newInstance(context); |
|
84 |
if (instance instanceof Formatter) { |
|
85 |
return (Formatter) instance; |
|
86 |
} else { |
|
87 |
throw new DKException("Formatter " + instance + " does not implement the " + Formatter.class.getName() + " interface"); |
|
88 |
} |
|
89 |
} catch (ClassNotFoundException e) { |
|
90 |
throw new DKException("No formatter class with name: " + className, e); |
|
91 |
} catch (NoSuchMethodException e) { |
|
92 |
throw new DKException("Formatter class with no valid constructor: " + className, e); |
|
93 |
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
|
94 |
throw new DKException("Formatter's constructor caused an error: " + className, e); |
|
95 |
} |
|
96 |
} |
|
97 |
} |