Compatibility fragment commit
[phpeclipse.git] / net.sourceforge.phpeclipse.32.compatibility / src / net / sourceforge / phpdt / internal / corext / phpdoc / PHPDocUtil.java
1 package net.sourceforge.phpdt.internal.corext.phpdoc;
2
3 import java.io.FileReader;
4 import java.io.IOException;
5
6 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
7
8 /**
9  * Utility class for static PHPdoc helper mehods
10  */
11 public class PHPDocUtil {
12
13         /**
14          * Generate a PHPDoc hover text if possible
15          * 
16          * @param hoverInfoBuffer
17          * @param filename
18          * @param location
19          */
20         public static void appendPHPDoc(StringBuffer hoverInfoBuffer,
21                         String filename, PHPIdentifierLocation location) {
22                 FileReader phpFileReader;
23                 hoverInfoBuffer.append(location.toString());
24                 hoverInfoBuffer.append(" - <b>");
25                 try {
26                         hoverInfoBuffer.append(getUsage(filename, location));
27                         hoverInfoBuffer.append("</b><br>");
28
29                         // read the phpdoc for the function
30                         if (location.getPHPDocOffset() >= 0) {
31                                 phpFileReader = new FileReader(filename);
32                                 char[] phpDocDeclarationCharArray = new char[location
33                                                 .getPHPDocLength()];
34                                 phpFileReader.skip(location.getPHPDocOffset());
35                                 phpFileReader.read(phpDocDeclarationCharArray, 0, location
36                                                 .getPHPDocLength());
37                                 PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader(
38                                                 phpDocDeclarationCharArray);
39                                 hoverInfoBuffer.append(phpdocConverter.getString());
40                                 // hoverInfoBuffer.append("<br><br>");
41                                 phpFileReader.close();
42                         }
43
44                 } catch (IOException e) {
45                         return;
46                 }
47         }
48
49         public static String getUsage(String filename,
50                         PHPIdentifierLocation location) {
51                 FileReader phpFileReader;
52                 String usage = location.getUsage();
53                 if (usage != null) {
54                         return usage;
55                 }
56                 usage = "";
57                 try {
58
59                         phpFileReader = new FileReader(filename);
60                         // read the function declaration
61                         if (location.getOffset() >= 0
62                                         && (location.isMethod() || location.isConstructor()
63                                                         || location.isFunction() || location.isDefine())) {
64                                 char[] functionDeclarationCharArray = new char[256];
65                                 int offset = location.getOffset();
66                                 phpFileReader.skip(offset);
67                                 int length = phpFileReader.read(functionDeclarationCharArray,
68                                                 0, 256);
69                                 if (length == -1) {
70                                         length = 256;
71                                 }
72                                 for (int i = 0; i < length; i++) {
73                                         if (functionDeclarationCharArray[i] == ')') {
74                                                 length = i + 1;
75                                                 break;
76                                         }
77                                         if (functionDeclarationCharArray[i] == '{'
78                                                         || functionDeclarationCharArray[i] == '}') {
79                                                 length = i;
80                                                 break;
81                                         }
82                                 }
83                                 usage = new String(functionDeclarationCharArray, 0, length);
84                         }
85                         phpFileReader.close();
86                 } catch (IOException e) {
87                         // do nothing
88                 }
89                 // cache the usage string:
90                 location.setUsage(usage);
91                 return usage;
92         }
93 }