Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package toXml;
- import java.awt.FileDialog;
- import java.awt.Frame;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- /**
- * Made by Cicada on MPGH. - http://www.mpgh.net/forum/member.php?u=2377376
- */
- public class ToXML extends JFrame {
- public static ToXML instance;
- public static JTextArea console;
- public static FileDialog input;
- public static FileDialog output;
- public static File inputFile;
- public static File outputFile;
- public ToXML() {
- setTitle("ToXML - Packet Parser by Cicada."); // Sets the JFrame title.
- setSize(600, 400); // Sets the JFrame size.
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when the user closes the window.
- setLocationRelativeTo(null); // Centers the window, as it is not relative to anything.
- setResizable(false); // Makes the window unresizable...
- setLayout(null); // Sets the Layout to null for exact component positioning.
- getContentPane().setLayout(null); // The same as above for removal of subtle positioning bugs that occur.
- }
- public static void main(String... args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Removes the nasty looking Java LookAndFeel .
- instance = new ToXML(); // Declare an Instance of this newly initialised class.
- console = new JTextArea();
- console.setEditable(false);
- JScrollPane scrollPane = new JScrollPane(console, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- scrollPane.setBounds(5, 5, 350, 362);
- instance.getContentPane().add(scrollPane);
- console.setText("A small Packet Parser program by Cicada.\nTo start, specify a File to read from,\nThen specify an output folder.\n");
- input = new FileDialog(new Frame());
- output = new FileDialog(new Frame());
- input.setTitle("Choose Input");
- output.setTitle("Choose Output");
- final JButton chooseInput = new JButton("Select In...");
- chooseInput.setBounds(360, 5, 110, 20);
- chooseInput.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- input.setVisible(true);
- try {
- inputFile = input.getFiles()[0];
- console.append("\nSelected Input File: " + input.getFiles()[0].getName());
- } catch (Exception ex) {
- }
- }
- });
- instance.getContentPane().add(chooseInput);
- final JButton chooseOutput = new JButton("Select Out...");
- chooseOutput.setBounds(478, 5, 110, 20);
- chooseOutput.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- output.setVisible(true);
- try {
- outputFile = output.getFiles()[0];
- console.append("\nSelected Output File: " + output.getFiles()[0].getName() + "\nWARNING: This will overwrite existing \nfiles.");
- } catch (Exception ex) {
- }
- }
- });
- instance.getContentPane().add(chooseOutput);
- JButton start = new JButton("Start");
- start.setBounds(360, 50, 230, 20);
- start.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- if (inputFile != null && outputFile != null) {
- chooseInput.setEnabled(false);
- chooseOutput.setEnabled(false);
- console.append("\nReading from Input...");
- try {
- BufferedReader reader = new BufferedReader(new FileReader(inputFile));
- String currentLine;
- if (outputFile.exists()) {
- if (outputFile.delete()) {
- console.append("\nDeleted Target Output file for Saving new.");
- } else {
- console.append("\nUnable to delete Output File.");
- }
- }
- outputFile.createNewFile();
- FileWriter writer = new FileWriter(outputFile);
- writer.write("<?xml version=\"1.0\"?>\n<!-- Generated using the Automatic File-To-XML parser by Cicada. -->\n<Packets>\n");
- while ((currentLine = reader.readLine()) != null) {
- currentLine = currentLine.trim();
- if (currentLine.contains("value Integer")) {
- String value = currentLine.split("value Integer")[1].split(" end")[0];
- Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
- Matcher matcher = pattern.matcher(value);
- if (matcher.find()) {
- value = matcher.group(1);
- String packetName = "(" + currentLine.split("slotid")[0].split("trait const")[1].split(",")[2];
- matcher = pattern.matcher(packetName);
- if (matcher.find()) {
- packetName = matcher.group(1);
- pattern = Pattern.compile("\\\"([^)]+)\\\"");
- matcher = pattern.matcher(packetName);
- if(matcher.find()) {
- packetName = matcher.group(1);
- System.out.println("Packet: " + packetName + ": " + value);
- console.append("\nPacket: " + packetName + ": " + value);
- System.out.println("Should append to file;");
- writer.append(" <Packet id=\"" + packetName + "\" type=\"" + value + "\"/>\n");
- }
- }
- }
- }
- }
- writer.append("</Packets>");
- writer.close();
- console.append("\nWrote to file successfully.\nOutput directory:\n" + outputFile.getPath());
- System.out.println("\nWrote to file successfully.\nOutput directory:\n" + outputFile.getPath());
- } catch (IOException ex) {
- console.append("Encountered IOException.");
- ex.printStackTrace();
- }
- } else {
- console.append("\nERROR: Please select " + (inputFile == null ? (outputFile == null ? "an Input and Output" : "Input") : "an Output") + " file.");
- }
- }
- });
- instance.getContentPane().add(start);
- instance.setVisible(true); // Sets the window visible, JFrames default this to false.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment