KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lexer > gen > GenerateLanguageDescription


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lexer.gen;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28
29 /**
30  * Abstract class for supporting a language description xml file generation.
31  *
32  * @author Miloslav Metelka
33  */

34 public abstract class GenerateLanguageDescription extends Task {
35
36     private String JavaDoc tokenTypesClassName;
37
38     private File JavaDoc languageDescriptionFile;
39
40     public GenerateLanguageDescription() {
41     }
42
43     public String JavaDoc getTokenTypesClassName() {
44         return tokenTypesClassName;
45     }
46     
47     public void setTokenTypesClassName(String JavaDoc tokenTypesClassName) {
48         this.tokenTypesClassName = tokenTypesClassName;
49     }
50     
51     public File JavaDoc getLanguageDescriptionFile() {
52         return languageDescriptionFile;
53     }
54     
55     public void setLanguageDescriptionFile(File JavaDoc languageDescriptionFile) {
56         this.languageDescriptionFile = languageDescriptionFile;
57     }
58
59     public void execute() throws BuildException {
60         String JavaDoc tokenTypesClassName = getTokenTypesClassName();
61         File JavaDoc langDescFile = getLanguageDescriptionFile();
62         
63         if (tokenTypesClassName == null || "".equals(tokenTypesClassName)) {
64             throw new BuildException("tokenTypesClassName attribute must be set");
65         }
66         if (langDescFile == null) {
67             throw new BuildException("languageDescriptionFile attribute must be set");
68         }
69         
70         String JavaDoc output;
71         try {
72             output = generate(tokenTypesClassName);
73         } catch (ClassNotFoundException JavaDoc e) {
74             throw new BuildException(e);
75         }
76   
77         try {
78             String JavaDoc writeType = langDescFile.exists()
79                 ? "appended to the end of"
80                 : "generated to";
81                 
82             // getAbsolutePath() used to be able to compile on JDK1.3
83
FileWriter JavaDoc langDescWriter = new FileWriter JavaDoc(langDescFile.getAbsolutePath(), true); // append
84

85             langDescWriter.write(output);
86             langDescWriter.close();
87             
88             log("Language description about class "
89                 + tokenTypesClassName + " successfully "
90                 + writeType + " file "
91                 + langDescFile
92             );
93
94         } catch (IOException JavaDoc e) {
95             throw new BuildException("IOException occurred", e);
96         }
97     }
98     
99     protected abstract String JavaDoc generate(String JavaDoc tokenTypesClassName) throws ClassNotFoundException JavaDoc;
100             
101 }
102
Popular Tags