View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.database.propertyconfig;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import net.sf.magicproject.clickable.targetable.card.SystemCard;
29  import net.sf.magicproject.database.DatabaseFactory;
30  import net.sf.magicproject.database.Proxy;
31  import net.sf.magicproject.database.data.CollectionData;
32  import net.sf.magicproject.database.data.StringData;
33  import net.sf.magicproject.database.data.TranslatableData;
34  import net.sf.magicproject.database.data.TranslatedCollectionData;
35  import net.sf.magicproject.database.data.TranslatedStringData;
36  import net.sf.magicproject.expression.Expression;
37  import net.sf.magicproject.expression.ExpressionFactory;
38  import net.sf.magicproject.expression.IntValue;
39  import net.sf.magicproject.expression.StringMethod;
40  import net.sf.magicproject.xml.XmlTools;
41  import net.sf.magicproject.xml.XmlParser.Node;
42  
43  /***
44   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
45   * @since 0.90
46   */
47  public class PropertyProxyConfig extends PropertyConfig {
48  
49  	private String delimiterLeft;
50  
51  	private String delimiterRight;
52  
53  	private Expression startingOffset;
54  
55  	private Expression endingOffset;
56  
57  	/***
58  	 * Available values of this property.
59  	 */
60  	public static Map<String, Expression> values = new HashMap<String, Expression>();
61  
62  	/***
63  	 * Create a new instance of this class.
64  	 * 
65  	 * @param node
66  	 *          the node containing definition of value.
67  	 */
68  	public PropertyProxyConfig(Node node) {
69  		super("card." + node.getAttribute("name"));
70  		delimiterLeft = node.getAttribute("delimiter-left");
71  		delimiterRight = node.getAttribute("delimiter-right");
72  		try {
73  			final ByteArrayOutputStream out = new ByteArrayOutputStream(20);
74  			XmlTools.writeAttrOptions(node, "starting-offset", out);
75  			startingOffset = ExpressionFactory
76  					.readNextExpression(new ByteArrayInputStream(out.toByteArray()));
77  			out.reset();
78  			XmlTools.writeAttrOptions(node, "ending-offset", out);
79  			endingOffset = ExpressionFactory
80  					.readNextExpression(new ByteArrayInputStream(out.toByteArray()));
81  			out.reset();
82  		} catch (IOException e) {
83  			e.printStackTrace();
84  		}
85  	}
86  
87  	@Override
88  	boolean isTranslated() {
89  		return DatabaseFactory.propertiesCacheConfig.get(getName()).isTranslated();
90  	}
91  
92  	@Override
93  	public final TranslatableData parseProperty(String cardName, String stream,
94  			Proxy proxy) {
95  		StringMethod.testedString = stream;
96  		int startValue = startingOffset.getValue(null, SystemCard.instance, null);
97  		if (startValue != -1) {
98  			values.put("%last-offset", new IntValue(startValue));
99  			int endValue = endingOffset.getValue(null, SystemCard.instance, null);
100 			if (endValue != -1) {
101 				values.put("%last-offset", new IntValue(endValue));
102 				String property = stream.substring(startValue, endValue).trim();
103 				if (delimiterLeft != null && delimiterRight != null
104 						&& delimiterLeft.length() > 0 && delimiterRight.length() > 0) {
105 					// is a collection
106 					final ArrayList<String> list = new ArrayList<String>();
107 					int lastOffset = 0;
108 					while ((lastOffset = property.indexOf(delimiterLeft, lastOffset)) != -1) {
109 						lastOffset += delimiterLeft.length();
110 						int boundOffset = property.indexOf(delimiterRight, lastOffset);
111 						if (boundOffset < 0) {
112 							break;
113 						}
114 
115 						list.add(proxy.getGlobalValueFromLocal(getName(), property
116 								.substring(lastOffset, boundOffset)));
117 						lastOffset = boundOffset + delimiterRight.length();
118 					}
119 					final String[] array = new String[list.size()];
120 					list.toArray(array);
121 					if (DatabaseFactory.propertiesCacheConfig.get(getName()) != null
122 							&& isTranslated()) {
123 						return new TranslatedCollectionData(this, array);
124 					}
125 					return new CollectionData(this, array);
126 				}
127 				// is a simple value
128 				if (DatabaseFactory.propertiesCacheConfig.get(getName()) != null
129 						&& isTranslated()) {
130 					return new TranslatedStringData(this, proxy.getGlobalValueFromLocal(
131 							getName(), property));
132 				}
133 				return new StringData(this, proxy.getGlobalValueFromLocal(getName(),
134 						property));
135 			}
136 		}
137 		return null;
138 	}
139 }