Search in sources :

Example 1 with ProjectImpl

use of org.gephi.project.impl.ProjectImpl in project gephi by gephi.

the class LoadTask method readProject.

private ProjectImpl readProject(ZipFile zipFile) throws Exception {
    ZipEntry entry = zipFile.getEntry("Project_xml");
    if (entry == null) {
        // Try legacy
        entry = zipFile.getEntry("Project");
    }
    if (entry != null) {
        InputStream is = null;
        try {
            is = zipFile.getInputStream(entry);
            InputStreamReader isReader = null;
            Xml10FilterReader filterReader = null;
            XMLStreamReader reader = null;
            try {
                XMLInputFactory inputFactory = XMLInputFactory.newInstance();
                if (inputFactory.isPropertySupported("javax.xml.stream.isValidating")) {
                    inputFactory.setProperty("javax.xml.stream.isValidating", Boolean.FALSE);
                }
                inputFactory.setXMLReporter(new XMLReporter() {

                    @Override
                    public void report(String message, String errorType, Object relatedInformation, Location location) throws XMLStreamException {
                    }
                });
                isReader = new InputStreamReader(is, "UTF-8");
                filterReader = new Xml10FilterReader(isReader);
                reader = inputFactory.createXMLStreamReader(filterReader);
                ProjectControllerImpl projectController = Lookup.getDefault().lookup(ProjectControllerImpl.class);
                ProjectsImpl projects = projectController.getProjects();
                ProjectImpl project = GephiReader.readProject(reader, projects);
                project.getLookup().lookup(ProjectInformationImpl.class).setFile(file);
                return project;
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (filterReader != null) {
                    filterReader.close();
                }
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }
    return null;
}
Also used : ProjectControllerImpl(org.gephi.project.impl.ProjectControllerImpl) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ProjectImpl(org.gephi.project.impl.ProjectImpl) ZipEntry(java.util.zip.ZipEntry) ProjectsImpl(org.gephi.project.impl.ProjectsImpl) XMLReporter(javax.xml.stream.XMLReporter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory) ProjectInformationImpl(org.gephi.project.impl.ProjectInformationImpl) Location(javax.xml.stream.Location)

Example 2 with ProjectImpl

use of org.gephi.project.impl.ProjectImpl 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 3 with ProjectImpl

use of org.gephi.project.impl.ProjectImpl in project gephi by gephi.

the class GephiWriter method writeProject.

public static void writeProject(XMLStreamWriter writer, Project project) throws Exception {
    writer.writeStartDocument("UTF-8", "1.0");
    writer.writeStartElement("projectFile");
    // Header
    writer.writeAttribute("version", VERSION);
    writer.writeStartElement("lastModifiedDate");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    writer.writeCharacters(sdf.format(cal.getTime()));
    writer.writeComment("yyyy-MM-dd HH:mm:ss");
    writer.writeEndElement();
    writer.writeComment("File saved with " + getVersion());
    ProjectInformation info = project.getLookup().lookup(ProjectInformation.class);
    ProjectMetaData metaData = project.getLookup().lookup(ProjectMetaData.class);
    // Start Project
    writer.writeStartElement("project");
    writer.writeAttribute("name", info.getName());
    writer.writeAttribute("ids", String.valueOf(((ProjectImpl) project).getWorkspaceIds()));
    // MetaData
    writer.writeStartElement("metadata");
    writer.writeStartElement("title");
    writer.writeCharacters(metaData.getTitle());
    writer.writeEndElement();
    writer.writeStartElement("keywords");
    writer.writeCharacters(metaData.getKeywords());
    writer.writeEndElement();
    writer.writeStartElement("description");
    writer.writeCharacters(metaData.getDescription());
    writer.writeEndElement();
    writer.writeStartElement("author");
    writer.writeCharacters(metaData.getAuthor());
    writer.writeEndElement();
    writer.writeEndElement();
    // End Metadata
    writer.writeEndElement();
    // End Project
    writer.writeEndElement();
    writer.writeEndDocument();
}
Also used : ProjectImpl(org.gephi.project.impl.ProjectImpl) Calendar(java.util.Calendar) ProjectMetaData(org.gephi.project.api.ProjectMetaData) ProjectInformation(org.gephi.project.api.ProjectInformation) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with ProjectImpl

use of org.gephi.project.impl.ProjectImpl in project gephi by gephi.

the class GephiReader method readProject.

public static ProjectImpl readProject(XMLStreamReader reader, ProjectsImpl projects) throws Exception {
    ProjectImpl project = null;
    boolean end = false;
    while (reader.hasNext() && !end) {
        Integer eventType = reader.next();
        if (eventType.equals(XMLEvent.START_ELEMENT)) {
            String name = reader.getLocalName();
            if ("projectFile".equalsIgnoreCase(name) || "gephiFile".equalsIgnoreCase(name)) {
                // Version
                String version = reader.getAttributeValue(null, "version");
                if (version == null || version.isEmpty() || Double.parseDouble(version) < Double.parseDouble(VERSION)) {
                    throw new GephiFormatException("Gephi project file version must be at least of version " + VERSION);
                }
            } else if ("project".equalsIgnoreCase(name)) {
                String projectName = reader.getAttributeValue(null, "name");
                project = new ProjectImpl(projectName);
                project.getLookup().lookup(WorkspaceProviderImpl.class);
                if (reader.getAttributeValue(null, "ids") != null) {
                    Integer workspaceIds = Integer.parseInt(reader.getAttributeValue(null, "ids"));
                    project.setWorkspaceIds(workspaceIds);
                }
            } else if ("workspace".equalsIgnoreCase(name)) {
                // Legacy workspace reading
                readWorkspaceLegacy(reader, project);
            }
        } else if (eventType.equals(XMLStreamReader.END_ELEMENT)) {
            if ("project".equalsIgnoreCase(reader.getLocalName())) {
                end = true;
            }
        }
    }
    return project;
}
Also used : WorkspaceProviderImpl(org.gephi.project.impl.WorkspaceProviderImpl) ProjectImpl(org.gephi.project.impl.ProjectImpl)

Aggregations

ProjectImpl (org.gephi.project.impl.ProjectImpl)4 ZipEntry (java.util.zip.ZipEntry)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 ProjectControllerImpl (org.gephi.project.impl.ProjectControllerImpl)2 WorkspaceProviderImpl (org.gephi.project.impl.WorkspaceProviderImpl)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 ZipFile (java.util.zip.ZipFile)1 Location (javax.xml.stream.Location)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLReporter (javax.xml.stream.XMLReporter)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 ProjectInformation (org.gephi.project.api.ProjectInformation)1 ProjectMetaData (org.gephi.project.api.ProjectMetaData)1 Workspace (org.gephi.project.api.Workspace)1