XMLParser.java Source code

Java tutorial

Introduction

Here is the source code for XMLParser.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Danilo
 */
public class XMLParser extends javax.swing.JFrame {
    private final static String CRYPT_KEY = "59A[XG^znsqsq8v{`Xhp3P9G";
    private static int Code = 0;
    private static int Perm = 0;
    private static String xml = "";
    private static String Creator = "";
    private static String line;
    private static String Codes = "";
    private static String BootCodes = "";
    private static String BootInfos = "";
    private static int iCode = 0;
    private static int BootP = 0;
    private static int BootPg = 0;
    private static int BootID = 0;
    private static int BootDelay = 1000;
    private static int BootPgCount;

    /**
     * Creates new form Main
     */
    public XMLParser() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jSpinner1 = new javax.swing.JSpinner();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Transformice XML Parser Plus");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jButton1.setText("START");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextPane1.setEditable(false);
        jScrollPane1.setViewportView(jTextPane1);

        jLabel1.setText("PERM[P]");
        jLabel1.setEnabled(false);

        jLabel2.setText("PAGE:");
        jLabel2.setEnabled(false);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "0", "1", "2", "3", "4", "5", "6",
                "7", "8", "9", "10", "11", "13", "17", "18", "19", "20", "21", "22", "24", "32", "41", "44" }));
        jComboBox1.setEnabled(false);

        jSpinner1.setEnabled(false);

        jCheckBox1.setText("Loop");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jLabel3.setText("CODE:");

        jTextField1.setText("0000000");

        jCheckBox2.setSelected(true);
        jCheckBox2.setText("Delay");
        jCheckBox2.setEnabled(false);
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });

        jCheckBox3.setText("Page");
        jCheckBox3.setEnabled(false);
        jCheckBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox3ActionPerformed(evt);
            }
        });

        jTextArea1.setEditable(false);
        jTextArea1.setColumns(20);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 10)); // NOI18N
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(3);
        jTextArea1.setTabSize(1);
        jTextArea1.setText("Credits:\nDevSaider/Ruslan (Base)\nBronce (Something)");
        jTextArea1.setAutoscrolls(false);
        jTextArea1.setEnabled(false);
        jTextArea1.setFocusable(false);
        jTextArea1.setOpaque(false);
        jTextArea1.setRequestFocusEnabled(false);
        jTextArea1.setVerifyInputWhenFocusTarget(false);
        jScrollPane2.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jCheckBox2).addComponent(jCheckBox3)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 48,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jCheckBox1))
                                .addGap(18, 18, 18)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        Short.MAX_VALUE)
                                                .addComponent(jTextField1).addComponent(jSpinner1)
                                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 105,
                                                        Short.MAX_VALUE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(jScrollPane1));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1).addComponent(jComboBox1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(jSpinner1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(jTextField1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBox3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jCheckBox2).addComponent(jButton1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 37,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                        .addGap(0, 0, 0)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)));

        jLabel3.getAccessibleContext().setAccessibleName("CODE: ");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.jButton1.setText("WORKING");
        this.jButton1.setEnabled(false);
        this.jComboBox1.setEnabled(false);
        this.jTextField1.setEnabled(false);
        this.jCheckBox3.setEnabled(false);
        this.jCheckBox2.setEnabled(false);
        this.jCheckBox1.setEnabled(false);
        this.jLabel1.setEnabled(false);
        this.jLabel2.setEnabled(false);
        this.jLabel3.setEnabled(false);
        this.jSpinner1.setEnabled(false);
        if (!this.jCheckBox1.isSelected()) {
            iCode = Integer.valueOf(this.jTextField1.getText());
            this.Start(1);
        } else {
            BootP = Integer.valueOf((this.jComboBox1.getSelectedItem()).toString());
            BootPg = Integer.valueOf((this.jSpinner1.getValue()).toString());
            if (BootPg < 1 || this.jCheckBox3.isSelected() == false) {
                BootPg = 1;
            }
            this.Start(0);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        if (!this.jCheckBox1.isSelected()) {
            this.jComboBox1.setEnabled(false);
            this.jCheckBox3.setEnabled(false);
            this.jTextField1.setEnabled(true);
            this.jLabel1.setEnabled(false);
            this.jLabel2.setEnabled(false);
            this.jLabel3.setEnabled(true);
            this.jCheckBox2.setEnabled(false);
            this.jSpinner1.setEnabled(false);

        } else {
            this.jComboBox1.setEnabled(true);
            this.jCheckBox3.setEnabled(true);
            this.jTextField1.setEnabled(false);
            this.jLabel1.setEnabled(true);
            this.jLabel2.setEnabled(true);
            this.jLabel3.setEnabled(false);
            this.jCheckBox2.setEnabled(true);
            if (this.jCheckBox3.isSelected()) {
                this.jSpinner1.setEnabled(true);
            }
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
        if (this.jCheckBox2.isSelected()) {
            BootDelay = 1000;
        } else {
            BootDelay = 0;
        }
    }//GEN-LAST:event_jCheckBox2ActionPerformed

    private void jCheckBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox3ActionPerformed
        if (this.jCheckBox3.isSelected()) {
            this.jSpinner1.setEnabled(true);
        } else {
            this.jSpinner1.setEnabled(false);
        }
    }//GEN-LAST:event_jCheckBox3ActionPerformed

    public void Start(int type) {
        new java.util.Timer().schedule(new java.util.TimerTask() {
            @Override
            public void run() {
                StartTools(type);
            }
        }, BootDelay);
    }

    public void StartTools(int type) {
        try {
            if (type == 0) {
                try {
                    URL url = new URL("http://api.micetigri.fr/maps/search/p" + BootP + "?page=" + BootPg
                            + "&sort=id&direction=asc");
                    URLConnection connection = url.openConnection();
                    connection.setRequestProperty("User-Agent",
                            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
                    try (BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()))) {
                        while ((line = bufferedReader.readLine()) != null) {
                            if (line.contains("query: ")) {
                                Object data[] = line.split("\"");
                                Codes = (String) data[1];
                            }
                        }
                        if (!Codes.equals("")) {
                            try {
                                if (!BootCodes.equals("")) {
                                    BootCodes += ";" + Codes;
                                } else {
                                    BootCodes += Codes;
                                }
                                this.jTextPane1.setText(BootCodes);
                                System.out.println(new StringBuilder("[List]: ").append(Codes).toString());
                                if (!this.jCheckBox3.isSelected()) {
                                    BootPg++;
                                    new java.util.Timer().schedule(new java.util.TimerTask() {
                                        @Override
                                        public void run() {
                                            StartTools(0);
                                        }
                                    }, BootDelay);
                                } else {
                                    this.StartTools(2);
                                }
                                BootPgCount++;
                            } catch (Exception error) {
                                System.out.println(new StringBuilder("[Codes]: ").append(error).toString());
                            }
                        } else {
                            System.out.println(new StringBuilder("Total (PG): ").append(BootPgCount).toString());
                            this.StartTools(2);
                        }
                    }
                } catch (Exception error) {
                    System.out.println(new StringBuilder("[0][Exception]: ").append(error).toString());
                }
            } else if (type == 2) {
                try {
                    String[] MapCodes = BootCodes.split(";");
                    String MapCode = "";
                    try {
                        MapCode = MapCodes[BootID];
                    } catch (Exception error) {
                        MapCode = "";
                    }
                    if (StringUtils.isNumeric(MapCode)) {
                        URL url = new URL("http://api.micetigri.fr/maps/xmlnew/" + MapCode);
                        URLConnection connection = url.openConnection();
                        connection.setRequestProperty("User-Agent",
                                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
                        try (BufferedReader bufferedReader = new BufferedReader(
                                new InputStreamReader(connection.getInputStream()))) {
                            while ((line = bufferedReader.readLine()) != null) {
                                if (line.contains("CODE")) {
                                    Object data[] = line.split("\"");
                                    Code = Integer.valueOf((String) data[1]);
                                } else if (line.contains("CREATOR")) {
                                    Object data[] = line.split("\"");
                                    Creator = (String) data[1];
                                } else if (line.contains("XML")) {
                                    Object data[] = line.split("\"");
                                    xml = decrypt((String) data[1], CRYPT_KEY);
                                } else if (line.contains("PERM")) {
                                    Object data[] = line.split("\"");
                                    Perm = Integer.valueOf((String) data[1]);
                                }
                            }
                            if (!xml.equals("") && !Creator.equals("")) {
                                BootInfos += (new StringBuilder("Code: '").append(Code).append("'\nCreator: '")
                                        .append(Creator).append("'\nPerma: '").append(Perm).append("'\nXML: '")
                                        .append(xml).append("'\n\n\n\n").toString());
                                this.jTextPane1.setText(BootInfos);
                                BootID++;
                                new java.util.Timer().schedule(new java.util.TimerTask() {
                                    @Override
                                    public void run() {
                                        StartTools(2);
                                    }
                                }, BootDelay);
                            }
                        }
                    } else {
                        this.jButton1.setText("START");
                        this.jButton1.setEnabled(true);
                        this.jComboBox1.setEnabled(true);
                        this.jCheckBox1.setEnabled(true);
                        this.jCheckBox2.setEnabled(true);
                        this.jLabel1.setEnabled(true);
                        this.jLabel2.setEnabled(true);
                        this.jCheckBox3.setEnabled(true);
                        if (this.jCheckBox3.isSelected()) {
                            this.jSpinner1.setEnabled(true);
                        }
                        System.out.println(new StringBuilder("Total de mapas: ").append(BootID).toString());
                    }
                } catch (Exception error) {
                    System.out.println(new StringBuilder("[1][Exception]: ").append(error).toString());
                }
            } else if (type == 1) {
                try {
                    URL url = new URL("http://api.micetigri.fr/maps/xmlnew/" + iCode);
                    URLConnection connection = url.openConnection();
                    connection.setRequestProperty("User-Agent",
                            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
                    try (BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()))) {
                        while ((line = bufferedReader.readLine()) != null) {
                            if (line.contains("CODE")) {
                                Object data[] = line.split("\"");
                                Code = Integer.valueOf((String) data[1]);
                            } else if (line.contains("CREATOR")) {
                                Object data[] = line.split("\"");
                                Creator = (String) data[1];
                            } else if (line.contains("XML")) {
                                Object data[] = line.split("\"");
                                xml = decrypt((String) data[1], CRYPT_KEY);
                            } else if (line.contains("PERM")) {
                                Object data[] = line.split("\"");
                                Perm = Integer.valueOf((String) data[1]);
                            }
                        }
                        if (!xml.equals("") && !Creator.equals("")) {
                            this.jTextPane1.setText(new StringBuilder("Code: '").append(Code)
                                    .append("'\nCreator: '").append(Creator).append("'\nPerma: '").append(Perm)
                                    .append("'\nXML: '").append(xml).append("'\n").toString());
                        } else {
                            this.jTextPane1.setText("xml not found !");
                        }
                    }
                    this.jButton1.setEnabled(true);
                    this.jLabel3.setEnabled(true);
                    this.jTextField1.setEnabled(true);
                    this.jCheckBox1.setEnabled(true);
                    this.jCheckBox2.setEnabled(false);
                    this.jButton1.setText("START");

                } catch (Exception error) {
                    System.out.println(new StringBuilder("[1][Exception]: ").append(error).toString());
                }
            }
        } catch (Exception error) {
            System.out.println(new StringBuilder("[ALL][IOException]: ").append(error).toString());
        }
    }

    public static String decrypt(String cryptedXML, String key) {
        int c;
        String finalXML = "";
        byte[] str = Base64.getDecoder().decode(cryptedXML);
        for (int i = 0; i < str.length; i++) {
            c = str[i];
            c -= key.charAt((i + 1) % key.length());
            finalXML += (char) (c & 255);
        }
        return finalXML;
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(XMLParser.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(XMLParser.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(XMLParser.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(XMLParser.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new XMLParser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
}