Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private NodeList getStrings(File f) throws SAXException, IOException {
return dom.getDocumentElement().getChildNodes();
}

private static HSSFCellStyle createTilteStyle(HSSFWorkbook wb) {
private static HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont bold = wb.createFont();
bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

Expand All @@ -160,7 +160,7 @@ private static HSSFCellStyle createCommentStyle(HSSFWorkbook wb) {
HSSFFont commentFont = wb.createFont();
commentFont.setColor(HSSFColor.GREEN.index);
commentFont.setItalic(true);
commentFont.setFontHeightInPoints((short)12);
commentFont.setFontHeightInPoints((short) 12);

HSSFCellStyle commentStyle = wb.createCellStyle();
commentStyle.setFont(commentFont);
Expand Down Expand Up @@ -213,28 +213,28 @@ private static void createTilte(HSSFWorkbook wb, HSSFSheet sheet) {
HSSFRow titleRow = sheet.getRow(0);

HSSFCell cell = titleRow.createCell(0);
cell.setCellStyle(createTilteStyle(wb));
cell.setCellStyle(createTitleStyle(wb));
cell.setCellValue("KEY");

sheet.setColumnWidth(cell.getColumnIndex(), (40 * 256));
}

private static void addLang2Tilte(HSSFWorkbook wb, HSSFSheet sheet, String lang) {
HSSFRow titleRow = sheet.getRow(0);
HSSFCell lastCell = titleRow.getCell((int)titleRow.getLastCellNum() - 1);
HSSFCell lastCell = titleRow.getCell((int) titleRow.getLastCellNum() - 1);
if (lang.equals(lastCell.getStringCellValue())) {
// language column already exists
return;
}
HSSFCell cell = titleRow.createCell((int)titleRow.getLastCellNum());
cell.setCellStyle(createTilteStyle(wb));
cell.setCellStyle(createTitleStyle(wb));
cell.setCellValue(lang);

sheet.setColumnWidth(cell.getColumnIndex(), (60 * 256));
}


private Map<String, Integer> exportDefLangToExcel(int rowIndex, String project, File src, NodeList strings, File f) throws FileNotFoundException, IOException {
private Map<String, Integer> exportDefLangToExcel(int rowIndex, String project, File src, NodeList strings, File f) throws IOException {
out.println();
out.println("Start processing DEFAULT language " + src.getName());

Expand Down Expand Up @@ -269,7 +269,8 @@ private Map<String, Integer> exportDefLangToExcel(int rowIndex, String project,
if (translatable != null && "false".equals(translatable.getNodeValue())) {
continue;
}
String key = item.getAttributes().getNamedItem("name").getNodeValue();

String key = getKey(item);
if (mConfig.isIgnoredKey(key)) {
continue;
}
Expand All @@ -283,24 +284,24 @@ private Map<String, Integer> exportDefLangToExcel(int rowIndex, String project,

cell = row.createCell(1);
cell.setCellStyle(textStyle);
cell.setCellValue(item.getTextContent());
cell.setCellValue(item.getTextContent().replace("\\'", "'").replace("\\\"", "\""));
} else if ("plurals".equals(item.getNodeName())) {
String key = item.getAttributes().getNamedItem("name").getNodeValue();
if (mConfig.isIgnoredKey(key)) {
continue;
}
String plurarName = key;
String pluralName = key;

HSSFRow row = sheet.createRow(rowIndex++);
HSSFCell cell = row.createCell(0);
cell.setCellValue(String.format("//plurals: %s", plurarName));
cell.setCellValue(String.format("//plurals: %s", pluralName));
cell.setCellStyle(plurarStyle);

NodeList items = item.getChildNodes();
for (int j = 0; j < items.getLength(); j++) {
Node plurarItem = items.item(j);
if ("item".equals(plurarItem.getNodeName())) {
String itemKey = plurarName + "#" + plurarItem.getAttributes().getNamedItem("quantity").getNodeValue();
String itemKey = pluralName + "#" + plurarItem.getAttributes().getNamedItem("quantity").getNodeValue();
keys.put(itemKey, rowIndex);

HSSFRow itemRow = sheet.createRow(rowIndex++);
Expand Down Expand Up @@ -348,9 +349,9 @@ private Map<String, Integer> exportDefLangToExcel(int rowIndex, String project,
return keys;
}

private void exportLangToExcel(String project, String lang, File src, NodeList strings, File f, Map<String, Integer> keysIndex) throws FileNotFoundException, IOException {
private void exportLangToExcel(String project, String lang, File src, NodeList strings, File f, Map<String, Integer> keysIndex) throws IOException {
out.println();
out.println(String.format("Start processing: '%s'", lang) + " " + src.getName());
out.println(String.format("Start processing: '%s' %s", lang, src.getName()));
Set<String> missedKeys = new HashSet<String>(keysIndex.keySet());

HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f));
Expand All @@ -371,7 +372,7 @@ private void exportLangToExcel(String project, String lang, File src, NodeList s
if (translatable != null && "false".equals(translatable.getNodeValue())) {
continue;
}
String key = item.getAttributes().getNamedItem("name").getNodeValue();
String key = getKey(item);
Integer index = keysIndex.get(key);
if (index == null) {
out.println("\t" + key + " - row does not exist");
Expand All @@ -390,9 +391,9 @@ private void exportLangToExcel(String project, String lang, File src, NodeList s

NodeList items = item.getChildNodes();
for (int j = 0; j < items.getLength(); j++) {
Node plurarItem = items.item(j);
if ("item".equals(plurarItem.getNodeName())) {
key = plurarName + "#" + plurarItem.getAttributes().getNamedItem("quantity").getNodeValue();
Node pluralItem = items.item(j);
if ("item".equals(pluralItem.getNodeName())) {
key = plurarName + "#" + pluralItem.getAttributes().getNamedItem("quantity").getNodeValue();
Integer index = keysIndex.get(key);
if (index == null) {
out.println("\t" + key + " - row does not exist");
Expand All @@ -403,7 +404,7 @@ private void exportLangToExcel(String project, String lang, File src, NodeList s
HSSFRow row = sheet.getRow(index);

HSSFCell cell = row.createCell(lastColumnIdx);
cell.setCellValue(plurarItem.getTextContent());
cell.setCellValue(pluralItem.getTextContent());
cell.setCellStyle(textStyle);
}
}
Expand Down Expand Up @@ -454,4 +455,18 @@ private void exportLangToExcel(String project, String lang, File src, NodeList s
out.println(String.format("'%s' was processed with MISSED KEYS - %d", lang, missedKeys.size()));
}
}

private String getKey(Node item) {
String key = item.getAttributes().getNamedItem("name").getNodeValue();
NodeList nodes = item.getChildNodes();
if (nodes.getLength() == 0) {
throw new IllegalArgumentException("Unpredictable node format at " + item);
}
Node text = nodes.item(0);
if (text.getNodeType() == Node.CDATA_SECTION_NODE) {
key += "!cdata";
}

return key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cz.tomaskypta.tools.langtool.importing;

/**
* Type of content to insert
*/
public enum ContentType {
TEXT, CDATA;

/**
* Detect content type based on key
* @param key key
* @return detected type
*/
public static ContentType detect(String key) {
if (key != null) {
int index = key.indexOf("!");
if (index != -1) {
String type = key.substring(index + 1);
if ("cdata".equals(type)) {
return CDATA;
}
}
}
return TEXT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,24 @@ public void fatalError(SAXParseException exception) throws SAXException {

private void addContentAsString(Document dom, Element root, String value, String nodeName, String key, String quantity) {
Element node = dom.createElement(nodeName);
ContentType type = ContentType.detect(key);

if (key != null) {
int index = key.indexOf("!");
if (index != -1) {
key = key.substring(0, index);
}
node.setAttribute("name", key);
}
if (quantity != null) {
node.setAttribute("quantity", quantity);
}
node.setTextContent(value);

if (type == ContentType.CDATA) {
node.appendChild(dom.createCDATASection(value));
} else {
node.setTextContent(value);
}
root.appendChild(node);
}

Expand Down