Skip to content

Commit

Permalink
done with adding commas as feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Liew Jun Tung committed Jul 3, 2015
1 parent 13b3fab commit b5c01f6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
6 changes: 4 additions & 2 deletions plugin/META-INF/plugin.xml
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>com.pandawarrior.androidXMLConverter</id>
<name>Android Strings.xml To CSV Converter</name>
<version>1.1.1</version>
<version>1.2.0</version>
<vendor email="pandawarrior91@gmail.com" url="https://github.com/pandawarrior91">Liew Jun Tung</vendor>

<description><![CDATA[
Expand All @@ -11,7 +11,9 @@
<p>You can access the plugin from the "Tools" menu at the IDE toolbar</p>
</br>
<p>Changelog</p>
<p>1.1.1</p>
<p></p>
<p>1.2.0</p>
<p>Support commas in data now
<p>Fixes plugin bug of displaying "Success" even though there are errors</p>
<a href ="https://github.com/pandawarrior91/Android-strings-xml-csv-converter">GitHub</a>
]]></description>
Expand Down
22 changes: 11 additions & 11 deletions plugin/src/org/pandawarrior/androidXMLConverter/GUI.java
Expand Up @@ -38,7 +38,7 @@ public class GUI extends DialogWrapper {
private JRadioButton arraysXmlRadioButton;
private String currentFolder;
private String selected;
private final static String ERROR_MSG1 = "Error! Your might a data that is empty. use 'null' or ' '(with a space) to replace it. ";
private final static String ERROR_MSG1 = "Error! ";

public GUI(Project project, String initialFolder) {
super(project, true);
Expand Down Expand Up @@ -177,12 +177,12 @@ public void run() {
} else if (selected.equals("plurals.xml")) {
WriteXml.parseArray(csv, folder, WriteXml.getPLURALS_FILE());
}
xcReadField.setText("");
xcWriteField.setText("");
xcLabel.setText("SUCCESS!");
xcLabel.setForeground(JBColor.GREEN);
cxReadField.setText("");
cxWriteField.setText("");
cxLabel.setText("SUCCESS!");
cxLabel.setForeground(JBColor.GREEN);
} catch (ArrayIndexOutOfBoundsException e1) {
cxLabel.setText(ERROR_MSG1);
cxLabel.setText(ERROR_MSG1 + e1.getMessage());
cxLabel.setForeground(JBColor.RED);
}

Expand All @@ -197,12 +197,12 @@ private void readXML(final String folder, final String csv) throws ArrayIndexOut
public void run() {
try {
ReadXml.parseAll(folder, csv);
cxReadField.setText("");
cxLabel.setText("SUCCESS!");
cxLabel.setForeground(JBColor.GREEN);
xcReadField.setText("");
xcLabel.setText("SUCCESS!");
xcLabel.setForeground(JBColor.GREEN);
} catch (Exception e) {
cxLabel.setText(ERROR_MSG1);
cxLabel.setForeground(JBColor.RED);
xcLabel.setText(ERROR_MSG1 + e.getMessage());
xcLabel.setForeground(JBColor.RED);
}

}
Expand Down
8 changes: 4 additions & 4 deletions xml_csv_parse/src/main/groovy/org/pandawarrior/ReadXml.groovy
Expand Up @@ -170,7 +170,7 @@ class ReadXml {
def tempXml = new XmlSlurper().parseText(xmlFile)
def tempResources = tempXml.string
tempResources.each {
tempMap[it.@name as String] = it.text().replaceAll(",", "@@")
tempMap[it.@name as String] = it.text()
}
mainMap[it.key] = tempMap
// println mainMap
Expand Down Expand Up @@ -221,10 +221,10 @@ class ReadXml {

protected static String createCSV(List headerList, List valueList) {
String csv
csv = headerList.join(",") + "\n"
csv = '\"'+headerList.join('\",\"') + '\"\n'
valueList.each {
csv += it.collect({it!=null?it:" "}).join(",")
csv += "\n"
csv += it.collect({it!=null?"\"${it}\"":'\"\"'}).join(',')
csv += '\n'
}
return csv
}
Expand Down
27 changes: 18 additions & 9 deletions xml_csv_parse/src/main/groovy/org/pandawarrior/WriteXml.groovy
Expand Up @@ -9,6 +9,7 @@ class WriteXml {

final static String ARRAY_FILE = "arrays"
final static String PLURALS_FILE = "plurals"
private static final String REGEX = /,(?=([^\"]*\"[^\"]*\")*[^\"]*$)/

static boolean parse(String csvPath, String moduleFolder) {
//load and split the file
Expand All @@ -32,21 +33,26 @@ class WriteXml {
protected static List getRows(String csvPath) {
String csv = new File(csvPath).getText()
String[] lines = csv.split('\n')
List<String[]> rows = lines.collect { it.split(',') }
List<String[]> rows = lines.collect{
it.split(REGEX, -1)
}
return rows
}

protected static List getHead(List rows) {
List head = rows.get(0)
head = head - "\"name\""
head = head - "name"
head = head - "\"translatable\""
head = head - "translatable"
// println head
return head
}

protected static Map getTransMap(List rows) {
Map transMap = [:]
for (int i = 1; i < rows.size(); i++) {
transMap[rows[i][0]] = rows[i][1].replaceAll("\\s", "")
transMap[rows[i][0]] = rows[i][1].replaceAll("\"", "")
}
return transMap
}
Expand All @@ -62,7 +68,7 @@ class WriteXml {
def name = column[0]
tempMap[name] = column[i + 2]
}
mainDict[head[i].replaceAll("\\s", "")] = tempMap
mainDict[head[i].replaceAll("\"", "")] = tempMap
}
return mainDict
}
Expand All @@ -88,7 +94,7 @@ class WriteXml {

}
}
mainArrayMap[head[i].replaceAll("\\s", "")] = tempMap
mainArrayMap[head[i].replaceAll("\"", "")] = tempMap
}
return mainArrayMap
}
Expand All @@ -109,11 +115,14 @@ class WriteXml {
xml.resources {
mainDictValue.each {
def key = it.key
def value = it.value.replaceAll("@@", ",")
if (fileName.equals("values") && transDict[key].equals("false")) {
string(name: key, translatable: transDict[key], value)
} else if (transDict[key].equals("true") && !value.equals("null") && !value.equals(" ")) {
string(name: key, value)
def value = it.value.replaceAll("\"", "")
if (fileName.equals("values") && transDict[key].equals("false") ||
fileName.equals("values") && !transDict[key]) {
string(name: key.replaceAll("\"", ""), translatable: transDict[key], value)
} else if (transDict[key].equals("true") && !value.equals("null") && !value.equals("") ||
transDict[key] && !value.equals("null") && !value.equals("")
) {
string(name: key.replaceAll("\"", ""), value)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions xml_csv_parse/xml_csv_parse.iml
Expand Up @@ -20,5 +20,7 @@
<orderEntry type="library" exported="" name="Gradle: org.codehaus.groovy:groovy-all:2.4.3" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" exported="" name="Gradle: com.xlson.groovycsv:groovycsv:1.0" level="project" />
<orderEntry type="library" exported="" name="Gradle: net.sf.opencsv:opencsv:2.1" level="project" />
</component>
</module>

0 comments on commit b5c01f6

Please sign in to comment.