-
Notifications
You must be signed in to change notification settings - Fork 38
/
ParserActions.java
988 lines (861 loc) · 38.6 KB
/
ParserActions.java
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
package net.sourceforge.fidocadj.circuit.controllers;
import java.io.*;
import java.util.*;
import java.net.*;
import net.sourceforge.fidocadj.circuit.*;
import net.sourceforge.fidocadj.circuit.model.*;
import net.sourceforge.fidocadj.export.*;
import net.sourceforge.fidocadj.globals.*;
import net.sourceforge.fidocadj.layers.*;
import net.sourceforge.fidocadj.primitives.*;
import net.sourceforge.fidocadj.graphic.*;
/** ParserActions: perform parsing of FidoCadJ code.
In general, those routines are constructed such as they are relatively
fault-tolerant. If an error is detected in the file, the parsing is
continued anyway and just an error message is sent to the console. Most
of the times, the user will not see the error and this is probably OK
since he/she will not be interested in it.
<pre>
This file is part of FidoCadJ.
FidoCadJ 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 3 of the License, or
(at your option) any later version.
FidoCadJ 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 FidoCadJ. If not,
@see <a href=http://www.gnu.org/licenses/>http://www.gnu.org/licenses/</a>.
Copyright 2007-2019 by Davide Bucci
</pre>
*/
public class ParserActions
{
private final DrawingModel model;
// This is the maximum number of tokens which will be considered in a line
static final int MAX_TOKENS=10000;
// True if FidoCadJ should use Windows style line feeds (appending \r
// to the text generated).
static final boolean useWindowsLineFeed=false;
// Name of the last file opened
public String openFileName = null;
/** Standard constructor: provide the database class.
@param pp the drawing model (database of the circuit).
*/
public ParserActions (DrawingModel pp)
{
model=pp;
}
/** Parse the circuit contained in the StringBuffer specified.
This function resets the primitive database and then parses the circuit.
@param s the string containing the circuit
*/
public void parseString(StringBuffer s)
{
model.getPrimitiveVector().clear();
addString(s, false);
model.setChanged(true);
}
/** Renders a split version of the macros contained in the given string.
@param s a string containing macros to be splitted.
@param splitStandardMacros if it is true, even the standard macros
will be split.
@return the split macros.
*/
public StringBuffer splitMacros(StringBuffer s,
boolean splitStandardMacros)
{
StringBuffer txt= new StringBuffer("");
DrawingModel Q=new DrawingModel();
Q.setLibrary(model.getLibrary()); // Inherit the library
Q.setLayers(model.getLayers()); // Inherit the layers
// from the obtained string, obtain the new Q object which will
// be exported and then loaded into the clipboard.
try {
ParserActions pas=new ParserActions(Q);
pas.parseString(s);
File temp= File.createTempFile("copy", ".fcd");
temp.deleteOnExit();
String frm="";
if(splitStandardMacros)
frm = "fcda";
else
frm = "fcd";
ExportGraphic.export(temp, Q, frm, 1,true,false, true,false,false);
FileInputStream input = new FileInputStream(temp);
BufferedReader bufRead = new BufferedReader(
new InputStreamReader(input, Globals.encoding));
String line=bufRead.readLine();
if (line==null) {
bufRead.close();
return new StringBuffer("");
}
txt = new StringBuffer();
do {
txt.append(line);
txt.append("\n");
line =bufRead.readLine();
} while (line != null);
bufRead.close();
} catch(IOException e) {
System.out.println("Error: "+e);
}
return txt;
}
/** Get the FidoCadJ text file.
@param extensions specify if FCJ extensions should be used
@return the sketch in the text FidoCadJ format
*/
public StringBuffer getText(boolean extensions)
{
int i;
StringBuffer s=registerConfiguration(extensions);
for (GraphicPrimitive g:model.getPrimitiveVector()){
s.append(g.toString(extensions));
if(useWindowsLineFeed)
s.append("\r");
}
return s;
}
/** If it is needed, provides all the configurations settings at
the beginning of the FidoCadJ file.
@param extensions it is true when FidoCadJ should export using
its extensions.
@return a StringBuffer containing the configuration settings in the
FidoCadJ file format.
*/
public StringBuffer registerConfiguration(boolean extensions)
{
StringBuffer s = new StringBuffer();
// This is something which is not contemplated by the original
// FidoCAD for Windows. If extensions are not activated, just exit.
if(!extensions) {
return s;
}
// Here is the beginning of the output. We can eventually provide
// some hints about the configuration of the software (if needed).
// We start by checking if the diameter of the electrical connection
// should be written.
// We consider that a difference of 1e-5 is small enough
if(Math.abs(Globals.diameterConnectionDefault-
Globals.diameterConnection)>1e-5)
{
s.append("FJC C "+Globals.diameterConnection+"\n");
}
s.append(checkAndRegisterLayers());
// Check if the line widths should be indicated
if(Math.abs(Globals.lineWidth -
Globals.lineWidthDefault)>1e-5)
{
s.append("FJC A "+Globals.lineWidth+"\n");
}
if(Math.abs(Globals.lineWidthCircles -
Globals.lineWidthCirclesDefault)>1e-5)
{
s.append("FJC B "+Globals.lineWidthCircles+"\n");
}
return s;
}
/** Check if the layers should be indicated.
*/
private StringBuffer checkAndRegisterLayers()
{
StringBuffer s=new StringBuffer();
Vector<LayerDesc> layerV=model.getLayers();
Vector<LayerDesc> standardLayers =
StandardLayers.createStandardLayers();
for(int i=0; i<layerV.size();++i) {
LayerDesc l = (LayerDesc)layerV.get(i);
if (l.getModified()) {
int rgb=l.getColor().getRGB();
float alpha=l.getAlpha();
s.append("FJC L "+i+" "+rgb+" "+alpha+"\n");
// We compare the layers to the standard configuration.
// If the name has been modified, the name configuration
// is also saved.
String defaultName=
((LayerDesc)standardLayers.get(i)).getDescription();
if (!l.getDescription().equals(defaultName)) {
s.append("FJC N "+i+" "+l.getDescription()+"\n");
}
}
}
return s;
}
/** Parse the circuit contained in the StringBuffer specified.
this funcion add the circuit to the current primitive database.
@param s the string containing the circuit
@param selectNew specify that the added primitives should be selected.
*/
public void addString(StringBuffer s, boolean selectNew)
//throws IOException
{
int i; // Character pointer within the string
int j; // Token counter within the string
boolean hasFCJ=false; // The last primitive had FCJ extensions
StringBuffer token=new StringBuffer();
String macroFont = model.getTextFont();
int macroFontSize = model.getTextFontSize();
// Flag indicating that the line is already too long and should not be
// processed anymore:
boolean lineTooLong=false;
GraphicPrimitive g = new PrimitiveLine(macroFont, macroFontSize);
// The tokenized command string.
String[] tokens=new String[MAX_TOKENS];
// Name and value fields for a primitive. Those arrays will contain
// the tokenized TJ commands which follow an appropriate FCJ modifier.
String[] name=null;
String[] value=null;
int vn=0, vv=0;
// Since the modifier FCJ follow the command, we need to save the
// tokens of the line previously read, as well as the number of
// tokens found in it.
String[] old_tokens=new String[MAX_TOKENS];
int old_j=0;
int macro_counter=0;
int l;
token.ensureCapacity(256);
/* This code is not very easy to read. If more extensions of the
original FidoCAD format (performed with the FCJ tag) are to be
implemented, it can be interesting to rewrite the parser as a
state machine.
*/
synchronized(this) {
Vector<LayerDesc> layerV=model.getLayers();
int k;
char c='\n';
int len;
// Actual line number. This is useful to indicate where errors are.
int lineNum=1;
j=0;
token.setLength(0);
len=s.length();
// The purpose of this code is to tokenize the lines. Things are
// made more complicated by the FCJ mechanism which acts as a
// modifier for the previous command.
for(i=0; i<len;++i){
c=s.charAt(i);
if(c=='\n' || c=='\r'|| i==len-1) { //The string is finished
lineTooLong=false;
if(i==len-1 && c!='\n' && c!=' '){
token.append(c);
}
++lineNum;
tokens[j]=token.toString();
if (token.length()==0) // Avoids trailing spaces
j--;
try{
// When we enter here, we have tokenized the current
// line and we kept in memory also the previous one.
// The first possibility is that the current line does
// not contain a FCJ modifier. In this case, process
// the previous line since we have all the information
// needed
// for doing that.
if(hasFCJ && !tokens[0].equals("FCJ")) {
hasFCJ = registerPrimitivesWithFCJ(hasFCJ,
tokens, g, old_tokens, old_j, selectNew);
}
if(tokens[0].equals("FCJ")) {
// FidoCadJ extension!
// Here the FCJ modifier changes something on the
// previous command. So ve check case by case what
// has to be modified.
if(hasFCJ && old_tokens[0].equals("MC")) {
macro_counter=2;
g=new PrimitiveMacro(model.getLibrary(),layerV,
macroFont, macroFontSize);
g.parseTokens(old_tokens, old_j+1);
} else if (hasFCJ && old_tokens[0].equals("LI")) {
g=new PrimitiveLine(macroFont, macroFontSize);
// We concatenate the two lines in a single
// array
// of tokens (the same code will be repeated
// several
// times for other commands also).
for(l=0; l<j+1; ++l)
old_tokens[l+old_j+1]=tokens[l];
// Update the number of tokens
old_j+=j+1;
// The actual parsing of the tokens is
// relegated
// to the primitive.
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
if(old_j>5 && old_tokens[old_j].equals("1")) {
macro_counter = 2;
} else {
model.addPrimitive(g,false,null);
}
} else if (hasFCJ && old_tokens[0].equals("BE")) {
g=new PrimitiveBezier(macroFont, macroFontSize);
for(l=0; l<j+1; ++l)
old_tokens[l+old_j+1]=tokens[l];
old_j+=j+1;
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
if(old_j>5 && old_tokens[old_j].equals("1")) {
macro_counter = 2;
} else {
model.addPrimitive(g,false,null);
}
} else if (hasFCJ && (old_tokens[0].equals("RV")||
old_tokens[0].equals("RP")))
{
g=new PrimitiveRectangle(macroFont,
macroFontSize);
for(l=0; l<j+1; ++l)
old_tokens[l+old_j+1]=tokens[l];
old_j+=j+1;
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
if(old_j>2 && old_tokens[old_j].equals("1")) {
macro_counter = 2;
} else {
model.addPrimitive(g,false,null);
}
} else if (hasFCJ && (old_tokens[0].equals("EV")||
old_tokens[0].equals("EP")))
{
g=new PrimitiveOval(macroFont, macroFontSize);
for(l=0; l<j+1; ++l)
old_tokens[l+old_j+1]=tokens[l];
old_j+=j+1;
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
if(old_j>2 && old_tokens[old_j].equals("1")) {
macro_counter = 2;
} else {
model.addPrimitive(g,false,null);
}
} else if (hasFCJ && (old_tokens[0].equals("PV")||
old_tokens[0].equals("PP")))
{
g=new PrimitivePolygon(macroFont,
macroFontSize);
for(l=0; l<j+1; ++l)
old_tokens[l+old_j+1]=tokens[l];
old_j+=j+1;
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
if(old_j>2 && old_tokens[old_j].equals("1")) {
macro_counter = 2;
} else {
model.addPrimitive(g,false,null);
}
} else if (hasFCJ && (old_tokens[0].equals("CV")||
old_tokens[0].equals("CP")))
{
g=new PrimitiveComplexCurve(macroFont,
macroFontSize);
for(l=0; l<j+1; ++l)
old_tokens[l+old_j+1]=tokens[l];
old_j+=j+1;
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
// If we have a name/value following, we
// put macro_counter (successively used by
// TY to determine that we are in a case in
// which
// TY commands must not be considered as
// separate).
if(old_j>2 && old_tokens[old_j].equals("1")) {
macro_counter = 2;
} else {
model.addPrimitive(g,false,null);
}
} else if (hasFCJ && old_tokens[0].equals("PL")) {
macro_counter = 2;
} else if (hasFCJ && old_tokens[0].equals("PA")) {
macro_counter = 2;
} else if (hasFCJ && old_tokens[0].equals("SA")) {
macro_counter = 2;
}
hasFCJ=false;
} else if(tokens[0].equals("FJC")) {
fidoConfig(tokens, j, layerV);
} else if(tokens[0].equals("LI")) {
// Save the tokenized line.
// We cannot create the macro until we parse the
// following line (which can be FCJ)
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
} else if(tokens[0].equals("BE")) {
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
} else if(tokens[0].equals("MC")) {
// Save the tokenized line.
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
} else if(tokens[0].equals("TE")) {
hasFCJ=false;
macro_counter=0;
g=new PrimitiveAdvText();
g.parseTokens(tokens, j+1);
g.setSelected(selectNew);
model.addPrimitive(g,false,null);
} else if(tokens[0].equals("TY")) {
// The TY command is somewhat special, because
// it can be used to specify the name and the value
// of a primitive or a macro. Therefore, we try
// to understand in which case we are
hasFCJ=false;
if(macro_counter==2) {
macro_counter--;
name=new String[j+1];
for(l=0; l<j+1;++l)
name[l]=tokens[l];
vn=j;
} else if(macro_counter==1) {
value=new String[j+1];
for(l=0; l<j+1;++l)
value[l]=tokens[l];
vv=j;
if (name!=null) g.setName(name,vn+1);
g.setValue(value,vv+1);
g.setSelected(selectNew);
model.addPrimitive(g, false,null);
macro_counter=0;
} else {
// If we are in the classical case of a simple
// isolated TY command, we process it.
g=new PrimitiveAdvText();
g.parseTokens(tokens, j+1);
g.setSelected(selectNew);
model.addPrimitive(g,false,null);
}
} else if(tokens[0].equals("PL")) {
hasFCJ=true;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
macro_counter=0;
old_j=j;
g=new PrimitivePCBLine(macroFont, macroFontSize);
g.parseTokens(tokens, j+1);
g.setSelected(selectNew);
} else if(tokens[0].equals("PA")) {
hasFCJ=true;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
macro_counter=0;
g=new PrimitivePCBPad(macroFont, macroFontSize);
old_j=j;
g.parseTokens(tokens, j+1);
g.setSelected(selectNew);
} else if(tokens[0].equals("SA")) {
hasFCJ=true;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
macro_counter=0;
g=new PrimitiveConnection(macroFont, macroFontSize);
g.parseTokens(tokens, j+1);
g.setSelected(selectNew);
//addPrimitive(g,false,false);
} else if(tokens[0].equals("EV")
||tokens[0].equals("EP"))
{
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
} else if(tokens[0].equals("RV")
||tokens[0].equals("RP"))
{
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
} else if(tokens[0].equals("PV")
||tokens[0].equals("PP"))
{
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
} else if(tokens[0].equals("CV")
||tokens[0].equals("CP"))
{
macro_counter=0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
hasFCJ=true;
}
} catch(IOException E) {
System.out.println("Error encountered: "+E.toString());
System.out.println("string parsing line: "+lineNum);
hasFCJ = true;
macro_counter = 0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
} catch(NumberFormatException F) {
System.out.println(
"I could not read a number at line: "
+lineNum);
hasFCJ = true;
macro_counter = 0;
for(l=0; l<j+1; ++l)
old_tokens[l]=tokens[l];
old_j=j;
}
j=0;
token.setLength(0);
} else if (c==' ' && !lineTooLong){ // Ready for next token
tokens[j]=token.toString();
token.setLength(0);
++j;
if (j>=MAX_TOKENS) {
System.out.println("Too much tokens!");
System.out.println("string parsing line: "+lineNum);
j=MAX_TOKENS-1;
lineTooLong=true;
continue;
}
} else {
if (!lineTooLong)
token.append(c);
}
}
// We need to process the very last line, which is contained in
// the tokens currently read.
try{
registerPrimitivesWithFCJ(hasFCJ, tokens, g, old_tokens, old_j,
selectNew);
} catch(IOException E) {
System.out.println("Error encountered: "+E.toString());
System.out.println("string parsing line: "+lineNum);
} catch(NumberFormatException F) {
System.out.println("I could not read a number at line: "
+lineNum);
}
model.sortPrimitiveLayers();
}
}
/** Handle the FCJ command for the program configuration.
*/
private void fidoConfig(String[] tokens, int ntokens,
Vector<LayerDesc> layerV)
{
double newConnectionSize = -1.0;
double newLineWidth = -1.0;
double newLineWidthCircles = -1.0;
// FidoCadJ Configuration
if(tokens[1].equals("C")) {
// Connection size
newConnectionSize = Double.parseDouble(tokens[2]);
} else if(tokens[1].equals("L")) {
// Layer configuration
int layerNum = Integer.parseInt(tokens[2]);
if (layerNum>=0&&layerNum<layerV.size()){
int rgb=Integer.parseInt(tokens[3]);
float alpha=Float.parseFloat(tokens[4]);
LayerDesc ll=(LayerDesc)(layerV.get(layerNum));
ll.getColor().setRGB(rgb);
ll.setAlpha(alpha);
ll.setModified(true);
}
} else if(tokens[1].equals("N")) {
// Layer name
int layerNum = Integer.parseInt(tokens[2]);
if (layerNum>=0&&layerNum<layerV.size()){
String lName="";
StringBuffer temp=new StringBuffer(25);
for(int t=3; t<ntokens+1; ++t) {
temp.append(tokens[t]);
temp.append(" ");
}
lName=temp.toString();
LayerDesc ll=(LayerDesc)(layerV.get(layerNum));
ll.setDescription(lName);
ll.setModified(true);
}
} else if(tokens[1].equals("A")) {
// Connection size
newLineWidth = Double.parseDouble(tokens[2]);
} else if(tokens[1].equals("B")) {
// Connection size
newLineWidthCircles = Double.parseDouble(tokens[2]);
}
// If the schematics has some configuration information, we need
// to set them up.
if (newConnectionSize>0) {
Globals.diameterConnection=newConnectionSize;
}
if (newLineWidth>0) {
Globals.lineWidth = newLineWidth;
}
if (newLineWidthCircles>0) {
Globals.lineWidthCircles = newLineWidthCircles;
}
}
/** This method checks if a primitive may have FCJ modifiers following.
If no further FCJ tokens are present, the primitive is created
immediately. If a FCJ token follows, we proceed to further parsing
what follows.
*/
private boolean registerPrimitivesWithFCJ(boolean hasFCJ_t,
String[] tokens,
GraphicPrimitive gg, String[] old_tokens, int old_j,
boolean selectNew)
throws IOException
{
String macroFont = model.getTextFont();
int macroFontSize = model.getTextFontSize();
Vector<LayerDesc> layerV=model.getLayers();
GraphicPrimitive g=gg;
boolean hasFCJ=hasFCJ_t;
boolean addPrimitive = false;
if(hasFCJ && !tokens[0].equals("FCJ")) {
if (old_tokens[0].equals("MC")) {
g=new PrimitiveMacro(model.getLibrary(),
layerV, macroFont, macroFontSize);
addPrimitive = true;
} else if (old_tokens[0].equals("LI")) {
g=new PrimitiveLine(macroFont, macroFontSize);
addPrimitive = true;
} else if (old_tokens[0].equals("BE")) {
g=new PrimitiveBezier(macroFont, macroFontSize);
addPrimitive = true;
} else if (old_tokens[0].equals("RP")||
old_tokens[0].equals("RV"))
{
g=new PrimitiveRectangle(macroFont, macroFontSize);
addPrimitive = true;
} else if (old_tokens[0].equals("EP")||
old_tokens[0].equals("EV"))
{
g=new PrimitiveOval(macroFont, macroFontSize);
addPrimitive = true;
} else if (old_tokens[0].equals("PP")
||old_tokens[0].equals("PV"))
{
g=new PrimitivePolygon(macroFont, macroFontSize);
addPrimitive = true;
} else if(old_tokens[0].equals("PL")) {
g=new PrimitivePCBLine(macroFont, macroFontSize);
addPrimitive = true;
} else if (old_tokens[0].equals("CP")
||old_tokens[0].equals("CV"))
{
g=new PrimitiveComplexCurve(macroFont, macroFontSize);
addPrimitive = true;
} else if(old_tokens[0].equals("PA")) {
g=new PrimitivePCBPad(macroFont, macroFontSize);
addPrimitive = true;
} else if(old_tokens[0].equals("SA")) {
g=new PrimitiveConnection(macroFont, macroFontSize);
addPrimitive = true;
}
}
if(addPrimitive) {
g.parseTokens(old_tokens, old_j+1);
g.setSelected(selectNew);
model.addPrimitive(g,false,null);
hasFCJ = false;
}
return hasFCJ;
}
/** Read all librairies contained in the given URL at the given prefix.
This is particularly useful to read librairies shipped in a jar
file.
@param s the URL containing the libraries.
@param prefix_s the prefix to be adopted for the keys of all elements
in the library. Most of the times it is the filename, except for
standard or internal libraries.
*/
public void loadLibraryInJar(URL s, String prefix_s)
{
String prefix=prefix_s;
if(s==null) {
if (prefix==null)
prefix="";
System.out.println("Resource not found! "+prefix);
return;
}
try {
readLibraryBufferedReader(new BufferedReader(new
InputStreamReader(s.openStream(), Globals.encoding)), prefix);
} catch (IOException E) {
System.out.println("Problems reading library: "+s.toString());
}
}
/** Read the library contained in a file
@param openFileName the name of the file to be loaded
@throws IOException when something goes horribly wrong. Most of the
times the filename is not found.
*/
public void readLibraryFile(String openFileName)
throws IOException
{
InputStreamReader input = new InputStreamReader(new
FileInputStream(openFileName), Globals.encoding);
BufferedReader bufRead = new BufferedReader(input);
String prefix="";
prefix = Globals.getFileNameOnly(openFileName);
if ("FCDstdlib".equals(prefix))
prefix="";
readLibraryBufferedReader(bufRead, prefix);
bufRead.close();
}
/** Read a library provided by a buffered reader. Adds all the macro keys
in memory, with the given prefix.
@param bufRead The buffered reader prepared with the stream containing
the library we want to read.
@param prefix The prefix which should be added to the macro key when
using a non standard macro.
@throws IOException when something goes horribly wrong.
*/
public void readLibraryBufferedReader(BufferedReader bufRead,
String prefix)
throws IOException
{
String macroName="";
String longName="";
String categoryName="";
String libraryName="";
int i;
String line="";
MacroDesc macroDesc;
while(true) {
// Read and process line by line.
line = bufRead.readLine();
if(line==null)
break;
// Avoid trailing spaces
line=line.trim();
// Avoid processing shorter lines
if (line.length()<=1)
continue;
// A category
if(line.charAt(0)=='{') {
categoryName="";
StringBuffer temp=new StringBuffer(25);
for(i=1; i<line.length()&&line.charAt(i)!='}'; ++i){
temp.append(line.charAt(i));
}
categoryName=temp.toString().trim();
if(i==line.length()) {
IOException e=new IOException(
"Category non terminated with }.");
throw e;
}
continue;
}
// A macro
if(line.charAt(0)=='[') {
macroName="";
longName="";
StringBuffer temp=new StringBuffer(25);
for(i=1; line.charAt(i)!=' ' &&
line.charAt(i)!=']' &&
i<line.length(); ++i){
temp.append(line.charAt(i));
}
macroName=temp.toString().trim();
int j;
temp=new StringBuffer(25);
for(j=i; j<line.length()&&line.charAt(j)!=']'; ++j){
temp.append(line.charAt(j));
}
longName=temp.toString();
if(j==line.length()) {
IOException e=new IOException(
"Macro name non terminated with ].");
throw e;
}
if ("FIDOLIB".equals(macroName)) {
libraryName = longName.trim();
continue;
} else {
if(!"".equals(prefix))
macroName=prefix+"."+macroName;
macroName=macroName.toLowerCase(new Locale("en"));
model.getLibrary().put(macroName, new
MacroDesc(macroName,"","","","", prefix));
/*System.out.printf("-- macroName:%s | longName:%s |
categoryName:%s | libraryName:%s | prefix:%s\n",
macroName,longName,categoryName,libraryName,prefix);*/
continue;
}
}
// TODO rewrite this block
if(!"".equals(macroName)){
// Add the macro name.
// NOTE: in FidoCAD, the macro prefix is somewhat case
// insensitive, since it indicates a file name and in
// Windows all file names are case insensitive. Under
// other operating systems, we need to be waaay much
// careful, hence we convert the macro name to lower case.
macroName = macroName.toLowerCase(new Locale("en"));
macroDesc = model.getLibrary().get(macroName);
if(macroDesc==null)
return;
macroDesc.name = longName;
macroDesc.key = macroName;
macroDesc.category = categoryName;
macroDesc.library = libraryName;
macroDesc.filename = prefix;
macroDesc.description = macroDesc.description + "\n" +
line;
}
}
}
/** Try to load all libraries ("*.fcl") files in the given directory.
FCDstdlib.fcl if exists will be considered as standard library.
@param s the directory in which the libraries should be present.
*/
public void loadLibraryDirectory(String s)
{
String[] files; // The names of the files in the directory.
File dir = new File(s);
// Obtain the list of files in the specified directory.
files = dir.list(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
// This filter allows to obtain all files with the fcd
// file extension
return name.toLowerCase(new Locale("en")).endsWith(".fcl");
}
});
// We first check if the directory is existing or is not empty.
if(!dir.exists() || files==null) {
if (!"".equals(s)){
System.out.println("Warning! Library directory is incorrect:");
System.out.println(s);
}
System.out.println(
"Activated FidoCadJ internal libraries and symbols.");
return;
}
// We read all the directory content, file by file
for (int i = 0; i < files.length; i++) {
File f; // One of the files in the directory.
f = new File(dir, files[i]);
try {
// Here we have a hopefully valid file in f, so we may read its
// contents
readLibraryFile(f.getPath());
} catch (IOException E) {
System.out.println("Problems reading library "+
f.getName()+" "+E);
}
}
}
}