notificationxmltablewriter.java
来自「UCS (Ultra Corba Simulator) is one more 」· Java 代码 · 共 209 行
JAVA
209 行
package com.corba.mnq.skelstub;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Implementation of XMLTableWriter
*
* @author illesp
*/
class NotificationXMLTableWriter implements ActionListener {
private DefaultTableModel model = null;
private JFrame parent = null;
private String[] head = null;
/**
* Constructor.
*
* @param header
* Table headers (may be written to a file).
* @param model
* The data. To be written to a file.
* @param parent
* The containing frame, needed for opening a dialog
* box.
*/
public NotificationXMLTableWriter(String[] header,
DefaultTableModel model, JFrame parent) {
this.model = model;
this.parent = parent;
this.head = header;
}
/**
* See
* {@link java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)}.
*
* @param e
* ActionEvent.
*/
public void actionPerformed(ActionEvent e) {
FileDialog fDialog = new FileDialog(parent, "Save as XML...");
fDialog.setMode(FileDialog.SAVE);
fDialog.pack();
fDialog.show();
String filename = fDialog.getFile();
String directory = fDialog.getDirectory();
try {
writeModelToXMLFile(model, head, filename, directory);
} catch (Exception e1) {
LOG.log(Level.SEVERE, e1.getMessage(), e);
}
}
/**
* Writes the model to the specified XML file
*
* @param myModel
* the model to write
* @param header
* the header of the model
* @param filename
* name of the file
* @param directory
* the directory where the file is located
* @throws IOException
* in case of IO problem
*/
static void writeModelToXMLFile(DefaultTableModel myModel,
String[] header, String filename, String directory)
throws IOException {
if (filename != null && !filename.equals("") && directory != null) {
FileWriter fout = new FileWriter(directory + filename);
int column = myModel.getColumnCount();
int row = myModel.getRowCount();
writeHeader(fout);
writeTrapListOpenTag(fout);
// write data to file
for (int x = 0; x < row; x++) {
writeTrap(myModel, header, fout, column, x, null);
}
writeTrapListCloseTag(fout);
fout.close();
}
}
/**
* write NotificationList closing tag
*
* @param fout
* FileWriter
* @throws IOException
*/
static void writeTrapListCloseTag(FileWriter fout) throws IOException {
fout.write("</notification_list>\n");
}
/**
* write NotificationList opening tag
*
* @param fout
* FileWriter
* @throws IOException
*/
static void writeTrapListOpenTag(FileWriter fout) throws IOException {
fout.write("<notification_list>\n");
}
/**
* Write xml header
*
* @param fout
* FileWriter
* @throws IOException
*/
static void writeHeader(FileWriter fout) throws IOException {
// writer header to file
fout.write("<?xml version=\"1.0\"?>\n");
}
/**
* Writes one notification
*
* @param myModel
* @param header
* @param fout
* @param column
* @param x
* @param type
* @throws IOException
*/
static void writeTrap(DefaultTableModel myModel, String[] header,
FileWriter fout, int column, int x, String type)
throws IOException {
fout.write(" <notification>\n");
for (int y = 0; y < column; y++) {
String value = (String) myModel.getValueAt(x, y);
if (value == null) {
value = "";
}
fout.write(" <attribute name=\""
+ header[y].toLowerCase() + "\" value=\""
+ value.replaceAll("\n", " ") + "\" />\n");
}
fout.write(" </notification>\n");
}
/**
* Writes one notification from a StructuredEvent
*
* @param myModel
* @param header
* @param fout
* @param column
* @param x
* @param type
*/
static void writeTrapFromEvent(DefaultTableModel myModel,
String[] header, FileWriter fout, int column, int x, String type) {
try {
if (fout != null) {
writeTrap(myModel, header, fout, myModel.getColumnCount(),
x, type);
}
} catch (IOException e) {
LOG
.log(
Level.SEVERE,
"IOException in XMLTabeWriter.writeNotificationFromEvent",
e);
fout = null;
}
}
/**
* Returns the logger object for use by
* {@link com.siemens.icm.nm.fw.logging.FFLoggable FFLoggable}.
*
* @return the logger to use
*/
public Logger getLogger() {
return LOG;
}
/** used for java.util.logging */
private static final Logger LOG = Logger
.getLogger(NotificationXMLTableWriter.class.getName());
}
/* EOF */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?