Skip to content

Commit 25c8582

Browse files
committed
Some refactoring.
1 parent 0449d07 commit 25c8582

File tree

4 files changed

+138
-60
lines changed

4 files changed

+138
-60
lines changed

lua-sample-app/converter.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
echo "Converts the XML files into the current directory to Lua files, so that they can be used by the app.lua application."
33
echo ""
4-
JAR="../target/xml2lua-1.0.jar"
4+
JAR="../target/xml2lua-1.0.0.jar"
55
if [ ! -f "$JAR" ];
66
then
77
echo "$JAR package not found. Considering you have maven installed, execute the following command at the upper dir: mvn package" >&2

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<name>Xml2Lua</name>
88
<description>Converts XML files to Lua format.</description>
99
<url>http://manoelcampos.github.io/JavaXml2Lua</url>
10-
<version>1.0</version>
10+
<version>1.0.0</version>
1111
<packaging>jar</packaging>
1212

1313
<organization>
@@ -220,6 +220,24 @@
220220
</dependency>
221221
</dependencies>
222222
</plugin>
223+
224+
<plugin>
225+
<groupId>org.apache.maven.plugins</groupId>
226+
<artifactId>maven-javadoc-plugin</artifactId>
227+
<version>2.10.4</version>
228+
<executions>
229+
<execution>
230+
<id>attach-javadocs</id>
231+
<goals>
232+
<goal>jar</goal>
233+
</goals>
234+
</execution>
235+
</executions>
236+
237+
<configuration>
238+
<failOnError>false</failOnError>
239+
</configuration>
240+
</plugin>
223241
</plugins>
224242
</build>
225243
</project>

src/main/java/com/manoelcampos/xml2lua/Xml2Lua.java

Lines changed: 115 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,55 @@
1919
*/
2020
package com.manoelcampos.xml2lua;
2121

22-
import java.io.BufferedWriter;
23-
import java.io.File;
24-
import java.io.FileWriter;
25-
import java.io.IOException;
22+
import org.w3c.dom.*;
23+
import org.xml.sax.SAXException;
24+
import org.xml.sax.SAXParseException;
2625

2726
import javax.xml.parsers.DocumentBuilder;
2827
import javax.xml.parsers.DocumentBuilderFactory;
2928
import javax.xml.parsers.ParserConfigurationException;
30-
31-
import org.w3c.dom.Document;
32-
import org.w3c.dom.Element;
33-
import org.w3c.dom.NamedNodeMap;
34-
import org.w3c.dom.Node;
35-
import org.w3c.dom.NodeList;
36-
import org.xml.sax.SAXException;
37-
import org.xml.sax.SAXParseException;
29+
import java.io.BufferedWriter;
30+
import java.io.File;
31+
import java.io.FileWriter;
32+
import java.io.IOException;
3833

