/** Writes the given contents to a file with the given fileName in the specified project. */ public void writeContent() { String contents = generateSpecfile(); InputStream contentInputStream = new ByteArrayInputStream(contents.getBytes()); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource resource = root.findMember(new Path(projectName)); if (!resource.exists() || !(resource instanceof IContainer)) { IStatus status = new Status( IStatus.ERROR, StubbyPlugin.PLUGIN_ID, IStatus.OK, "Project \"" + projectName + "\" does not exist.", null); StubbyLog.logError(new CoreException(status)); } IContainer container = (IContainer) resource; IResource specsFolder = container.getProject().findMember("SPECS"); // $NON-NLS-1$ IFile file = container.getFile(new Path(specfileName)); if (specsFolder != null) { file = ((IFolder) specsFolder).getFile(new Path(specfileName)); } final IFile openFile = file; try { InputStream stream = contentInputStream; if (file.exists()) { file.setContents(stream, true, true, null); } else { file.create(stream, true, null); } stream.close(); } catch (IOException e) { StubbyLog.logError(e); } catch (CoreException e) { StubbyLog.logError(e); } Display.getCurrent() .asyncExec( new Runnable() { public void run() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); try { IDE.openEditor(page, openFile, true); } catch (PartInitException e) { StubbyLog.logError(e); } } }); }
private void parse(IFile pomFile) { DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder; try { docbuilder = docfactory.newDocumentBuilder(); Document docroot = docbuilder.parse(pomFile.getContents()); model = new PomModel(docroot); } catch (ParserConfigurationException e) { StubbyLog.logError(e); } catch (SAXException e) { StubbyLog.logError(e); } catch (IOException e) { StubbyLog.logError(e); } catch (CoreException e) { StubbyLog.logError(e); } }
/** * Parse the feature.xml and write the generated specfile. * * @param featureFile The feature.xml file. */ public void write(IFile featureFile) { // Populate Main package model StubbyPackageModel stubbyPackageModel = new StubbyPackageModel(featureFile); MainPackage mainPackage = new MainPackage(); stubbyPackageModel.populatePackageData(mainPackage); stubbyPackageModel.populatePackagePreambleData(mainPackage); stubbyPackageModel.populateDocFiles(mainPackage); List<IFile> includedFeatureFiles = stubbyPackageModel.getIncudedFeatures(); // Populate Sub package model List<SubPackage> subPackages = new ArrayList<SubPackage>(); for (IFile includedFeatureFile : includedFeatureFiles) { StubbyPackageModel strubbySubPackageModel = new StubbyPackageModel(includedFeatureFile); SubPackage subPackage = new SubPackage(); strubbySubPackageModel.populatePackageData(subPackage); subPackages.add(subPackage); } if (!stubbyPackageModel.isAllIncludedFeatureFound()) { String message = ""; if (stubbyPackageModel.getMissingFeaturesAsString().indexOf(",") >= 0) { message = "Stub out an RPM specfile for '" + mainPackage.getName() + "' fails because '" + stubbyPackageModel.getMissingFeaturesAsString() + "' features were not found.\n\n" + "Please, add these features somewhere in your workspace.\n"; } else { message = "Stub out an RPM specfile for '" + mainPackage.getName() + "' fails because '" + stubbyPackageModel.getMissingFeaturesAsString() + "' feature was not found.\n\n" + "Please, add these feature somewhere in your workspace.\n"; } MessageDialog.openError(StubbyPlugin.getActiveWorkbenchShell(), null, message); } else { // Write generated files to the main feature project StubbyGenerator generator = new StubbyGenerator(mainPackage, subPackages); String packageName = "eclipse-" + generator.getPackageName(mainPackage.getName()); try { generator.writeContent( featureFile.getProject().getName(), packageName.toLowerCase() + ".spec", generator.generateSpecfile()); } catch (CoreException e) { StubbyLog.logError(e); } } }
/** * Writes the given contents to a file with the given fileName in the specified project. * * @param projectName The name of the project to put the file into. * @throws CoreException Thrown when the project doesn't exist. */ public void writeContent(String projectName) throws CoreException { String fileName = model.getPackageName().toLowerCase() + ".spec"; String contents = generateSpecfile(); InputStream contentInputStream = new ByteArrayInputStream(contents.getBytes()); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource resource = root.findMember(new Path(projectName)); if (!resource.exists() || !(resource instanceof IContainer)) { throwCoreException("Project \"" + projectName + "\" does not exist."); } IContainer container = (IContainer) resource; final IFile file = container.getFile(new Path(fileName)); try { InputStream stream = contentInputStream; if (file.exists()) { file.setContents(stream, true, true, null); } else { file.create(stream, true, null); } stream.close(); } catch (IOException e) { StubbyLog.logError(e); } StubbyPlugin.getActiveWorkbenchShell() .getDisplay() .asyncExec( new Runnable() { public void run() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); try { IDE.openEditor(page, file, true); } catch (PartInitException e) { StubbyLog.logError(e); } } }); }