org.gvsig.app.mainpligin#4835.diff

JoaquĆ­n del Cerro Murciano, 05/11/2018 09:18 PM

Download (9.75 KB)

View differences:

src/main/java/org/gvsig/app/extension/ProjectExtension.java (copia de trabajo)
37 37
import java.util.Set;
38 38
import java.util.prefs.Preferences;
39 39
import java.util.zip.ZipEntry;
40
import java.util.zip.ZipException;
41
import java.util.zip.ZipFile;
40 42
import java.util.zip.ZipOutputStream;
41 43
import javax.imageio.ImageIO;
42 44

  
......
446 448
		return writeProject(file, p, true);
447 449
	}
448 450

  
449
	/**
450
	 * Escribe el proyecto en XML. Pero permite decidir si se pide confirmaci?n
451
	 * para sobreescribir
452
	 *
453
	 * @param file
454
	 *            Fichero.
455
	 * @param p
456
	 *            Proyecto.
457
	 * @param askConfirmation
458
	 *            boolean
459
	 */
460
	public boolean writeProject(File file, Project p, boolean askConfirmation) {
461
		if (askConfirmation && file.exists()) {
462
			int resp = JOptionPane.showConfirmDialog((Component) PluginServices
463
					.getMainFrame(), PluginServices.getText(this,
464
					"fichero_ya_existe_seguro_desea_guardarlo"), PluginServices
465
					.getText(this, "guardar"), JOptionPane.YES_NO_OPTION);
466
			if (resp != JOptionPane.YES_OPTION) {
467
				return false;
468
			}
469
		}
470
		NotificationManager.addInfo(PluginServices.getText(this,
471
				"writing_project") + ": " + file.getName());
451
    /**
452
     * Escribe el proyecto en disco. 
453
     * Pero permite decidir si se pide confirmaci?n para sobreescribir
454
     *
455
     * @param file Fichero.
456
     * @param p Proyecto.
457
     * @param askConfirmation boolean
458
     * @return
459
     */
460
public boolean writeProject(File file, Project p, boolean askConfirmation) {
461
        I18nManager i18n = ToolsLocator.getI18nManager();
462
        ApplicationManager application = ApplicationLocator.getManager();
463
        if (askConfirmation && file.exists()) {
464
            int resp = application.confirmDialog(
465
                    i18n.getTranslation("fichero_ya_existe_seguro_desea_guardarlo"), 
466
                    i18n.getTranslation("guardar"), 
467
                    JOptionPane.YES_NO_OPTION, 
468
                    JOptionPane.QUESTION_MESSAGE, 
469
                    "Overwrite_project_file"
470
            );
471
            if (resp != JOptionPane.YES_OPTION) {
472
                return false;
473
            }
474
        }
475
        FileOutputStream fout=null;
476
        ZipOutputStream zout=null;
477
        LOG.info("Writing project '"+ file.getAbsolutePath()+"'.");
478
        try {
479
            fireBeforeSavingFileEvent(new SaveEvent(this,SaveEvent.BEFORE_SAVING, file));
472 480

  
473
		// write it out as XML
474
		try {
475
			fireBeforeSavingFileEvent(new SaveEvent(this,
476
					SaveEvent.BEFORE_SAVING, file));
477
            
478
            FileOutputStream fout = new FileOutputStream(file);
479
            ZipOutputStream zout = new ZipOutputStream(fout);
481
            fout = new FileOutputStream(file);
482
            zout = new ZipOutputStream(fout);
480 483
            p.saveState(zout);
481
            
484

  
482 485
            zout.putNextEntry(new ZipEntry("preview.jpg"));
483 486
            BufferedImage img = ApplicationLocator.getManager().getUIManager().getImagePreview();
484 487
            img = scale(img, 0.40);
485 488
            try {
486 489
                ImageIO.write(img, "jpg", zout);
487 490
            } catch (IOException ex) {
488
                LOG.warn("Can't save preview image'.",ex);
491
                LOG.warn("Can't save preview image'.", ex);
489 492
            }
490
            
493
            fireAfterSavingFileEvent(new SaveEvent(this, SaveEvent.AFTER_SAVING, file));
491 494
            IOUtils.closeQuietly(zout);
492 495
            IOUtils.closeQuietly(fout);
496

  
497
            if( !isValidZIP(file) ) {
498
                throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'");
499
            }
493 500
            
494
			fireAfterSavingFileEvent(new SaveEvent(this,
495
					SaveEvent.AFTER_SAVING, file));
501
            PluginServices.getMainFrame().setTitle(file.getName());
502
            setPath(file.toString());
496 503

  
497
			PluginServices.getMainFrame().setTitle(file.getName());
498
			setPath(file.toString());
504
        } catch (Exception e) {
505
            application.messageDialog(
506
                    i18n.getTranslation("_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again"), 
507
                    null, 
508
                    i18n.getTranslation("guardar"), 
509
                    JOptionPane.ERROR_MESSAGE, 
510
                    "Problems_saving_the_project"
511
            );
512
            LOG.warn("Error writing project '"+file.getAbsolutePath()+"'.", e);
513
            return false;
514
        } finally {
515
            IOUtils.closeQuietly(zout);
516
            IOUtils.closeQuietly(fout);
517
        }
518
        
519
        LOG.warn("Wrote project '"+file.getAbsolutePath()+"'.");
520
        return true;
521
    }
499 522

  
500
		} catch (PersistenceException e) {
501
			String messagestack = e.getLocalizedMessageStack();
502
			NotificationManager.addError(
503
					PluginServices.getText(this, "error_writing_project")
504
							+ ": " + file.getName() + "\n" + messagestack, e);
505
			return false;
506
		} catch (Exception e) {
507
			NotificationManager.addError(
508
					PluginServices.getText(this, "error_writing_project")
509
							+ ": " + file.getName(), e);
510
			return false;
511
		}
512
		NotificationManager.addInfo(PluginServices.getText(this,
513
				"wrote_project") + ": " + file.getName());
514
		return true;
515
	}
