KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > Util


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.javacore.parser;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
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 /**
32  *
33  * @author sdedic
34  * @version
35  */

36 public class Util {
37     static final String JavaDoc ATTR_FILE_ENCODING = "Content-Encoding"; // NOI18N
38

39     /** Retrieves file's encoding. Returns the default encoding for java files,
40      * if the file's specific encoding is not set.
41      * Disclaimer: this method is <B>not an API method</B>. It may not
42      * be available in future releases.
43      * @param someFile file object to retrieve encoding for
44      * @return encoding string, or null if no explicit encoding is given in file's
45      * or IDE's settings.
46      *
47      */

48     public static String JavaDoc getFileEncoding(FileObject someFile) {
49         String JavaDoc enc = (String JavaDoc) 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 JavaDoc readContents(Reader JavaDoc r, long size) throws IOException JavaDoc {
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 JavaDoc(buffer,0,offset);
70     }
71     
72     
73     /**
74      * Method that allows to find its
75      * CloneableEditorSupport from given DataObject
76      * @return the support or null if the CloneableEditorSupport
77      * was not found
78      * This method is hot fix for issue #53309
79      * this methd was copy/pasted from OpenSupport.Env class
80      * @param dob an instance of DataObject
81      */

82     public static CloneableEditorSupport findCloneableEditorSupport(DataObject dob) {
83         Object JavaDoc 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