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