1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.firemox.xml.magic;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.FileReader;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28
29 import javax.xml.XMLConstants;
30
31 import net.sf.firemox.token.IdConst;
32 import net.sf.firemox.tools.MToolKit;
33
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.FilenameUtils;
36 import org.apache.commons.io.IOUtils;
37 import org.kohsuke.args4j.CmdLineException;
38 import org.kohsuke.args4j.CmdLineParser;
39
40 /***
41 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42 * @since 0.90
43 */
44 public final class Oracle2XmlNoRules {
45
46 /***
47 * May the validated cards (recycled directory) be patched by the new ones?
48 */
49 private static final boolean UPDATE_CARD = false;
50
51 /***
52 * Bounds
53 */
54 private static final int MAXI = Integer.MAX_VALUE;
55
56 private static Options options;
57
58 /***
59 * Prevent this class to be instantiated.
60 */
61 private Oracle2XmlNoRules() {
62 super();
63 }
64
65 /***
66 * @param oracleFile
67 * the oracle format text file.
68 * @param destinationDir
69 * the directory where built card will be placed.
70 * @param recycledDir
71 * the directory where already built card are placed.
72 */
73 @SuppressWarnings("null")
74 public void serialize(File oracleFile, File destinationDir, File recycledDir) {
75 if (!oracleFile.exists() || !oracleFile.isFile()) {
76 System.err.println("The file '" + oracleFile + "' does not exist");
77 System.exit(-1);
78 return;
79 }
80
81 if (!destinationDir.exists() || !destinationDir.isDirectory()) {
82 System.err.println("The destination directory '" + destinationDir
83 + "' does not exist");
84 System.exit(-1);
85 return;
86 }
87 final StringBuilder cardText = new StringBuilder();
88 int nbCard = 0;
89 MToolKit.tbsName = "norules-mtg";
90 try {
91 final BufferedReader in = new BufferedReader(new FileReader(oracleFile));
92 PrintWriter out = null;
93 while (nbCard < MAXI) {
94 String line = in.readLine();
95 if (line == null)
96 break;
97 String cardName = line.trim();
98 if (cardName.length() == 0) {
99 continue;
100 }
101
102
103 String fileName = MToolKit.getExactKeyName(cardName) + ".xml";
104 if (new File(recycledDir, fileName).exists()) {
105 if (UPDATE_CARD) {
106 File patchFile = MToolKit.getFile("tbs/norules-mtg/recycled/"
107 + fileName);
108 File tempFile = File.createTempFile(fileName, "temp");
109 FileUtils.copyFile(patchFile, tempFile);
110 final BufferedReader inExist = new BufferedReader(new FileReader(
111 tempFile));
112 final PrintWriter outExist = new PrintWriter(new FileOutputStream(
113 patchFile));
114 String lineExist = null;
115 boolean found = false;
116 while ((lineExist = inExist.readLine()) != null) {
117 if (!found && lineExist.contains("name=\"")) {
118 lineExist = lineExist
119 .substring(0, lineExist.indexOf("name=\""))
120 + "name=\""
121 + cardName
122 + lineExist.substring(lineExist.indexOf("\"", lineExist
123 .indexOf("name=\"")
124 + "name=\"".length() + 2));
125 found = true;
126 }
127 outExist.println(lineExist);
128 }
129 IOUtils.closeQuietly(inExist);
130 IOUtils.closeQuietly(outExist);
131
132 if (!found) {
133 System.err.println(">> Error patching card '" + cardName + "'");
134
135
136 }
137 }
138 skipCard(in);
139 continue;
140 }
141
142 out = new PrintWriter(new FileOutputStream(new File(destinationDir,
143 fileName)));
144
145
146 cardText.setLength(0);
147 cardText.append("<!--\n\t");
148
149 line = in.readLine().trim().replaceAll("//(.*//)", "").toLowerCase();
150 cardText.append('\t').append(line).append("\n");
151
152 while (true) {
153 line = in.readLine();
154 if (line == null || line.length() == 0)
155 break;
156 line = line.replaceAll("--", "");
157 cardText.append('\t').append(line).append('\n');
158 }
159
160 cardText.append(" -->");
161 out.println("<?xml version='1.0'?>");
162
163 out.print("<card xmlns='");
164 out.println(IdConst.NAME_SPACE + "'");
165 out.print("\txmlns:xsi='");
166 out.print(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
167 out.println("'");
168 out.print("\txsi:schemaLocation='");
169 out.println(IdConst.NAME_SPACE + " ../../" + IdConst.TBS_XSD + "'");
170 out.println("\tname=\"" + cardName + "\">");
171 out.println("<rules-author-comment>riclas</rules-author-comment>\n");
172 out.println(cardText.toString());
173 out.println("</card>");
174 IOUtils.closeQuietly(out);
175 nbCard++;
176 }
177 } catch (FileNotFoundException e) {
178 System.err.println("Error openning file '" + oracleFile + "' : " + e);
179 } catch (IOException e) {
180 System.err.println("IOError reading file '" + oracleFile + "' : " + e);
181 }
182 System.out.println("Success Parsing : " + nbCard + " card(s)");
183 }
184
185 /***
186 * <ul>
187 * Argument are (in this order :
188 * <li>Oracle source file
189 * <li>destination directory
190 * </ul>
191 *
192 * @param args
193 */
194 public static void main(String... args) {
195 options = new Options();
196 final CmdLineParser parser = new CmdLineParser(options);
197 try {
198 parser.parseArgument(args);
199 } catch (CmdLineException e) {
200
201 if (!options.isHelp()) {
202 System.out.println("Wrong parameters : " + e.getMessage());
203 } else {
204 System.out.println("Usage");
205 }
206 parser.setUsageWidth(100);
207 parser.printUsage(System.out);
208 System.exit(-1);
209 return;
210 }
211
212 if (options.isVersion()) {
213
214 System.out.println("Version is " + IdConst.VERSION);
215 System.exit(-1);
216 return;
217 }
218
219 if (options.isHelp()) {
220
221 System.out.println("Usage");
222 parser.setUsageWidth(100);
223 parser.printUsage(System.out);
224 System.exit(-1);
225 return;
226 }
227
228
229 final String oracle = options.getOracleFile();
230
231
232 String destination = FilenameUtils.separatorsToWindows(options
233 .getDestination());
234 if (!destination.endsWith("/")) {
235 destination += "/";
236 }
237
238
239 try {
240 new File(destination).mkdirs();
241 } catch (Exception e) {
242
243 }
244 new Oracle2XmlNoRules().serialize(MToolKit.getFile(oracle), MToolKit
245 .getFile(destination), MToolKit.getFile("tbs/norules-mtg/recycled/"));
246 }
247
248 /***
249 * @param in
250 */
251 private void skipCard(BufferedReader in) throws IOException {
252 String line = in.readLine();
253 while (line != null && line.length() > 0) {
254 line = in.readLine();
255 }
256 }
257 }