View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * FileHandler.java
26  *
27  * Provides persistance for the Agent Generator
28  *****************************************************************************/
29  
30  package zeus.generator;
31  
32  import java.util.*;
33  import java.io.*;
34  import java.awt.*;
35  import java.awt.event.*;
36  
37  import zeus.util.*;
38  import zeus.concepts.*;
39  import zeus.generator.code.*;
40  
41  public class FileHandler {
42  
43    static final String BEGIN_GENERATOR		= "BEGIN_GENERATOR";
44    static final String BEGIN_PREAMBLE		= "BEGIN_PREAMBLE";
45    static final String SYSTEM			= ":system";
46    static final String SYSTEM_NAME               = "ZEUS-Agent-Generator";
47    static final String VERSION			= ":version";
48    static final String ONTOLOGY			= ":ontology";
49    static final String END_PREAMBLE		= "END_PREAMBLE";
50    static final String BEGIN_AGENT_NAME_INDEX	= "BEGIN_AGENT_NAME_INDEX";
51    static final String END_AGENT_NAME_INDEX	= "END_AGENT_NAME_INDEX";
52    static final String BEGIN_AGENT_ICON_INDEX	= "BEGIN_AGENT_ICON_INDEX";
53    static final String END_AGENT_ICON_INDEX	= "END_AGENT_ICON_INDEX";
54    static final String BEGIN_TASK_NAME_INDEX	= "BEGIN_TASK_NAME_INDEX";
55    static final String END_TASK_NAME_INDEX	= "END_TASK_NAME_INDEX";
56    static final String BEGIN_AGENT_LIST		= "BEGIN_AGENT_LIST";
57    static final String END_AGENT_LIST		= "END_AGENT_LIST";
58    static final String BEGIN_TASK_LIST		= "BEGIN_TASK_LIST";
59    static final String END_TASK_LIST		= "END_TASK_LIST";
60    static final String BEGIN_GENERATION_PLAN	= "BEGIN_GENERATION_PLAN";
61    static final String END_GENERATION_PLAN	= "END_GENERATION_PLAN";
62    static final String END_GENERATOR		= "END_GENERATOR";
63    static final String PLATFORM                  = ":platform";
64    static final String DIRECTORY                 = ":directory";
65    static final String BEGIN_NAMESERVER_LIST     = "BEGIN_NAMESERVER_LIST";
66    static final String END_NAMESERVER_LIST       = "END_NAMESERVER_LIST";
67    static final String BEGIN_FACILITATOR_LIST    = "BEGIN_FACILITATOR_LIST";
68    static final String END_FACILITATOR_LIST      = "END_FACILITATOR_LIST";
69    static final String BEGIN_VISUALISER_LIST     = "BEGIN_VISUALISER_LIST";
70    static final String END_VISUALISER_LIST       = "END_VISUALISER_LIST";
71    static final String BEGIN_DBPROXY_LIST        = "BEGIN_DBPROXY_LIST";
72    static final String END_DBPROXY_LIST          = "END_DBPROXY_LIST";
73  
74  
75    protected static final String TFILE = "___file";
76    protected int count = 0;
77  
78    public static final int WARNING_MASK = 1;
79    public static final int ERROR_MASK   = 2;
80  
81    protected String error = null;
82    protected String warning = null;
83  
84    protected GeneratorModel genmodel;
85    protected GenerationPlan genplan;
86    protected OntologyDb     ontology;
87  
88  
89    public FileHandler(OntologyDb ontology, GeneratorModel genmodel,
90                       GenerationPlan genplan) {
91       this.genmodel = genmodel;
92       this.genplan = genplan;
93       this.ontology = ontology;
94    }
95  
96    //---------------------------------------------------------------------------
97    public int saveFile(File f) {
98      error = null;
99      warning = null;
100     int status = 0;
101 
102     int num = 0;
103     String dir = f.getParent();
104     String name = (dir != null) ? dir + File.separator + TFILE : TFILE;
105     File file = new File(name);
106 
107     char sys_char = File.separatorChar;
108     char zeus_char = SystemProps.getProperty("file.separator").charAt(0);
109 
110     String ontology_file = ontology.getFilename();
111     ontology_file = Misc.relativePath(dir,ontology_file);
112     ontology_file = ontology_file.replace(sys_char,zeus_char);
113 
114     String gen_dir = genplan.getDirectory();
115     gen_dir = Misc.relativePath(dir,gen_dir);
116     gen_dir = gen_dir.replace(sys_char,zeus_char);
117 
118     while( file.exists() )
119        file = new File(name + (num++));
120 
121     try {
122       String[] identifiers, names;
123       String   icon;
124 
125       PrettyPrintWriter out = new PrettyPrintWriter(file);
126 
127       out.pprint(0,BEGIN_GENERATOR);
128       out.pprint(1,BEGIN_PREAMBLE);
129       out.pprint(2,SYSTEM   + " \"" + SYSTEM_NAME + "\"");
130       out.pprint(2,VERSION + " \"" + SystemProps.getProperty("version.id") + "\"");
131       out.pprint(2,ONTOLOGY + " \"" + Misc.escape(ontology_file) + "\"");
132       out.pprint(1,END_PREAMBLE);
133 
134       out.println();
135       out.pprint(1,BEGIN_AGENT_NAME_INDEX);
136       identifiers = genmodel.getAgentIds();
137       names = genmodel.getAgentNames();
138       for(int i = 0; i < names.length; i++ )
139          out.pprint(2, identifiers[i] + " " + names[i]);
140       out.pprint(1,END_AGENT_NAME_INDEX);
141 
142       out.println();
143       out.pprint(1,BEGIN_AGENT_ICON_INDEX);
144       for(int i = 0; i < identifiers.length; i++ ) {
145          icon = genmodel.getAgentIcon(identifiers[i]);
146          icon = Misc.relativePath(dir,icon);
147          icon = icon.replace(sys_char,zeus_char);
148          out.pprint(2, identifiers[i] + " \"" + Misc.escape(icon) + "\"");
149       }
150       out.pprint(1,END_AGENT_ICON_INDEX);
151 
152       out.println();
153       out.pprint(1,BEGIN_TASK_NAME_INDEX);
154       identifiers = genmodel.getTaskIds();
155       names = genmodel.getTaskNames();
156       for(int i = 0; i < names.length; i++ )
157          out.pprint(2, identifiers[i] + " " + names[i]);
158       out.pprint(1,END_TASK_NAME_INDEX);
159 
160       out.println();
161       out.pprint(1,BEGIN_AGENT_LIST);
162       AgentDescription[] agents = genmodel.getAgents();
163       for(int i = 0; i < agents.length; i++ )
164          out.println(agents[i].pprint(2*2));
165       out.pprint(1,END_AGENT_LIST);
166 
167       out.println();
168       out.pprint(1,BEGIN_TASK_LIST);
169       AbstractTask[] tasks = genmodel.getTasks();
170       for(int i = 0; i < tasks.length; i++ )
171          out.println(tasks[i].pprint(2*2));
172       out.pprint(1,END_TASK_LIST);
173 
174       out.println();
175       out.pprint(1,BEGIN_GENERATION_PLAN);
176       out.pprint(2,BEGIN_PREAMBLE);
177       out.pprint(3,PLATFORM + " \"" + genplan.getPlatform() + "\"");
178       out.pprint(3,DIRECTORY + " \"" + Misc.escape(gen_dir) + "\"");
179       out.pprint(2,END_PREAMBLE);
180 
181       AgentInfo[] agentInfo = genplan.getAgents();
182       if ( agentInfo.length > 0 ) {
183          out.pprint(2,BEGIN_AGENT_LIST);
184          for(int i = 0; i < agentInfo.length; i++ )
185             out.println(agentInfo[i].pprint(3*2));
186          out.pprint(2,END_AGENT_LIST);
187       }
188 
189       TaskInfo[] taskInfo = genplan.getTasks();
190       if ( taskInfo.length > 0 ) {
191          out.pprint(2,BEGIN_TASK_LIST);
192          for(int i = 0; i < taskInfo.length; i++ )
193             out.println(taskInfo[i].pprint(3*2));
194          out.pprint(2,END_TASK_LIST);
195       }
196 
197       NameserverInfo[] nameserverInfo = genplan.getNameservers();
198       if ( nameserverInfo.length > 0 ) {
199          out.pprint(2,BEGIN_NAMESERVER_LIST);
200          for(int i = 0; i < nameserverInfo.length; i++ )
201             out.println(nameserverInfo[i].pprint(3*2));
202          out.pprint(2,END_NAMESERVER_LIST);
203       }
204 
205       FacilitatorInfo[] facilitatorInfo = genplan.getFacilitators();
206       if ( facilitatorInfo.length > 0 ) {
207          out.pprint(2,BEGIN_FACILITATOR_LIST);
208          for(int i = 0; i < facilitatorInfo.length; i++ )
209             out.println(facilitatorInfo[i].pprint(3*2));
210          out.pprint(2,END_FACILITATOR_LIST);
211       }
212 
213       VisualiserInfo[] visualiserInfo = genplan.getVisualisers();
214       if ( visualiserInfo.length > 0 ) {
215          out.pprint(2,BEGIN_VISUALISER_LIST);
216          for(int i = 0; i < visualiserInfo.length; i++ )
217             out.println(visualiserInfo[i].pprint(3*2));
218          out.pprint(2,END_VISUALISER_LIST);
219       }
220 
221       DbProxyInfo[] dbProxyInfo = genplan.getDbProxys();
222       if ( dbProxyInfo.length > 0 ) {
223          out.pprint(2,BEGIN_DBPROXY_LIST);
224          for(int i = 0; i < dbProxyInfo.length; i++ )
225             out.println(dbProxyInfo[i].pprint(3*2));
226          out.pprint(2,END_DBPROXY_LIST);
227       }
228       out.pprint(1,END_GENERATION_PLAN);
229       out.pprint(0,END_GENERATOR);
230       out.flush();
231       out.close();
232 
233       if ( f.exists() ) f.delete();
234       file.renameTo(f);
235     }
236     catch(Exception e) {
237       error = e.toString();
238       status |= ERROR_MASK;
239     }
240     return status;
241   }
242 
243 
244   //------------------------------------------------------------------------
245   public int openFile(File file) {
246     Assert.notNull(file);
247     int status = 0;
248     genplan.purge();
249 
250     try {
251        Parser parser = new Parser(new FileInputStream(file));
252        String dir = null;
253        try {
254           dir = file.getParentFile().getCanonicalPath();
255        }
256        catch(IOException e) {
257           dir = file.getParentFile().getAbsolutePath();
258        }
259        parser.generator(this, genmodel, genplan, ontology, dir);
260     }
261     catch(Exception e) {
262       genmodel.clear();
263       genplan.reset();
264       ontology.clear();
265 
266       error = e.toString();
267       status |= ERROR_MASK;
268     }
269     if ( warning != null )
270        status |= WARNING_MASK;
271 
272     return status;
273   }
274 
275   public void __setWarning(String info) {
276      if ( warning == null )
277         warning = info;
278      else
279         warning += "\n" + info;
280   }
281   public void __setError(String info) {
282      if ( error == null )
283         error = info;
284      else
285         error += "\n" + info;
286   }
287 
288   public String getWarning() {
289     return warning;
290   }
291   public String getError() {
292     return error;
293   }
294 }