/** * Reads the contents of a {@link File} to a {@link String}. * * @param file the instance of {@link File} to be read. * @return a {@link String} containing the contents of the file. */ private String readFiletoString(File file) { ArrayList<String> datalines = new ArrayList<String>(); BufferedReader brlocal; try { brlocal = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); String line; while ((line = brlocal.readLine()) != null) { datalines.add(line); } brlocal.close(); } catch (FileNotFoundException e) { Activator.log("Error when reading a txt/ann from the file system to import it", e); } catch (IOException e) { Activator.log("Error when reading a txt/ann from the file system to import it", e); } String filedata = ""; for (String dataline : datalines) { filedata += dataline + "\n"; } return filedata; }
/** * Constructor for this page. * * @param workbench the current workbench. * @param selection the current selection. */ public ImportTxtAnnWizardPage(IWorkbench workbench, IStructuredSelection selection) { super(workbench, selection); setTitle("Requirements Editor Import Wizard"); setDescription("Select your txt/ann files to import"); if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) { IProject project = ProjectLocator.getProjectOfSelectionList((IStructuredSelection) selection); String requirementsFolderLocation = null; try { requirementsFolderLocation = project.getPersistentProperty( new QualifiedName("", "eu.scasefp7.eclipse.core.ui.rqsFolder")); } catch (CoreException e) { Activator.log("Error retrieving project property (requirements folder location)", e); } IContainer container = project; if (requirementsFolderLocation != null) { if (project.findMember(new Path(requirementsFolderLocation)).exists()) container = (IContainer) project.findMember(new Path(requirementsFolderLocation)); } setContainerFieldValue(container.getFullPath().toString()); } }
/** * The Finish button was pressed. Create rqs files according to the imported txt and ann files. * * @return {@code true} if rqs files are created correctly, {@code false} otherwise. */ public boolean finish() { if (!ensureSourceIsValid()) { return false; } saveWidgetValues(); @SuppressWarnings("unchecked") Iterator<FileSystemElement> resourcesEnum = getSelectedResources().iterator(); ArrayList<File> fileSystemObjects = new ArrayList<File>(); while (resourcesEnum.hasNext()) { File fileSystemObject = (File) resourcesEnum.next().getFileSystemObject(); String filename = fileSystemObject.getName(); int i = filename.lastIndexOf('.'); String extension = filename.substring(i + 1); if (i <= 0 || !(extension.equals("txt") || extension.equals("ann"))) { setErrorMessage("All files imported must have the txt or ann extension!"); return false; } fileSystemObjects.add(fileSystemObject); } HashSet<String> hasTxt = new HashSet<String>(); HashSet<String> hasAnn = new HashSet<String>(); for (File fileSystemObject : fileSystemObjects) { String filename = fileSystemObject.getName(); int i = filename.lastIndexOf('.'); String name = filename.substring(0, i); String extension = filename.substring(i + 1); if (extension.equals("txt")) hasTxt.add(name); else if (extension.equals("ann")) hasAnn.add(name); } for (File fileSystemObject : fileSystemObjects) { String filename = fileSystemObject.getName(); String name = filename.substring(0, filename.lastIndexOf('.')); if (hasAnn.contains(name) && !hasTxt.contains(name)) { setErrorMessage("You cannot import an ann file without the corresponding txt!"); return false; } } HashMap<String, TxtAndAnnFile> newFileSystemObjects = new HashMap<String, TxtAndAnnFile>(); for (File fileSystemObject : fileSystemObjects) { String filename = fileSystemObject.getName(); String name = filename.substring(0, filename.lastIndexOf('.')); if (!newFileSystemObjects.containsKey(name)) newFileSystemObjects.put(name, new TxtAndAnnFile()); newFileSystemObjects.get(name).addFile(fileSystemObject); } for (final TxtAndAnnFile txtAndAnnFile : newFileSystemObjects.values()) { IRunnableWithProgress op = new WorkspaceModifyOperation(null) { protected void execute(IProgressMonitor monitor) throws CoreException, InterruptedException { String fileName = txtAndAnnFile.getName() + ".rqs"; String filedata; if (txtAndAnnFile.hasAnnFile()) { filedata = RQStoANNHelpers.transformTXTandANNtoRQS( readFiletoString(txtAndAnnFile.txtFile), readFiletoString(txtAndAnnFile.annFile)); } else { filedata = "REQUIREMENTS\n------------\n"; filedata += readFiletoString(txtAndAnnFile.txtFile); filedata += "------------\n\nANNOTATIONS\n------------\n"; filedata += "------------\n"; } InputStream stream = new ByteArrayInputStream(filedata.getBytes(StandardCharsets.UTF_8)); IPath resourcePath = getResourcePath(); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IContainer container = (IContainer) root.findMember(resourcePath); container.getFile(new Path(fileName)).create(stream, true, monitor); IEditorDescriptor desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(fileName); IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); page.openEditor( new FileEditorInput(container.getFile(new Path(fileName))), desc.getId()); } }; try { getContainer().run(false, true, op); } catch (InterruptedException e) { Activator.log("Error importing a txt and/or an ann file", e); return false; } catch (InvocationTargetException e) { Activator.log("Error importing a txt and/or an ann file", e); return false; } } return true; }