KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > sync > FileRepresentation


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 package org.netbeans.modules.xml.core.sync;
20
21 import java.util.Date JavaDoc;
22 import java.io.*;
23 import java.net.URL JavaDoc;
24
25 import org.xml.sax.*;
26
27 import org.openide.filesystems.*;
28 import org.openide.loaders.DataObject;
29
30 import org.netbeans.modules.xml.core.*;
31 import org.netbeans.modules.xml.core.lib.*;
32 import org.netbeans.modules.xml.core.sync.*;
33
34 /**
35  * This representation stays for external representation at filesystem.
36  * It may be modified externally at any time!
37  *
38  * @author Petr Kuzel
39  * @version
40  */

41 public class FileRepresentation extends SyncRepresentation {
42
43     private final DataObject dataObject;
44     private Date JavaDoc lastSave;
45     
46     /** Creates new FileRepresentation */
47     public FileRepresentation (DataObject dataObject, Synchronizator sync) {
48         super (sync);
49
50         this.dataObject = dataObject;
51
52         lastSave = getFileObject().lastModified();
53     }
54
55
56     /**
57      */

58     private FileObject getFileObject () {
59         return dataObject.getPrimaryFile();
60     }
61
62
63     /**
64      * Does this representation wraps given model?
65      */

66     public boolean represents(Class JavaDoc type) {
67         return FileObject.class.isAssignableFrom(type);
68     }
69
70     /**
71      * Update the representation without marking it as modified.
72      * User MUST use SaveCookie explicitly to update it.
73      */

74     public void update(Object JavaDoc change) {
75 // SaveCookie save = (SaveCookie) getCookie(SaveCookie.class);
76
// if (save != null) {
77
// save.save();
78
// lastUpdate = getFileObject().lastModified();
79
// } else {
80
// //!!! is is modified and does not have save cookie
81
// // introduce prepare() + commit() ??
82
// }
83
}
84
85     /**
86      * Return accepted update class
87      */

88     public Class JavaDoc getUpdateClass() {
89         return null;
90     }
91
92     /**
93      * Is this representation modified since last sync?
94      */

95     public boolean isModified() {
96         return lastSave.getTime() < getFileObject().lastModified().getTime();
97     }
98
99     /**
100      * @return select button diplay name used during notifying concurent modification
101      * conflict.
102      */

103     public String JavaDoc getDisplayName() {
104         return Util.THIS.getString ("PROP_File_representation");
105     }
106
107     /**
108      * Return modification passed as update parameter to all slave representations.
109      */

110     public Object JavaDoc getChange(Class JavaDoc type) {
111
112         if (type == null || type.isAssignableFrom(InputSource.class)) {
113             try {
114                 //!!! try to autodectect encoding since these routines in Xerces2 are bad
115
InputSource source = new InputSource(getFileObject().getURL().toExternalForm());
116                 InputStream in = new BufferedInputStream(getFileObject().getInputStream());
117                 String JavaDoc encoding = EncodingHelper.detectEncoding(in);
118                 if ( encoding == null ) {
119                     encoding = "UTF8"; //!!! // NOI18N
120
}
121                 source.setCharacterStream(new InputStreamReader(in, encoding));
122                 return source;
123                 
124             } catch (IOException ex) {
125                 return null;
126             }
127         } else if (type.isAssignableFrom(InputStream.class)) {
128             try {
129                 return getFileObject().getInputStream();
130             } catch (IOException ex) {
131                 return null;
132             }
133         }
134
135         throw new RuntimeException JavaDoc("FileRepresentation does not support: " + type); // NOI18N
136
}
137
138     public int level() {
139         return 0;
140     }
141
142     // Listents for extarnal modification and deletion
143
private class FileListener extends FileChangeAdapter {
144
145         /** Fired when a file is changed.
146          * @param fe the event describing context where action has taken place
147          */

148         public void fileChanged (FileEvent fe) {
149             getSynchronizator().representationChanged(FileObject.class);
150         }
151
152     } // end of inner class FileListener
153

154 }
155
Popular Tags