KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > NameSpace


1 package persistence.antlr;
2
3 /**
4  * ANTLR Translator Generator
5  * Project led by Terence Parr at http://www.jGuru.com
6  * Software rights: http://www.antlr.org/license.html
7  *
8  * Container for a C++ namespace specification. Namespaces can be
9  * nested, so this contains a vector of all the nested names.
10  *
11  * @author David Wagner (JPL/Caltech) 8-12-00
12  *
13  */

14
15 import java.util.Vector JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 public class NameSpace {
21     private Vector JavaDoc names = new Vector JavaDoc();
22     private String JavaDoc _name;
23
24     public NameSpace(String JavaDoc name) {
25           _name = new String JavaDoc(name);
26         parse(name);
27     }
28
29      public String JavaDoc getName()
30      {
31         return _name;
32      }
33     
34     /**
35      * Parse a C++ namespace declaration into seperate names
36      * splitting on :: We could easily parameterize this to make
37      * the delimiter a language-specific parameter, or use subclasses
38      * to support C++ namespaces versus java packages. -DAW
39      */

40     protected void parse(String JavaDoc name) {
41         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(name, "::");
42         while (tok.hasMoreTokens())
43             names.addElement(tok.nextToken());
44     }
45
46     /**
47      * Method to generate the required C++ namespace declarations
48      */

49     void emitDeclarations(PrintWriter JavaDoc out) {
50         for (Enumeration JavaDoc n = names.elements(); n.hasMoreElements();) {
51             String JavaDoc s = (String JavaDoc)n.nextElement();
52             out.println("ANTLR_BEGIN_NAMESPACE(" + s + ")");
53         }
54     }
55
56     /**
57      * Method to generate the required C++ namespace closures
58      */

59     void emitClosures(PrintWriter JavaDoc out) {
60         for (int i = 0; i < names.size(); ++i)
61             out.println("ANTLR_END_NAMESPACE");
62     }
63 }
64
Popular Tags