List of usage examples for org.jdom2.output Format getEncoding
public String getEncoding()
From source file:com.globalsight.dispatcher.bo.JobTask.java
License:Apache License
private void createTargetFile(JobBO p_job, String[] p_targetSegments) throws IOException { OutputStream writer = null;/*from w w w . j a va2s .c o m*/ File fileStorage = CommonDAO.getFileStorage(); File srcFile = p_job.getSrcFile(); Account account = DispatcherDAOFactory.getAccountDAO().getAccount(p_job.getAccountId()); File trgDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + p_job.getJobID() + File.separator + AppConstants.XLF_TARGET_FOLDER); File trgFile = new File(trgDir, srcFile.getName()); FileUtils.copyFile(srcFile, trgFile); String encoding = FileUtil.getEncodingOfXml(trgFile); try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(p_job.getSrcFile()); Element root = doc.getRootElement(); // Get root element Namespace namespace = root.getNamespace(); Element fileElem = root.getChild("file", namespace); XPathFactory xFactory = XPathFactory.instance(); XPathExpression<Element> expr = xFactory.compile("//trans-unit", Filters.element(), null, namespace); List<Element> tuList = expr.evaluate(fileElem.getChild("body", namespace)); for (int tuIndex = 0, trgIndex = 0; tuIndex < tuList.size() && trgIndex < p_targetSegments.length; tuIndex++, trgIndex++) { if (p_targetSegments[trgIndex] == null) { continue; } Element elem = (Element) tuList.get(tuIndex); Element srcElem = elem.getChild("source", namespace); Element trgElem = elem.getChild("target", namespace); if (srcElem == null || srcElem.getContentSize() == 0) { trgIndex--; continue; } if (trgElem != null) { setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding); } else { trgElem = new Element("target", namespace); setTargetSegment(trgElem, p_targetSegments[trgIndex], encoding); elem.addContent(trgElem); } } XMLOutputter xmlOutput = new XMLOutputter(); Format format = Format.getRawFormat(); format.setEncoding(encoding); writer = new FileOutputStream(trgFile); xmlOutput.setFormat(format); writeBOM(writer, format.getEncoding()); xmlOutput.output(doc, writer); p_job.setTrgFile(trgFile); logger.info("Create Target File: " + trgFile); } catch (JDOMException e1) { logger.error("CreateTargetFile Error: ", e1); } catch (IOException e1) { logger.error("CreateTargetFile Error: ", e1); } finally { if (writer != null) writer.close(); } }