KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > UTSaveCookie


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks;
21
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.net.URISyntaxException JavaDoc;
28 import java.text.ParseException JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import net.fortuna.ical4j.model.ValidationException;
31 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
32 import org.netbeans.modules.tasklist.usertasks.translators.ICalExportFormat;
33 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
34 import org.openide.cookies.SaveCookie;
35 import org.openide.filesystems.FileLock;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.loaders.DataObject;
39
40 /**
41  * SaveCookie for .ics files.
42  *
43  * @author tl
44  */

45 public class UTSaveCookie implements SaveCookie {
46     private DataObject do_;
47     private UserTaskList utl;
48     
49     /**
50      * Creates a new instance of UTSaveCookie
51      *
52      * @param do_ TaskListDataObject
53      * @param utl task list
54      */

55     public UTSaveCookie(DataObject do_, UserTaskList utl) {
56         this.do_ = do_;
57         this.utl = utl;
58     }
59
60     public void save() throws IOException JavaDoc {
61         ICalExportFormat io = new ICalExportFormat();
62         
63         FileObject file = do_.getPrimaryFile();
64         FileLock lock = file.lock();
65         try {
66             Writer JavaDoc w = new OutputStreamWriter JavaDoc(new BufferedOutputStream JavaDoc(
67                     file.getOutputStream(lock)), "UTF-8"); // NOI18N
68
try {
69                 io.writeList(utl, w, false);
70             } catch (ParseException JavaDoc e) {
71                 throw new IOException JavaDoc(e.getMessage());
72             } catch (URISyntaxException JavaDoc e) {
73                 throw new IOException JavaDoc(e.getMessage());
74             } catch (ValidationException e) {
75                 throw new IOException JavaDoc(e.getMessage());
76             } finally {
77                 try {
78                     w.close();
79                 } catch (IOException JavaDoc e) {
80                     UTUtils.LOGGER.log(Level.WARNING,
81                             "failed closing file", e); // NOI18N
82
}
83             }
84         } finally {
85             lock.releaseLock();
86         }
87
88         // Remove permissions for others on the file when on Unix
89
// varieties
90
if (new File JavaDoc("/bin/chmod").exists()) { // NOI18N
91
try {
92                 Runtime.getRuntime().exec(
93                      new String JavaDoc[] {"/bin/chmod", "go-rwx", // NOI18N
94
FileUtil.toFile(file).getAbsolutePath()});
95             } catch (Exception JavaDoc e) {
96                 // Silently accept
97
UTUtils.LOGGER.log(Level.INFO,
98                         "chmod call failed", e); // NOI18N
99
}
100         }
101
102         do_.setModified(false);
103     }
104 }
105
Popular Tags