();
103 | String url = HTTPS + SUBMISSIONLIST.replace(":u", user.getHandle());
104 |
105 | try {
106 | //TODO: Add login to fetch the spoj signedlist
107 | Connection.Response loginForm = Jsoup.connect(LOGINURL)
108 | .timeout(10000).method(Connection.Method.GET).execute();
109 |
110 | System.out.println("Spoj username - "+ ((SpojUser)
111 | user).getUsername());
112 | loginForm = Jsoup.connect(LOGINURL).data("next", "/")
113 | .data("login_user", ((SpojUser) user).getUsername())
114 | .data("password", ((SpojUser) user).getPass())
115 | .data("autologin", "1").cookies(loginForm.cookies())
116 | .method(Connection.Method.POST).execute();
117 |
118 |
119 | Connection.Response response = Jsoup.connect(url).timeout(10000)
120 | .cookies(loginForm.cookies()).method(Connection.Method.GET).execute();
121 |
122 | String lines[] = response.body().split("\n");
123 | // ignore first 9 lines 0...1...2......8
124 | if (lines.length < 9) {
125 | System.out.println("Error! Invalid Format of Spoj signedlist");
126 | } else {
127 | int i = 9; // start from 9th line
128 | String end = "\\------------------------------------------------------------------------------/";
129 |
130 | while (i < lines.length && lines[i].compareTo(end) != 0) {
131 | String subEntry[] = lines[i].split("\\|");
132 |
133 | // if solution is AC or status is a score
134 | String status = subEntry[4].trim();
135 | if (status.compareToIgnoreCase("AC") == 0
136 | || status.matches("-?\\d+(\\.\\d+)?")) {
137 | String problem = subEntry[3].trim();
138 |
139 | String sId = subEntry[1].trim();
140 | Problem p = new SpojProblem(problem, "");
141 |
142 | String lang = subEntry[7].trim();
143 | String time = subEntry[2].trim();
144 |
145 | Submission submission = new SpojSubmission(sId, "", p,
146 | user);
147 |
148 | submission.setTimestamp(time);
149 | submission.setLanguage(LanguagesEnum
150 | .findExtension(lang));
151 |
152 | subs.add(submission);
153 | }
154 | i++;
155 | }
156 | }
157 | } catch (Exception e) {
158 | System.out.println("spoj: Error fetching list. " + " -> "
159 | + e.getMessage());
160 | // e.printStackTrace();
161 | }
162 | System.out.println("spoj: fetched List " + subs.size());
163 | return subs;
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/src/com/koldbyte/codebackup/plugins/spoj/core/entities/SpojProblem.java:
--------------------------------------------------------------------------------
1 | package com.koldbyte.codebackup.plugins.spoj.core.entities;
2 |
3 | import java.io.IOException;
4 |
5 | import org.jsoup.Jsoup;
6 | import org.jsoup.nodes.Document;
7 | import org.jsoup.nodes.Element;
8 | import org.jsoup.nodes.Entities.EscapeMode;
9 |
10 | import com.koldbyte.codebackup.core.entities.Problem;
11 |
12 | public class SpojProblem extends Problem {
13 | private final String HTTPS = "https://";
14 | private final String PROBLEMURL = "www.spoj.com/problems/:p/";
15 |
16 | @Override
17 | public String fetchProblemStatement() {
18 | String problem = getProblemId();
19 | String url = HTTPS + PROBLEMURL.replace(":p", problem);
20 | /*-
21 | * Spoj Problem Page looks like this
22 | *
23 | *
24 | * .......
25 | *
26 | */
27 |
28 | Document doc;
29 |
30 | try {
31 | doc = Jsoup.connect(url).timeout(10000).get();
32 |
33 | // remove html entities from the code
34 | doc.outputSettings().escapeMode(EscapeMode.xhtml);
35 |
36 | Element problemBody = doc.getElementById("problem-body");
37 |
38 | System.out.println("spoj: fetched problem " + problemId);
39 |
40 | this.setProblemStatement(problemBody.html());
41 | } catch (IOException e) {
42 | System.err.println("spoj: Error fetching Problem Statement "
43 | + problemId + " -> " + e.getMessage());
44 | // e.printStackTrace();
45 | }
46 |
47 | return this.problemStatement;
48 | }
49 |
50 | @Override
51 | public String getUrl() {
52 | if (url == null || url.isEmpty()) {
53 |
54 | String problemUrl = PROBLEMURL;
55 |
56 | // replace ":p" with the problem id
57 | problemUrl.replace(":p", problemId);
58 |
59 | url = HTTPS + PROBLEMURL;
60 | this.setUrl(url);
61 | }
62 | return url;
63 | }
64 |
65 | @Override
66 | public String getProblemId() {
67 | if (problemId == null || problemId.isEmpty()) {
68 | String u = getUrl();
69 | u = u.replace(HTTPS, "");
70 | u = u.replace("www.spoj.com/problems/", "");
71 | u = u.replace("/", "");
72 | this.setProblemId(u);
73 | }
74 | return this.problemId;
75 | }
76 |
77 | public SpojProblem(String problemId, String url) {
78 | super(problemId, url);
79 | }
80 |
81 | public SpojProblem(String url) {
82 | super(url);
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/src/com/koldbyte/codebackup/plugins/spoj/core/entities/SpojSubmission.java:
--------------------------------------------------------------------------------
1 | package com.koldbyte.codebackup.plugins.spoj.core.entities;
2 |
3 | import java.io.IOException;
4 |
5 | import org.jsoup.Connection;
6 | import org.jsoup.Jsoup;
7 |
8 | import com.koldbyte.codebackup.core.entities.Problem;
9 | import com.koldbyte.codebackup.core.entities.Submission;
10 | import com.koldbyte.codebackup.core.entities.User;
11 |
12 | public class SpojSubmission extends Submission {
13 | private final String HTTPS = "https://";
14 | private final String SUBMITTEDURL = "www.spoj.com/files/src/save/:s/";
15 | private final String LOGINURL = HTTPS + "www.spoj.com/login";
16 |
17 | @Override
18 | public String fetchSubmittedCode() {
19 | /*
20 | * Requires username and password to get the source code so simulate
21 | * login
22 | */
23 |
24 | try {
25 | Connection.Response loginForm = Jsoup.connect(LOGINURL)
26 | .timeout(10000).method(Connection.Method.GET).execute();
27 |
28 | // System.out.println("Spoj username - "+ ((SpojUser)
29 | // user).getUsername());
30 | loginForm = Jsoup.connect(LOGINURL).data("next", "/")
31 | .data("login_user", ((SpojUser) user).getUsername())
32 | .data("password", ((SpojUser) user).getPass())
33 | .data("autologin", "1").cookies(loginForm.cookies())
34 | .method(Connection.Method.POST).execute();
35 |
36 | // login done...now use the cookies to whenever u are fetching code
37 | String url = HTTPS + SUBMITTEDURL.replace(":s", submissionId);
38 | String code = Jsoup.connect(url).ignoreContentType(true)
39 | .cookies(loginForm.cookies()).method(Connection.Method.GET)
40 | .execute().body();
41 |
42 | if (code.isEmpty()) {
43 | System.err.println("Error! Invalid Spoj Credentials");
44 | } else {
45 | System.out.println("spoj: fetched code " + submissionId);
46 | setCode(code);
47 | }
48 | } catch (IOException e) {
49 | System.err.println("spoj: Error Fetching code" + submissionId
50 | + " -> " + e.getMessage());
51 | // e.printStackTrace();
52 | }
53 |
54 | return code;
55 | }
56 |
57 | @Override
58 | public String getSubmissionIdFromUrl() {
59 | String url = getSubmissionUrl();
60 | url = url.replace(HTTPS, "");
61 | url = url.replace("www.spoj.com/files/src/save/", "");
62 | url = url.replace("/", "");
63 | return url;
64 | }
65 |
66 | @Override
67 | public String getSubmissionUrlFromId() {
68 | String subId = HTTPS + SUBMITTEDURL.replace(":s", getSubmissionId());
69 | return subId;
70 | }
71 |
72 | public SpojSubmission(String submissionId, String submissionUrl,
73 | Problem problem, User user) {
74 | super(submissionId, submissionUrl, problem, user);
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/src/com/koldbyte/codebackup/plugins/spoj/core/entities/SpojUser.java:
--------------------------------------------------------------------------------
1 | package com.koldbyte.codebackup.plugins.spoj.core.entities;
2 |
3 | import com.koldbyte.codebackup.core.entities.User;
4 |
5 | public class SpojUser extends User {
6 | private final String HTTPS = "https://";
7 | private final String PROFILEURL = "www.spoj.com/users/:u/";
8 |
9 | private String username;
10 | private String pass;
11 |
12 | public String getUsername() {
13 | return username;
14 | }
15 |
16 | public String getPass() {
17 | return pass;
18 | }
19 |
20 | public void setUsername(String username) {
21 | this.username = username;
22 | }
23 |
24 | public void setPass(String pass) {
25 | this.pass = pass;
26 | }
27 |
28 | public SpojUser(String handle) {
29 | super(handle);
30 | }
31 |
32 | public SpojUser(String handle, String profileUrl) {
33 | super(handle, profileUrl);
34 | }
35 |
36 | @Override
37 | public String getHandleFromProfileUrl() {
38 | String handle = profileUrl;
39 | handle = handle.replace(HTTPS, "");
40 | handle = handle.replace("www.spoj.com/users/", "");
41 | handle = handle.replace("/", "");
42 | return handle;
43 | }
44 |
45 | @Override
46 | public String getProfileUrlFromHandle() {
47 | return HTTPS + PROFILEURL.replace(":u", this.handle);
48 | }
49 |
50 | @Override
51 | public Boolean isValidUser() {
52 | return true;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/com/koldbyte/codebackup/utils/HTTPRequest.java:
--------------------------------------------------------------------------------
1 | package com.koldbyte.codebackup.utils;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.DataOutputStream;
5 | import java.io.InputStreamReader;
6 | import java.net.HttpURLConnection;
7 | import java.net.URL;
8 |
9 | public class HTTPRequest {
10 | private String userAgent = "Mozilla/5.0";
11 | private String url = "http://www.google.com/search?q=mkyong";
12 | private String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
13 | private int connectTimeout = 10000;
14 | private int socketTimeout = 10000;
15 |
16 | public HTTPRequest(String userAgent, String url, String urlParameters) {
17 | super();
18 | this.userAgent = userAgent;
19 | this.url = url;
20 | this.urlParameters = urlParameters;
21 | }
22 |
23 | public HTTPRequest(String url, String urlParameters) {
24 | super();
25 | this.url = url;
26 | this.urlParameters = urlParameters;
27 | }
28 |
29 | public String getUserAgent() {
30 | return userAgent;
31 | }
32 |
33 | public void setUserAgent(String userAgent) {
34 | this.userAgent = userAgent;
35 | }
36 |
37 | public String getUrl() {
38 | return url;
39 | }
40 |
41 | public void setUrl(String url) {
42 | this.url = url;
43 | }
44 |
45 | public String getUrlParameters() {
46 | return urlParameters;
47 | }
48 |
49 | public void setUrlParameters(String urlParameters) {
50 | this.urlParameters = urlParameters;
51 | }
52 |
53 | // HTTP GET request
54 | public StringBuffer sendGet() throws Exception {
55 | URL obj = new URL(url);
56 | HttpURLConnection con = (HttpURLConnection) obj.openConnection();
57 |
58 | // set Timeouts
59 | con.setConnectTimeout(connectTimeout);
60 | con.setReadTimeout(socketTimeout);
61 |
62 | // optional default is GET
63 | con.setRequestMethod("GET");
64 |
65 | // add request header
66 | con.setRequestProperty("User-Agent", userAgent);
67 |
68 | // int responseCode = con.getResponseCode();
69 | // System.out.println("\nSending 'GET' request to URL : " + url);
70 | // System.out.println("Response Code : " + responseCode);
71 |
72 | BufferedReader in = new BufferedReader(new InputStreamReader(
73 | con.getInputStream()));
74 | String inputLine;
75 | StringBuffer response = new StringBuffer();
76 |
77 | while ((inputLine = in.readLine()) != null) {
78 | response.append(inputLine);
79 | }
80 | in.close();
81 |
82 | // print result
83 | // System.out.println(response.toString());
84 |
85 | return response;
86 | }
87 |
88 | // HTTP POST request
89 | public StringBuffer sendPost() throws Exception {
90 |
91 | URL obj = new URL(url);
92 | HttpURLConnection con = (HttpURLConnection) obj.openConnection();
93 |
94 | // set Timeouts
95 | con.setConnectTimeout(connectTimeout);
96 | con.setReadTimeout(socketTimeout);
97 |
98 | // add request header
99 | con.setRequestMethod("POST");
100 | con.setRequestProperty("User-Agent", userAgent);
101 | con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
102 |
103 | // Send post request
104 | con.setDoOutput(true);
105 | DataOutputStream wr = new DataOutputStream(con.getOutputStream());
106 | wr.writeBytes(urlParameters);
107 | wr.flush();
108 | wr.close();
109 |
110 | // int responseCode = con.getResponseCode();
111 | // System.out.println("\nSending 'POST' request to URL : " + url);
112 | // System.out.println("Post parameters : " + urlParameters);
113 | // System.out.println("Response Code : " + responseCode);
114 |
115 | BufferedReader in = new BufferedReader(new InputStreamReader(
116 | con.getInputStream()));
117 | String inputLine;
118 | StringBuffer response = new StringBuffer();
119 |
120 | while ((inputLine = in.readLine()) != null) {
121 | response.append(inputLine);
122 | }
123 | in.close();
124 |
125 |
126 | return response;
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/src/com/koldbyte/codebackup/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | package com.koldbyte.codebackup.utils;
2 |
3 | import org.jsoup.Jsoup;
4 | import org.jsoup.nodes.Document;
5 | import org.jsoup.safety.Whitelist;
6 |
7 | public class StringUtils {
8 | public static StringBuffer explode(StringBuffer content, String pre,
9 | String post) {
10 | String result = content.substring(content.indexOf(pre) + 1,
11 | content.indexOf(post));
12 | return new StringBuffer(result);
13 | }
14 |
15 | public static String br2nl(String html) {
16 | if (html == null)
17 | return html;
18 | Document document = Jsoup.parse(html);
19 | document.outputSettings(new Document.OutputSettings()
20 | .prettyPrint(false));// makes html() preserve linebreaks and
21 | // spacing
22 | document.select("br").append("\\n");
23 | document.select("p").prepend("\\n\\n");
24 | String s = document.html().replaceAll("\\\\n", "\n");
25 | return Jsoup.clean(s, "", Whitelist.none(),
26 | new Document.OutputSettings().prettyPrint(false));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/org/eclipse/wb/swing/FocusTraversalOnArray.java:
--------------------------------------------------------------------------------
1 | /*******************************************************************************
2 | * Copyright (c) 2011 Google, Inc.
3 | * All rights reserved. This program and the accompanying materials
4 | * are made available under the terms of the Eclipse Public License v1.0
5 | * which accompanies this distribution, and is available at
6 | * http://www.eclipse.org/legal/epl-v10.html
7 | *
8 | * Contributors:
9 | * Google, Inc. - initial API and implementation
10 | *******************************************************************************/
11 | package org.eclipse.wb.swing;
12 |
13 | import java.awt.Component;
14 | import java.awt.Container;
15 | import java.awt.FocusTraversalPolicy;
16 |
17 | /**
18 | * Cyclic focus traversal policy based on array of components.
19 | *
20 | * This class may be freely distributed as part of any application or plugin.
21 | *
22 | * @author scheglov_ke
23 | */
24 | public class FocusTraversalOnArray extends FocusTraversalPolicy {
25 | private final Component m_Components[];
26 | ////////////////////////////////////////////////////////////////////////////
27 | //
28 | // Constructor
29 | //
30 | ////////////////////////////////////////////////////////////////////////////
31 | public FocusTraversalOnArray(Component components[]) {
32 | m_Components = components;
33 | }
34 | ////////////////////////////////////////////////////////////////////////////
35 | //
36 | // Utilities
37 | //
38 | ////////////////////////////////////////////////////////////////////////////
39 | private int indexCycle(int index, int delta) {
40 | int size = m_Components.length;
41 | int next = (index + delta + size) % size;
42 | return next;
43 | }
44 | private Component cycle(Component currentComponent, int delta) {
45 | int index = -1;
46 | loop : for (int i = 0; i < m_Components.length; i++) {
47 | Component component = m_Components[i];
48 | for (Component c = currentComponent; c != null; c = c.getParent()) {
49 | if (component == c) {
50 | index = i;
51 | break loop;
52 | }
53 | }
54 | }
55 | // try to find enabled component in "delta" direction
56 | int initialIndex = index;
57 | while (true) {
58 | int newIndex = indexCycle(index, delta);
59 | if (newIndex == initialIndex) {
60 | break;
61 | }
62 | index = newIndex;
63 | //
64 | Component component = m_Components[newIndex];
65 | if (component.isEnabled() && component.isVisible() && component.isFocusable()) {
66 | return component;
67 | }
68 | }
69 | // not found
70 | return currentComponent;
71 | }
72 | ////////////////////////////////////////////////////////////////////////////
73 | //
74 | // FocusTraversalPolicy
75 | //
76 | ////////////////////////////////////////////////////////////////////////////
77 | public Component getComponentAfter(Container container, Component component) {
78 | return cycle(component, 1);
79 | }
80 | public Component getComponentBefore(Container container, Component component) {
81 | return cycle(component, -1);
82 | }
83 | public Component getFirstComponent(Container container) {
84 | return m_Components[0];
85 | }
86 | public Component getLastComponent(Container container) {
87 | return m_Components[m_Components.length - 1];
88 | }
89 | public Component getDefaultComponent(Container container) {
90 | return getFirstComponent(container);
91 | }
92 | }
--------------------------------------------------------------------------------