KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > doc > javadoc > Api


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * Caucho Technology permits redistribution, modification and use
5  * of this file in source and binary form ("the Software") under the
6  * Caucho Developer Source License ("the License"). The following
7  * conditions must be met:
8  *
9  * 1. Each copy or derived work of the Software must preserve the copyright
10  * notice and this notice unmodified.
11  *
12  * 2. Redistributions of the Software in source or binary form must include
13  * an unmodified copy of the License, normally in a plain ASCII text
14  *
15  * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and
16  * may not be used to endorse products derived from this software.
17  * "Resin" or "Caucho" may not appear in the names of products derived
18  * from this software.
19  *
20  * This Software is provided "AS IS," without a warranty of any kind.
21  * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
22  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
24  *
25  * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
26  * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR
27  * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE
28  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
29  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
30  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
31  * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGES.
33  *
34  * @author Sam
35  */

36
37 package com.caucho.doc.javadoc;
38
39 import com.caucho.config.ConfigException;
40 import com.caucho.log.Log;
41 import com.caucho.util.CharBuffer;
42 import com.caucho.util.Crc64;
43 import com.caucho.util.L10N;
44 import com.caucho.vfs.Path;
45 import com.caucho.vfs.Vfs;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 import java.io.FileNotFoundException JavaDoc;
51
52 /**
53  * An Api is a javadoc generated api.
54  */

55 public class Api {
56   static protected final Logger JavaDoc log = Log.open(Api.class);
57   static final L10N L = new L10N(Api.class);
58
59   private String JavaDoc _id;
60   private String JavaDoc _name;
61   private String JavaDoc _description;
62   private String JavaDoc _location;
63   private String JavaDoc _indexString;
64   private ArrayList JavaDoc<Path> _index = new ArrayList JavaDoc<Path>();
65
66   private Path _locationPath;
67   private boolean _isLocal;
68   private boolean _isLocalAbsolute;
69
70   /**
71    * A unique id for the api, required.
72    */

73   public void setId(String JavaDoc id)
74     throws ConfigException
75   {
76     for (int i = 0; i < id.length(); i++) {
77       if (!Character.isJavaIdentifierPart(id.charAt(i)))
78         throw new ConfigException(L.l("illegal character in `{0}': {1}","id",id.charAt(i)));
79     }
80     _id = id;
81   }
82
83   /**
84    * A unique id for the api.
85    */

86   public String JavaDoc getId()
87   {
88     return _id;
89   }
90
91   /**
92    * A descriptive name for the api, default is to use location.
93    */

94   public void setName(String JavaDoc name)
95   {
96     _name = name;
97   }
98
99   /**
100    * A descriptive name for the api.
101    */

102   public String JavaDoc getName()
103   {
104     return _name;
105   }
106
107   /**
108    * A long descriptiion for the api, optional.
109    */

110   public void setDescription(String JavaDoc description)
111   {
112     _description = description;
113   }
114
115   /**
116    * A long description for the api, optional.
117    */

118   public String JavaDoc getDescription()
119   {
120     return _description;
121   }
122
123   /**
124    * The location of a javadoc generated api, can be a url, required.
125    * <p>Examples:
126    * <ul>
127    * <li>http://java.sun.com/j2se/1.4.2/docs/api
128    * <li>file://usr/local/java/axis_1-1/docs/apiDocs
129    * <li>resin/
130    * </ul>
131    */

132   public void setLocation(String JavaDoc location)
133   {
134     if (!location.endsWith("/")) {
135       CharBuffer cb = CharBuffer.allocate();
136       cb.append(location);
137       cb.append('/');
138       _location = cb.close();
139     }
140     else
141       _location = location;
142   }
143
144   /**
145    * The location of a javadoc generated api.
146    */

147   public String JavaDoc getLocation()
148   {
149     return _location;
150   }
151
152   /**
153    * The location of a javadoc generated api, as a vfs Path object.
154    */

155   Path getLocationPath()
156   {
157     return _locationPath;
158   }
159
160   /**
161    * The location of a javadoc generated html index file, can be relative in
162    * which case it is relative to `location'. Default is "index-all.html".
163    */

164   public void setIndex(String JavaDoc index)
165   {
166     _indexString = index;
167   }
168
169   public void init()
170     throws ConfigException
171   {
172     if (_id == null)
173       throw new ConfigException(L.l("`{0}' is required","id"));
174
175     if (_location == null)
176       throw new ConfigException(L.l("`{0}' is required","location"));
177
178     if (_name == null)
179       _name = _location.toString();
180
181     if (_indexString == null)
182       _indexString = "index-all.html";
183
184     _locationPath = Vfs.lookup(_location);
185
186     int split = _indexString.indexOf('#');
187
188     if (split > -1) {
189       CharBuffer before = new CharBuffer(_indexString.substring(0,split));
190       CharBuffer after = new CharBuffer(_indexString.substring(split + 1));
191       CharBuffer index = CharBuffer.allocate();
192
193       boolean isIndex = false;
194
195       for (int i = 1; i <= 27; i++) {
196         index.append(before);
197         index.append(i);
198         index.append(after);
199
200         Path indexPath = _locationPath.lookup(index.toString());
201
202         if (indexPath.exists()) {
203           isIndex = true;
204           _index.add(indexPath);
205         }
206
207         index.clear();
208       }
209
210       if (!isIndex) {
211         throw new ConfigException(L.l("`{0}' not found", _locationPath.lookup(_indexString)));
212       }
213     }
214     else
215       _index.add(_locationPath.lookup(_indexString));
216
217     if (_locationPath.getScheme().equals("file")) {
218       _isLocal = true;
219       Path pwd = Vfs.getPwd();
220
221       if (!_locationPath.getPath().startsWith(pwd.getPath()))
222         _isLocalAbsolute = true;
223     }
224   }
225
226   long generateCrc64(long crc)
227   {
228     crc = Crc64.generate(crc,_location);
229     return Crc64.generate(crc,_index.toString());
230   }
231
232   /**
233    * The location of all javadoc generated html index files.
234    */

235   public ArrayList JavaDoc<Path> getIndexes()
236   {
237     return _index;
238   }
239
240
241   /**
242    * An api that is local to the server.
243    */

244   public boolean isLocal()
245   {
246     return _isLocal;
247   }
248
249   /**
250    * An api that is local to the server, but somewhere on the filesystem
251    * outside of the context of the web application.
252    */

253   public boolean isLocalAbsolute()
254   {
255     return _isLocalAbsolute;
256   }
257
258   /**
259    * A location href, relative to the web-app root, appropriately rewritten to
260    * handle remote locations and locations that are local absolute.
261    */

262
263   String JavaDoc getLocationHref(String JavaDoc file)
264   {
265     CharBuffer cb = CharBuffer.allocate();
266
267     if (_isLocalAbsolute) {
268       cb.append(_id);
269       cb.append('/');
270     }
271     else {
272       cb.append(_location);
273     }
274
275     cb.append(file);
276
277     return cb.close();
278   }
279 }
280
281
282
Popular Tags