001: /*
002: * $Id: Doc.java,v 1.8 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.doc;
011:
012: import java.util.ArrayList;
013: import java.util.Hashtable;
014: import java.io.IOException;
015: import anvil.codec.Code;
016: import anvil.codec.ConstantPool;
017: import anvil.codec.Info;
018:
019: public class Doc implements DocConstants {
020:
021: public static final Doc EMPTY_DOC = new Doc(0, null, "", null);
022:
023: private int _type;
024: private String _name;
025: private String _ident;
026: private String _text;
027: private Doc _head = null;
028: private Doc _tail = null;
029: private Doc _next = null;
030:
031: public Doc(int type, String ident, String text, Doc[] childs) {
032: _type = type;
033: _ident = ident;
034: _name = DocParser.getIdentity(type);
035: _text = text;
036: if (childs != null) {
037: int n = childs.length;
038: if (n > 0) {
039: _head = childs[0];
040: _tail = childs[n - 1];
041: for (int i = 0; i < n - 1; i++) {
042: childs[i].setNext(childs[i + 1]);
043: }
044: }
045: }
046: }
047:
048: public Doc(String name, String text) {
049: _name = name;
050: _type = DocParser.getIdentity(name);
051: text = text.trim();
052: switch (_type) {
053: case T_MODULE:
054: case T_CONST:
055: case T_VAR:
056: case T_FUNCTION:
057: case T_CLASS:
058: case T_INTERFACE:
059: case T_MEMBER:
060: case T_ATTRIBUTE:
061: case T_REFERENCE:
062: case T_OPERATOR:
063: case T_CONSTRUCTOR:
064: case T_METHOD:
065: case T_PARAM:
066: case T_THROWS:
067: case T_DEFINE:
068: int i = 0;
069: int n = text.length();
070: if (n > 2) {
071: if (text.charAt(0) == '"') {
072: i++;
073: while (i < n) {
074: if (text.charAt(i) == '"') {
075: break;
076: }
077: i++;
078: }
079: _ident = text.substring(1, i);
080: if (i < n) {
081: _text = text.substring(i + 1).trim();
082: } else {
083: _text = "";
084: }
085: break;
086: }
087: }
088: while (i < n) {
089: if (Character.isWhitespace(text.charAt(i))) {
090: break;
091: }
092: i++;
093: }
094: _ident = text.substring(0, i);
095: _text = text.substring(i).trim();
096: break;
097:
098: default:
099: _ident = null;
100: _text = text;
101: break;
102: }
103: }
104:
105: public String toString() {
106: if (_ident != null) {
107: return "@" + _name + " '" + _ident + "' " + _text;
108: } else {
109: return "@" + _name + " " + _text;
110: }
111: }
112:
113: public int getType() {
114: return _type;
115: }
116:
117: public String getName() {
118: return _name;
119: }
120:
121: public String getIdent() {
122: return _ident;
123: }
124:
125: public String getText() {
126: return _text;
127: }
128:
129: public String getFirstSentence() {
130: int i = _text.indexOf('.');
131: if (i > 0) {
132: return _text.substring(0, i + 1);
133: } else {
134: return _text;
135: }
136: }
137:
138: public Doc getNext() {
139: return _next;
140: }
141:
142: public void setNext(Doc next) {
143: _next = next;
144: }
145:
146: public boolean hasChilds() {
147: return _head != null;
148: }
149:
150: public Doc[] getChilds() {
151: int c = 0;
152: Doc h = _head;
153: while (h != null) {
154: c++;
155: h = h._next;
156: }
157: Doc[] childs = new Doc[c];
158: if (c > 0) {
159: c = 0;
160: h = _head;
161: while (h != null) {
162: childs[c++] = h;
163: h = h._next;
164: }
165: }
166: return childs;
167: }
168:
169: public int getChildCount() {
170: int c = 0;
171: Doc h = _head;
172: while (h != null) {
173: c++;
174: h = h._next;
175: }
176: return c;
177: }
178:
179: public Doc getFirstChild() {
180: return _head;
181: }
182:
183: public void addChild(Doc node) {
184: if (_head == null) {
185: _head = _tail = node;
186: } else {
187: _tail.setNext(node);
188: _tail = node;
189: }
190: node.setNext(null);
191: }
192:
193: public Doc getChild(int index) {
194: if (index < 0) {
195: return null;
196: }
197: Doc h = _head;
198: while ((h != null) && (index-- > 0)) {
199: h = h._next;
200: }
201: return h;
202: }
203:
204: public Doc[] find(String type, String ident) {
205: int t = 0;
206: if (type != null) {
207: t = DocParser.getIdentity(type);
208: if (t == 0) {
209: return new Doc[0];
210: }
211: }
212: return find(t, ident);
213: }
214:
215: public Doc[] find(int type, String ident) {
216: String s;
217: Doc h = _head;
218: ArrayList list = new ArrayList();
219: while (h != null) {
220: if ((type == 0) || (h.getType() == type)) {
221: if (ident == null) {
222: list.add(h);
223: } else {
224: s = h.getIdent();
225: if (s != null && s.equals(ident)) {
226: list.add(h);
227: }
228: }
229: }
230: h = h._next;
231: }
232: return (Doc[]) list.toArray(new Doc[list.size()]);
233: }
234:
235: public Doc findFirst(String type, String ident) {
236: int t = 0;
237: if (type != null) {
238: t = DocParser.getIdentity(type);
239: if (t == 0) {
240: return null;
241: }
242: }
243: return findFirst(t, ident);
244: }
245:
246: public Doc findFirst(int type, String ident) {
247: String s;
248: Doc h = _head;
249: while (h != null) {
250: if ((type == 0) || (h.getType() == type)) {
251: if (ident == null) {
252: return h;
253: } else {
254: s = h.getIdent();
255: if (s != null && s.equals(ident)) {
256: return h;
257: }
258: }
259: }
260: h = h._next;
261: }
262: return null;
263: }
264:
265: public String getChildText(int type, String ident) {
266: Doc doc = findFirst(type, ident);
267: if (doc != null) {
268: return doc.getText();
269: }
270: return null;
271: }
272:
273: public void compile(Code code) {
274: ConstantPool pool = code.getPool();
275: int clazz = pool.addClass("anvil/doc/Doc");
276: code.anew(clazz);
277: code.dup();
278: code.iconst(_type);
279: code.astring(_ident);
280: code.astring(_text);
281: if (_head != null) {
282: code.iconst(getChildCount());
283: code.anewarray(clazz);
284: Doc p = _head;
285: int i = 0;
286: while (p != null) {
287: code.dup();
288: code.iconst(i++);
289: p.compile(code);
290: p = p._next;
291: code.aastore();
292: }
293: } else {
294: code.aconst_null();
295: }
296: code
297: .invokespecial(pool
298: .addMethodRef(clazz, "<init>",
299: "(ILjava/lang/String;Ljava/lang/String;[Lanvil/doc/Doc;)V"));
300: }
301:
302: public void debug(String fill) {
303: System.out.print(fill);
304: System.out.print("@");
305: System.out.print(DocParser.getIdentity(_type));
306: System.out.print('(');
307: System.out.print(_type);
308: System.out.print(')');
309: System.out.print(' ');
310: if (_ident != null) {
311: System.out.print('"');
312: System.out.print(_ident);
313: System.out.print('"');
314: System.out.print(' ');
315: }
316: if (_text != null) {
317: System.out.println(_text);
318: }
319: fill = fill + " ";
320: Doc h = _head;
321: while (h != null) {
322: h.debug(fill);
323: h = h._next;
324: }
325: }
326:
327: public void debug() {
328: debug("");
329: }
330:
331: }
|