View Javadoc

1   /*
2    * Created on Dec 29, 2004
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  }