1 19 package org.netbeans.modules.javacore.parser; 20 21 import java.io.IOException ; 22 import java.io.Reader ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import org.netbeans.modules.javacore.JMManager; 27 import org.openide.filesystems.FileObject; 28 import org.openide.loaders.DataObject; 29 import org.openide.text.CloneableEditorSupport; 30 31 36 public class Util { 37 static final String ATTR_FILE_ENCODING = "Content-Encoding"; 39 48 public static String getFileEncoding(FileObject someFile) { 49 String enc = (String ) someFile.getAttribute(ATTR_FILE_ENCODING); 50 51 if (enc == null) { 52 enc = JMManager.getDefaultEncoding(); 53 } 54 if ("".equals(enc)) 55 return null; 56 else 57 return enc; 58 } 59 60 public static String readContents(Reader r, long size) throws IOException { 61 assert size<Integer.MAX_VALUE; 62 char buffer[] = new char[(int)size]; 63 int offset = 0; 64 while (offset < buffer.length) { 65 long read = r.read(buffer, offset, buffer.length - offset); 66 if (read == -1) break; 67 offset += read; 68 } 69 return new String (buffer,0,offset); 70 } 71 72 73 82 public static CloneableEditorSupport findCloneableEditorSupport(DataObject dob) { 83 Object obj = dob.getCookie(CloneableEditorSupport.class); 84 if (obj instanceof CloneableEditorSupport) { 85 return (CloneableEditorSupport)obj; 86 } 87 obj = dob.getCookie(org.openide.cookies.OpenCookie.class); 88 if (obj instanceof CloneableEditorSupport) { 89 return (CloneableEditorSupport)obj; 90 } 91 obj = dob.getCookie(org.openide.cookies.EditorCookie.class); 92 if (obj instanceof CloneableEditorSupport) { 93 return (CloneableEditorSupport)obj; 94 } 95 return null; 96 } 97 98 public static boolean isModified(FileObject fo) { 99 return getModifiedDataObject(fo)!=null; 100 } 101 102 public static DataObject getModifiedDataObject(FileObject fo) { 103 DataObject[] dobjs = DataObject.getRegistry().getModified(); 104 for (int i = 0; i < dobjs.length; i++) { 105 FileObject fobj = dobjs[i].getPrimaryFile(); 106 if (fobj != null && fobj.isValid() && fobj.equals(fo)) { 107 return dobjs[i]; 108 } 109 } 110 JMManager.ModifiedDOProvider p = JMManager.ModifiedDOProvider.getModifiedDOProvider(); 111 if (p != null) { 112 return p.getModifiedDataObject(fo); 113 } 114 return null; 115 } 116 } 117 | Popular Tags |