426a5e92f790df4697dd1729b4c81c7b7b717eaa
[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                                 if (location.isDefine()) {
97                                         length = getClosingParenthesis(functionDeclarationCharArray);
98                                         if (length < 0)
99                                                 return "";
100                                 } else {
101                                         for (int i = 0; i < length; i++) {
102                                                 if (functionDeclarationCharArray[i] == ')') {
103                                                         length = i + 1;
104                                                         break;
105                                                 }
106                                                 if (functionDeclarationCharArray[i] == '{'
107                                                                 || functionDeclarationCharArray[i] == '}') {
108                                                         length = i;
109                                                         break;
110                                                 }
111                                         }
112                                 }
113                                 usage = new String(functionDeclarationCharArray, 0, length);
114                                 // cache the usage string:
115                                 location.setUsage(usage);
116                         }
117                         phpFileReader.close();
118
119                 } catch (IOException e) {
120                         // do nothing
121                 }
122                 return usage;
123         }
124
125         private static InputStreamReader createReader(String filename) {
126                 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
127                                 .getFileForLocation(new Path(filename));
128                 if (file != null) {
129                         try {
130                                 return new InputStreamReader(new FileInputStream(file
131                                                 .getLocation().toString()), file.getCharset());
132                         } catch (UnsupportedEncodingException e) {
133                                 // do nothing
134                         } catch (FileNotFoundException e) {
135                                 // do nothing
136                         } catch (CoreException e) {
137                                 // do nothing
138                         }
139                 }
140                 return null;
141         }
142
143         private static int getClosingParenthesis(char[] buffer) {
144                 int p = 0;
145                 boolean dq = false;
146                 boolean sq = false;
147
148                 for (int i = 0; i < buffer.length; i++) {
149                         if (buffer[i] == '\\') {
150                                 i++;
151                                 continue;
152                         }
153                         if (dq) {
154                                 dq = (buffer[i] != '"');
155                                 continue;
156                         }
157                         if (sq) {
158                                 sq = (buffer[i] != '\'');
159                                 continue;
160                         }
161                         switch (buffer[i]) {
162                         case '(':
163                                 p++;
164                                 break;
165                         case ')':
166                                 p--;
167                                 if (p < 0)
168                                         return i;
169                                 break;
170                         case '"':
171                                 dq = true;
172                                 break;
173                         case '\'':
174                                 sq = true;
175                                 break;
176                         }
177                 }
178                 return -1;
179         }
180
181 }