523
    boolean isValidZIP(final File file) {
524
        ZipFile zipfile = null;
525
        try {
526
            zipfile = new ZipFile(file);
527
            return true;
528
        } catch (IOException e) {
529
            return false;
530
        } finally {
531
            try {
532
                if (zipfile != null) {
533
                    zipfile.close();
534
                    zipfile = null;
535
                }
536
            } catch (IOException e) {
537
            }
538
        }
539
    }
516 540

  
517 541
    private BufferedImage scale(BufferedImage before, double factor) {
518 542
        int w = (int) (before.getWidth()*factor);
src/main/java/org/gvsig/app/project/DefaultProject.java (copia de trabajo)
30 30
import java.io.FileInputStream;
31 31
import java.io.FileNotFoundException;
32 32
import java.io.FileOutputStream;
33
import java.io.IOException;
33 34
import java.io.InputStream;
34 35
import java.io.OutputStream;
35 36
import java.io.Serializable;
......
44 45
import java.util.List;
45 46
import java.util.Map;
46 47
import java.util.Set;
48
import java.util.zip.ZipException;
49
import java.util.zip.ZipFile;
47 50

  
48 51
import org.cresques.cts.IProjection;
49 52

  
......
69 72
import org.gvsig.fmap.mapcontext.layers.FLayers;
70 73
import org.gvsig.tools.ToolsLocator;
71 74
import org.gvsig.tools.dynobject.DynStruct;
72
import org.gvsig.tools.exception.ListBaseException;
73 75
import org.gvsig.tools.observer.ObservableHelper;
74 76
import org.gvsig.tools.observer.Observer;
75 77
import org.gvsig.tools.persistence.PersistenceManager;
......
542 544
        } catch (FileNotFoundException e) {
543 545
            throw new PersistenceException(e);
544 546
        }
547
        if( !isValidZIP(out) ) {
548
            throw new PersistenceException(new ZipException());
549
        }
545 550
        this.fname = out;
546 551
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out);
547 552
    }
548 553

  
554
    boolean isValidZIP(final File file) {
555
        ZipFile zipfile = null;
556
        try {
557
            zipfile = new ZipFile(file);
558
            return true;
559
        } catch (IOException e) {
560
            return false;
561
        } finally {
562
            try {
563
                if (zipfile != null) {
564
                    zipfile.close();
565
                    zipfile = null;
566
                }
567
            } catch (IOException e) {
568
            }
569
        }
570
    }
571
   
549 572
    public File getFile() {
550 573
        return this.fname;
551 574
    }
src/main/resources-plugin/i18n/text.properties (copia de trabajo)
1287 1287
_Change_view_projection=CRS Vista
1288 1288
_Change_view_projection_to_projection_of_the_selected_item_in_the_list=Cambia la proyecci\u00f3n de la Vista a la proyecci\u00f3n del item seleccionado en la lista.
1289 1289
_precision_comparing_decimal_numbers=Precisi\u00f3n de comparaci\u00f3n de n\u00fameros decimales
1290
_default_expression_evaluator=Evaluador de expresiones por defecto
1290
_default_expression_evaluator=Evaluador de expresiones por defecto
1291
_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again=Problemas guardando el proyecto.\nEs posible que este no se haya guardado correctamente y no se pueda volver a cargar.
src/main/resources-plugin/i18n/text_en.properties (copia de trabajo)
1206 1206
_Change_view_projection=CRS View
1207 1207
_Change_view_projection_to_projection_of_the_selected_item_in_the_list=Change View projection to the projection of the selected item in the list
1208 1208
_precision_comparing_decimal_numbers=Precision comparing decimal numbers
1209
_default_expression_evaluator=Default expression evaluator
1209
_default_expression_evaluator=Default expression evaluator
1210
_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again=Problems saving the project.\nIt is possible that this was not saved properly and can not be loaded again.