Étape 2: Code
Je ne pouvais pas joindre le code parce que la mise en forme serait gâcher.Alors, je l’ai ai copié ci-dessous. Les explications sont ci-dessous.
App.Java :
package org.freesideatlanta.msds ;
importation java.util.ArrayList ;
/**
*
* Praznav
*/
public class App {}
Public Shared Sub main (String [] args) {}
try {}
String filename = args [0] ;
ChemicalReader reader = new ChemicalReader(filename) ;
Produits chimiques ArrayList < String > = reader.getChemicalNames() ;
Catalog MsdsCatalog = new MsdsCatalog() ;
MsdsWriter writer = new MsdsWriter() ;
pour (produit chimique String : substances chimiques) {}
Fiche signalétique FS = catalog.query(chemical) ;
String text = msds.getText() ;
Writer.Write (industrie chimique, texte) ;
}
Writer.Close() ;
} catch (ArrayIndexOutOfBoundsException e) {}
System.out.println ("Usage: App [filename]") ;
System.out.println(e.getMessage()) ;
}
}
}
Reader.java chimique :
package org.freesideatlanta.msds ;
importation java.util.ArrayList ;
import java.IO ;
public class ChemicalReader {}
Nom de la chaîne ;
ArrayList < String > chemicalList = new ArrayList < String > () ; Liste de tous les produits chimiques. Numéro de ligne est l’index + 1
Ligne de chaîne ; Chaîne qui contient la ligne courante du fichier
BufferedReader bufRead ;
public ChemicalReader (String filename) {}
nom = nom de fichier ;
}
public ArrayList < String > getChemicalNames() {}
try {}
FileReader input = new FileReader(name) ;
bufRead = new BufferedReader(input) ;
System.out.println ("lecture commence maintenant...") ;
System.out.println("___________________________________________________________________________________");
System.out.println() ;
ligne = bufRead.readLine() ; lit la première ligne
getAllChemicals() ;
bufRead.close() ; ferme le lecteur
replaceBadCharacters() ;
} catch (IOException e) {}
System.out.println(e.getMessage()) ;
System.out.println ("erreur ! u suck à ce ") ;
}
Return chemicalList ;
}
private void replaceBadCharacters() {}
Cette interates par le biais de l’arraylist et
remplace chaque espace avec un +
int taille = chemicalList.size() ; variable pour la taille de la liste de tableau
pour (int i = 0; j’ai < taille; i ++) {/ / un itération pour chaque index dans arraylist
String un = chemicalList.get (i) .replaceAll ("", « + ») ; crée une nouvelle variable et remplace l’espace avec un +
chemicalList.remove(i) ; Il sort la corde usagée à l’index
chemicalList.add(i, a) ; insère la nouvelle chaîne
}
}
private void getAllChemicals() {}
Ce lit chaque ligne du document txt et
met chaque ligne dans index distinct d’une
ArrayList.
try {}
tandis que (ligne! = null) {/ / tout en boucle qui parcourt chaque ligne jusqu'à ce qu’il n’est pas un
System.out.println ("chimique:" + ligne) ; imprime le nom chimique
chemicalList.add(line.trim()) ; Il ajoute à l’instance arraylist créé ci-dessus
chemicalList.add(line) ;
ligne = "" ;
ligne = bufRead.readLine() ; ligne suivante
}
} catch (IOException e) {}
System.out.println(e.getMessage()) ;
System.out.println ("erreur ! u suck à ce ") ;
}
}
}
MSDS.java :
package org.freesideatlanta.msds ;
public class {FS
Nom de la chaîne ;
String MSDStext ;
public FS (String un) {}
nom = un ;
}
public String getText () {}
Return MSDStext ;
}
public void changeText(String a) {}
MSDStext = a ;
}
}
MSDSCatalog.java :
package org.freesideatlanta.msds ;
Import org.apache.http.* ;
Import org.apache.http.client.* ;
Import org.apache.http.impl.client.* ;
Import org.apache.http.client.methods.* ;
Import org.apache.http.util.* ;
Import org.jsoup.Jsoup ;
Import org.jsoup.nodes.Document ;
Import org.jsoup.nodes.Element ;
Import org.jsoup.select.Elements ;
importation java.util.ArrayList ;
import java.IO ;
public class MsdsCatalog {}
HttpClient client ;
String URLhere = « http://hazard.com/msds/gn.cgi?query= » ;
ArrayList < String > errorsHere ;
FS chemicalsMSDS ;
Corps de chaîne ;
String edittedBody ;
public MsdsCatalog() {}
client = new DefaultHttpClient() ; Démarrez le client
URLhere = « http://hazard.com/msds/gn.cgi?query= » ; C’est la partie générique de l’URL commun à tous les sites
errorsHere = new ArrayList < String > () ;
}
FS publics interroger (chaîne chimique) {}
try {}
URLhere = « http://hazard.com/msds/gn.cgi?query= » ;
chemicalsMSDS = new MSDS(chemical) ;
System.out.println ("Prochaine" produit chimique"+ « \n » +" chimique: "+ chimique) ;
URLhere = URLhere + produit chimique ;
HttpGet méthode = new HttpGet(URLhere); / / insère l’URL à la méthode
System.out.println(URLhere) ;
Réponse de HttpResponse = client.execute(method) ; Obtient une réponse à partir de l’URL
Entité de HttpEntity = response.getEntity() ; crée une entité
Body = EntityUtils.toString(entity) ; Convertit l’entité à une chaîne et d’annonces pour le corps
Boolean hasJtBakerDb = (body.indexOf("jtbaker.com") > = 0) ;
Boolean hasSafetyCard = (body.indexOf("mf/cards/file") > = 0) ;
Boolean hasFileCard = (body.indexOf("href=f") > = 0) ;
Si (hasJtBakerDb || hasSafetyCard || hasFileCard) {}
cas affirmatif
System.out.println ("aucune erreur") ;
chemicalsMSDS = getMSDS() ;
} else {}
cas négatif
System.out.println ("erreur ! CE PRODUIT CHIMIQUE N’EST TROUVÉ SUR LA BASE DE DONNÉES!") ;
System.out.println ("Ceci s’ajouteront à la liste d’erreurs ») ;
errorsHere.add(chemical) ;
}
} catch (IOException e) {}
System.out.println(e.getMessage()) ;
System.out.println ("erreur ! u suck à ce ") ;
}
Return chemicalsMSDS ;
}
privé FS getMSDS() {}
Boolean hasSafetyCard = (body.indexOf("mf/cards/file") > = 0) ;
Boolean hasFisher = (body.indexOf("fscim") > = 0) ;
Si (hasSafetyCard == true) {}
chemicalsMSDS = retrieveSafetyCard() ;
} ElseIf (hasFisher == true) {}
chemicalsMSDS = retrieveFisher() ;
} else {}
chemicalsMSDS = retrieveMSDS() ;
}
Return chemicalsMSDS ;
}
privé FS retrieveSafetyCard() {}
try {}
Abc du document = Jsoup.connect(URLhere).get() ;
Liens des éléments = abc.select("a[href]") ;
pour (int i = 0; i < links.size(); i ++) {}
Boolean isSafetyCard = (links.get (i) .html () .indexOf ("Safety Card") > = 0) ;
Si {(isSafetyCard)
String newURL = links.get(i).attr("abs:href") ;
HttpGet méthode = new HttpGet(newURL) ;
Réponse de HttpResponse = client.execute(method) ;
Entité de HttpEntity = response.getEntity() ;
String MSDSq = EntityUtils.toString(entity) ;
Document doc_one = Jsoup.parse(MSDSq) ;
MSDSq = doc_one.body().text() ;
chemicalsMSDS.changeText(MSDSq) ;
J’ai = links.size() + 10 ;
} else {}
}
}
} catch (IOException e) {}
System.out.println(e.getMessage()) ;
System.out.println ("erreur ! u suck à ce ") ;
}
Return chemicalsMSDS ;
}
privé FS retrieveFisher() {}
try {}
Abc du document = Jsoup.connect(URLhere).get() ;
Liens des éléments = abc.select("a[href]") ;
pour (int i = 0; i < links.size(); i ++) {
Boolean isFisher = (links.get (i) .html () .indexOf ("pêcheur") > = 0) ;
Si (isFisher == true) {}
String newURL = links.get(i).attr("abs:href") ;
HttpGet méthode = new HttpGet(newURL) ;
Réponse de HttpResponse = client.execute(method) ;
Entité de HttpEntity = response.getEntity() ;
String MSDSq = EntityUtils.toString(entity) ;
Document doc_one = Jsoup.parse(MSDSq) ;
MSDSq = doc_one.body().text() ;
chemicalsMSDS.changeText(MSDSq) ;
J’ai = links.size() + 10 ;
} else {}
}
}
} catch (IOException e) {}
System.out.println(e.getMessage()) ;
System.out.println ("erreur ! u suck à ce ") ;
}
Return chemicalsMSDS ;
}
privé FS retrieveMSDS() {}
try {}
Abc du document = Jsoup.connect(URLhere).get() ;
Liens des éléments = abc.select("a[href]") ;
pour (int i = 0; i < links.size(); i ++) {
Boolean isJtBaker = (links.get (i) .html () .indexOf ("Mallinckrodt Baker") > = 0) ;
Boolean isErrors = (links.get(i).attr("abs:href").indexOf("msds/errors.html") > = 0) ;
Boolean isSearch = (links.get(i).attr("abs:href").indexOf("msds/search.html") > = 0) ;
isArchive booléen = (links.get(i).attr("abs:href").indexOf("msds/index.php") > = 0) ;
Si (isJtBaker == true || isErrors == true || isSearch == true || isArchive == true) {}
} else {}
String newURL = links.get(i).attr("abs:href") ;
HttpGet méthode = new HttpGet(newURL) ;
Réponse de HttpResponse = client.execute(method) ;
Entité de HttpEntity = response.getEntity() ;
String MSDSq = EntityUtils.toString(entity) ;
Document doc_one = Jsoup.parse(MSDSq) ;
MSDSq = doc_one.body().text() ;
chemicalsMSDS.changeText(MSDSq) ;
J’ai = links.size() + 10 ;
}
}
} catch (IOException e) {}
System.out.println(e.getMessage()) ;
System.out.println ("erreur ! u suck à ce ") ;
}
Return chemicalsMSDS ;
}
private String deleteString (int beginIndex, int endIndex, String un) {}
String toBeDeleted = a.substring (beginIndex, endIndex) ;
return a.replace (toBeDeleted, "") ;
}
}
MSDSWriter.java :
package org.freesideatlanta.msds ;
import java.IO ;
/*
* praznav
*/
public class MsdsWriter {}
Fstream FileWriter ;
BufferedWriter
public MsdsWriter () {}
try {}
FStream = new FileWriter("output.txt") ;
sortir = new BufferedWriter(fstream) ;
} catch (Exception e) {}
System.out.println(e.getMessage()) ;
}
}
Public Sub write (String chemical, String text) {}
try {}
out.Append(Chemical) ;
out.newLine() ;
out.Append("___") ;
out.newLine() ;
out.Append(Text) ;
out.newLine() ;
out.newLine() ;
out.newLine() ;
} catch (System.Exception e) {//Catch exception le cas échéant
System.Err.println ("Error:" + e.getMessage()) ;
}
}
{} public void close()
try {}
out.Close() ;
} catch (Exception e) {}
System.out.println(e.getMessage()) ;
}
}
}
Voici un résumé de chaque fichier :
--Application--
contenant la méthode main et instancie toutes les autres classes
--ChemicalReader--
Compiler une liste des produits chimiques comme un objet array quelconque
Convertir un nom chimique avec des espaces dans une liste de paramètres amical d’URL
--MsdsCatalog--
Exécuter une requête sur le nom « convivial » chimique et obtenir un résultat
Si ce résultat est valable pour nos besoins
Quant à la priorité : sécurité carte URL est préféré, ensuite des liens URL Fisher, puis passer par restant
--FS--
String nom = msds.getChemicalName() ;
String text = msds.getText() ;
--MsdsWriter--
Générer des fichiers de texte de sortie dans un sous répertoire