📄 configmanager.java
字号:
package com.sutternow.swingkar;
import org.dom4j.*;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.swing.DocumentTreeModel;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import java.io.*;
import java.net.URL;
import com.javanovic.karapansapi.strutsgen.DBReverse;
import com.javanovic.karapansapi.xml.*;
import com.sutternow.swingkar.Preferences.Preferences;
import com.sutternow.swingkar.Preferences.PreferencesLoader;
import com.sutternow.swingkar.util.StrutsGenRunner;
import com.sutternow.templates.TemplatesDefs;
import javax.swing.*;
/**
* Created by IntelliJ IDEA.
* User: payne
* Date: Jan 27, 2003
* Time: 11:33:03 AM
* To change this template use Options | File Templates.
*/
public class ConfigManager {
private static ConfigManager instance = null;
private Preferences pref;
public ConfigManager(String configFileName) throws Exception {
srcFileName = configFileName;
configDoc = parse(configFileName);
if (instance == null ) {
instance = this;
}
if (pref == null) {
pref = PreferencesLoader.loadPreferences();
}
}
public void loadNewConfig(String configFileName) throws Exception {
srcFileName = configFileName;
configDoc = parse(configFileName);
}
public ConfigManager() {
if (instance == null ) {
instance = this;
}
/* do not recommend calling it like this */
pref = PreferencesLoader.loadPreferences();
}
public Preferences getPreferences() {
return pref;
}
public void setPreferences(Preferences newPrefs) {
pref = newPrefs;
PreferencesLoader.storePerfernces(newPrefs);
}
/**
* Returns an instance of <code>ProjectManager</code>.
*
* @return The instance value
*/
public static ConfigManager getInstance() {
return instance;
}
protected Document parse(String xmlFile) throws DocumentException {
SAXReader reader = new SAXReader();
return reader.read(xmlFile);
}
public Document getBaseDocument() {
return configDoc;
}
public DocumentTreeModel getTreeModel() {
return new DocumentTreeModel(configDoc.getDocument());
}
public DocumentTreeModel getJustBuilds() {
Document blah = this.configDoc;
try {
blah = DocumentHelper.parseText(configDoc.selectSingleNode("//strutscreator").asXML());
System.out.println(configDoc.selectSingleNode("//strutscreator").asXML());
} catch (DocumentException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
return new DocumentTreeModel(blah);
}
public TemplatesDefs getTemplateDefinitions() {
TemplatesDefs defs = new TemplatesDefs();
Unmarshaller um = new Unmarshaller(defs);
try {
/* templates.xml is expected to be at the root of a class path element */
URL configURL = defs.getClass().getResource("/templates.xml");
um.unmarshal(new FileReader(configURL.getPath()));
} catch (MarshalException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ValidationException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (java.io.IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
return defs;
}
public boolean isRefreshNeeded() {
return needsRefresh;
}
public synchronized void requestRefresh() {
needsRefresh = true;
}
public void doRefresh() {
needsRefresh = false;
}
public void deleteNode(Node sXPath) {
sXPath.detach();
saveChanges();
}
public Element addRole(String roleName) {
Element se = configDoc.getRootElement().element("security").addElement("security-role");
se.setText(roleName);
return se;
}
public Element addGlobalValue(String globalValName) {
Element se = configDoc.getRootElement().addElement("global-value-ref");
se.addElement("name").setText(globalValName);
this.saveChanges();
return se;
}
public Element addGlobalReference(String globalRefName) {
Element e = configDoc.getRootElement().addElement("global-reference");
e.addElement("name").setText(globalRefName);
e.addElement("bean-name").setText("");
e.addElement("query-name").setText("");
e.addElement("scope").setText("");
this.saveChanges();
return e;
}
public Element addMenu(String menuName) {
Element e = configDoc.getRootElement().addElement("menu");
e.addElement("title").setText(menuName);
return e;
}
public Element addQuery(String queryName) {
Element e = configDoc.getRootElement().addElement("menu");
e.addElement("title").setText(queryName);
return e;
}
public Element addBean (String beanName) {
/* Bean Layout
<name>Users</name>
<generate-dao>true</generate-dao>
<generate-process>true</generate-process>
<page-length>20</page-length>
<cache-mtl>1</cache-mtl>
<javascript-validation>false</javascript-validation>
*/
Element elm = configDoc.getRootElement().addElement("bean");
elm.addElement("name").setText(beanName);
elm.addElement("generate-dao").setText("true");
elm.addElement("generate-process").setText("true");
elm.addElement("page-length").setText("20");
elm.addElement("cache-mtl").setText("1");
elm.addElement("javacript-validation").setText("false");
elm.addElement("primary-key");
elm.addElement("attribute");
return elm;
}
public Element addColumn (Element parent, String columnName) {
/* Column Layout
<column>
<name>password</name>
<title>Password</title>
<type>password</type>
<validation>
<required>true</required>
<max-length>20</max-length>
</validation>
</column>
*/
Element newColumn = parent.addElement("column");
newColumn.addElement("name").setText(columnName);
newColumn.addElement("title").setText(columnName);
newColumn.addElement("type").setText("string");
Element validation= newColumn.addElement("validation");
validation.addElement("required").setText("false");
validation.addElement("max-length").setText("20");
return newColumn;
}
public Element addConstraint(String constraintName) {
Element se = configDoc.getRootElement().element("security").addElement("security-constraint");
se.addElement("name").setText(constraintName);
//se.setText(constraintName);
return se;
}
public void saveBuildConfig(Element exportNode) {
try {
Node oldNode = configDoc.selectSingleNode("//build");
oldNode.detach(); // delete old Node
configDoc.getRootElement().add(exportNode);
saveChanges();
} catch (Exception e) {
e.printStackTrace();
System.err.println("unable to do this");
}
}
public void doReverse() {
DBReverse rev = new DBReverse();
//todo validate the all the requirements are in place before making this call.
/*
Strutscreator tree = rev.reverse(dbElement.element("dbms").getText(), dbElement.element("driver").getText(),
dbSchema, dbElement.element("url").getText(),
dbElement.element("user").getText(), dbElement.element("password").getText());
*/
Strutscreator tree = new Strutscreator();
Unmarshaller um = new Unmarshaller(tree);
try {
um.unmarshal(new StringReader(configDoc.asXML()));
rev.freshenBeans(tree);
} catch (MarshalException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ValidationException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
//marshall back to file
XMLHandler handler = new XMLHandlerImpl();
try {
handler.save(this.srcFileName, tree);
this.loadNewConfig(this.srcFileName);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (XMLException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
public void doGeneration() {
StrutsGenRunner runner = new StrutsGenRunner();
if (srcFileName != null) {
System.out.println("Generating Project Content:");
runner.doGeneration(srcFileName);
} else {
System.out.println("Source config file needs to be saved before Struts Generation process is run.");
saveChanges();
if (srcFileName !=null) {
System.out.println("Generating Project Content:");
runner.doGeneration(srcFileName);
}
}
}
public void saveAs(File newOutFile) {
try {
FileWriter out = new FileWriter(newOutFile);
XMLWriter writer = new XMLWriter(out, OutputFormat.createCompactFormat());
writer.write(configDoc);
out.close();
srcFileName = newOutFile.getAbsolutePath();
this.requestRefresh();
} catch (Exception e) {
System.out.println("Problem saving file.");
e.printStackTrace();
}
}
public void saveChanges() {
if (srcFileName == null) {
JFileChooser fc = new JFileChooser();
// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(null);
if (fc.getSelectedFile() != null) {
System.out.println(fc.getSelectedFile());
this.saveAs(fc.getSelectedFile());
srcFileName = fc.getSelectedFile().getAbsolutePath();
}
} else {
try {
FileWriter out = new FileWriter(srcFileName);
XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
writer.write(configDoc);
out.close();
this.requestRefresh();
} catch (Exception e) {
System.out.println("Problem saving file.");
}
}
}
protected void loadEmptyTree() {
Strutscreator tree = new Strutscreator();
Property prop = new Property();
prop.setAuthor(pref.getAuthorName());
prop.setCompany(pref.getCompanyName());
prop.setName("Application Name");
prop.setVersion("1.0");
tree.setProperty(prop);
Database db = new Database();
db.setMaximumConnections(30);
db.setMinimumConnections(5);
db.setJndi("");
db.setDbms("mysql");
db.setDriver("null");
db.setUrl("null");
db.setUser("");
db.setPassword("");
tree.setDatabase(db);
Build build = new Build();
build.setCompiler("modern");
build.setDateFormat(pref.getDateFormat());
build.setDirectory(".");
build.setPackage(pref.getPackageName());
build.setWarFileName("app.war");
build.setServletContainerName("Servlet Container");
build.setServletContainerDir(".");
tree.setBuild(build);
tree.setSecurity(new Security());
java.io.StringWriter writer = new java.io.StringWriter();
try {
tree.marshal(writer);
configDoc = DocumentHelper.parseText(writer.toString());
} catch (DocumentException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (Exception e) {
e.printStackTrace();
}
this.requestRefresh();
srcFileName = null;
}
private String srcFileName;
private Document configDoc;
private boolean needsRefresh;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -