1 package net.sourceforge.phpdt.internal.corext.phpdoc;
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;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.Path;
17 * Utility class for static PHPdoc helper mehods
19 public class PHPDocUtil {
22 * Generate a PHPDoc hover text if possible
24 * @param hoverInfoBuffer
28 public static void appendPHPDoc(StringBuffer hoverInfoBuffer,
29 String filename, PHPIdentifierLocation location) {
30 hoverInfoBuffer.append(location.toString());
31 hoverInfoBuffer.append(" - <b>");
33 hoverInfoBuffer.append(getUsage(filename, location));
34 hoverInfoBuffer.append("</b><br>");
36 // read the phpdoc for the function
37 if (location.getPHPDocOffset() >= 0) {
38 InputStreamReader phpFileReader = createReader(filename);
39 if (phpFileReader == null)
41 char[] phpDocDeclarationCharArray = new char[location
43 phpFileReader.skip(location.getPHPDocOffset());
44 phpFileReader.read(phpDocDeclarationCharArray, 0, location
46 PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader(
47 phpDocDeclarationCharArray);
48 hoverInfoBuffer.append(phpdocConverter.getString());
49 phpFileReader.close();
52 } catch (IOException e) {
58 static String getEncoding(String filename) {
59 String encoding = null;
60 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
61 .getFileForLocation(new Path(filename));
64 encoding = file.getCharset();
65 } catch (CoreException e) {
66 // TODO: should log the fact that we could not get the encoding?
72 public static String getUsage(String filename,
73 PHPIdentifierLocation location) {
74 String usage = location.getUsage();
81 InputStreamReader phpFileReader = createReader(filename);
82 if (phpFileReader == null)
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,
96 for (int i = 0; i < length; i++) {
97 if (functionDeclarationCharArray[i] == ')') {
101 if (functionDeclarationCharArray[i] == '{'
102 || functionDeclarationCharArray[i] == '}') {
107 usage = new String(functionDeclarationCharArray, 0, length);
108 // cache the usage string:
109 location.setUsage(usage);
111 phpFileReader.close();
113 } catch (IOException e) {
119 private static InputStreamReader createReader(String filename) {
120 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
121 .getFileForLocation(new Path(filename));
124 return new InputStreamReader(new FileInputStream(file
125 .getLocation().toString()), file.getCharset());
126 } catch (UnsupportedEncodingException e) {
128 } catch (FileNotFoundException e) {
130 } catch (CoreException e) {