Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.52 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.TextArea;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyEvent;
  9. import java.io.*;
  10. import java.net.*;
  11. import java.util.ArrayList;
  12.  
  13. import javax.swing.BorderFactory;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JMenu;
  18. import javax.swing.JMenuBar;
  19. import javax.swing.JMenuItem;
  20. import javax.swing.JPanel;
  21. import javax.swing.JPopupMenu;
  22. import javax.swing.JSplitPane;
  23. import javax.swing.JTextField;
  24.  
  25. public class Switchfo
  26. {
  27.     static int INFOBOXNUMBER = 3;
  28.     static final Dimension d = new Dimension(600,500);
  29.     static final Dimension d2 = new Dimension(530,50);
  30.     static final Dimension d3 = new Dimension(500,50);
  31.     static final Dimension d4 = new Dimension(500,380);
  32.     static final Font CSMS = new Font(Font.SANS_SERIF, 0, 11);
  33.     static final Color LIGHTG = new Color(238,238,238);
  34.     static final TextArea source = new TextArea("", 0, 0,TextArea.SCROLLBARS_VERTICAL_ONLY);
  35.     static final JFrame frame = new JFrame("Switchfobox");
  36.     static final JTextField input = new JTextField("Aviansie");
  37.     static final JButton MONSTER = new JButton("Monster");
  38.     static final JButton NPC = new JButton("NPC");
  39.     static final JButton BONUSES = new JButton("Bonuses");
  40.     static final JButton[] BUTTONS = new JButton[]{ MONSTER, NPC, BONUSES };
  41.     static final JButton start = new JButton("Go");
  42.     static final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  43.     static final JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  44.  
  45.     public static void main(String[] args)
  46.     {
  47.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.         frame.setSize(d);
  49.         frame.setResizable(false);
  50.         start.setEnabled(true);
  51.         source.setFont(CSMS);
  52.         source.setBackground(LIGHTG);
  53.         input.setSize(d4);
  54.         JPanel inputPanel = new JPanel(new BorderLayout());
  55.         JPanel wrapperPanel = new JPanel(new BorderLayout());
  56.         final JPanel outputPanel = new JPanel(new BorderLayout());
  57.         wrapperPanel.add(input, BorderLayout.CENTER);
  58.         wrapperPanel.add(start, BorderLayout.SOUTH);
  59.         inputPanel.add(wrapperPanel, BorderLayout.NORTH);
  60.         outputPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 0));
  61.         outputPanel.add(source);
  62.         start.setMnemonic(KeyEvent.VK_C);
  63.         enableOtherButtons(MONSTER);
  64.         MONSTER.addActionListener(new ActionListener(){
  65.  
  66.             public void actionPerformed(ActionEvent arg0) {
  67.                 INFOBOXNUMBER = 3;
  68.                 enableOtherButtons(MONSTER);
  69.             }});
  70.        
  71.         NPC.addActionListener(new ActionListener(){
  72.  
  73.             public void actionPerformed(ActionEvent arg0) {
  74.                 enableOtherButtons(NPC);
  75.                 INFOBOXNUMBER = 2;
  76.             }});
  77.        
  78.         BONUSES.addActionListener(new ActionListener(){
  79.  
  80.             public void actionPerformed(ActionEvent arg0) {
  81.                 enableOtherButtons(BONUSES);
  82.                 INFOBOXNUMBER = 1;
  83.             }});
  84.        
  85.         start.addActionListener(new ActionListener(){
  86.  
  87.             public void actionPerformed(ActionEvent arg0) {
  88.                 try {
  89.                     source.setText(runstuff(input.getText()));
  90.                 } catch (IOException e) {
  91.                     source.setText("Error trying [[" + input.getText() + "]]");
  92.                 }
  93.             }});
  94.         splitPane.setTopComponent(inputPanel);
  95.         inputPanel.setPreferredSize(d2);
  96.         inputPanel.setMinimumSize(d2);
  97.         inputPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
  98.         splitPane.setBottomComponent(outputPanel);
  99.         JPanel c = new JPanel();
  100.         inputPanel.add(c);
  101.         JPanel frame2 = new JPanel();
  102.         //frame2.add(start);
  103.         for (JButton b : BUTTONS) {
  104.             frame2.add(b);
  105.         }
  106.         splitPane.setPreferredSize(d3);
  107.         sp.setBottomComponent(splitPane);
  108.         sp.setTopComponent(frame2);
  109.         frame.add(sp);
  110.         frame.setLocation(100,200);
  111.         frame.setVisible(true);
  112.     }
  113.    
  114.     private static void enableOtherButtons(JButton a) {
  115.         for (JButton b : BUTTONS) {
  116.             if (a != b)
  117.                 b.setEnabled(true);
  118.             else
  119.                 b.setEnabled(false);
  120.         }
  121.     }
  122.     public static String runstuff(String page) throws IOException
  123.     {
  124.             String[] paramnames = new String[0];
  125.             String INFOBOX = "";
  126.             switch ( INFOBOXNUMBER ) {
  127.                 case 1 :
  128.                     paramnames = BONUSESPARAMS;
  129.                     INFOBOX = "Infobox bonuses multi";
  130.                     break;
  131.                 case 2 :
  132.                     paramnames = NPCPARAMS;
  133.                     INFOBOX = "Infobox npc multi";
  134.                     break;
  135.                 case 3 :
  136.                     paramnames = MONSTERPARAMS;
  137.                     INFOBOX = "Infobox monster multi";
  138.                     break;
  139.             }
  140.             URL oracle;
  141.             URLConnection yc;
  142.             BufferedReader in;
  143.             String inputLine;
  144.             oracle = new URL("http://runescape.wikia.com/wiki/" + page.replace(" ","_") + "?action=raw");
  145.             yc = oracle.openConnection();
  146.             yc.connect();
  147.             in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
  148.             ArrayList<ArrayList<String>> infoboxes = new ArrayList<ArrayList<String>>();
  149.             ArrayList<String> infobox = null;
  150.             boolean started = false;
  151.             int extrabraces = 0;
  152.             String text = null;
  153.             int textn = 0;
  154.             int infoboxn = 0;
  155.             boolean neverStarted = true;
  156.             while ((inputLine = in.readLine()) != null) { //System.out.println(inputLine);
  157.                 if (inputLine.matches(".*\\{\\{(?:[Tt]emplate:)?[Ss]witch.*")) {
  158.                         started = true;
  159.                 }
  160.                 if (inputLine.matches(".*\\|\\s*text\\d+\\s*=\\s*.*")) {
  161.                     String txtx = inputLine.replaceFirst(".*\\|\\s*text\\d+\\s*=\\s*(.*?)(\\}\\})?","$1");
  162.                     text = txtx;
  163.                     textn++;
  164.                 }
  165.                 if (started) {
  166.                     if (inputLine.matches(".*\\|\\s*item\\d+\\s*=.*")) {
  167.                         infobox = new ArrayList<String>();
  168.                         neverStarted = false;
  169.                         infoboxes.add(infobox);
  170.                         infoboxn++;
  171.                     }
  172.                     if (infobox instanceof ArrayList<?>) {
  173.                         // Just in case
  174.                         inputLine = inputLine.replaceAll("\\{\\{(.|[Ss]kill.*?)\\}\\}", "-&o&-$1-&c&-");
  175.                         infobox.add(inputLine);
  176.                     }
  177.                     if (inputLine.matches(".*\\{\\{.*"))
  178.                         extrabraces++;
  179.                     if (inputLine.matches(".*\\}\\}\\}\\}.*"))
  180.                         extrabraces -= 2;
  181.                     else if (inputLine.matches(".*\\}\\}.*"))
  182.                         extrabraces--;
  183.                     if (extrabraces == 0)
  184.                         started = false;
  185.                     if ( text != null && infobox != null && infoboxn == textn ) {
  186.                         infobox.add("|version="+text);
  187.                         text = null;
  188.                     }
  189.                 }
  190.             }
  191.             in.close();
  192.             if (neverStarted)
  193.                 return "No switchfobox found on [[" + page + "]].";
  194.             ArrayList<ArrayList<String>> params = new ArrayList<ArrayList<String>>();
  195.  
  196.             for (String a : paramnames) {
  197.                 ArrayList<String> b = new ArrayList<String>();
  198.                 b.add(a);
  199.                 params.add(b);
  200.             }
  201.             int infox = 0;
  202.             for (ArrayList<String> a : infoboxes) {
  203.                 infox++;
  204.                 for (String b : a) {
  205.                     int pos = -1;
  206.                     String prm = null;
  207.                     for (int i = 0; i < paramnames.length; i++) {
  208.                         String px = paramnames[i];
  209.                         if (b.matches(".*\\|\\s*"+px+"\\s*=.*")) {
  210.                             pos = i;
  211.                             prm = b.replaceAll(".*?"+px+"\\s*=\\s*(.*?)\\s*", "$1");
  212.                         }
  213.                     }
  214.                     if (pos > -1) {
  215.                         ArrayList<String> inff = params.get(pos);
  216.                         inff.add(prm);
  217.                         if (infox == 1)
  218.                             inff.add(prm);
  219.                     }
  220.                 }
  221.             }
  222.             for (ArrayList<String> a : params) {
  223.                 if (a.get(0).equals("version"))
  224.                     a.set(1,null);
  225.                 else {
  226.                     int dupes = 0;
  227.                     for (int i = 2; i < a.size(); i++) {
  228.                         String px = a.get(i);
  229.                         if (px.equals(a.get(1)))
  230.                             dupes++;
  231.                     }
  232.                     if (dupes < a.size()-2)
  233.                         a.set(1, null);
  234.                     else if (dupes > 1 && dupes == a.size()-2) {
  235.                         for (int i = 2; i < a.size(); i++) {
  236.                                 a.set(i,null);
  237.                             }
  238.                     }
  239.                     for (int i = 2; i < a.size(); i++) {
  240.                         String px = a.get(i);
  241.                         for (int j = i+1; j < a.size(); j++) {
  242.                             String px2 = a.get(j);
  243.                             if (px == null || px2 == null) {
  244.                             }
  245.                             else if (px.equals(px2) && !px2.matches("\\s*\\$\\d+\\s*") && j!=i) {
  246.                                 a.set(j, "$"+(i-1));
  247.                             }
  248.                         }
  249.                     }
  250.                 }
  251.             }
  252.             String ret = "";
  253.             ret += "{{" + INFOBOX;
  254.             for (ArrayList<String> a : params) {
  255.                 String paramname = a.get(0);
  256.                 if (a.size() > 1)
  257.                     ret += "\n";
  258.                 for (int i = 1; i < a.size(); i++) {
  259.                     String paramx = a.get(i);
  260.                     if (paramx != null) {
  261.                         paramx = paramx.replaceAll("\\s*(.*?)\\}\\}$","$1")
  262.                                 .replaceAll("-&o&-", "{{")
  263.                                 .replaceAll("-&c&-", "}}");
  264.                         if (i == 1)
  265.                             ret += "|"+paramname+" = "+paramx;
  266.                         else
  267.                             ret += "|"+paramname+(i-1)+" = "+paramx;
  268.                     }
  269.                 }
  270.             }
  271.             ret += "\n}}";
  272.             return ret;
  273.      }
  274.    
  275.     static final String[] MONSTERPARAMS = {
  276.         "version",
  277.         "name",
  278.         "image",
  279.         "aka",
  280.         "release",
  281.         "update",
  282.         "level",
  283.         "LP",
  284.         "experience",
  285.         "slaylvl",
  286.         "slayxp",
  287.         "assigned",
  288.         "turael",
  289.         "spria",
  290.         "mazchna",
  291.         "achtryn",
  292.         "chaeldar",
  293.         "sumona",
  294.         "vannaka",
  295.         "duradel",
  296.         "lapalok",
  297.         "kuradal",
  298.         "Turael",
  299.         "Spria",
  300.         "Mazchna",
  301.         "Achtryn",
  302.         "Chaeldar",
  303.         "Sumona",
  304.         "Vannaka",
  305.         "Duradel",
  306.         "Lapalok",
  307.         "Kuradal",
  308.         "members",
  309.         "aggressive",
  310.         "poisonous",
  311.         "immune to stun",
  312.         "immune to poison",
  313.         "attack",
  314.         "defence",
  315.         "ranged",
  316.         "magic",
  317.         "attack style",
  318.         "attack speed",
  319.         "max hit",
  320.         "weakness",
  321.         "always drops",
  322.         "examine",
  323.         "abilities"
  324.     };
  325.    
  326.     static final String[] BONUSESPARAMS = {
  327.         "version",
  328.         "requirements",
  329.         "class",
  330.         "slot",
  331.         "degrades",
  332.         "mainDamage",
  333.         "mainAccuracy",
  334.         "offDamage",
  335.         "offAccuracy",
  336.         "style",
  337.         "armour",
  338.         "life",
  339.         "prayer",
  340.         "strength",
  341.         "ranged",
  342.         "magic",
  343.         "aspeed",
  344.         "image",
  345.         "caption",
  346.         "image2",
  347.         "caption2"
  348.     };
  349.    
  350.     static final String[] NPCPARAMS = {
  351.         "version",
  352.         "name",
  353.         "aka",
  354.         "image",
  355.         "release",
  356.         "update",
  357.         "race",
  358.         "members",
  359.         "quest",
  360.         "location",
  361.         "shop",
  362.         "gender",
  363.         "examine",
  364.         "map"
  365.     };
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement