Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class Main {
    public static String[][] getSpreadSheet(String docId, String tab) {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                @Override
                public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
                }

                @Override
                public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            } };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            //sslSocketFactory.

            // All set up, we can get a resource through https now:
            final URLConnection urlCon = new URL(
                    "https://docs.google.com/spreadsheets/d/" + docId + "/export?format=csv&gid=" + tab)
                            .openConnection();
            // Tell the url connection object to use our socket factory which bypasses security checks
            ((HttpsURLConnection) urlCon).setSSLSocketFactory(sslSocketFactory);

            final InputStream input = urlCon.getInputStream();
            BufferedReader r = new BufferedReader(new InputStreamReader(input));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
                total.append("\n");
            }

            String theString = total.toString();

            String[][] out = null;

            String rows[] = theString.split("\n");
            out = new String[rows.length][];
            for (int i = 0; i < out.length; i++) {
                String columns[] = rows[i].split(",");
                out[i] = new String[columns.length];
                int corrected = 0;
                for (int j = 0; j < columns.length; j++) {
                    if (columns[j].length() > 0 && columns[j].charAt(0) == '"') {
                        out[i][j - corrected] = (columns[j] + ", " + columns[j + 1]).replace("\"", "");
                        j++;
                        corrected += 1;
                    } else {
                        out[i][j - corrected] = columns[j];
                    }
                }
            }
            return out;

        } catch (final Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}