Guest User

Cicada File-To-XML.

a guest
Feb 21st, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.30 KB | None | 0 0
  1. package toXml;
  2.  
  3. import java.awt.FileDialog;
  4. import java.awt.Frame;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.io.Writer;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15.  
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextArea;
  20. import javax.swing.UIManager;
  21. import javax.swing.UnsupportedLookAndFeelException;
  22.  
  23. /**
  24.  * Made by Cicada on MPGH. - http://www.mpgh.net/forum/member.php?u=2377376
  25.  */
  26. public class ToXML extends JFrame {
  27.    
  28.     public static ToXML instance;
  29.     public static JTextArea console;
  30.     public static FileDialog input;
  31.     public static FileDialog output;
  32.     public static File inputFile;
  33.     public static File outputFile;
  34.    
  35.     public ToXML() {
  36.         setTitle("ToXML - Packet Parser by Cicada."); // Sets the JFrame title.
  37.         setSize(600, 400); // Sets the JFrame size.
  38.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exits the program when the user closes the window.
  39.         setLocationRelativeTo(null); // Centers the window, as it is not relative to anything.
  40.         setResizable(false); // Makes the window unresizable...
  41.         setLayout(null); // Sets the Layout to null for exact component positioning.
  42.         getContentPane().setLayout(null); // The same as above for removal of subtle positioning bugs that occur.
  43.     }
  44.    
  45.     public static void main(String... args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
  46.         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // Removes the nasty looking Java LookAndFeel .
  47.         instance = new ToXML(); // Declare an Instance of this newly initialised class.
  48.        
  49.         console = new JTextArea();
  50.         console.setEditable(false);
  51.         JScrollPane scrollPane = new JScrollPane(console, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  52.         scrollPane.setBounds(5, 5, 350, 362);
  53.         instance.getContentPane().add(scrollPane);
  54.        
  55.         console.setText("A small Packet Parser program by Cicada.\nTo start, specify a File to read from,\nThen specify an output folder.\n");
  56.        
  57.         input = new FileDialog(new Frame());
  58.         output = new FileDialog(new Frame());
  59.        
  60.         input.setTitle("Choose Input");
  61.         output.setTitle("Choose Output");
  62.        
  63.        
  64.         final JButton chooseInput = new JButton("Select In...");
  65.         chooseInput.setBounds(360, 5, 110, 20);
  66.         chooseInput.addActionListener(new ActionListener() {
  67.            
  68.             public void actionPerformed(ActionEvent event) {
  69.                 input.setVisible(true);
  70.                 try {
  71.                     inputFile = input.getFiles()[0];
  72.                     console.append("\nSelected Input File: " + input.getFiles()[0].getName());
  73.                 } catch (Exception ex) {
  74.                 }
  75.             }
  76.         });
  77.         instance.getContentPane().add(chooseInput);
  78.        
  79.         final JButton chooseOutput = new JButton("Select Out...");
  80.         chooseOutput.setBounds(478, 5, 110, 20);
  81.         chooseOutput.addActionListener(new ActionListener() {
  82.            
  83.             public void actionPerformed(ActionEvent event) {
  84.                 output.setVisible(true);
  85.                 try {
  86.                     outputFile = output.getFiles()[0];
  87.                     console.append("\nSelected Output File: " + output.getFiles()[0].getName() + "\nWARNING: This will overwrite existing \nfiles.");
  88.                 } catch (Exception ex) {
  89.                 }
  90.             }
  91.         });
  92.         instance.getContentPane().add(chooseOutput);
  93.        
  94.         JButton start = new JButton("Start");
  95.         start.setBounds(360, 50, 230, 20);
  96.         start.addActionListener(new ActionListener() {
  97.            
  98.             public void actionPerformed(ActionEvent event) {
  99.                 if (inputFile != null && outputFile != null) {
  100.                     chooseInput.setEnabled(false);
  101.                     chooseOutput.setEnabled(false);
  102.                     console.append("\nReading from Input...");
  103.                    
  104.                     try {
  105.                         BufferedReader reader = new BufferedReader(new FileReader(inputFile));
  106.                         String currentLine;
  107.                        
  108.                         if (outputFile.exists()) {
  109.                             if (outputFile.delete()) {
  110.                                 console.append("\nDeleted Target Output file for Saving new.");
  111.                             } else {
  112.                                 console.append("\nUnable to delete Output File.");
  113.                             }
  114.                         }
  115.                        
  116.                         outputFile.createNewFile();
  117.                        
  118.                         FileWriter writer = new FileWriter(outputFile);
  119.                         writer.write("<?xml version=\"1.0\"?>\n<!-- Generated using the Automatic File-To-XML parser by Cicada. -->\n<Packets>\n");
  120.                        
  121.                         while ((currentLine = reader.readLine()) != null) {
  122.                             currentLine = currentLine.trim();
  123.                            
  124.                             if (currentLine.contains("value Integer")) {
  125.                                 String value = currentLine.split("value Integer")[1].split(" end")[0];
  126.                                
  127.                                 Pattern pattern = Pattern.compile("\\(([^)]+)\\)");
  128.                                 Matcher matcher = pattern.matcher(value);
  129.                                 if (matcher.find()) {
  130.                                     value = matcher.group(1);
  131.                                    
  132.                                     String packetName = "(" + currentLine.split("slotid")[0].split("trait const")[1].split(",")[2];
  133.                                     matcher = pattern.matcher(packetName);
  134.                                    
  135.                                     if (matcher.find()) {
  136.                                         packetName = matcher.group(1);
  137.                                    
  138.                                         pattern = Pattern.compile("\\\"([^)]+)\\\"");
  139.                                         matcher = pattern.matcher(packetName);
  140.                                        
  141.                                         if(matcher.find()) {
  142.                                             packetName = matcher.group(1);
  143.                                            
  144.                                             System.out.println("Packet: " + packetName + ": " + value);
  145.                                             console.append("\nPacket: " + packetName + ": " + value);
  146.                                            
  147.                                             System.out.println("Should append to file;");
  148.                                             writer.append(" <Packet id=\"" + packetName + "\" type=\"" + value + "\"/>\n");
  149.                                         }
  150.  
  151.                                     }
  152.                                    
  153.                                 }
  154.                             }
  155.                            
  156.                         }
  157.                         writer.append("</Packets>");
  158.                         writer.close();
  159.                         console.append("\nWrote to file successfully.\nOutput directory:\n" + outputFile.getPath());
  160.                         System.out.println("\nWrote to file successfully.\nOutput directory:\n" + outputFile.getPath());
  161.                     } catch (IOException ex) {
  162.                         console.append("Encountered IOException.");
  163.                         ex.printStackTrace();
  164.                     }
  165.                 } else {
  166.                     console.append("\nERROR: Please select " + (inputFile == null ? (outputFile == null ? "an Input and Output" : "Input") : "an Output") + " file.");
  167.                 }
  168.             }
  169.         });
  170.         instance.getContentPane().add(start);
  171.        
  172.         instance.setVisible(true); // Sets the window visible, JFrames default this to false.
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment