KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > java > JavaPackage


1 /*
2  * JavaPackage.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2003 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.java;
35
36 import java.io.File JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38
39 import net.percederberg.grammatica.code.CodeElement;
40 import net.percederberg.grammatica.code.CodeStyle;
41
42 /**
43  * A class generating a Java package declaration.
44  *
45  * @author Per Cederberg, <per at percederberg dot net>
46  * @version 1.0
47  */

48 public class JavaPackage extends CodeElement {
49
50     /**
51      * The base package.
52      */

53     private JavaPackage basePackage;
54
55     /**
56      * The package name.
57      */

58     private String JavaDoc name;
59
60     /**
61      * Creates a new Java package with the specified name.
62      *
63      * @param name the package name (including dots '.')
64      */

65     public JavaPackage(String JavaDoc name) {
66         this(null, name);
67     }
68
69     /**
70      * Creates a new Java package with the specified base package and name.
71      *
72      * @param base the base package
73      * @param name the package name (including dots '.')
74      */

75     public JavaPackage(JavaPackage base, String JavaDoc name) {
76         this.basePackage = base;
77         this.name = name;
78     }
79
80     /**
81      * Returns a string representation of this package.
82      *
83      * @return a string representation of this package
84      */

85     public String JavaDoc toString() {
86         if (basePackage == null) {
87             return name;
88         } else {
89             return basePackage.toString() + "." + name;
90         }
91     }
92
93     /**
94      * Returns a numeric category number for the code element. A lower
95      * category number implies that the code element should be placed
96      * before code elements with a higher category number within a
97      * declaration.
98      *
99      * @return the category number
100      */

101     public int category() {
102         return 1;
103     }
104
105     /**
106      * Returns the directory file containing the package files.
107      *
108      * @param baseDir the base output directory
109      *
110      * @return the package directory
111      */

112     public File JavaDoc toFile(File JavaDoc baseDir) {
113         String JavaDoc firstName;
114         String JavaDoc restName;
115         int pos;
116
117         if (basePackage != null) {
118             baseDir = basePackage.toFile(baseDir);
119         }
120         restName = this.name;
121         while ((pos = restName.indexOf('.')) > 0) {
122             firstName = restName.substring(0, pos);
123             restName = restName.substring(pos + 1);
124             baseDir = new File JavaDoc(baseDir, firstName);
125         }
126         return new File JavaDoc(baseDir, restName);
127     }
128
129     /**
130      * Prints the code element to the specified output stream.
131      *
132      * @param out the output stream
133      * @param style the code style to use
134      * @param indent the indentation level
135      */

136     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
137         out.println("package " + toString() + ";");
138     }
139 }
140
Popular Tags