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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
| import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Elle recup ligne de forme : set ... = ... sans le $(...)
* @author ksmaili
*return : 1 fichier xml + le fichier texte de type CSH que l'on veux
*/
public class RecupLineWithoutSymboleTest {
private static String[] testVar;
private static ArrayList<String> indexes = new ArrayList<>();
private static ArrayList<String> Liste1 = new ArrayList<String>();
public static void main(String[] args) throws IOException {
final String fichier = "C:\\Users\\ksmaili\\workspace\\fichiertxtP2CSH.txt";
String xmlFileName = "C:\\Users\\ksmaili\\workspace\\XMLpartie2.xml";
Classement cl = new Classement();
// lit le fichier ligne par ligne
try {
// lire un fichier
final InputStream ips = new FileInputStream(fichier);
final InputStreamReader ipsr = new InputStreamReader(ips);
final BufferedReader br = new BufferedReader(ipsr);
// lire ligne par ligne
String ligne;
while ((ligne = br.readLine()) != null) {
// System.out.println(ligne);
// je splite pour avoir la clé et la valeur pour une comparaison prochaine
testVar = ligne.split("=");
if (ligne.matches("^set.*=.*") && !ligne.contains("$")) {
// je ne fais rien car la ligne est correcte et correspond par exemple a set blabla = 12
} else if (ligne.matches("^set.*=.*") && ligne.contains("$")) {
// parcourir la liste de la methode lineDollar()
ArrayList<String> listVal = lineDollar(ligne);
Iterator it = listVal.iterator();
while(it.hasNext() ) {
//System.out.println(it.next());
}
// Vérifier la position avant ou apres du $...
/*
if (it.next() > 1){
if (){
}
}*/
// Si la ligne est du style set blabla = $asd + 34 alors il me faut supprimer $asd et le signe +
ligne = count(ligne);
} else {
// dans le cas contraire exemple eewewewewewewewew ou esd = wwewe alors je mettre la ligne vide
ligne = "";
}
// Si la ligne n´est pas vide
if (ligne.length() > 0) {
// je verifie si son index peut etre creer et sauvegarder dans la liste d´indexes ou pas
if (creerIndex(testVar[0])) {
//System.out.println("On récupère : " + ligne);
Pattern p=Pattern.compile("^set(.*)=(.*)");
Matcher m=p.matcher(ligne);
while(m.find()) {
//System.out.println(m.group(1)); // affiche les clés
//System.out.println(m.group(2)); // affiche les valeurs des clés
// Maintenant je vérifie si la clé est déjà présente dans la map
boolean cle = cl.verif(m.group(1));
boolean valeur = cl.verif(m.group(2));
// Si elle n'est pas présente, on l'ajoute dans la map
if (cle != true && valeur != true ){
cl.add(m.group(1), m.group(2));
//System.out.println(cle);
}
}
}
}
else
{
// stoker testvar dans une liste afin de pouvoir l'écrire plus tard
Liste1.add(testVar[0]);
}
}
br.close(); // On ferme le flux
// ecrire le xml
cl.ecrire(xmlFileName);
//ouvrir un fichier en ecriture
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(new File("C:\\Users\\ksmaili\\workspace\\nouveaufichier.txt")));
// boucler sur les 6 er lignes de la liste de lignes sans traitement
int i;
int fin = 5;
for (i=0; i<fin; i++){
buf.write(Liste1.get(i)+"\n");
}
// ecrire la partie qui dépend du dico
buf.write("#>"+ "\n");
// cree iterator pour parcourir cl.recup()
Iterator<String> iterat = cl.recup().iterator();
while (iterat.hasNext() ){
Object element = iterat.next().trim();
//System.out.print(element + " ");
buf.write("set " + element +" =" + " {"+element+"}" +"\n");
}
buf.write("#<"+ "\n");
//ecrire les lignes suivantes de la liste de lignes sans traitement
for (i=fin; i<Liste1.size(); i++){
buf.write(Liste1.get(i)+"\n");
}
buf.close();
} catch (final Exception e) {
System.out.println(e.toString());
}
finally{
}
// if(!ligne.contains("$")){
}
catch (final Exception e) {
System.out.println(e.toString());
}finally{
}
}
private static boolean creerIndex(String[] testVar2) {
// TODO Auto-generated method stub
return false;
}
/**
* suppimes tout les mots commencant par $ ainsi que les +
* @param text
* @return String depourvu de $ et + exemple set a = 8
*/
public static String count(String text) {
String a = "";
String var2 = text;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '$') {
final int pos2 = countAt(text, i, ' ');
if (pos2 > 0) {
a = text.substring(i, pos2);
if (a.length() > 0) {
var2 = var2.replace(a, "");
}
} else {
a = text.substring(i, text.length());
if (a.length() > 0) {
var2 = var2.replace(a, "");
}
}
}
}
testVar = var2.split("=");
if (testVar[1].trim().equals("+") || testVar[1].trim().isEmpty()) {
var2 = "";
} else {
testVar[1] = testVar[1].replace("+", "");
var2 = testVar[0] + "= " + (testVar[1].trim());
}
return var2;
}
/**
* Récupère le morceau de ligne avec le $
* @param text
* @return String avec $[et ce qui suit avec]
*/
public static ArrayList<String> lineDollar(String text) {
String a = "";
ArrayList<String> po = new ArrayList<String>();
//String var2 = text;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '$') {
final int pos2 = countAt(text, i, ' ');
if (pos2 > 0) {
a = text.substring(i, pos2);
po.add(a);
} else {
a = text.substring(i, text.length());
po.add(a);
}
}
}
return po;
}
/**
* cherches a partir d´un text donné, et a partir d´une position donnée, la prochaine position du caractere a chercher
* @param text
* @param pos
* @param cara
*@return position du caractere s´il y en a , -1 s´il ny en a pas.
*/
public static int countAt(String text, int pos, char cara) {
for (int i = pos; i < text.length(); i++) {
if (text.charAt(i) == cara) {
return i;
}
}
return -1;
}
/**
* verifies si l´index est deja present ou pas dans la list d´indexes, s´il n´est pas present alors on le sauvegarde
* @param indexStr
* @return si l´index existe deja, alors false sinon true
*/
public static boolean creerIndex(String indexStr) {
final String[] indexVar = indexStr.split(" ");
boolean isCreated = true;
if (indexVar.length > 1) {
if (!indexes.contains(indexVar[1])) {
indexes.add(indexVar[1]);
} else {
isCreated = false;
}
}
return isCreated;
}
} |
Partager