1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.xml;
22
23 import java.io.IOException;
24
25 import net.sf.magicproject.token.IdConst;
26 import net.sf.magicproject.tools.MToolKit;
27
28 import org.xml.sax.SAXException;
29
30 /***
31 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
32 * @since 0.82
33 */
34 public class XmlDeckTranslator {
35
36 /***
37 * Constructor.
38 *
39 * @param tbsName
40 * An input stream containing a complete e.g. configuration file
41 * @exception SAXException
42 * @exception IOException
43 */
44 public XmlDeckTranslator(String tbsName) throws SAXException, IOException {
45 if (parser == null) {
46 parser = new XmlParser();
47 }
48 synchronized (parser) {
49 String ori = IdConst.TBS_DIR + "/" + tbsName + "/deck-translation.xml";
50 config = parser.parse(MToolKit.getResourceAsStream(ori));
51 }
52 }
53
54 /***
55 * Return the translated card name of specified original card name.
56 *
57 * @param cardName
58 * the name to translate.
59 * @return the translated card name
60 */
61 public String convert(String cardName) {
62 String cardNameRec = cardName;
63 for (Object obj : config) {
64 if (obj instanceof XmlParser.Node) {
65 try {
66 cardNameRec = (String) XmlDeckTranslator.class.getMethod(
67 ((XmlParser.Node) obj).getTag(), XmlParser.Node.class,
68 String.class).invoke(this, obj, cardNameRec);
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72 }
73 }
74 return cardNameRec;
75 }
76
77 /***
78 * @param node
79 * the node containing the replacement expressions.
80 * @param cardName
81 * the unconverted card name.
82 * @return the converted name.
83 */
84 public String replace(XmlParser.Node node, String cardName) {
85 return cardName.replace(node.getAttribute("this"), node.getAttribute("by"));
86 }
87
88 /***
89 *
90 */
91 private static XmlParser parser;
92
93 /***
94 *
95 */
96 private static XmlParser.Node config;
97 }