use of org.gephi.project.spi.WorkspaceXMLPersistenceProvider in project gephi by gephi.
the class LoadTask method run.
@Override
public void run() {
Progress.start(progressTicket);
Progress.setDisplayName(progressTicket, NbBundle.getMessage(LoadTask.class, "LoadTask.name"));
try {
ZipFile zip = null;
try {
zip = new ZipFile(file);
ProjectImpl project = readProject(zip);
if (project != null) {
// Enumerate workspaces
List<String> workspaceEntries = new ArrayList<>();
for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements(); ) {
ZipEntry entry = e.nextElement();
if (entry.getName().matches("Workspace_[0-9]*_xml")) {
workspaceEntries.add(entry.getName());
}
}
// Get providers
Collection<WorkspacePersistenceProvider> providers = PersistenceProviderUtils.getPersistenceProviders();
//Setup progress
Progress.switchToDeterminate(progressTicket, (1 + providers.size()) * workspaceEntries.size());
// Read workspaces
for (String workspaceEntry : workspaceEntries) {
WorkspaceImpl workspace = readWorkspace(project, workspaceEntry, zip);
Progress.progress(progressTicket);
if (workspace != null) {
for (WorkspacePersistenceProvider provider : providers) {
if (provider instanceof WorkspaceXMLPersistenceProvider) {
readWorkspaceChildrenXML((WorkspaceXMLPersistenceProvider) provider, workspace, zip);
} else if (provider instanceof WorkspaceBytesPersistenceProvider) {
readWorkspaceChildrenBytes((WorkspaceBytesPersistenceProvider) provider, workspace, zip);
}
Progress.progress(progressTicket);
if (cancel) {
break;
}
}
}
if (cancel) {
break;
}
}
}
Progress.switchToIndeterminate(progressTicket);
//Add project
ProjectControllerImpl projectController = Lookup.getDefault().lookup(ProjectControllerImpl.class);
if (project != null && !cancel) {
if (!cancel) {
//Set current workspace
WorkspaceProviderImpl workspaces = project.getLookup().lookup(WorkspaceProviderImpl.class);
for (Workspace workspace : workspaces.getWorkspaces()) {
WorkspaceInformationImpl info = workspace.getLookup().lookup(WorkspaceInformationImpl.class);
if (info.isOpen()) {
workspaces.setCurrentWorkspace(workspace);
break;
}
}
// Open project
projectController.openProject(project);
}
}
} finally {
if (zip != null) {
zip.close();
}
}
} catch (Exception ex) {
if (ex instanceof GephiFormatException) {
throw (GephiFormatException) ex;
}
throw new GephiFormatException(GephiReader.class, ex);
}
Progress.finish(progressTicket);
}
use of org.gephi.project.spi.WorkspaceXMLPersistenceProvider in project gephi by gephi.
the class GephiReader method readWorkspaceLegacy.
private static void readWorkspaceLegacy(XMLStreamReader reader, ProjectImpl project) throws Exception {
WorkspaceImpl workspace = project.getLookup().lookup(WorkspaceProviderImpl.class).newWorkspace();
WorkspaceInformationImpl info = workspace.getLookup().lookup(WorkspaceInformationImpl.class);
//Name
info.setName(reader.getAttributeValue(null, "name"));
//Status
String workspaceStatus = reader.getAttributeValue(null, "status");
if (workspaceStatus.equals("open")) {
info.open();
} else if (workspaceStatus.equals("closed")) {
info.close();
} else {
info.invalid();
}
Map<String, WorkspaceXMLPersistenceProvider> providers = new LinkedHashMap<>();
for (WorkspacePersistenceProvider w : Lookup.getDefault().lookupAll(WorkspacePersistenceProvider.class)) {
String id = w.getIdentifier();
if (id != null && !id.isEmpty() && w instanceof WorkspaceXMLPersistenceProvider) {
providers.put(w.getIdentifier(), (WorkspaceXMLPersistenceProvider) w);
}
}
boolean end = false;
while (reader.hasNext() && !end) {
Integer eventType = reader.next();
if (eventType.equals(XMLEvent.START_ELEMENT)) {
String name = reader.getLocalName();
WorkspaceXMLPersistenceProvider pp = providers.get(name);
if (pp != null) {
try {
pp.readXML(reader, workspace);
} catch (UnsupportedOperationException e) {
}
}
} else if (eventType.equals(XMLStreamReader.END_ELEMENT)) {
if ("workspace".equalsIgnoreCase(reader.getLocalName())) {
end = true;
}
}
}
}
use of org.gephi.project.spi.WorkspaceXMLPersistenceProvider in project gephi by gephi.
the class SaveTask method run.
@Override
public void run() {
Progress.start(progressTicket);
Progress.setDisplayName(progressTicket, NbBundle.getMessage(SaveTask.class, "SaveTask.name"));
File writeFile = null;
try {
String tempFileName = file.getName() + "_temp" + System.currentTimeMillis();
writeFile = new File(file.getParent(), tempFileName);
FileOutputStream outputStream = null;
ZipOutputStream zipOut = null;
BufferedOutputStream bos = null;
DataOutputStream dos = null;
try {
//Stream
int zipLevel = NbPreferences.forModule(SaveTask.class).getInt(ZIP_LEVEL_PREFERENCE, 9);
outputStream = new FileOutputStream(writeFile);
zipOut = new ZipOutputStream(outputStream);
zipOut.setLevel(zipLevel);
bos = new BufferedOutputStream(zipOut);
dos = new DataOutputStream(bos);
//Providers and workspace
Collection<WorkspacePersistenceProvider> providers = PersistenceProviderUtils.getPersistenceProviders();
Workspace[] workspaces = project.getLookup().lookup(WorkspaceProviderImpl.class).getWorkspaces();
//Setup progress
Progress.switchToDeterminate(progressTicket, 1 + (1 + providers.size()) * workspaces.length);
//Write Project
writeProject(dos, zipOut);
Progress.progress(progressTicket);
//Write Workspace files
for (Workspace ws : workspaces) {
writeWorkspace(ws, dos, zipOut);
Progress.progress(progressTicket);
for (WorkspacePersistenceProvider provider : providers) {
if (provider instanceof WorkspaceXMLPersistenceProvider) {
writeWorkspaceChildrenXML(ws, (WorkspaceXMLPersistenceProvider) provider, dos, zipOut);
} else if (provider instanceof WorkspaceBytesPersistenceProvider) {
writeWorkspaceChildrenBytes(ws, (WorkspaceBytesPersistenceProvider) provider, dos, zipOut);
}
Progress.progress(progressTicket);
if (cancel) {
break;
}
}
if (cancel) {
break;
}
}
Progress.switchToIndeterminate(progressTicket);
zipOut.finish();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException ex1) {
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ex1) {
}
}
if (zipOut != null) {
try {
zipOut.close();
} catch (IOException ex1) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex1) {
}
}
}
Progress.finish(progressTicket);
//Rename file
if (!cancel && writeFile.exists()) {
//Delete original file
if (file.exists()) {
file.delete();
}
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
FileLock lock = tempFileObject.lock();
tempFileObject.rename(lock, getFileNameWithoutExt(file), getFileExtension(file));
lock.releaseLock();
}
} catch (Exception ex) {
if (ex instanceof GephiFormatException) {
throw (GephiFormatException) ex;
}
throw new GephiFormatException(SaveTask.class, ex);
} finally {
if (writeFile != null && writeFile.exists()) {
FileObject tempFileObject = FileUtil.toFileObject(writeFile);
try {
tempFileObject.delete();
} catch (IOException ex) {
}
}
}
Progress.finish(progressTicket);
}
Aggregations