diff --git a/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/exporting/ToolExport.java b/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/exporting/ToolExport.java index 006f46a..4b3d34f 100644 --- a/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/exporting/ToolExport.java +++ b/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/exporting/ToolExport.java @@ -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); @@ -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); @@ -213,7 +213,7 @@ 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)); @@ -221,20 +221,20 @@ private static void createTilte(HSSFWorkbook wb, HSSFSheet sheet) { 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 exportDefLangToExcel(int rowIndex, String project, File src, NodeList strings, File f) throws FileNotFoundException, IOException { + private Map exportDefLangToExcel(int rowIndex, String project, File src, NodeList strings, File f) throws IOException { out.println(); out.println("Start processing DEFAULT language " + src.getName()); @@ -269,7 +269,8 @@ private Map 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; } @@ -283,24 +284,24 @@ private Map 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++); @@ -348,9 +349,9 @@ private Map exportDefLangToExcel(int rowIndex, String project, return keys; } - private void exportLangToExcel(String project, String lang, File src, NodeList strings, File f, Map keysIndex) throws FileNotFoundException, IOException { + private void exportLangToExcel(String project, String lang, File src, NodeList strings, File f, Map 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 missedKeys = new HashSet(keysIndex.keySet()); HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f)); @@ -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"); @@ -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"); @@ -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); } } @@ -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; + } } diff --git a/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ContentType.java b/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ContentType.java new file mode 100644 index 0000000..cc4c006 --- /dev/null +++ b/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ContentType.java @@ -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; + } +} diff --git a/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ToolImport.java b/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ToolImport.java index dbdf5bd..35c1441 100644 --- a/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ToolImport.java +++ b/lang-tool/src/main/java/cz/tomaskypta/tools/langtool/importing/ToolImport.java @@ -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); }