KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > CodeElementContainer


1 /*
2  * CodeElementContainer.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-2004 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code;
35
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.PrintWriter JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.LinkedList JavaDoc;
41
42 /**
43  * The abstract base class for all code element containers. The code
44  * element containers contains other code elements.
45  *
46  * @author Per Cederberg, <per at percederberg dot net>
47  * @version 1.5
48  */

49 public abstract class CodeElementContainer extends CodeElement {
50
51     /**
52      * The code element contents. This list contains the code elements
53      * contained inside this element.
54      */

55     private LinkedList JavaDoc contents = new LinkedList JavaDoc();
56
57     /**
58      * Adds a code element to this container.
59      *
60      * @param elem the code element to add
61      */

62     protected void addElement(CodeElement elem) {
63         if (!contents.contains(elem)) {
64             contents.add(elem);
65         }
66     }
67
68     /**
69      * Prints all the contained code elements to the specified output
70      * stream. The code elements will be sorted by their category
71      * number before printing.
72      *
73      * @param out the output stream
74      * @param style the code style to use
75      * @param indent the indentation level
76      */

77     protected void printContents(PrintWriter JavaDoc out,
78                                  CodeStyle style,
79                                  int indent) {
80
81         CodeElement prev = null;
82         CodeElement next;
83
84         Collections.sort(contents);
85         for (int i = 0; i < contents.size(); i++) {
86             next = (CodeElement) contents.get(i);
87             printSeparator(out, style, prev, next);
88             next.print(out, style, indent);
89             prev = next;
90         }
91     }
92
93     /**
94      * Prints the lines separating two elements. By default this
95      * method prints a newline before the first element, and between
96      * elements with different category numbers.
97      *
98      * @param out the output stream
99      * @param style the code style to use
100      * @param prev the previous element, or null if first
101      * @param next the next element, or null if last
102      */

103     protected void printSeparator(PrintWriter JavaDoc out,
104                                   CodeStyle style,
105                                   CodeElement prev,
106                                   CodeElement next) {
107
108         if (prev == null || next == null) {
109             // Do nothing
110
} else if (prev.category() != next.category()) {
111             out.println();
112         }
113     }
114
115     /**
116      * Creates a file and the parent directories if they didn't exist.
117      *
118      * @param file the file to create
119      *
120      * @throws IOException if the file couldn't be created properly
121      */

122     protected void createFile(File JavaDoc file) throws IOException JavaDoc {
123         File JavaDoc dir = file.getParentFile();
124
125         if (!dir.exists()) {
126             dir.mkdirs();
127         }
128         if (!file.exists()) {
129             try {
130                 file.createNewFile();
131             } catch (IOException JavaDoc e) {
132                 throw new IOException JavaDoc("couldn't create " + file + ": " +
133                                       e.getMessage());
134             }
135         }
136     }
137 }
138
Popular Tags