5bf73a7523fa1896c50c753162b0f271d316b046
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / phpdoc / PHPDocUtil.java
1 package net.sourceforge.phpdt.internal.corext.phpdoc;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.UnsupportedEncodingException;
8
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
11
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.Path;
15
16 /**
17  * Utility class for static PHPdoc helper mehods
18  */
19 public class PHPDocUtil {
20
21         /**
22          * Generate a PHPDoc hover text if possible
23          * 
24          * @param hoverInfoBuffer
25          * @param filename
26          * @param location
27          */
28         public static void appendPHPDoc(StringBuffer hoverInfoBuffer,
29                         String filename, PHPIdentifierLocation location) {
30                 hoverInfoBuffer.append(location.toString());
31                 hoverInfoBuffer.append(" - <b>");
32                 try {
33                         hoverInfoBuffer.append(getUsage(filename, location));
34                         hoverInfoBuffer.append("</b><br>");
35
36                         // read the phpdoc for the function
37                         if (location.getPHPDocOffset() >= 0) {
38                                 InputStreamReader phpFileReader = createReader(filename);
39                                 if (phpFileReader == null)
40                                         return;
41                                 char[] phpDocDeclarationCharArray = new char[location
42                                                 .getPHPDocLength()];
43                                 phpFileReader.skip(location.getPHPDocOffset());
44                                 phpFileReader.read(phpDocDeclarationCharArray, 0, location
45                                                 .getPHPDocLength());
46                                 PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader(
47                                                 phpDocDeclarationCharArray);
48                                 hoverInfoBuffer.append(phpdocConverter.getString());
49                                 phpFileReader.close();
50                         }
51
52                 } catch (IOException e) {
53                         // TODO: smell
54                         return;
55                 }
56         }
57
58         static String getEncoding(String filename) {
59                 String encoding = null;
60                 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
61                                 .getFileForLocation(new Path(filename));
62                 if (file != null) {
63                         try {
64                                 encoding = file.getCharset();
65                         } catch (CoreException e) {
66                                 // TODO: should log the fact that we could not get the encoding?
67                         }
68                 }
69                 return encoding;
70         }
71
72         public static String getUsage(String filename,
73                         PHPIdentifierLocation location) {
74                 String usage = location.getUsage();
75                 if (usage != null) {
76                         return usage;
77                 }
78                 usage = "";
79                 try {
80
81                         InputStreamReader phpFileReader = createReader(filename);
82                         if (phpFileReader == null)
83                                 return "";
84                         // read the function declaration
85                         if (location.getOffset() >= 0
86                                         && (location.isMethod() || location.isConstructor()
87                                                         || location.isFunction() || location.isDefine())) {
88                                 char[] functionDeclarationCharArray = new char[256];
89                                 int offset = location.getOffset();
90                                 phpFileReader.skip(offset);
91                                 int length = phpFileReader.read(functionDeclarationCharArray,
92                                                 0, 256);
93                                 if (length == -1) {
94                                         length = 256;
95                                 }
96                                 for (int i = 0; i < length; i++) {
97                                         if (functionDeclarationCharArray[i] == ')') {
98                                                 length = i + 1;
99                                                 break;
100                                         }
101                                         if (functionDeclarationCharArray[i] == '{'
102                                                         || functionDeclarationCharArray[i] == '}') {
103                                                 length = i;
104                                                 break;
105                                         }
106                                 }
107                                 usage = new String(functionDeclarationCharArray, 0, length);
108                                 // cache the usage string:
109                                 location.setUsage(usage);
110                         }
111                         phpFileReader.close();
112
113                 } catch (IOException e) {
114                         // do nothing
115                 }
116                 return usage;
117         }
118
119         private static InputStreamReader createReader(String filename) {
120                 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
121                                 .getFileForLocation(new Path(filename));
122                 if (file != null) {
123                         try {
124                                 return new InputStreamReader(new FileInputStream(file
125                                                 .getLocation().toString()), file.getCharset());
126                         } catch (UnsupportedEncodingException e) {
127                                 // do nothing
128                         } catch (FileNotFoundException e) {
129                                 // do nothing
130                         } catch (CoreException e) {
131                                 // do nothing
132                         }
133                 }
134                 return null;
135         }
136
137 }