Search in sources :

Example 1 with WorkspaceXMLPersistenceProvider

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);
}
Also used : ProjectControllerImpl(org.gephi.project.impl.ProjectControllerImpl) WorkspaceImpl(org.gephi.workspace.impl.WorkspaceImpl) WorkspaceProviderImpl(org.gephi.project.impl.WorkspaceProviderImpl) ProjectImpl(org.gephi.project.impl.ProjectImpl) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) WorkspacePersistenceProvider(org.gephi.project.spi.WorkspacePersistenceProvider) WorkspaceInformationImpl(org.gephi.workspace.impl.WorkspaceInformationImpl) XMLStreamException(javax.xml.stream.XMLStreamException) WorkspaceXMLPersistenceProvider(org.gephi.project.spi.WorkspaceXMLPersistenceProvider) ZipFile(java.util.zip.ZipFile) WorkspaceBytesPersistenceProvider(org.gephi.project.spi.WorkspaceBytesPersistenceProvider) Workspace(org.gephi.project.api.Workspace)

Example 2 with WorkspaceXMLPersistenceProvider

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;
            }
        }
    }
}
Also used : WorkspaceImpl(org.gephi.workspace.impl.WorkspaceImpl) WorkspaceProviderImpl(org.gephi.project.impl.WorkspaceProviderImpl) WorkspaceInformationImpl(org.gephi.workspace.impl.WorkspaceInformationImpl) WorkspacePersistenceProvider(org.gephi.project.spi.WorkspacePersistenceProvider) LinkedHashMap(java.util.LinkedHashMap) WorkspaceXMLPersistenceProvider(org.gephi.project.spi.WorkspaceXMLPersistenceProvider)

Example 3 with WorkspaceXMLPersistenceProvider

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);
}
Also used : WorkspaceProviderImpl(org.gephi.project.impl.WorkspaceProviderImpl) DataOutputStream(java.io.DataOutputStream) WorkspacePersistenceProvider(org.gephi.project.spi.WorkspacePersistenceProvider) IOException(java.io.IOException) IOException(java.io.IOException) WorkspaceXMLPersistenceProvider(org.gephi.project.spi.WorkspaceXMLPersistenceProvider) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) WorkspaceBytesPersistenceProvider(org.gephi.project.spi.WorkspaceBytesPersistenceProvider) FileLock(org.openide.filesystems.FileLock) FileObject(org.openide.filesystems.FileObject) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Workspace(org.gephi.project.api.Workspace)

Aggregations

WorkspaceProviderImpl (org.gephi.project.impl.WorkspaceProviderImpl)3 WorkspacePersistenceProvider (org.gephi.project.spi.WorkspacePersistenceProvider)3 WorkspaceXMLPersistenceProvider (org.gephi.project.spi.WorkspaceXMLPersistenceProvider)3 Workspace (org.gephi.project.api.Workspace)2 WorkspaceBytesPersistenceProvider (org.gephi.project.spi.WorkspaceBytesPersistenceProvider)2 WorkspaceImpl (org.gephi.workspace.impl.WorkspaceImpl)2 WorkspaceInformationImpl (org.gephi.workspace.impl.WorkspaceInformationImpl)2 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ProjectControllerImpl (org.gephi.project.impl.ProjectControllerImpl)1 ProjectImpl (org.gephi.project.impl.ProjectImpl)1