KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > test > DjentelParser


1 package com.genimen.djeneric.tools.generator.test;
2
3 import java.awt.AWTEvent JavaDoc;
4 import java.awt.BorderLayout JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.FlowLayout JavaDoc;
7 import java.awt.Toolkit JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.event.KeyEvent JavaDoc;
10 import java.awt.event.MouseEvent JavaDoc;
11 import java.awt.event.WindowEvent JavaDoc;
12 import java.io.BufferedReader JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import javax.swing.ImageIcon JavaDoc;
18 import javax.swing.JButton JavaDoc;
19 import javax.swing.JCheckBox JavaDoc;
20 import javax.swing.JLabel JavaDoc;
21 import javax.swing.JOptionPane JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.JScrollPane JavaDoc;
24 import javax.swing.JSplitPane JavaDoc;
25 import javax.swing.JTextArea JavaDoc;
26 import javax.swing.JTree JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28 import javax.swing.event.TreeSelectionEvent JavaDoc;
29 import javax.swing.tree.DefaultTreeModel JavaDoc;
30 import javax.swing.tree.TreePath JavaDoc;
31
32 import com.genimen.djeneric.repository.DjPersistenceManager;
33 import com.genimen.djeneric.repository.DjPersistenceManagerFactory;
34 import com.genimen.djeneric.tools.common.DjenericTool;
35 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
36 import com.genimen.djeneric.tools.generator.core.Node;
37 import com.genimen.djeneric.tools.generator.core.ParseException;
38 import com.genimen.djeneric.tools.generator.core.SimpleNode;
39 import com.genimen.djeneric.tools.generator.core.nodes.TemplateNode;
40 import com.genimen.djeneric.util.DjLogger;
41 import com.genimen.djeneric.util.DjStringReplacer;
42
43 public class DjentelParser extends DjenericTool
44 {
45   private static final long serialVersionUID = 1L;
46   String JavaDoc indent;
47   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
48   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
49   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
50   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
51   JButton JavaDoc _butParse = new JButton JavaDoc();
52   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
53   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
54   JButton JavaDoc jButton2 = new JButton JavaDoc();
55   int curIdx = 1;
56   JSplitPane JavaDoc jSplitPane2 = new JSplitPane JavaDoc();
57   JTextArea JavaDoc _sampleCode = new JTextArea JavaDoc();
58   JScrollPane JavaDoc _scrCode = new JScrollPane JavaDoc();
59   JTextArea JavaDoc _value = new JTextArea JavaDoc();
60   JScrollPane JavaDoc _scrValue = new JScrollPane JavaDoc();
61   JSplitPane JavaDoc jSplitPane1 = new JSplitPane JavaDoc();
62   JScrollPane JavaDoc _scrTree = new JScrollPane JavaDoc();
63   JTree JavaDoc _tree = new JTree JavaDoc();
64   JLabel JavaDoc _lblStatus = new JLabel JavaDoc();
65   String JavaDoc NEWLINE = System.getProperty("line.separator", "\n");
66   TemplateNode _parseTreeRoot = null;
67   ParseTreeNode _treeRoot = null;
68   DefaultTreeModel JavaDoc _treeModel = null;
69   JButton JavaDoc jButton3 = new JButton JavaDoc();
70   JCheckBox JavaDoc _chkNormalize = new JCheckBox JavaDoc();
71
72   public DjentelParser()
73   {
74     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
75
76     try
77     {
78       jbInit();
79
80       nextSample();
81
82       setTitle("Djentel, the Djeneric Template Language Parser v1.0");
83       setSize(800, 600);
84
85     }
86     catch (Exception JavaDoc e)
87     {
88       JOptionPane.showMessageDialog(this, e.getMessage());
89       DjLogger.log(e);
90       System.exit(-1);
91     }
92   }
93
94   public static ImageIcon JavaDoc getImageIcon(String JavaDoc fileName)
95   {
96     URL JavaDoc url = DjentelParser.class.getResource(fileName);
97     if (url != null) return new ImageIcon JavaDoc(url);
98     else return new ImageIcon JavaDoc("");
99     // Default to empty image
100
}
101
102   private void jbInit() throws Exception JavaDoc
103   {
104     this.getContentPane().setLayout(borderLayout1);
105     jPanel1.setLayout(borderLayout2);
106     _butParse.setText("(Re)parse");
107     _butParse.addActionListener(new java.awt.event.ActionListener JavaDoc()
108     {
109       public void actionPerformed(ActionEvent JavaDoc e)
110       {
111         _butParse_actionPerformed(e);
112       }
113     });
114     jPanel3.setLayout(flowLayout1);
115     jButton2.setToolTipText("Next example");
116     jButton2.setText(">>");
117     jButton2.addActionListener(new java.awt.event.ActionListener JavaDoc()
118     {
119       public void actionPerformed(ActionEvent JavaDoc e)
120       {
121         jButton2_actionPerformed(e);
122       }
123     });
124     _sampleCode.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
125     _sampleCode.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
126     {
127       public void keyPressed(KeyEvent JavaDoc e)
128       {
129         _sampleCode_keyPressed(e);
130       }
131     });
132     _value.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
133     _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc()
134     {
135       public void valueChanged(TreeSelectionEvent JavaDoc e)
136       {
137         _tree_valueChanged(e);
138       }
139     });
140     _tree.addMouseListener(new java.awt.event.MouseAdapter JavaDoc()
141     {
142       public void mouseClicked(MouseEvent JavaDoc e)
143       {
144         _tree_mouseClicked(e);
145       }
146     });
147     _lblStatus.setText("Ok");
148     jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
149     jButton3.setText("Evaluate");
150     jButton3.addActionListener(new java.awt.event.ActionListener JavaDoc()
151     {
152       public void actionPerformed(ActionEvent JavaDoc e)
153       {
154         jButton3_actionPerformed(e);
155       }
156     });
157     flowLayout1.setAlignment(FlowLayout.LEFT);
158     _chkNormalize.setSelected(true);
159     _chkNormalize.setText("Normalize");
160     _scrValue.setAutoscrolls(true);
161     this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
162     jPanel1.add(jPanel2, BorderLayout.EAST);
163     jPanel2.add(jButton3, null);
164     jPanel2.add(_butParse, null);
165     jPanel1.add(jPanel3, BorderLayout.CENTER);
166     jPanel3.add(jButton2, null);
167     jPanel3.add(_chkNormalize, null);
168     jPanel1.add(_lblStatus, BorderLayout.NORTH);
169     this.getContentPane().add(jSplitPane2, BorderLayout.CENTER);
170
171     jSplitPane2.add(_scrCode, JSplitPane.BOTTOM);
172     jSplitPane2.add(jSplitPane1, JSplitPane.TOP);
173
174     jSplitPane1.add(_scrTree, JSplitPane.TOP);
175     jSplitPane1.add(_scrValue, JSplitPane.BOTTOM);
176     _scrValue.getViewport().add(_value, null);
177     _scrTree.getViewport().add(_tree, null);
178     _scrCode.getViewport().add(_sampleCode, null);
179     jSplitPane1.setDividerLocation(320);
180     jSplitPane2.setDividerLocation(300);
181     _sampleCode.setAutoscrolls(true);
182     _tree.setRootVisible(true);
183     _tree.setShowsRootHandles(true);
184     _tree.setAutoscrolls(true);
185     _tree.setToggleClickCount(0);
186     _tree.setDoubleBuffered(true);
187     _tree.setCellRenderer(new ParseTreeCellRenderer());
188     _tree.putClientProperty("JTree.lineStyle", "Angled");
189
190   }
191
192   public String JavaDoc parse(String JavaDoc tpl) throws Exception JavaDoc
193   {
194     DjentelParserEngine parser = DjentelParserEngine.parse(tpl);
195     return parser.getResult();
196   }
197
198   public Node getTree(String JavaDoc tpl) throws Exception JavaDoc
199   {
200     return DjentelParserEngine.getTree(tpl);
201   }
202
203   protected void processWindowEvent(WindowEvent JavaDoc e)
204   {
205     if (e.getID() == WindowEvent.WINDOW_CLOSING)
206     {
207       System.exit(0);
208     }
209     super.processWindowEvent(e);
210
211   }
212
213   public static void main(String JavaDoc[] args)
214   {
215     try
216     {
217       UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
218       DjentelParser frame = new DjentelParser();
219       frame.validate();
220       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
221       Dimension JavaDoc frameSize = frame.getSize();
222       if (frameSize.height > screenSize.height) frameSize.height = screenSize.height;
223
224       if (frameSize.width > screenSize.width) frameSize.width = screenSize.width;
225
226       frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
227       frame.setVisible(true);
228
229     }
230     catch (Exception JavaDoc e)
231     {
232       DjLogger.log(e);
233     }
234   }
235
236   String JavaDoc getSample(int idx) throws Exception JavaDoc
237   {
238     InputStream JavaDoc is = this.getClass().getResourceAsStream(idx + ".tpl");
239     if (is == null)
240     {
241       return null;
242     }
243     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
244     String JavaDoc line;
245     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
246     while ((line = br.readLine()) != null)
247     {
248       sb.append(line + NEWLINE);
249     }
250     is.close();
251     return sb.toString();
252   }
253
254   void rebuildTree(SimpleNode root)
255   {
256     _treeRoot = new ParseTreeNode(_tree, root);
257     _treeModel = new DefaultTreeModel JavaDoc(_treeRoot);
258     buildTree(_tree, _treeRoot);
259
260     _tree.setModel(_treeModel);
261
262     _treeModel.nodeStructureChanged(_treeRoot);
263     expandAll(_treeRoot);
264   }
265
266   void buildTree(JTree JavaDoc tree, ParseTreeNode treeNode)
267   {
268
269     SimpleNode parseNode = (SimpleNode) treeNode.getNode();
270     for (int i = 0; i < parseNode.getChildCount(); i++)
271     {
272       SimpleNode child = parseNode.getChild(i);
273       ParseTreeNode ptn = new ParseTreeNode(tree, child);
274       treeNode.insertAsChild(ptn);
275       buildTree(tree, ptn);
276     }
277   }
278
279   void nextSample()
280   {
281     try
282     {
283       String JavaDoc code = getSample(curIdx++);
284       if (code == null)
285       {
286         curIdx = 1;
287         code = getSample(curIdx++);
288       }
289       if (code == null)
290       {
291         _lblStatus.setText("No more samples");
292       }
293       _sampleCode.setText(code);
294       reparse();
295       _lblStatus.setText(_lblStatus.getText() + " (Sample " + (curIdx - 1) + ")");
296     }
297     catch (ParseException px)
298     {
299       handleParseException(px);
300     }
301     catch (Exception JavaDoc x)
302     {
303       curIdx = 1;
304       DjLogger.log(x);
305     }
306
307   }
308
309   public void expandAll(ParseTreeNode node)
310   {
311     _tree.expandPath(new TreePath JavaDoc(node.getPath()));
312     int cc = node.getChildCount();
313     for (int i = 0; i < cc; i++)
314     {
315       ParseTreeNode child = (ParseTreeNode) node.getChildAt(i);
316       expandAll(child);
317     }
318   }
319
320   void handleParseException(ParseException px)
321   {
322     DjLogger.log(px);
323     _value.setText(px.getMessage());
324     _lblStatus.setText("Parse error: " + px.getMessage());
325
326     try
327     {
328       int offset = _sampleCode.getLineStartOffset(px.getLine() - 1) + px.getColumn() - 1;
329
330       _sampleCode.setCaretPosition(offset);
331       _sampleCode.requestFocus();
332     }
333     catch (Exception JavaDoc x2)
334     {
335       DjLogger.log(x2);
336     }
337   }
338
339   void reparse()
340   {
341     try
342     {
343       DjStringReplacer sr = new DjStringReplacer(_sampleCode.getText());
344       String JavaDoc code = sr.replace("\r", "");
345       _parseTreeRoot = DjentelParserEngine.getTree(code, _chkNormalize.isSelected());
346
347       rebuildTree(_parseTreeRoot);
348       _lblStatus.setText("Parsed ok");
349     }
350     catch (ParseException px)
351     {
352       handleParseException(px);
353     }
354     catch (Throwable JavaDoc x)
355     {
356       DjLogger.log(x);
357       _value.setText(x.getMessage());
358       _lblStatus.setText("Parse error: " + x.getMessage());
359     }
360
361   }
362
363   void jButton2_actionPerformed(ActionEvent JavaDoc e)
364   {
365     nextSample();
366   }
367
368   void _butParse_actionPerformed(ActionEvent JavaDoc e)
369   {
370     reparse();
371
372   }
373
374   public ParseTreeNode getSelectedNode()
375   {
376     if (_tree.getSelectionPath() == null) return null;
377
378     TreePath JavaDoc tp = _tree.getSelectionPath();
379
380     return (ParseTreeNode) (tp.getLastPathComponent());
381   }
382
383   void _tree_valueChanged(TreeSelectionEvent JavaDoc e)
384   {
385     ParseTreeNode tn = getSelectedNode();
386     if (tn == null) return;
387     SimpleNode node = (SimpleNode) tn.getNode();
388     _value.setText(node.toString());
389     showLocation();
390   }
391
392   void showLocation()
393   {
394     try
395     {
396       ParseTreeNode tn = getSelectedNode();
397       if (tn == null) return;
398       SimpleNode sn = (SimpleNode) tn.getNode();
399       if (sn.beginLine == 0)
400       {
401         _lblStatus.setText("Location unspecified");
402       }
403       else
404       {
405         int offset = _sampleCode.getLineStartOffset(sn.beginLine - 1) + sn.beginColumn - 1;
406
407         _sampleCode.setCaretPosition(offset);
408         _lblStatus.setText("Located at " + sn.beginLine + ":" + sn.beginColumn + " (offset " + offset + ")");
409       }
410     }
411     catch (Exception JavaDoc x)
412     {
413       DjLogger.log(x);
414     }
415   }
416
417   void _tree_mouseClicked(MouseEvent JavaDoc e)
418   {
419     if (e.getClickCount() > 1) _sampleCode.requestFocus();
420   }
421
422   void evaluate(DjPersistenceManager mgr, TemplateNode parseTree)
423   {
424     try
425     {
426       long id = 7;
427
428       long tm = System.currentTimeMillis();
429
430       // Hashtable constants = new Hashtable();
431
// constants.put("rootdir", "d:/gendir");
432
// parseTree.setGlobalConstants(constants);
433

434       String JavaDoc str = parseTree.evaluate(mgr, id);
435       System.out.println("Generation took " + (System.currentTimeMillis() - tm) + "ms");
436
437       _value.setText(str);
438       _value.setCaretPosition(0);
439
440       System.out.println("Filename = [" + parseTree.getFileName() + "]");
441     }
442     catch (ParseException px)
443     {
444       handleParseException(px);
445     }
446     catch (Exception JavaDoc x)
447     {
448       _value.setText(x.getMessage());
449       DjLogger.log(x);
450     }
451   }
452
453   void jButton3_actionPerformed(ActionEvent JavaDoc e)
454   {
455     try
456     {
457       DjPersistenceManagerFactory factory = new DjPersistenceManagerFactory(this, "d:/djeneric/repositories.xml");
458       DjPersistenceManager mgr = factory.createInstance("Local on Hypersonic");
459       try
460       {
461         evaluate(mgr, _parseTreeRoot);
462       }
463       finally
464       {
465         mgr.close();
466       }
467     }
468     catch (Exception JavaDoc x)
469     {
470       DjLogger.log(x);
471     }
472   }
473
474   public void update()
475   {
476   }
477
478   void _sampleCode_keyPressed(KeyEvent JavaDoc e)
479   {
480     if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK)
481     {
482       //undoManager.undo();
483
}
484     if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK)
485     {
486       //undoManager.redo();
487

488     }
489   }
490
491 }
Popular Tags