Add Critic.java

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1290 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
alfred 2006-08-16 05:49:09 +00:00
parent e78205b8d0
commit fed802b139
8 changed files with 159 additions and 25 deletions

View File

@ -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<String> dirScan(String path) { // use HashSet, persue speed rather than space
HashSet<String> filelist = new HashSet<String>();
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;
}
}

View File

@ -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");
}
}

View File

@ -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) {

View File

@ -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");

View File

@ -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")) {

View File

@ -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() {
addNode((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()), ui.getInput("Input Node Name"));
System.out.println();
private void delNode() {
treeModel.removeNodeFromParent((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()));
}
public void addNode(DefaultMutableTreeNode parentNode, Object child) {
private void addNode() {
addNode((DefaultMutableTreeNode)(tree.getSelectionPath().getLastPathComponent()), ui.getInput("Input Node Name"));
}
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());

View File

@ -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();
}

View File

@ -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")));