/** * Export the code data to the given directory * * @param dstDirPath * @param corpus * @throws IOException */ public static void exportCodeData(SourceCodeCorpus corpus) throws IOException { String dstDirPath = Config.getInstance().getCodeCorpusDir(); // create a directory File dstDir = new File(dstDirPath); if (!dstDir.isDirectory()) { dstDir.mkdir(); } // record the basic information in the "/basicInfo" String basicInfoFilePath = Paths.get(dstDirPath, "basicInfo").toString(); FileWriter basicInfoWriter = new FileWriter(basicInfoFilePath); // fileType + segmentationLength String basicInfoStr = "fileType=" + "\t" + corpus.getFileType() + "\r\n" + "segmentationLength=" + "\t" + corpus.getSegmentationLength(); basicInfoWriter.write(basicInfoStr); basicInfoWriter.close(); // corpus built by original code contents String codeContentDirPath = Paths.get(dstDirPath, "codeContentCorpus").toString(); File codeContentDir = new File(codeContentDirPath); if (!codeContentDir.isDirectory()) { codeContentDir.mkdir(); } // corpus built by code segments String codeSegmentDirPath = Paths.get(dstDirPath, "codeSegmentCorpus").toString(); File codeSegmentDir = new File(codeSegmentDirPath); if (!codeSegmentDir.isDirectory()) { codeSegmentDir.mkdir(); } // corpus built by class name list String classNameDirPath = Paths.get(dstDirPath, "classNameCorpus").toString(); File classNameDir = new File(classNameDirPath); if (!classNameDir.isDirectory()) { classNameDir.mkdir(); } // corpus built by method name list String methodNameDirPath = Paths.get(dstDirPath, "methodNameCorpus").toString(); File methodNameDir = new File(methodNameDirPath); if (!methodNameDir.isDirectory()) { methodNameDir.mkdir(); } // Traverse every file in the source code file list for (SourceCode oneCodeFile : corpus.getSourceCodeList()) { String fileName; FileWriter writer; // For each source code file, the name in the original content corpus is the full class name fileName = Paths.get(codeContentDirPath, oneCodeFile.getFullClassName()).toString(); writer = new FileWriter(fileName); writer.write(oneCodeFile.getContent()); writer.close(); // For each source code file, the name in the code segments corpus is the full class // name+@+"the segment index".java String[] codeSegmentArray = oneCodeFile.getCodeSegmentList().toArray(new String[0]); for (int i = 0; i < codeSegmentArray.length; i++) { fileName = Paths.get(codeSegmentDirPath, oneCodeFile.getFullClassName() + "@" + i + ".java") .toString(); writer = new FileWriter(fileName); writer.write(codeSegmentArray[i]); writer.close(); } // For each source code file, the name in the class Names corpus is the full class name fileName = Paths.get(classNameDirPath, oneCodeFile.getFullClassName()).toString(); writer = new FileWriter(fileName); String classNamesString = new String(); for (String oneClassName : oneCodeFile.getClassNameList()) { classNamesString += oneClassName + " "; } writer.write(classNamesString.trim()); writer.close(); // For each source code file, the name in the method Names corpus is the full class name fileName = Paths.get(methodNameDirPath, oneCodeFile.getFullClassName()).toString(); writer = new FileWriter(fileName); String methodNamesString = new String(); for (String oneMethodName : oneCodeFile.getMethodNameList()) { methodNamesString += oneMethodName + " "; } writer.write(methodNamesString.trim()); writer.close(); } }