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 if (location.isDefine()) {
97 length = getClosingParenthesis(functionDeclarationCharArray);
101 for (int i = 0; i < length; i++) {
102 if (functionDeclarationCharArray[i] == ')') {
106 if (functionDeclarationCharArray[i] == '{'
107 || functionDeclarationCharArray[i] == '}') {
113 usage = new String(functionDeclarationCharArray, 0, length);
114 // cache the usage string:
115 location.setUsage(usage);
117 phpFileReader.close();
119 } catch (IOException e) {
125 private static InputStreamReader createReader(String filename) {
126 IFile file = PHPeclipsePlugin.getWorkspace().getRoot()
127 .getFileForLocation(new Path(filename));
130 return new InputStreamReader(new FileInputStream(file
131 .getLocation().toString()), file.getCharset());
132 } catch (UnsupportedEncodingException e) {
134 } catch (FileNotFoundException e) {
136 } catch (CoreException e) {
143 private static int getClosingParenthesis(char[] buffer) {
148 for (int i = 0; i < buffer.length; i++) {
149 if (buffer[i] == '\\') {
154 dq = (buffer[i] != '"');
158 sq = (buffer[i] != '\'');