View Javadoc

1   /*
2    * Created on 27 févr. 2005
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.test;
22  
23  import java.io.OutputStream;
24  import java.util.Iterator;
25  
26  import net.sf.magicproject.test.IdTest;
27  import net.sf.magicproject.tools.MToolKit;
28  import net.sf.magicproject.xml.XmlConfiguration;
29  import net.sf.magicproject.xml.XmlParser;
30  import net.sf.magicproject.xml.XmlTest;
31  import net.sf.magicproject.xml.XmlToMDB;
32  import net.sf.magicproject.xml.tbs.Tbs;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.82
37   */
38  public class Test implements XmlToMDB {
39  
40  	/***
41  	 * Write the contained test of the specified node <br>
42  	 * <ul>
43  	 * Structure of stream : Data[size]
44  	 * <li>test identifiant [1]</li>
45  	 * <li>tested on identifiant [1]</li>
46  	 * <li>depending on the test [...]</li>
47  	 * </ul>
48  	 * 
49  	 * @param node
50  	 *          the XML test container structure
51  	 * @param out
52  	 *          outputstream where the card structure will be saved
53  	 * @return the amount of written action, so return alway ZERO.
54  	 * @see net.sf.magicproject.test.IdTest
55  	 */
56  	public final int buildMdb(XmlParser.Node node, OutputStream out) {
57  		try {
58  			if (node == null) {
59  				// write the null test
60  				IdTest.TRUE.serialize(out);
61  			} else {
62  				String referenceName = node.getAttribute("ref");
63  				if (referenceName != null) {
64  					// a reference to a declared test
65  					if ("context.test".equals(node.getAttribute("ref").toString())) {
66  						IdTest.CONTEXT_TEST.serialize(out);
67  					} else {
68  						if (Tbs.resolveReferences) {
69  							if (Tbs.referencedTest.get(referenceName) == null) {
70  								XmlConfiguration.error++;
71  								System.out.println("\t>> Referenced test '" + referenceName
72  										+ "' is unknown");
73  								return 0;
74  							}
75  						}
76  						IdTest.REFERENCED_TEST.serialize(out);
77  						MToolKit.writeString(out, referenceName);
78  					}
79  				} else {
80  					final Iterator<?> it = node.iterator();
81  					while (it.hasNext()) {
82  						Object obj = it.next();
83  						if (obj instanceof XmlParser.Node) {
84  							return XmlTest.getTest(((XmlParser.Node) obj).getTag()).buildMdb(
85  									(XmlParser.Node) obj, out);
86  						}
87  					}
88  					// no test
89  					IdTest.TRUE.serialize(out);
90  				}
91  			}
92  		} catch (Throwable e2) {
93  			XmlConfiguration.error++;
94  			System.out.println("\t>> Error found in test '"
95  					+ (node == null ? "null" : node.getTag()) + "' : " + e2.getMessage()
96  					+ ". Context=" + (node == null ? "null" : node.toString()));
97  		}
98  		return 0;
99  	}
100 
101 }