Switch to unified view

a/tool/src/spdxlib/SPDXfile.java b/tool/src/spdxlib/SPDXfile.java
...
...
14
   */
14
   */
15
15
16
package spdxlib;
16
package spdxlib;
17
17
18
import definitions.Process;
18
import definitions.Process;
19
import definitions.is;
19
import java.io.File;
20
import java.io.File;
20
import java.util.ArrayList;
21
import java.util.ArrayList;
22
import script.log;
23
import utils.files;
21
24
22
public final class SPDXfile {
25
public final class SPDXfile {
23
    
26
    
24
    
27
    
25
    // The variables listed here follow the same order as
28
    // The variables listed here follow the same order as
...
...
51
   public TagValueCollection 
54
   public TagValueCollection 
52
           data = new TagValueCollection(); // where all data tags are placed
55
           data = new TagValueCollection(); // where all data tags are placed
53
   
56
   
54
   public String 
57
   public String 
55
            rawText; // text for the SPDX file
58
            rawText; // text for the SPDX file
59
   
60
   String[] 
61
           lines; // where we keep all the lines that were separated
56
   
62
   
57
   /**
63
   /**
58
    * Constructor where we initialize this object by serving an SPDX text
64
    * Constructor where we initialize this object by serving an SPDX text
59
    * file as source of knowledge to fill up the contents
65
    * file as source of knowledge to fill up the contents
60
    * @param file 
66
    * @param file 
...
...
64
       this.file = file;
70
       this.file = file;
65
       // do the normal reading
71
       // do the normal reading
66
       read(file);
72
       read(file);
67
   }
73
   }
68
74
69
    public SPDXfile(File file, String text) {
75
//    public SPDXfile(File file, String text) {
70
        // assign the file pointer
76
//        // assign the file pointer
71
       this.file = file;
77
//       this.file = file;
72
       rawText = text;
78
//       rawText = text;
73
       // do the normal reading
79
//       // do the normal reading
74
       read(text);
80
//       read(text);
75
    }
81
//    }
76
   
82
   
77
    
83
    
78
    
84
    
79
    /**
85
    /**
80
     * A reader for the version 1.1 of SPDX tag value format
86
     * A reader for the version 1.1 of SPDX tag value format
...
...
96
         
102
         
97
        // get the file pointer for future reference
103
        // get the file pointer for future reference
98
        data.tagFile = file;
104
        data.tagFile = file;
99
        // get the complete content of the file to a string file
105
        // get the complete content of the file to a string file
100
        rawText = utils.files.readAsString(file);
106
        rawText = utils.files.readAsString(file);
107
        lines = rawText.split("\n");
101
        // read all available data from the given file
108
        // read all available data from the given file
102
        data.read(rawText);
109
        data.read(lines);
103
        // process all this information into meaningful data
110
        // process all this information into meaningful data
104
        processData();
111
        processData();
105
    }
112
    }
106
    
113
    
107
   /**
114
   /**
108
    * Refreshes the data on this SPDX object with information directly
115
    * Refreshes the data on this SPDX object with information directly
109
    * provided from a text string
116
    * provided from a text string
110
    * @param text Content to be processed by the SPDX parser
117
    * @param text Content to be processed by the SPDX parser
111
    */
118
    */
112
     public void read(String text){
119
//     public void read(String text){
113
          // read all available data from the given file
120
//          // read all available data from the given file
114
        data.read(text);
121
//        data.read(text);
115
        // process all this information into meaningful data
122
//        // process all this information into meaningful data
116
        processData();
123
//        processData();
117
     }
124
//     }
118
     
125
     
119
     
126
     
120
     
127
     
121
   /**
128
   /**
122
    * Based on the information available on the "data" object, the SPDX
129
    * Based on the information available on the "data" object, the SPDX
...
...
300
        //System.out.println(tag.title + " - " + tag);
307
        //System.out.println(tag.title + " - " + tag);
301
            // are we talking about custom people tags like WindRiver?
308
            // are we talking about custom people tags like WindRiver?
302
            if(tag.title.equalsIgnoreCase("Creator")){
309
            if(tag.title.equalsIgnoreCase("Creator")){
303
                Person person = new Person();
310
                Person person = new Person();
304
                creatorSection.people.add(person);
311
                creatorSection.people.add(person);
305
                person.tagPerson = tag;
312
                person.setTagPerson(tag);
306
            }
313
            }
307
             
314
             
308
            // process strict Creator tag
315
            // process strict Creator tag
309
            if(tag.title.equalsIgnoreCase("Creator->Person")){
316
            if(tag.title.equalsIgnoreCase("Creator->Person")){
310
                Person person = new Person();
317
                Person person = new Person();
311
                creatorSection.people.add(person);
318
                creatorSection.people.add(person);
312
                person.tagPerson = tag;
319
                person.setTagPerson(tag);
313
            }
320
            }
314
            
321
            
315
            // process organization details (we add them to last added person)
322
            // process organization details (we add them to last added person)
316
            if(tag.title.equalsIgnoreCase("Creator->Organization")){
323
            if(tag.title.equalsIgnoreCase("Creator->Organization")){
317
                Person person = getLastCreator();
324
                Person person = getLastCreator();
318
                person.tagOrganization = tag;
325
                person.setTagOrganization(tag);
319
            }
326
            }
320
327
321
            // process the tool details if available
328
            // process the tool details if available
322
            if(tag.title.equalsIgnoreCase("Creator->Tool")){
329
            if(tag.title.equalsIgnoreCase("Creator->Tool")){
323
                Person person = getLastCreator();
330
                Person person = getLastCreator();
324
                person.tagTool = tag;
331
                person.setTagTool(tag);
325
            }
332
            }
326
    }
333
    }
327
    
334
    
328
    
335
    
329
   
336
   
...
...
411
        
418
        
412
        if(tag.title.equalsIgnoreCase("Reviewer->Person")){
419
        if(tag.title.equalsIgnoreCase("Reviewer->Person")){
413
            if(reviewer == null){
420
            if(reviewer == null){
414
                reviewer = new Person();
421
                reviewer = new Person();
415
            }
422
            }
416
                reviewer.tagPerson = tag;
423
                reviewer.setTagPerson(tag);
417
                return;
424
                return;
418
        }
425
        }
419
426
420
        if(tag.title.equalsIgnoreCase("Reviewer->Organizations")){
427
        if(tag.title.equalsIgnoreCase("Reviewer->Organizations")){
421
            if(reviewer == null){
428
            if(reviewer == null){
422
                reviewer = new Person();
429
                reviewer = new Person();
423
            }
430
            }
424
                reviewer.tagOrganization = tag;
431
                reviewer.setTagOrganization(tag);
425
                return;
432
                return;
426
        }
433
        }
427
        
434
        
428
        if(tag.title.equalsIgnoreCase("Reviewer->Tool")){
435
        if(tag.title.equalsIgnoreCase("Reviewer->Tool")){
429
            if(reviewer == null){
436
            if(reviewer == null){
430
                reviewer = new Person();
437
                reviewer = new Person();
431
            }
438
            }
432
                reviewer.tagTool = tag;
439
                reviewer.setTagTool(tag);
433
                return;
440
                return;
434
        }
441
        }
435
        
442
        
436
        
443
        
437
        if(tag.title.equalsIgnoreCase("ReviewDate")){
444
        if(tag.title.equalsIgnoreCase("ReviewDate")){
...
...
460
        return getId() + licenseOutput;
467
        return getId() + licenseOutput;
461
    }
468
    }
462
    
469
    
463
    /**
470
    /**
464
     * Adds another SPDX document as a dependency of this SPDX
471
     * Adds another SPDX document as a dependency of this SPDX
472
     * @param component
465
     */
473
     */
466
    public void addComponent(SPDXfile component){
474
    public void addComponent(SPDXfile component){
467
        // Add our component as a dependency
475
        // Add our component as a dependency
468
        System.err.println("UY76 - Adding component " 
476
        System.err.println("UY76 - Adding component " 
469
                + component.file.getName());
477
                + component.file.getName());
...
...
487
        // add this dependency to our text
495
        // add this dependency to our text
488
        rawText = rawText.concat(snippet);
496
        rawText = rawText.concat(snippet);
489
        // save the result to disk
497
        // save the result to disk
490
        utils.files.SaveStringToFile(file, rawText);
498
        utils.files.SaveStringToFile(file, rawText);
491
    }
499
    }
500
501
    /**
502
     * A critical feature is when we need to change a give tag inside the 
503
     * physical file on disk. This is not a straightforward action, might
504
     * have different types of consequences that are not yet handled.
505
     * @param tag The tag that we want to modify
506
     * @param original The original text portion we want to change
507
     * @param replacement The new string of text that will be written
508
     * @return Returns the modified tag value or the same if nothing changed
509
     */
510
    public TagValue changeTag(TagValue tag, String original, String replacement) {
511
        // preflight checks
512
        if(tag.isMultiLine){
513
            log.write(is.ERROR, "SPDX change tag error, multi-line tags are "
514
                    + "not yet supported: %1", tag.toString());
515
            return tag;
516
        }
517
518
        if(tag.linePosition < 0){
519
            log.write(is.ERROR, "SPDX change tag error, line position "
520
                 + "is necessary but was not found: %1", "" + tag.linePosition);
521
            return tag;
522
        }
523
524
        // do the change of text inside the tag
525
        String newText = tag.raw.replace(original, replacement);
526
        // save this info on the original tag object
527
        tag.raw = newText;
528
        
529
        // remove break lines, otherwise it adds a redundant line afterwards
530
        newText = newText.replace("\n", "");
531
        // replace the old line with the new contents
532
        lines[tag.linePosition] = newText;
533
        return tag;
534
    }
535
    
536
    /**
537
     * After we are finished making changes, we can save the text file
538
     */
539
    public void commitChanges(){
540
        // save everything to disk
541
        String modifiedText = "";
542
        for(String line : lines){
543
            modifiedText += line + "\n";
544
        }
545
        rawText = modifiedText;
546
        // write file on disk
547
        files.SaveStringToFile(file, rawText);
548
    }
549
550
    /**
551
     * Whenever a given tag does not exist, there are cases when we need to add
552
     * one. This method permits to add a tag right after one that already exists 
553
     * @param linePosition The tag used as anchor
554
     * @param text the text to be included
555
     */
556
    public void addTag(int linePosition, String text) {
557
        lines[linePosition] += "\n" + text;
558
    }
492
    
559
    
493
}
560
}