BndEditModel newBundleModel = new BndEditModel();
// Load project file and model
IFile projectFile = container.getProject().getFile(Project.BNDFILE);
BndEditModel projectModel;
final Document projectDocument;
try {
if (projectFile.exists()) {
byte[] bytes = FileUtils.readFully(projectFile.getContents());
projectDocument = new Document(new String(bytes, projectFile.getCharset()));
} else {
projectDocument = new Document("");
}
projectModel = new BndEditModel();
projectModel.loadFrom(projectDocument);
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, e.getMessage(), e));
}
// Check if we need to enable sub-bundles on the project file
boolean enableSubs;
List<String> subBndFiles = projectModel.getSubBndFiles();
List<String> availableHeaders = calculateProjectOnlyHeaders(projectModel.getAllPropertyNames());
Collection<String> bundleSpecificHeaders = calculateBundleSpecificHeaders(availableHeaders);
if (subBndFiles == null || subBndFiles.isEmpty()) {
final EnableSubBundlesDialog subBundlesDialog = new EnableSubBundlesDialog(parentShell, availableHeaders, bundleSpecificHeaders);
if (subBundlesDialog.open() != Window.OK) {
monitor.setCanceled(true);
return;
}
enableSubs = subBundlesDialog.isEnableSubBundles();
bundleSpecificHeaders = subBundlesDialog.getSelectedProperties();
} else {
enableSubs = false;
}
// Enable subs and copy entries from project model to new bundle model
if (enableSubs) {
projectModel.setSubBndFiles(Arrays.asList(new String[] {
"*.bnd"
}));
for (String propertyName : bundleSpecificHeaders) {
Object value = projectModel.genericGet(propertyName);
projectModel.genericSet(propertyName, null);
newBundleModel.genericSet(propertyName, value);
}
// Save the project model
projectModel.saveChangesTo(projectDocument);
FileUtils.writeFully(projectDocument.get(), projectFile, false);
}
// Generate the new bundle model
Document newBundleDocument = new Document("");
newBundleModel.saveChangesTo(newBundleDocument);
try {
newBundleInputStream = new ByteArrayInputStream(newBundleDocument.get().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
newBundleInputStream = null;
}
}