// flush the buffer to file after processing is finished private void writeToContentChunk( final String tmpContent, final File outputFileName, final boolean needWriteDitaTag) throws IOException { logger.info("Writing " + outputFileName); OutputStreamWriter ditaFileOutput = null; try { ditaFileOutput = new OutputStreamWriter(new FileOutputStream(outputFileName), UTF8); if (outputFileName.getPath().equals(changeTable.get(outputFileName.getPath()))) { // if the output file is newly generated file // write the xml header and workdir PI into new file writeStartDocument(ditaFileOutput); final File workDir = outputFileName.getParentFile().getAbsoluteFile(); if (!OS_NAME.toLowerCase().contains(OS_NAME_WINDOWS)) { writeProcessingInstruction(ditaFileOutput, PI_WORKDIR_TARGET, workDir.getAbsolutePath()); } else { writeProcessingInstruction( ditaFileOutput, PI_WORKDIR_TARGET, UNIX_SEPARATOR + workDir.getAbsolutePath()); } writeProcessingInstruction( ditaFileOutput, PI_WORKDIR_TARGET_URI, workDir.toURI().toString()); if ((conflictTable.get(outputFileName.getPath()) != null)) { final String relativePath = getRelativeUnixPath( filePath + UNIX_SEPARATOR + FILE_NAME_STUB_DITAMAP, conflictTable.get(outputFileName.getPath())); String path2project = getRelativeUnixPath(relativePath); if (null == path2project) { path2project = ""; } writeProcessingInstruction(ditaFileOutput, PI_PATH2PROJ_TARGET, path2project); writeProcessingInstruction( ditaFileOutput, PI_PATH2PROJ_TARGET_URI, path2project.isEmpty() ? "./" : toURI(path2project).toString()); } } if (needWriteDitaTag) { final AttributesImpl atts = new AttributesImpl(); addOrSetAttribute(atts, ATTRIBUTE_NAMESPACE_PREFIX_DITAARCHVERSION, DITA_NAMESPACE); addOrSetAttribute( atts, ATTRIBUTE_PREFIX_DITAARCHVERSION + COLON + ATTRIBUTE_NAME_DITAARCHVERSION, "1.2"); writeStartElement(ditaFileOutput, ELEMENT_NAME_DITA, atts); } // write the final result to the output file ditaFileOutput.write(tmpContent); if (needWriteDitaTag) { writeEndElement(ditaFileOutput, ELEMENT_NAME_DITA); } ditaFileOutput.flush(); } finally { if (ditaFileOutput != null) { ditaFileOutput.close(); } } }