1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.awt.BorderLayout;
import java.awt.TextArea;
import java.io.File;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ExtractData {
static String fichier;
public static void main(String[] args) throws FileNotFoundException,IOException{
String goodOut = null;
goodOut = sauvegardeExplo();
ALL(fichier, goodOut+"DataExtraction.csv");
}
@SuppressWarnings("unused")
public static ArrayList<Process> findIdsFromFile(String filePath)throws FileNotFoundException{
Scanner scanner = new Scanner(new File(fichier));
ArrayList<Process> processList = new ArrayList<Process>();
ArrayList<String> idsList = new ArrayList<String>();
int i=0;
while (scanner.hasNextLine()){
String ch=scanner.nextLine();
if(ch.contains("<")){
Matcher matcher = Pattern.compile("(<ns3:ProcessID>([a-zA-Z0-9]*)</ns3:ProcessID>)").matcher(ch);
Matcher matcher1 = Pattern.compile("(<ns3:AssemblyLine>([a-zA-Z0-9]*)</ns3:AssemblyLine>)").matcher(ch);
Matcher matcher2 = Pattern.compile("(<ns3:DayOfYear>([a-zA-Z0-9]*)</ns3:DayOfYear>)").matcher(ch);
Matcher matcher3 = Pattern.compile("(<ns3:SequenceOfDay>([a-zA-Z0-9]*)</ns3:SequenceOfDay>)").matcher(ch);
String id = "";
String id1 = "";
String id2 = "";
String id3 = "";
while (matcher.find() & matcher1.find() & matcher2.find() & matcher3.find()){
try{
id = matcher.group(2);
id1 = matcher1.group(2);
id2 = matcher2.group(2);
id3 = matcher3.group(2);
}catch(Exception e){
System.out.println(e);
}
Process p=new Process(id.trim(),id1.trim(),id2.trim(),id3.trim());
idsList.add(id.trim());
idsList.add(id1.trim());
idsList.add(id2.trim());
idsList.add(id3.trim());
System.out.println(p);
processList.add(p);}
i++;
}
} scanner.close();
return processList;
}
public static ArrayList<Process> findProcessFromFile(String FilePath)throws FileNotFoundException{
Scanner scanner = new Scanner(new File(FilePath));
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
StringBuilder sb3 = new StringBuilder();
while (scanner.hasNextLine()){
sb.append(scanner.nextLine());
sb1.append(scanner.nextLine());
sb2.append(scanner.nextLine());
sb3.append(scanner.nextLine());
}
ArrayList<Process> processList = new ArrayList<Process>();
ArrayList<String> idsList = new ArrayList<String>();
Matcher matcher = Pattern.compile("(<ns3:ProcessID>([a-zA-Z0-9]*)</ns3:ProcessID>)").matcher(sb.toString());
Matcher matcher1 = Pattern.compile("(<ns3:AssemblyLine>([a-zA-Z0-9]*)</ns3:AssemblyLine>)").matcher(sb1.toString());
Matcher matcher2 = Pattern.compile("(<ns3:DayOfYear>([a-zA-Z0-9]*)</ns3:DayOfYear>)").matcher(sb2.toString());
Matcher matcher3 = Pattern.compile("(<ns3:SequenceOfDay>([a-zA-Z0-9]*)</ns3:SequenceOfDay>)").matcher(sb3.toString());
while (matcher.find() & matcher1.find() & matcher2.find() & matcher3.find()){
String id = matcher.group(2);
String id1 = matcher1.group(2);
String id2 = matcher2.group(2);
String id3 = matcher3.group(2);
Process p=new Process(id.trim(),id1.trim(),id2.trim(),id3.trim());
idsList.add(id.trim());
idsList.add(id1.trim());
idsList.add(id2.trim());
idsList.add(id3.trim());
processList.add(p);
} scanner.close();
return processList;
}
public static void ouvertureExplo(){
JFileChooser explorateur = new JFileChooser(new File("C:/Users/1cambien/Desktop"));
PopUpFile();
if (explorateur.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { // si l'instruction showOpenDialog renvoie APPROVE_OPTION
fichier = explorateur.getSelectedFile().toString();
System.out.println(fichier);
}
}
public static String sauvegardeExplo(){
String outPath = null;
String goodOut = null;
PopUpSave();
JFileChooser explorateur = new JFileChooser(new File("C:/Users/1cambien/Desktop"));
explorateur.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (explorateur.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { // si l'instruction showSaveDialog renvoie APPROVE_OPTION
fichier = explorateur.getCurrentDirectory().toString();
outPath = explorateur.getSelectedFile().toString();
System.out.println(outPath);
goodOut = outPath.replace("\\","/");
System.out.println(goodOut);
}
return goodOut+"/";
}
public static void ALL(String fichier, String outPath) throws FileNotFoundException, IOException{
ouvertureExplo();
ArrayList<Process> listeProcess = findIdsFromFile(fichier);
PrintWriter out = new PrintWriter(outPath);
out.append("sep=,\n");
out.append("ProcessID,AssemblyLine,Day_of_the_year,Sequence_of_Day\n");
for (Process p : listeProcess){
out.append(p.ProcessID+","+p.AssemblyLine+","+p.Day_of_the_year+","+p.Sequence_of_Day+"\n");
}
out.flush();
out.close();
}
public static void PopUpSave(){
JFrame win = new JFrame();
win.setSize(400, 100);
win.setVisible(true);
JPanel content = new JPanel();
win.setContentPane(content);
win.setTitle("Step 1");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea jta;
jta = new JTextArea();
jta.setText("Please select the folder to save the file");
JScrollPane scrollingArea = new JScrollPane(jta);
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
}
public static void PopUpFile(){
JFrame win = new JFrame();
win.setSize(400, 100);
win.setTitle("Step 2");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
JPanel content = new JPanel();
win.setContentPane(content);
JTextArea jta;
jta = new JTextArea();
jta.setText("Now, please select the file");
JScrollPane scrollingArea = new JScrollPane(jta);
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
}
} |
Partager