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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Lo�c Le Coq
* Copyright (C) 2013 Marko Zivkovic
*
* Contact Information: marko88zivkovic at gmail dot com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version. This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the
* GNU General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
* This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
* during his Bachelor thesis at the computer science department of ETH Z�rich,
* in the year 2013 and/or during future work.
*
* It is a reengineered version of XLogo written by Lo�c Le Coq, published
* under the GPL License at http://xlogo.tuxfamily.org/
*
* Contents of this file were initially written by Lo�c Le Coq,
* modifications, extensions, refactorings might have been applied by Marko Zivkovic
*/
/**
* Title : XLogo
* Description : XLogo is an interpreter for the Logo
* programming language
* @author Loïc Le Coq
*/
package xlogo.utils;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.Component;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import javax.swing.ImageIcon;
import xlogo.kernel.MyCalculator;
import xlogo.kernel.Affichage;
import xlogo.storage.WSManager;
import xlogo.storage.workspace.Language;
import xlogo.Logo;
public class Utils {
/**
* Marko : Here a technique was used for loading images, that happens to be from the last milenium.
* https://weblogs.java.net/blog/chet/archive/2004/07/imageio_just_an.html
*
* The way of the 3rs millenium is ImageIO
*
* @param nom
* @param jf
* @return
*/
public static ImageIcon dimensionne_image(String name,Component jf){
Image img = Toolkit.getDefaultToolkit().getImage(Utils.class.getResource(name));
return new ImageIcon(img.getScaledInstance(20, 20, Image.SCALE_SMOOTH));
/*
Image image;
try
{
image = ImageIO.read(new File("xlogo/utils/"+nom));
return image.getScaledInstance(22, 22, Image.SCALE_SMOOTH);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
/ *
Image image=null;
image= Toolkit.getDefaultToolkit().getImage(Utils.class.getResource(nom));
MediaTracker tracker=new MediaTracker(jf);
tracker.addImage(image,0);
try{tracker.waitForID(0);}
catch(InterruptedException e1){}
double largeur_ecran=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int largeur=image.getWidth(jf);
int hauteur=image.getHeight(jf);
// On fait attention à la résolution de l'utilisateur
double facteur = largeur_ecran/1024.0; //les images sont prévues pour 1024x768
if ((int)(facteur+0.001)!=1){
image=image.getScaledInstance((int)(facteur*largeur),(int)(facteur*hauteur),Image.SCALE_SMOOTH);
tracker=new MediaTracker(jf);
tracker.addImage(image,0);
try{tracker.waitForID(0);}
catch(InterruptedException e1){}
}
return image;
*/
}
public static void recursivelySetFonts(Component comp, Font font) {
comp.setFont(font);
if (comp instanceof Container) {
Container cont = (Container) comp;
for(int j=0, ub=cont.getComponentCount(); j<ub; ++j)
recursivelySetFonts(cont.getComponent(j), font);
}
}
public static String rajoute_backslash(String st){
StringBuffer tampon=new StringBuffer();
for(int j=0;j<st.length();j++){
char c=st.charAt(j);
if (c=='\\') tampon.append("\\\\");
else if (c==' ') tampon.append("\\e");
else if ("()[]#".indexOf(c)!=-1) tampon.append("\\"+c);
else tampon.append(c);
}
return(new String(tampon));
}
/**
* Escape string, backslash ...
* @param chaine
* @return
*/
public static String SortieTexte(String chaine){ // Enlève les backslash
StringBuffer buffer=new StringBuffer();
boolean backslash=false;
boolean ignore=false;
for (int j=0;j<chaine.length();j++){
char c=chaine.charAt(j);
if (backslash) {
if (c=='e') buffer.append(' ');
// else if (c=='\\') buffer.append('\\');
else if (c=='n') buffer.append("\n");
else if(c=='v') buffer.append("");
else if(c=='l') {
ignore=true;
}
else if("[]()#\\".indexOf(c)>-1) buffer.append(c);
backslash=false;
}
else {
if (c=='\\') backslash=true;
else if (!ignore) buffer.append(c);
else if (c==' ') ignore=false;
}
}
return MyCalculator.getOutputNumber(new String(buffer));
}
/**
* This method is formatting the String st.<br>
* - Unused white spaces are deleted.<br>
* - The character \ is modified to \\ <br>
* - The sequence "\ " is modified to "\e"<br>
* - The sequence "\ " is modified to "\e"<br>
* - The sequence "\ " is modified to "\e"<br>
* - The sequence "\ " is modified to "\e"<br>
* @param st The String instruction to format
* @return The formatted instructions
*/
public static StringBuffer decoupe(String st) {
StringBuffer buffer = new StringBuffer();
// If last character is a white space
boolean espace=false;
// If last character is a backslash
boolean backslash=false;
// If last character is a word
boolean mot=false;
int crochet_liste=0;
// boolean variable=false;
// If XLogo is running a program
boolean execution_lancee=Affichage.execution_lancee;
for(int i=0;i<st.length();i++){
char c=st.charAt(i);
if (c==' ') {
if (!espace&&buffer.length()!=0) {
if (backslash) buffer.append("\\e");
else {
buffer.append(c);
espace=true;
mot=false;
// variable=false;
}
backslash=false;
}
}
else if(c=='\\'&&!backslash) {
espace=false;
backslash=true;
}
else if(c=='\"'){
if (espace&&crochet_liste<=0){
mot=true;
}
buffer.append(c);
espace=false;
backslash=false;
}
else if (c==':'){
/* if (espace&&crochet_liste<=0){
variable=true;
}*/
buffer.append(c);
espace=false;
backslash=false;
}
else if (c=='['||c==']'||c=='('||c==')'){
//Modifications apportées
if (backslash) {
buffer.append("\\"+c);
backslash=false;
}
else {
if (c=='[') crochet_liste++;
else if (c==']') crochet_liste--;
if (espace||buffer.length()==0) {buffer.append(c+" ");espace=true;}
else {
buffer.append(" "+c+" ");
mot=false;
espace=true;
}
}
}
else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='='||c=='<'||c=='>'||c=='&'||c=='|'){
//System.out.println(mot+" "+espace);
// à modifier (test + fin)
if (mot||crochet_liste>0) {
buffer.append(c);
if (espace) espace=false;
}
else {
String op=String.valueOf(c);
// Looking for operator <= or >=
if (c=='<'||c=='>'){
if (i+1<st.length()){
if (st.charAt(i+1)=='='){
op+="=";
i++;
}
}
}
if (espace) buffer.append(op+" ");
else {
espace=true;
if (buffer.length()!=0) buffer.append(" "+op+" ");
// If buffer is empty no white space before
else buffer.append(op+" ");
}
}
}
else{
if (backslash){
if (c=='n') buffer.append("\\n");
else if (c=='\\') buffer.append("\\\\");
else if (c=='v'&& execution_lancee) buffer.append("\"");
else if(c=='e'&& execution_lancee) buffer.append("\\e");
else if (c=='#') buffer.append("\\#");
else if (c=='l'&&execution_lancee) buffer.append("\\l");
else {
buffer.append(c);
}
}
else {
buffer.append(c);
}
backslash=false;
espace=false;
}
}
// System.out.println(buffer);
// Remove the space when the user write only "*" or "+" in the command line
//if (buffer.length()>0&&buffer.charAt(0)==' ') buffer.deleteCharAt(0);
return (buffer);
}
public static String specialCharacterXML(String st){
st=st.replaceAll("&","&");
st=st.replaceAll("<","<");
st=st.replaceAll("\"",""");
st=st.replaceAll(">",">");
st=st.replaceAll("'","'");
return st;
}
public static String readLogoFile(String path) throws IOException{ // ADAPT READ LOGO FILE
String txt="";
// The old format before XLogo 0.9.23 is no longer supported from version 0.9.30
try{
// New format for XLogo >=0.923
// Encoded with UTF-8
StringBuffer sb=new StringBuffer();
FileInputStream fr = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fr, "UTF8");
BufferedReader brd=new BufferedReader(isr);
while (brd.ready()){
sb.append(brd.readLine());
sb.append("\n");
}
txt=new String(sb);
}
catch(FileNotFoundException e1){
// tentative fichier réseau
try{
URL url =new java.net.URL(path);
StringBuffer sb=new StringBuffer();
java.io.InputStream fr = url.openStream();
InputStreamReader isr = new InputStreamReader(fr, "UTF8");
BufferedReader brd=new BufferedReader(isr);
while (brd.ready()){
String st=brd.readLine();
sb.append(st);
sb.append("\n");
}
txt=new String(sb);
}
catch( java.net.MalformedURLException e){
System.out.println("File not found: "+path.toString());
}
}
catch(Exception e){e.printStackTrace();}
if (txt.startsWith("# "+Logo.messages.getString("mainCommand"))){
int id=txt.indexOf("\n");
if (id!=-1){
WSManager.getUserConfig().setMainCommand(txt.substring(("# "+Logo.messages.getString("mainCommand")).length(),id).trim());
txt=txt.substring(id+1);
}
};
return txt;
}
/**
* Store a string to a logo file path, UTF9 encoding,
* Write the main command to the head of the file with a #
* @param path
* @param txt
* @throws IOException
*/
public static void writeLogoFile(String path,String txt) throws IOException{ // ADAPT write logo file
try{
if (!WSManager.getUserConfig().getMainCommand().trim().equals("")) {
String heading="# "+Logo.messages.getString("mainCommand")+" "+WSManager.getUserConfig().getMainCommand()+"\n";
txt=heading+txt;
}
FileOutputStream f = new FileOutputStream(path);
BufferedOutputStream b = new BufferedOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(b, "UTF8");
osw.write(txt);
osw.close();
b.close();
f.close();
}
catch(FileNotFoundException e1){e1.printStackTrace();}
}
public static boolean fileExists(String name){
File f=new File(name);
return f.exists();
}
/**
* @param name
* @return
* @author Marko Zivkovic
*/
public static boolean isFile(String path){
File f = new File(path);
return f.isFile();
}
/**
* @param path
* @return
* @author Marko Zivkovic
*/
public static boolean isDirectory(String path){
File f = new File(path);
return f.isDirectory();
}
/**
* Implementation inspired by "JAVA ist auch eine Insel" - Christian Ullenboom, Galileo Computing
* <p> If destination exists, it will be replaced
* @param src
* @param dest
* @throws IOException If there is a problem with either src or dest
* @author Marko Zivkovic
*/
public static void copyFile(String src, String dest) throws IOException
{
copyFile(new File(src), new File(dest));
}
/**
* Implementation inspired by "JAVA ist auch eine Insel" - Christian Ullenboom, Galileo Computing
* <p> If destination exists, it will be replaced
* @param src
* @param dest
* @throws IOException If there is a problem with either src or dest
* @author Marko Zivkovic
*/
public static void copyFile(File src, File dest) throws IOException
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] buffer = new byte[0xFFFF];
for (int len; (len = fis.read(buffer)) != -1;)
{
fos.write(buffer, 0, len);
}
}catch (IOException e) {
throw e;
}
finally {
if (fis != null)
try { fis.close(); } catch (IOException e) {}
if (fos != null)
try { fos.close(); } catch (IOException e) {}
}
}
/**
* First copy file to dest and then delete file.
* @param file
* @param dest
* @throws IOException If there is a problem with either file or dest
* @author Marko Zivkovic
*/
public static void moveFile(String file, String dest) throws IOException
{
moveFile(new File(file), new File(dest));
}
/**
* First copy file to dest and then delete file.
* @param file
* @param dest
* @throws IOException If there is a problem with either file or dest
* @author Marko Zivkovic
*/
public static void moveFile(File file, File dest) throws IOException
{
copyFile(file, dest);
file.delete();
}
public static String primitiveName(String generic){
Language lang = WSManager.getInstance().getWorkspaceConfigInstance().getLanguage();
Locale locale = lang.getLocale();
ResourceBundle prim = ResourceBundle.getBundle(
"primitives", locale);
String st = prim.getString(generic);
StringTokenizer str = new StringTokenizer(st);
while (str.hasMoreTokens()) {
st = str.nextToken();
}
return st;
}
}
|