3934
/**
4035
* Converts XML files to <a href="http://lua.org">Lua</a> files.
4136
*
4237
* @author Manoel Campos da Silva Filho
4338
*/
4439
public class Xml2Lua {
40+
/** @see #getXmlFilePath() */
41+
private final String xmlFilePath;
42+
/** @see #isPrintLuaCode() */
43+
private boolean printLuaCode;
44+
/** @see #isUseFirstXmlTagAsLuaTableName() */
45+
private boolean useFirstXmlTagAsLuaTableName;
4546

4647
/**
47-
* Instantiates the class to parse an XML file, generating a Lua file
48-
* with the same name.
48+
* Instantiates the parser to convert a XML file to a Lua file.
4949
*
50-
* @param xmlFilePath Path to the XML file to be parsed
51-
* @param printLuaCode if the generated Lua code must be printed or not
52-
* @param useFirstXmlTagAsLuaTableName if true, indicates the name of the first
53-
* tag into the XML file will be used as the name of the
54-
* Lua table to be generated.
55-
* If false, the name of this first tag will be ignored
56-
* and a lua "return" command will be used instead.
57-
* This way, the Lua developer who will use the generated Lua file
58-
* can define the name he/she wants to use for the generated Lua table.
50+
* @param xmlFilePath path to the XML file to be parsed
51+
*/
52+
public Xml2Lua(final String xmlFilePath) {
53+
this.xmlFilePath = xmlFilePath;
54+
this.printLuaCode = true;
55+
this.useFirstXmlTagAsLuaTableName = false;
56+
}
57+
58+
/**
59+
* Convert the XML file to Lua.
60+
* @return true if the conversion was successful, false otherwise.
5961
*/
60-
public Xml2Lua(final String xmlFilePath, final boolean printLuaCode, final boolean useFirstXmlTagAsLuaTableName) {
62+
public boolean convert() {
6163
try {
6264
final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
6365
final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
6466
final Document doc = docBuilder.parse(new File(xmlFilePath));
6567

6668
//Normalizes text representation
6769
doc.getDocumentElement().normalize();
68-
final String luaTableName = getLuaTableName(useFirstXmlTagAsLuaTableName, doc);
70+
final String luaTableName = getLuaTableName(doc);
6971
final StringBuilder luaCode = new StringBuilder(luaTableName + " {\n");
7072

7173
//Gets the attributes of the XML root node.
@@ -76,46 +78,32 @@ public Xml2Lua(final String xmlFilePath, final boolean printLuaCode, final boole
7678
luaCode.append(convertXmlChildNodesToLua(nodes, ""));
7779
luaCode.append("}");
7880

79-
final String luaFileName = xmlFilePath.replaceFirst("\\.xml", ".lua");
80-
final FileWriter file = new FileWriter(luaFileName);
81+
final FileWriter file = new FileWriter(getLuaFileName());
8182
try (BufferedWriter out = new BufferedWriter(file)) {
8283
out.write(luaCode.toString());
8384
}
8485

8586
if (printLuaCode) {
8687
System.out.println(luaCode.toString());
8788
}
88-
System.out.printf("\nLua file %s generated successfully from the %s.\n\n", luaFileName, xmlFilePath);
89-
} catch (SAXParseException err) {
90-
System.out.println("** Parsing error" + ", line "
91-
+ err.getLineNumber() + ", uri " + err.getSystemId());
92-
System.out.println(" " + err.getMessage());
89+
90+
return true;
9391
} catch (SAXException | ParserConfigurationException | IOException e) {
9492
throw new RuntimeException(e);
9593
}
9694
}
9795

98-
private String getLuaTableName(boolean useFirstXmlNodeAsLuaTableName, Document doc) {
99-
if (useFirstXmlNodeAsLuaTableName) {
100-
return doc.getDocumentElement().getNodeName() + " = ";
101-
}
102-
103-
return "return ";
104-
}
105-
10696
/**
10797
* Converts the child elements from a given XML node to the corresponding Lua code.
10898
*
10999
* @param nodes nodes child nodes from a given XML node
110100
* @param space a string containing the amount of spaces to be used to indent the generated Lua code
111101
* @return a StringBuilder containing the Lua code generated for the child elements of the given XML node
112102
*/
113-
private static StringBuilder convertXmlChildNodesToLua(final NodeList nodes, String space) {
103+
private static StringBuilder convertXmlChildNodesToLua(final NodeList nodes, final String space) {
114104
final StringBuilder sb = new StringBuilder();
115-
space += " ";
116105
for (int i = 0; i < nodes.getLength(); i++) {
117-
final Node node = nodes.item(i);
118-
sb.append(convertXmlNodeToLua(space, node));
106+
sb.append(convertXmlNodeToLua(space + " ", nodes.item(i)));
119107
}
120108

121109
return sb;
@@ -124,7 +112,7 @@ private static StringBuilder convertXmlChildNodesToLua(final NodeList nodes, Str
124112
/**
125113
* Converts a XML node to the corresponding Lua code.
126114
*
127-
* @param node the XML node to convert to Lua code
115+
* @param node the XML node to convert to Lua code
128116
* @param space a string containing the amount of spaces to be used to indent the generated Lua code
129117
* @return a StringBuilder containing the Lua code generated for the given XML node
130118
*/
@@ -143,7 +131,7 @@ private static StringBuilder convertXmlNodeToLua(final String space, final Node
143131
/**
144132
* Converts a XML node with doesn't have any child element to the corresponding Lua code.
145133
*
146-
* @param node the XML node to convert to Lua code
134+
* @param node the XML node to convert to Lua code
147135
* @param space a string containing the amount of spaces to be used to indent the generated Lua code
148136
* @return a StringBuilder containing the Lua code generated for the given XML node
149137
*/
@@ -155,7 +143,7 @@ private static StringBuilder convertXmlNodeWithNoChildToLua(final Element node,
155143
final NamedNodeMap attrs = node.getAttributes();
156144
if (attrs.getLength() == 0) {
157145
sb.append(node.getNodeName())
158-
.append(" = \"").append(value).append("\", ");
146+
.append(" = \"").append(value).append("\", ");
159147
return sb;
160148
}
161149

@@ -186,7 +174,7 @@ private static String getValueOfFirstXmlChildNode(final Element node) {
186174
/**
187175
* Converts a XML node with has children elements to the corresponding Lua code.
188176
*
189-
* @param node the XML node to convert to Lua code
177+
* @param node the XML node to convert to Lua code
190178
* @param space a string containing the amount of spaces to be used to indent the generated Lua code
191179
* @return a StringBuilder containing the Lua code generated for the given XML node
192180
*/
@@ -210,13 +198,13 @@ private static Node getFirstAttributeFromXmlNode(final Element node) {
210198
* Generates the Lua code to start a Lua sub-table (a tabela which belongs to another table),
211199
* from a given XML node.
212200
*
213-
* @param node the XML node to generate the code to start a Lua sub-table
201+
* @param node the XML node to generate the code to start a Lua sub-table
214202
* @param xmlNodeIdAttrAsLuaTableIndex if true and the XML node has an attribute
215-
* named "id", such an attribute is used as the indexes
216-
* of the elements of the Lua sub-table to generate.
217-
* If it's false and there is an attribute named "id",
218-
* it will be included as an attribute inside the Lua sub-table.
219-
* @param space a string containing the amount of spaces to be used to indent the generated Lua code
203+
* named "id", such an attribute is used as the indexes
204+
* of the elements of the Lua sub-table to generate.
205+
* If it's false and there is an attribute named "id",
206+
* it will be included as an attribute inside the Lua sub-table.
207+
* @param space a string containing the amount of spaces to be used to indent the generated Lua code
220208
* @return a String containing the Lua code for the beginning of a lua sub-table generated from the XML node
221209
*/
222210
private static String generateLuaSubTableFromXmlNode(final Element node, final boolean xmlNodeIdAttrAsLuaTableIndex, final String space) {
@@ -228,7 +216,7 @@ private static String generateLuaSubTableFromXmlNode(final Element node, final b
228216
}
229217

230218
return space + "{\n" + space + space +
231-
firstAttr.getNodeName() + "=" + firstAttr.getNodeValue();
219+
firstAttr.getNodeName() + "=" + firstAttr.getNodeValue();
232220
}
233221

234222
return space + " {\n" + space;
@@ -256,7 +244,78 @@ as the index of the Lua (sub)table.
256244
.append(attrs.item(i).getNodeValue()).append("', ");
257245
}
258246
}
259-
247+
260248
return sb.toString();
261249
}
250+
251+
/**
252+
* Gets the name of the Lua file to generate, based on the XML file name.
253+
*
254+
* @return the name of Lua file to generate
255+
*/
256+
public String getLuaFileName() {
257+
return xmlFilePath.replaceFirst("\\.xml", ".lua");
258+
}
259+
260+
/**
261+
* Gets the name to assign for the Lua table to be generated from the XML file.
262+
* @param doc the loaded XML document
263+
* @return
264+
*/
265+
private String getLuaTableName(final Document doc) {
266+
if (useFirstXmlTagAsLuaTableName) {
267+
return doc.getDocumentElement().getNodeName() + " = ";
268+
}
269+
270+
return "return ";
271+
}
272+
273+
/**
274+
* Checks if the generated Lua code must be printed or not.
275+
*/
276+
public boolean isPrintLuaCode() {
277+
return printLuaCode;
278+
}
279+
280+
/**
281+
* Defines if the generated Lua code must be printed or not.
282+
*/
283+
public Xml2Lua setPrintLuaCode(final boolean enable) {
284+
this.printLuaCode = enable;
285+
return this;
286+
}
287+
288+
/**
289+
* Checks if the name of the first tag into the XML file will be used as the name of the
290+
* Lua table to be generated.
291+
* @return if it returns true, the name of the first tag into the XML file will be used as the name of the
292+
* Lua table to be generated. If it returns false, the name of this first tag will be ignored
293+
* and a lua "return" command will be used instead.
294+
* This way, the Lua developer who will use the generated Lua file
295+
* can define the name he/she wants to use for the generated Lua table.
296+
*/
297+
public boolean isUseFirstXmlTagAsLuaTableName() {
298+
return useFirstXmlTagAsLuaTableName;
299+
}
300+
301+
/**
302+
* Defines if the name of the first tag into the XML file will be used as the name of the
303+
* Lua table to be generated.
304+
* @param enable if true, the name of the first tag into the XML file will be used as the name of the
305+
* Lua table to be generated. If false, the name of this first tag will be ignored
306+
* and a lua "return" command will be used instead.
307+
* This way, the Lua developer who will use the generated Lua file
308+
* can define the name he/she wants to use for the generated Lua table.
309+
*/
310+
public Xml2Lua setUseFirstXmlTagAsLuaTableName(final boolean enable) {
311+
this.useFirstXmlTagAsLuaTableName = enable;
312+
return this;
313+
}
314+
315+
/**
316+
* Gets the path to the XML file to be parsed.
317+
*/
318+
public String getXmlFilePath() {
319+
return xmlFilePath;
320+
}
262321
}

src/main/java/com/manoelcampos/xml2lua/Xml2LuaApp.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static void main(String[] args) {
3737
System.exit(-1);
3838
}
3939

40-
final String xmlFileName = args[0];
41-
new Xml2Lua(xmlFileName, true, false);
40+
final Xml2Lua parser = new Xml2Lua(args[0]);
41+
parser.convert();
42+
System.out.printf("\nLua file %s generated successfully from the %s.\n\n", parser.getLuaFileName(), parser.getXmlFilePath());
4243
}
4344
}

0 commit comments

Comments
 (0)