From 6f05f7f26eedef4af2fb3b6a370f6348b12090b8 Mon Sep 17 00:00:00 2001 From: Dominique Blouin <dominique.blouin@telecom-paristech.fr> Date: Tue, 17 Mar 2015 22:06:23 +0000 Subject: [PATCH] add /src/myutil/CheckConnection.java for checking network connection; add popups to notify errors in SearhBox --- TURTLE.iml | 15 ++ bin/config.xml | 2 +- executablecode/executablecode.iml | 11 ++ src/myutil/CheckConnection.java | 191 +++++++++++++++++++++ src/myutil/GoogleSearch.java | 42 ++++- src/ui/window/JDialogSearchBox.java | 250 +++++++++++++++++++--------- 6 files changed, 420 insertions(+), 91 deletions(-) create mode 100644 TURTLE.iml create mode 100644 executablecode/executablecode.iml create mode 100644 src/myutil/CheckConnection.java diff --git a/TURTLE.iml b/TURTLE.iml new file mode 100644 index 0000000000..365e1a2f62 --- /dev/null +++ b/TURTLE.iml @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <output url="file://$MODULE_DIR$/bin" /> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/javacode" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/oldsrc" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="library" name="bin" level="project" /> + </component> +</module> \ No newline at end of file diff --git a/bin/config.xml b/bin/config.xml index a6e5f80655..2636e27e85 100755 --- a/bin/config.xml +++ b/bin/config.xml @@ -60,7 +60,7 @@ <ExternalCommand2Host data="localhost"/> <ExternalCommand2 data="/packages/uppaal(4.0.11/uppaal /home/enrici/TURTLE/uppaal/spec.xml"/> -<LastOpenFile data="/home/hrua/Downloads/stuxnet_6_12_split.xml"/> +<LastOpenFile data="/home/hrua/data/[1] Eurecom/Semester Project/stuxnet.xml"/> diff --git a/executablecode/executablecode.iml b/executablecode/executablecode.iml new file mode 100644 index 0000000000..6ec1bae923 --- /dev/null +++ b/executablecode/executablecode.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/example" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/src/myutil/CheckConnection.java b/src/myutil/CheckConnection.java new file mode 100644 index 0000000000..322c87c7eb --- /dev/null +++ b/src/myutil/CheckConnection.java @@ -0,0 +1,191 @@ +package myutil; +import java.io.IOException; +import java.net.*; +import java.lang.Runtime; + +/**Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille + + ludovic.apvrille AT enst.fr + + This software is a computer program whose purpose is to allow the + edition of TURTLE analysis, design and deployment diagrams, to + allow the generation of RT-LOTOS or Java code from this diagram, + and at last to allow the analysis of formal validation traces + obtained from external tools, e.g. RTL from LAAS-CNRS and CADP + from INRIA Rhone-Alpes. + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited + liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + /** + * CheckConnection + * check internet connection or remote machine + * Creation: 17/03/2015 + * @version 1.0 17/03/2015 + * @author Huy TRUONG + * @see + */ + + + +public final class CheckConnection { + // TODO: input: URL or IPADRESS in STRING FORMAT. //should return true or false + public static final int KO_RESOLVE_DOMAIN = 1; + public static final int OK_DEFAULT_URL = 2; + + private static final String default_url_1 = "google.com"; + private static final String default_url_2 = "http://ttool.telecom-paristech.fr"; + + + public static final boolean checkInternetConnection() { + try { + if (checkConnectionWithAddr(default_url_1) && checkConnectionWithURL(default_url_2)) { + return Boolean.TRUE; + }else{ + return Boolean.FALSE; + } + } catch (IOException e) { + return Boolean.FALSE; + } catch (InterruptedException e) { + return Boolean.FALSE; + } + } + + public static final boolean checkConnectionWithAddr(String addr) throws IOException, InterruptedException { + Process ping = Runtime.getRuntime().exec("ping -c 1 " + addr); + int returnVal = ping.waitFor(); + boolean reachable = (returnVal == 0); + return reachable; + } + + public static final boolean checkConnectionWithURL(String _url){ + Boolean isConnect= false; + URL url; + try { + url = new URL(_url); + URLConnection conn = url.openConnection(); + conn.connect(); + isConnect= true; + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return isConnect; + } + + + + + + + /*public static final int KO_RESOLVE_DOMAIN = 2; + public static final int OK_DEFAULT_URL = 1; + + private static final String CHECK_DEFAULT_URL = "www.google.com"; + private static final String CHECK_DEAFULT_IP = "8.8.8.8"; + + + + + public void + + *//** + * Check connection to specific ip address + * @return + *//* + public boolean canConnectToAddr(){ + InetAddress addr = new InetAddress() + return Boolean.TRUE; + } + + *//** + * Check connection to default ipaddr : 8.8.8.8 + * @return + *//* + private boolean canConnectToDefaultAddr(){ + return Boolean.TRUE; + } + + *//** + * Check connection to default URL: www.google.com + * @return + *//* + private boolean canConnectToDefaultURL(){ + Boolean isConnect = Boolean.FALSE; + URL url; + try { + url = new URL(CHECK_DEFAULT_URL); + URLConnection conn = url.openConnection(); + conn.connect(); + isConnect= true; + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return isConnect; + } + + *//** + * check the connection to specific URL + * @param u: string of url + * @return: TRUE: be able to connect to URL. + * @return: FALSE: not be able to connect to URL. + *//* + public boolean canConnectToURL(String u){ + Boolean isConnect = Boolean.FALSE; + URL url; + try { + url = new URL(u); + URLConnection conn = url.openConnection(); + conn.connect(); + isConnect= true; + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return isConnect; + } + + *//** + * Check the Internet connection by try to connect to URL "www.google.com" and ip address 8.8.8.8 + * + * @return: OK_DEFAULT_URL = be able to connect to www.google.com + * @return: KO_RESOLVE_DOMAIN = be able to connect to ip 8.8.8.8, but can resolve www.google.com + *//* + public int isInternetConnection(){ + if (canConnectToDefaultURL()) + return OK_DEFAULT_URL; + if (!canConnectToDefaultURL()){ + if (canConnectToDefaultAddr()) + return KO_RESOLVE_DOMAIN; + } + return 0; + }*/ +} diff --git a/src/myutil/GoogleSearch.java b/src/myutil/GoogleSearch.java index b315f3070c..d3de1f97cd 100644 --- a/src/myutil/GoogleSearch.java +++ b/src/myutil/GoogleSearch.java @@ -66,15 +66,17 @@ public class GoogleSearch { public static final String google = "http://www.google.com/search?hl=en&q="; public static final String googleScholar="http://scholar.google.com/scholar?ht=en&q="; + public static final String ENCODING_ERROR = "encodeding_error"; + public static final String IOEx = "IOExeption"; - public static final ArrayList<GoogleSearch> getGoogleResult(String search) throws UnsupportedEncodingException, IOException{ + public static final ArrayList<GoogleSearch> getGoogleResult(String search) { ArrayList<GoogleSearch> r = new ArrayList<GoogleSearch>(); String title="" ; String url ="" ; String desc=""; GoogleSearch gs; - + try{ Document doc = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get(); //System.out.println(doc.toString()); @@ -128,22 +130,35 @@ public class GoogleSearch { } } return r; + }catch (NullPointerException e) { + return null; + }catch (UnsupportedEncodingException e){ + gs = new GoogleSearch(); + gs.setTitle(ENCODING_ERROR); + r.add(gs); + return r; + }catch (IOException e){ + gs = new GoogleSearch(); + gs.setTitle(IOEx); + r.add(gs); + return r; + } } - public static final ArrayList<GoogleSearch> getGoogleScholarResult(String search) throws UnsupportedEncodingException, IOException{ - try{ + public static final ArrayList<GoogleSearch> getGoogleScholarResult(String search){ + ArrayList<GoogleSearch> r = new ArrayList<GoogleSearch>(); String title=""; String url=""; String desc=""; String authors=""; String citedNumber=""; - String citedLinks=""; - String related=""; + String citedLinks=""; + String related=""; - GoogleSearch gs; - + GoogleSearch gs; + try{ //SSystem.out.println("call this"); Document doc = Jsoup.connect(googleScholar + URLEncoder.encode(search, charset)).userAgent(userAgent).get(); @@ -227,6 +242,16 @@ public class GoogleSearch { return r; }catch (NullPointerException e) { return null; + }catch (UnsupportedEncodingException e){ + gs = new GoogleSearch(); + gs.setTitle(ENCODING_ERROR); + r.add(gs); + return r; + }catch (IOException e){ + gs = new GoogleSearch(); + gs.setTitle(IOEx); + r.add(gs); + return r; } } @@ -244,6 +269,7 @@ public class GoogleSearch { public GoogleSearch(){ } + public String getTitle() { return title; } diff --git a/src/ui/window/JDialogSearchBox.java b/src/ui/window/JDialogSearchBox.java index 6cddbe353b..d38d6df5f5 100644 --- a/src/ui/window/JDialogSearchBox.java +++ b/src/ui/window/JDialogSearchBox.java @@ -57,10 +57,7 @@ import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; -import javax.swing.JFrame; -import javax.swing.JButton; -import javax.swing.JPanel; -import javax.swing.JTable; +import javax.swing.text.*; import myutil.GoogleSearch; import myutil.GraphicLib; @@ -69,29 +66,26 @@ import java.lang.Object; import java.awt.Desktop; import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; -import myutil.GraphicLib; -import javax.swing.table.AbstractTableModel; -import javax.swing.*; //import javax.swing.event.*; import java.util.*; import myutil.TableSorter; - -import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; -import javax.swing.table.TableCellRenderer; -import javax.swing.table.DefaultTableCellRenderer; +import myutil.CheckConnection; +//TODO : change display to tab. +//TODO: decorate the text box +//TODO : click on Search does not change status immediately public class JDialogSearchBox extends javax.swing.JFrame { - + public static final String bold= "bold"; + public static final String normal = "normal"; private javax.swing.JList ListKeywords; private javax.swing.JCheckBox databaseCb; - private javax.swing.JTextArea detailText; + private javax.swing.JTextPane detailText; private javax.swing.JCheckBox googleCb; private javax.swing.JCheckBox googleScholarCb; private javax.swing.JScrollPane jScrollPane1; @@ -116,6 +110,7 @@ public class JDialogSearchBox extends javax.swing.JFrame { int searchGoogleScholar; ArrayList<GoogleSearch> resultDatabase; int searchDatabase; + private ArrayList<Object[]> rows; /** Creates new form */ @@ -147,7 +142,8 @@ public class JDialogSearchBox extends javax.swing.JFrame { googleScholarCb = new javax.swing.JCheckBox(); databaseCb = new javax.swing.JCheckBox(); jScrollPane3 = new javax.swing.JScrollPane(); - detailText = new javax.swing.JTextArea(); + StyledDocument doc = new DefaultStyledDocument(); + detailText = new javax.swing.JTextPane(doc); jScrollPane4 = new javax.swing.JScrollPane(); resultTable = new javax.swing.JTable(); listModel = new DefaultListModel(); @@ -156,6 +152,8 @@ public class JDialogSearchBox extends javax.swing.JFrame { jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE ); searchGoogle=0; @@ -180,9 +178,15 @@ public class JDialogSearchBox extends javax.swing.JFrame { databaseCb.setText("Database"); - detailText.setColumns(20); - detailText.setLineWrap(true); - detailText.setRows(5); + detailText.setBounds(0,0,20,5); + detailText.setEditable(false); + + + + + //detailText.setColumns(20); + //detailText.setLineWrap(true); + //detailText.setRows(5); jScrollPane3.setViewportView(detailText); resultTable.setModel(new javax.swing.table.DefaultTableModel( @@ -289,12 +293,9 @@ public class JDialogSearchBox extends javax.swing.JFrame { searchBt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { - try { - searchBtActionPerformed(evt); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + jLabel5.setText("The crawler is running ...."); + searchBtActionPerformed(evt); + } }); @@ -319,7 +320,6 @@ public class JDialogSearchBox extends javax.swing.JFrame { resultTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { - // TODO Auto-generated method stub selectrow(e); } }); @@ -335,10 +335,10 @@ public class JDialogSearchBox extends javax.swing.JFrame { try { d.browse(uri); } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); + JOptionPane.showMessageDialog(null, "URL is invalid", "Warning", + JOptionPane.WARNING_MESSAGE); } - + } //see below @@ -346,14 +346,12 @@ public class JDialogSearchBox extends javax.swing.JFrame { }); pack(); }// - - protected void open(URI uri) { - // TODO Auto-generated method stub - - } - private void removeBtActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: + + + private void removeBtActionPerformed(java.awt.event.ActionEvent evt) { + this.jLabel5.setText("Ready"); + this.jLabel5.updateUI(); int index = this.ListKeywords.getSelectedIndex(); listModel.remove(index); @@ -369,7 +367,7 @@ public class JDialogSearchBox extends javax.swing.JFrame { int size = listModel.getSize(); - if (size == 0) { //Nobody's left, disable firing. + if (size == 0) { this.removeBt.setEnabled(false); } else { //Select an index. @@ -383,46 +381,122 @@ public class JDialogSearchBox extends javax.swing.JFrame { } } - private void searchBtActionPerformed(java.awt.event.ActionEvent evt) throws UnsupportedEncodingException, IOException { - // TODO add your handling code here: - this.jLabel5.setText("The crawler is running ...."); - ArrayList<GoogleSearch> resultGoogle=null; - ArrayList<GoogleSearch> resultGoogleScholar=null; - int id; - rows=new ArrayList<Object[]>(); - String query = this.searchBox.getText(); - if (query != ""){ - if (this.searchGoogle==1) - resultGoogle = GoogleSearch.getGoogleResult(this.searchBox.getText()); - if (this.searchGoogleScholar==1) - resultGoogleScholar= GoogleSearch.getGoogleScholarResult(this.searchBox.getText()); - } - DefaultTableModel model = (DefaultTableModel) this.resultTable.getModel(); - model.setRowCount(0); - this.detailText.setText(""); - if (resultGoogleScholar!=null) - putGoogleScholarToTable(resultGoogleScholar); - if (resultGoogle!=null) - putGoogleToTable(resultGoogle); - for (Object[] o :rows) - { - id = (int) (o[0]); - GoogleSearch gs = (GoogleSearch)(o[1]); - String source = (String)(o[2]); - model.addRow(new Object[]{id,gs.getTitle(),gs.getAuthors(),gs.getUrl(),source}); - } - this.jLabel5.setText("Finished"); - } + private void searchBtActionPerformed(java.awt.event.ActionEvent evt) { + + //reset content of table + DefaultTableModel model = (DefaultTableModel) this.resultTable.getModel(); + model.setRowCount(0); + this.detailText.setText(""); + this.detailText.updateUI(); + + // ensure there is at least the resources for crawling. + if (this.searchGoogle == 0 && this.searchGoogleScholar == 0 && this.searchDatabase == 0) { + JOptionPane.showMessageDialog(null, "Please select the resource to search","Warning", + JOptionPane.WARNING_MESSAGE); + } + + int id; + rows = new ArrayList<Object[]>(); + + // check internet connection before crawling from google or google scholar + Boolean internetConnectionAvailable = null; + if (this.searchGoogle == 1 || this.searchGoogleScholar == 1) { + internetConnectionAvailable = CheckConnection.checkInternetConnection(); + if (internetConnectionAvailable) { + + ArrayList<GoogleSearch> resultGoogle = null; + ArrayList<GoogleSearch> resultGoogleScholar = null; + + + //get the content of searhBox + String query = this.searchBox.getText(); + if (query != "") { + if (this.searchGoogle == 1) { + jLabel5.setText("Retrieving data from Google"); + jLabel5.updateUI(); + resultGoogle = GoogleSearch.getGoogleResult(this.searchBox.getText()); + jLabel5.setText("Done"); + jLabel5.updateUI(); + if (resultGoogle == null) { + JOptionPane.showMessageDialog(null, "Can't get the result from Google\n" + , "Retrieving data is failed", + JOptionPane.ERROR_MESSAGE); + }else if (resultGoogle != null) { + + if(resultGoogle.get(0).getTitle() == GoogleSearch.IOEx) { + JOptionPane.showMessageDialog(null, "Can't connect to Google\n " + + "Please check the internet connection","Connection Error", + JOptionPane.ERROR_MESSAGE); + } else { + putGoogleToTable(resultGoogle); + } + } + } + + if (this.searchGoogleScholar == 1) { + jLabel5.setText("Retrieving data from Google Scholar"); + jLabel5.updateUI(); + resultGoogleScholar = GoogleSearch.getGoogleScholarResult(this.searchBox.getText()); + jLabel5.setText("Done"); + jLabel5.updateUI(); + if (resultGoogleScholar == null) { + JOptionPane.showMessageDialog(null, "Can't get the result from Google Scholar \n" + , "Retrieving data is failed", + JOptionPane.ERROR_MESSAGE); + }else if (resultGoogleScholar != null) { + if (resultGoogleScholar.get(0).getTitle() == GoogleSearch.IOEx) { + JOptionPane.showMessageDialog(null, "Can't connect to Google Scholar\n " + + "Please check the internet connection","Connection Error", + JOptionPane.ERROR_MESSAGE); + } else { + putGoogleScholarToTable(resultGoogleScholar); + } + } + } + } + + + + + }else{ + JOptionPane.showMessageDialog(null, "No internet connection", + "Connection Error", + JOptionPane.ERROR_MESSAGE); + } + } + + + //TODO : cralwer data from DB, must check the connection. + if (this.searchDatabase ==1) { + this.jLabel5.setText("Retrieving data from DB"); + this.repaint(); + for (long i = 1; i < 100000000; i++) ; + } + + + + + //Show table + for (Object[] o : rows) { + id = (Integer) (o[0]); + GoogleSearch gs = (GoogleSearch) (o[1]); + String source = (String) (o[2]); + model.addRow(new Object[]{id, gs.getTitle(), gs.getAuthors(), gs.getUrl(), source}); + } + this.jLabel5.setText("Finished"); + this.jLabel5.updateUI(); + + } - private void selectrow(ListSelectionEvent evt) { - // TODO add your handling code here: + private void selectrow(ListSelectionEvent evt) { + DefaultTableModel model = (DefaultTableModel) this.resultTable.getModel(); int rowindex = resultTable.getSelectedRow(); int id =0; if(rowindex >=0) - id= (int) resultTable.getValueAt(rowindex, 0); + id= (Integer) resultTable.getValueAt(rowindex, 0); GoogleSearch selected=null; for (Object[] o : this.rows){ if (o[0].equals(id)){ @@ -433,22 +507,25 @@ public class JDialogSearchBox extends javax.swing.JFrame { presentDataInDetail(selected); } - private void googleCbActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: + private void googleCbActionPerformed(java.awt.event.ActionEvent evt) { + this.jLabel5.setText("Ready"); + this.jLabel5.updateUI(); if (this.googleCb.isSelected()) this.searchGoogle=1; else this.searchGoogle=0; } - private void googleScholarCbActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: + private void googleScholarCbActionPerformed(java.awt.event.ActionEvent evt) { + this.jLabel5.setText("Ready"); + this.jLabel5.updateUI(); if (this.googleScholarCb.isSelected()) this.searchGoogleScholar=1; else this.searchGoogleScholar=0; } - private void databaseCbActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: + private void databaseCbActionPerformed(java.awt.event.ActionEvent evt) { + this.jLabel5.setText("Ready"); + this.jLabel5.updateUI(); if (this.databaseCb.isSelected()) this.searchDatabase=1; else this.searchDatabase=0; @@ -495,17 +572,26 @@ public class JDialogSearchBox extends javax.swing.JFrame { } public void presentDataInDetail(GoogleSearch gs){ - if (gs != null) + + if (gs != null) { - String detail = gs.getTitle()+"\n\n"; - detail = detail + gs.getAuthors()+"\n"; - detail = detail + gs.getUrl()+"\n"; - detail = detail + gs.getCitedNumber()+"\n\n\n"; - - detail = detail + gs.getDesc()+"\n"; + + String detail = ""; + if (gs.getTitle()!=null) + detail = detail+ gs.getTitle()+"\n"; + if (gs.getAuthors()!=null) + detail = detail + gs.getAuthors()+"\n"; + if (gs.getUrl()!=null) + detail = detail + gs.getUrl()+"\n"; + if (gs.getCitedNumber()!=null) + detail = detail + gs.getCitedNumber()+"\n"; + if (gs.getDesc()!=null) + detail = detail + gs.getDesc()+"\n"; this.detailText.setText(detail); } + + } -- GitLab