1 package com.genimen.djeneric.tools.generator.test; 2 3 import java.awt.AWTEvent ; 4 import java.awt.BorderLayout ; 5 import java.awt.Dimension ; 6 import java.awt.FlowLayout ; 7 import java.awt.Toolkit ; 8 import java.awt.event.ActionEvent ; 9 import java.awt.event.KeyEvent ; 10 import java.awt.event.MouseEvent ; 11 import java.awt.event.WindowEvent ; 12 import java.io.BufferedReader ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.net.URL ; 16 17 import javax.swing.ImageIcon ; 18 import javax.swing.JButton ; 19 import javax.swing.JCheckBox ; 20 import javax.swing.JLabel ; 21 import javax.swing.JOptionPane ; 22 import javax.swing.JPanel ; 23 import javax.swing.JScrollPane ; 24 import javax.swing.JSplitPane ; 25 import javax.swing.JTextArea ; 26 import javax.swing.JTree ; 27 import javax.swing.UIManager ; 28 import javax.swing.event.TreeSelectionEvent ; 29 import javax.swing.tree.DefaultTreeModel ; 30 import javax.swing.tree.TreePath ; 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 indent; 47 BorderLayout borderLayout1 = new BorderLayout (); 48 JPanel jPanel1 = new JPanel (); 49 BorderLayout borderLayout2 = new BorderLayout (); 50 JPanel jPanel2 = new JPanel (); 51 JButton _butParse = new JButton (); 52 JPanel jPanel3 = new JPanel (); 53 FlowLayout flowLayout1 = new FlowLayout (); 54 JButton jButton2 = new JButton (); 55 int curIdx = 1; 56 JSplitPane jSplitPane2 = new JSplitPane (); 57 JTextArea _sampleCode = new JTextArea (); 58 JScrollPane _scrCode = new JScrollPane (); 59 JTextArea _value = new JTextArea (); 60 JScrollPane _scrValue = new JScrollPane (); 61 JSplitPane jSplitPane1 = new JSplitPane (); 62 JScrollPane _scrTree = new JScrollPane (); 63 JTree _tree = new JTree (); 64 JLabel _lblStatus = new JLabel (); 65 String NEWLINE = System.getProperty("line.separator", "\n"); 66 TemplateNode _parseTreeRoot = null; 67 ParseTreeNode _treeRoot = null; 68 DefaultTreeModel _treeModel = null; 69 JButton jButton3 = new JButton (); 70 JCheckBox _chkNormalize = new JCheckBox (); 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 e) 87 { 88 JOptionPane.showMessageDialog(this, e.getMessage()); 89 DjLogger.log(e); 90 System.exit(-1); 91 } 92 } 93 94 public static ImageIcon getImageIcon(String fileName) 95 { 96 URL url = DjentelParser.class.getResource(fileName); 97 if (url != null) return new ImageIcon (url); 98 else return new ImageIcon (""); 99 } 101 102 private void jbInit() throws Exception 103 { 104 this.getContentPane().setLayout(borderLayout1); 105 jPanel1.setLayout(borderLayout2); 106 _butParse.setText("(Re)parse"); 107 _butParse.addActionListener(new java.awt.event.ActionListener () 108 { 109 public void actionPerformed(ActionEvent 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 () 118 { 119 public void actionPerformed(ActionEvent e) 120 { 121 jButton2_actionPerformed(e); 122 } 123 }); 124 _sampleCode.setFont(new java.awt.Font ("Monospaced", 0, 12)); 125 _sampleCode.addKeyListener(new java.awt.event.KeyAdapter () 126 { 127 public void keyPressed(KeyEvent e) 128 { 129 _sampleCode_keyPressed(e); 130 } 131 }); 132 _value.setFont(new java.awt.Font ("Monospaced", 0, 12)); 133 _tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener () 134 { 135 public void valueChanged(TreeSelectionEvent e) 136 { 137 _tree_valueChanged(e); 138 } 139 }); 140 _tree.addMouseListener(new java.awt.event.MouseAdapter () 141 { 142 public void mouseClicked(MouseEvent 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 () 151 { 152 public void actionPerformed(ActionEvent 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 parse(String tpl) throws Exception 193 { 194 DjentelParserEngine parser = DjentelParserEngine.parse(tpl); 195 return parser.getResult(); 196 } 197 198 public Node getTree(String tpl) throws Exception 199 { 200 return DjentelParserEngine.getTree(tpl); 201 } 202 203 protected void processWindowEvent(WindowEvent 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 [] args) 214 { 215 try 216 { 217 UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 218 DjentelParser frame = new DjentelParser(); 219 frame.validate(); 220 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 221 Dimension 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 e) 231 { 232 DjLogger.log(e); 233 } 234 } 235 236 String getSample(int idx) throws Exception 237 { 238 InputStream is = this.getClass().getResourceAsStream(idx + ".tpl"); 239 if (is == null) 240 { 241 return null; 242 } 243 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 244 String line; 245 StringBuffer sb = new StringBuffer (); 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 (_treeRoot); 258 buildTree(_tree, _treeRoot); 259 260 _tree.setModel(_treeModel); 261 262 _treeModel.nodeStructureChanged(_treeRoot); 263 expandAll(_treeRoot); 264 } 265 266 void buildTree(JTree 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 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 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 (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 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 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 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 e) 364 { 365 nextSample(); 366 } 367 368 void _butParse_actionPerformed(ActionEvent e) 369 { 370 reparse(); 371 372 } 373 374 public ParseTreeNode getSelectedNode() 375 { 376 if (_tree.getSelectionPath() == null) return null; 377 378 TreePath tp = _tree.getSelectionPath(); 379 380 return (ParseTreeNode) (tp.getLastPathComponent()); 381 } 382 383 void _tree_valueChanged(TreeSelectionEvent 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 x) 412 { 413 DjLogger.log(x); 414 } 415 } 416 417 void _tree_mouseClicked(MouseEvent 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 434 String 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 x) 447 { 448 _value.setText(x.getMessage()); 449 DjLogger.log(x); 450 } 451 } 452 453 void jButton3_actionPerformed(ActionEvent 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 x) 469 { 470 DjLogger.log(x); 471 } 472 } 473 474 public void update() 475 { 476 } 477 478 void _sampleCode_keyPressed(KeyEvent e) 479 { 480 if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK) 481 { 482 } 484 if (e.getKeyCode() == KeyEvent.VK_Z && e.getModifiers() == KeyEvent.CTRL_MASK + KeyEvent.SHIFT_MASK) 485 { 486 488 } 489 } 490 491 } | Popular Tags |