1 19 package org.netbeans.modules.xml.core.sync; 20 21 import java.util.Date ; 22 import java.io.*; 23 import java.net.URL ; 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 41 public class FileRepresentation extends SyncRepresentation { 42 43 private final DataObject dataObject; 44 private Date lastSave; 45 46 47 public FileRepresentation (DataObject dataObject, Synchronizator sync) { 48 super (sync); 49 50 this.dataObject = dataObject; 51 52 lastSave = getFileObject().lastModified(); 53 } 54 55 56 58 private FileObject getFileObject () { 59 return dataObject.getPrimaryFile(); 60 } 61 62 63 66 public boolean represents(Class type) { 67 return FileObject.class.isAssignableFrom(type); 68 } 69 70 74 public void update(Object change) { 75 } 84 85 88 public Class getUpdateClass() { 89 return null; 90 } 91 92 95 public boolean isModified() { 96 return lastSave.getTime() < getFileObject().lastModified().getTime(); 97 } 98 99 103 public String getDisplayName() { 104 return Util.THIS.getString ("PROP_File_representation"); 105 } 106 107 110 public Object getChange(Class type) { 111 112 if (type == null || type.isAssignableFrom(InputSource.class)) { 113 try { 114 InputSource source = new InputSource(getFileObject().getURL().toExternalForm()); 116 InputStream in = new BufferedInputStream(getFileObject().getInputStream()); 117 String encoding = EncodingHelper.detectEncoding(in); 118 if ( encoding == null ) { 119 encoding = "UTF8"; } 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 ("FileRepresentation does not support: " + type); } 137 138 public int level() { 139 return 0; 140 } 141 142 private class FileListener extends FileChangeAdapter { 144 145 148 public void fileChanged (FileEvent fe) { 149 getSynchronizator().representationChanged(FileObject.class); 150 } 151 152 } 154 } 155 | Popular Tags |