From fed802b13935fb477523c73d0be32d0edef339d7 Mon Sep 17 00:00:00 2001 From: alfred Date: Wed, 16 Aug 2006 05:49:09 +0000 Subject: [PATCH] Add Critic.java git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1290 6f19259b-4bc3-4df7-8a09-765794883524 --- .../org/tianocore/migration/Common.java | 52 ++++++++++++++-- .../org/tianocore/migration/Critic.java | 33 +++++++++++ .../org/tianocore/migration/FirstPanel.java | 13 +++- .../org/tianocore/migration/ModuleInfo.java | 2 +- .../org/tianocore/migration/ModuleReader.java | 8 +-- .../tianocore/migration/MsaTreeEditor.java | 59 ++++++++++++++++--- .../org/tianocore/migration/MsaWriter.java | 5 +- .../migration/SourceFileReplacer.java | 10 +++- 8 files changed, 158 insertions(+), 24 deletions(-) create mode 100644 Tools/Source/MigrationTools/org/tianocore/migration/Critic.java diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/Common.java b/Tools/Source/MigrationTools/org/tianocore/migration/Common.java index 10bc2836d9..83b4e869b6 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/Common.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/Common.java @@ -1,13 +1,11 @@ package org.tianocore.migration; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.io.*; +import java.util.regex.*; +import java.util.*; public class Common { - public static String sourcefiletostring(String filename) throws Exception { + public static String file2string(String filename) throws Exception { BufferedReader rd = new BufferedReader(new FileReader(filename)); StringBuffer wholefile = new StringBuffer(); String line; @@ -27,6 +25,48 @@ public class Common { tempdir = new File(mtrseparate.group(1)); if (!tempdir.exists()) tempdir.mkdirs(); } + } + + public static void string2file(String content, String filename) throws Exception { + ensureDir(filename); + PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(filename))); + outfile.append(content); + outfile.flush(); + outfile.close(); + } + + public static HashSet dirScan(String path) { // use HashSet, persue speed rather than space + HashSet filelist = new HashSet(); + String[] list = new File(path).list(); + File test; + + for (int i = 0 ; i < list.length ; i++) { + test = new File(path + File.separator + list[i]); + if (test.isDirectory()) { + dirScan(path + File.separator + list[i]); + } else { + filelist.add(path + File.separator + list[i]); + } + } + return filelist; + } + + public static void toDoAll(String path, ForDoAll fda) throws Exception { // filter of file type can be done in toDo + String[] list = new File(path).list(); + File test; + + for (int i = 0 ; i < list.length ; i++) { + test = new File(path + File.separator + list[i]); + if (test.isDirectory()) { + toDoAll(path + File.separator + list[i], fda); + } else { + fda.toDo(path + File.separator + list[i]); + } + } + } + + public static interface ForDoAll { + public void toDo(String filepath) throws Exception; } } diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/Critic.java b/Tools/Source/MigrationTools/org/tianocore/migration/Critic.java new file mode 100644 index 0000000000..a3dbc4b78e --- /dev/null +++ b/Tools/Source/MigrationTools/org/tianocore/migration/Critic.java @@ -0,0 +1,33 @@ +package org.tianocore.migration; + +import java.util.regex.*; + +public class Critic implements Common.ForDoAll { + Critic() { + filepath = null; + } + Critic(String path) { + filepath = path; + } + + private String filepath = null; + + private static Pattern ptnheadcomment = Pattern.compile("^\\/\\*\\+\\+(.*?)\\-\\-\\*\\/",Pattern.DOTALL); + private static Matcher mtrheadcomment; + + public void toDo(String filepath) throws Exception { + if (filepath.contains(".c") || filepath.contains(".h")) { + String wholeline = Common.file2string(filepath); + mtrheadcomment = ptnheadcomment.matcher(wholeline); + if (mtrheadcomment.find()) { //as we find only the head comment here, use 'if' not 'while' + wholeline = mtrheadcomment.replaceFirst("/** @file$1**/"); + Common.string2file(wholeline, filepath + "_"); + } + } + } + + public static void fireAt(String path) throws Exception { + Common.toDoAll(path, new Critic()); + System.out.println("Critic Done"); + } +} diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java b/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java index 9f853e42f2..d0a2fc2dd5 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/FirstPanel.java @@ -27,7 +27,7 @@ public class FirstPanel extends JPanel implements ActionListener, UI { private String modulepath; private ModuleInfo mi; - private JButton moduleButton, goButton, msaEditorButton; + private JButton moduleButton, goButton, msaEditorButton, criticButton; private JTextField moduletext; private JTextArea log; private JFileChooser fc; @@ -47,6 +47,9 @@ public class FirstPanel extends JPanel implements ActionListener, UI { msaEditorButton = new JButton("MsaEditor"); msaEditorButton.addActionListener(this); + criticButton = new JButton("Critic"); + criticButton.addActionListener(this); + moduletext = new JTextField(30); filebox = new JCheckBox("Output to logfile", true); @@ -59,6 +62,7 @@ public class FirstPanel extends JPanel implements ActionListener, UI { modulePanel.add(screenbox); modulePanel.add(goButton); modulePanel.add(msaEditorButton); + modulePanel.add(criticButton); add(modulePanel); log = new JTextArea(20,25); @@ -138,6 +142,13 @@ public class FirstPanel extends JPanel implements ActionListener, UI { println(en.getMessage()); } } + if ( e.getSource() == criticButton) { + try { + Critic.fireAt(modulepath); + } catch (Exception en) { + println(en.getMessage()); + } + } } public void itemStateChanged(ItemEvent e) { diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java index 5e3bbb50d0..13d3436d95 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java @@ -113,7 +113,7 @@ public class ModuleInfo { show(hashr8only, "hashr8only : "); } - new MsaWriter(modulepath, this, db).flush(); + new MsaWriter(modulepath, this, db, ui).flush(); // remove temp directory //File tempdir = new File(modulepath + File.separator + "temp"); diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java index 46fb4994bb..4eb3246331 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/ModuleReader.java @@ -54,17 +54,13 @@ public class ModuleReader { } public void readInf(String name) throws Exception { - System.out.println("Parsing INF file: " + name); - BufferedReader rd = new BufferedReader(new FileReader(modulepath + File.separator + name)); - String line; + System.out.println("\nParsing INF file: " + name); String wholeline; - String[] linecontext; - boolean inSrc = false; Matcher mtrinfequation; Matcher mtrsection; Matcher mtrfilename; - wholeline = Common.sourcefiletostring(modulepath + File.separator + name); + wholeline = Common.file2string(modulepath + File.separator + name); mtrsection = ptnsection.matcher(wholeline); while (mtrsection.find()) { if (mtrsection.group(1).matches("defines")) { diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/MsaTreeEditor.java b/Tools/Source/MigrationTools/org/tianocore/migration/MsaTreeEditor.java index 35c1160aaf..19c3dcd984 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/MsaTreeEditor.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/MsaTreeEditor.java @@ -5,12 +5,44 @@ import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; +import org.tianocore.ModuleSurfaceAreaDocument; + public class MsaTreeEditor extends JPanel { /** * Define class Serial Version UID */ private static final long serialVersionUID = 3169905938472150649L; +/* + MsaTreeEditor(ModuleInfo m, UI u, ModuleSurfaceAreaDocument md) { + mi = m; + ui = u; + msadoc = md; + + //rootNode = msadoc.getDomNode(); + rootNode = new DefaultMutableTreeNode("Root Node"); + treeModel = new DefaultTreeModel(rootNode); + tree = new JTree(treeModel); + tree.setEditable(true); + tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); + tree.setShowsRootHandles(false); + tree.addMouseListener(mouseadapter); + + JScrollPane scrollPane = new JScrollPane(tree); + add(scrollPane); + + popupmenu = new JPopupMenu(); + menuitemadd = new JMenuItem("addNode"); + menuitemdel = new JMenuItem("deleteNode"); + popupmenu.add(menuitemadd); + popupmenu.add(menuitemdel); + menuitemadd.addActionListener(actionListener); + menuitemdel.addActionListener(actionListener); + + addNode(rootNode, "1st"); + addNode(rootNode, "2nd"); + } +*/ MsaTreeEditor(ModuleInfo m, UI u) { mi = m; ui = u; @@ -28,8 +60,8 @@ public class MsaTreeEditor extends JPanel { add(scrollPane); popupmenu = new JPopupMenu(); - JMenuItem menuitemadd = new JMenuItem("addNode"); - JMenuItem menuitemdel = new JMenuItem("deleteNode"); + menuitemadd = new JMenuItem("addNode"); + menuitemdel = new JMenuItem("deleteNode"); popupmenu.add(menuitemadd); popupmenu.add(menuitemdel); menuitemadd.addActionListener(actionListener); @@ -41,10 +73,12 @@ public class MsaTreeEditor extends JPanel { private ModuleInfo mi; private UI ui; + //private ModuleSurfaceAreaDocument msadoc; private JTree tree; private DefaultMutableTreeNode rootNode; private DefaultTreeModel treeModel; + private JMenuItem menuitemadd, menuitemdel; private JPopupMenu popupmenu; private MouseAdapter mouseadapter = new MouseAdapter() { @@ -57,21 +91,32 @@ public class MsaTreeEditor extends JPanel { }; private ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent ae) { - addNode(); + if (ae.getSource() == menuitemadd) { + addNode(); + } else if (ae.getSource() == menuitemdel) { + delNode(); + } } }; - public void addNode() { + private void delNode() { + treeModel.removeNodeFromParent((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent())); + } + + private void addNode() { addNode((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()), ui.getInput("Input Node Name")); - System.out.println(); } - public void addNode(DefaultMutableTreeNode parentNode, Object child) { + private void addNode(DefaultMutableTreeNode parentNode, Object child) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount()); tree.scrollPathToVisible(new TreePath(childNode.getPath())); } - + /* + public static void init(ModuleInfo mi, UI ui, ModuleSurfaceAreaDocument msadoc) throws Exception { + init(mi, ui); + } + */ public static void init(ModuleInfo mi, UI ui) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/MsaWriter.java b/Tools/Source/MigrationTools/org/tianocore/migration/MsaWriter.java index cf9c2a2719..06e18686f5 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/MsaWriter.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/MsaWriter.java @@ -20,15 +20,17 @@ import org.tianocore.SupportedArchitectures.Enum; import org.apache.xmlbeans.*; public class MsaWriter { - MsaWriter(String path, ModuleInfo moduleinfo, Database database) { + MsaWriter(String path, ModuleInfo moduleinfo, Database database, UI u) { modulepath = path; mi = moduleinfo; db = database; + ui = u; } private String modulepath; private ModuleInfo mi; private Database db; + private UI ui; private ModuleSurfaceAreaDocument msadoc = ModuleSurfaceAreaDocument.Factory.newInstance(); @@ -187,6 +189,7 @@ public class MsaWriter { BufferedWriter bw = new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + mi.modulename + ".msa")); fulfillMsadoc().save(bw, options); + //MsaTreeEditor.init(mi, ui, msadoc); bw.flush(); bw.close(); } diff --git a/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java b/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java index 03617ab716..d213693c24 100644 --- a/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java +++ b/Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java @@ -85,11 +85,14 @@ public class SourceFileReplacer { outname = inname; } ui.println("\nModifying file: " + inname); + Common.string2file(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname), modulepath + File.separator + "result" + File.separator + outname); + /* Common.ensureDir(modulepath + File.separator + "result" + File.separator + outname); outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname))); outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname)); outfile.flush(); outfile.close(); + */ } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) { if (inname.contains(".H")) { outname = inname.replaceFirst(".H", ".h"); @@ -97,11 +100,14 @@ public class SourceFileReplacer { outname = inname; } ui.println("\nCopying file: " + inname); + Common.string2file(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname), modulepath + File.separator + "result" + File.separator + outname); + /* Common.ensureDir(modulepath + File.separator + "result" + File.separator + outname); outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname))); - outfile.append(Common.sourcefiletostring(modulepath + File.separator + "temp" + File.separator + inname)); + outfile.append(Common.file2string(modulepath + File.separator + "temp" + File.separator + inname)); outfile.flush(); outfile.close(); + */ } } @@ -112,7 +118,7 @@ public class SourceFileReplacer { private void addr8only() throws Exception { String paragraph = null; - String line = Common.sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c"); + String line = Common.file2string(Database.defaultpath + File.separator + "R8Lib.c"); Common.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c"); PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c"))); PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h"))); -- 2.39.2