Changes:
[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.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, String filename, PHPIdentifierLocation location) {
21     FileReader phpFileReader;
22     hoverInfoBuffer.append(location.toString());
23     hoverInfoBuffer.append(" - \n");
24     try {
25                         hoverInfoBuffer.append( getUsage(filename, location) );
26                         hoverInfoBuffer.append('\n');
27
28       //        read the phpdoc for the function
29       if (location.getPHPDocOffset() >= 0) {
30         phpFileReader = new FileReader(filename);
31         char[] phpDocDeclarationCharArray = new char[location.getPHPDocLength()];
32         phpFileReader.skip(location.getPHPDocOffset());
33         phpFileReader.read(phpDocDeclarationCharArray, 0, location.getPHPDocLength());
34         PHPDocCharArrayCommentReader phpdocConverter = new PHPDocCharArrayCommentReader(phpDocDeclarationCharArray);
35         hoverInfoBuffer.append(phpdocConverter.getString());
36         hoverInfoBuffer.append('\n');
37         phpFileReader.close();
38       }
39
40     } catch (IOException e) {
41       return;
42     }
43   }
44
45   public static String getUsage(String filename, PHPIdentifierLocation location) {
46     FileReader phpFileReader;
47     String usage = location.getUsage();
48     if (usage!=null) {
49         return usage;
50     }
51     usage = "";
52     try {
53                         
54       phpFileReader = new FileReader(filename);
55       // read the function declaration
56       if (location.getOffset() >= 0 && (location.isMethod() || location.isConstructor() || location.isFunction() || location.isDefine())) {
57         char[] functionDeclarationCharArray = new char[256];
58         int offset = location.getOffset();
59         phpFileReader.skip(offset);
60         int length = phpFileReader.read(functionDeclarationCharArray, 0, 256);
61         if (length == -1) {
62           length = 256;
63         }
64         for (int i = 0; i < length; i++) {
65           if (functionDeclarationCharArray[i] == ')') {
66             length = i + 1;
67             break;
68           }
69           if (functionDeclarationCharArray[i] == '{' || functionDeclarationCharArray[i] == '}') {
70             length = i;
71             break;
72           }
73         }
74                                 usage = new String(functionDeclarationCharArray, 0, length);
75       }
76       phpFileReader.close();
77     } catch (IOException e) {
78       // do nothing
79     }
80     // cache the usage string:
81                 location.setUsage(usage);
82     return usage;
83   }
84 }