KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > test > DjScriptParser


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

429     // Hashtable constants = new Hashtable();
430
// constants.put("rootdir", "d:/gendir");
431
// parseTree.setGlobalConstants(constants);
432

433     // String str = parseTree.evaluate(mgr, id);
434
// System.out.println("Generation took " + (System.currentTimeMillis() -
435
// 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 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, "/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