--- a/tool/src/spdxlib/SPDXfile.java
+++ b/tool/src/spdxlib/SPDXfile.java
@@ -16,8 +16,11 @@
package spdxlib;
import definitions.Process;
+import definitions.is;
import java.io.File;
import java.util.ArrayList;
+import script.log;
+import utils.files;
public final class SPDXfile {
@@ -53,6 +56,9 @@
public String
rawText; // text for the SPDX file
+
+ String[]
+ lines; // where we keep all the lines that were separated
/**
* Constructor where we initialize this object by serving an SPDX text
@@ -66,13 +72,13 @@
read(file);
}
- public SPDXfile(File file, String text) {
- // assign the file pointer
- this.file = file;
- rawText = text;
- // do the normal reading
- read(text);
- }
+// public SPDXfile(File file, String text) {
+// // assign the file pointer
+// this.file = file;
+// rawText = text;
+// // do the normal reading
+// read(text);
+// }
@@ -98,8 +104,9 @@
data.tagFile = file;
// get the complete content of the file to a string file
rawText = utils.files.readAsString(file);
+ lines = rawText.split("\n");
// read all available data from the given file
- data.read(rawText);
+ data.read(lines);
// process all this information into meaningful data
processData();
}
@@ -109,12 +116,12 @@
* provided from a text string
* @param text Content to be processed by the SPDX parser
*/
- public void read(String text){
- // read all available data from the given file
- data.read(text);
- // process all this information into meaningful data
- processData();
- }
+// public void read(String text){
+// // read all available data from the given file
+// data.read(text);
+// // process all this information into meaningful data
+// processData();
+// }
@@ -302,26 +309,26 @@
if(tag.title.equalsIgnoreCase("Creator")){
Person person = new Person();
creatorSection.people.add(person);
- person.tagPerson = tag;
+ person.setTagPerson(tag);
}
// process strict Creator tag
if(tag.title.equalsIgnoreCase("Creator->Person")){
Person person = new Person();
creatorSection.people.add(person);
- person.tagPerson = tag;
+ person.setTagPerson(tag);
}
// process organization details (we add them to last added person)
if(tag.title.equalsIgnoreCase("Creator->Organization")){
Person person = getLastCreator();
- person.tagOrganization = tag;
+ person.setTagOrganization(tag);
}
// process the tool details if available
if(tag.title.equalsIgnoreCase("Creator->Tool")){
Person person = getLastCreator();
- person.tagTool = tag;
+ person.setTagTool(tag);
}
}
@@ -413,7 +420,7 @@
if(reviewer == null){
reviewer = new Person();
}
- reviewer.tagPerson = tag;
+ reviewer.setTagPerson(tag);
return;
}
@@ -421,7 +428,7 @@
if(reviewer == null){
reviewer = new Person();
}
- reviewer.tagOrganization = tag;
+ reviewer.setTagOrganization(tag);
return;
}
@@ -429,7 +436,7 @@
if(reviewer == null){
reviewer = new Person();
}
- reviewer.tagTool = tag;
+ reviewer.setTagTool(tag);
return;
}
@@ -462,6 +469,7 @@
/**
* Adds another SPDX document as a dependency of this SPDX
+ * @param component
*/
public void addComponent(SPDXfile component){
// Add our component as a dependency
@@ -489,5 +497,64 @@
// save the result to disk
utils.files.SaveStringToFile(file, rawText);
}
+
+ /**
+ * A critical feature is when we need to change a give tag inside the
+ * physical file on disk. This is not a straightforward action, might
+ * have different types of consequences that are not yet handled.
+ * @param tag The tag that we want to modify
+ * @param original The original text portion we want to change
+ * @param replacement The new string of text that will be written
+ * @return Returns the modified tag value or the same if nothing changed
+ */
+ public TagValue changeTag(TagValue tag, String original, String replacement) {
+ // preflight checks
+ if(tag.isMultiLine){
+ log.write(is.ERROR, "SPDX change tag error, multi-line tags are "
+ + "not yet supported: %1", tag.toString());
+ return tag;
+ }
+
+ if(tag.linePosition < 0){
+ log.write(is.ERROR, "SPDX change tag error, line position "
+ + "is necessary but was not found: %1", "" + tag.linePosition);
+ return tag;
+ }
+
+ // do the change of text inside the tag
+ String newText = tag.raw.replace(original, replacement);
+ // save this info on the original tag object
+ tag.raw = newText;
+
+ // remove break lines, otherwise it adds a redundant line afterwards
+ newText = newText.replace("\n", "");
+ // replace the old line with the new contents
+ lines[tag.linePosition] = newText;
+ return tag;
+ }
+
+ /**
+ * After we are finished making changes, we can save the text file
+ */
+ public void commitChanges(){
+ // save everything to disk
+ String modifiedText = "";
+ for(String line : lines){
+ modifiedText += line + "\n";
+ }
+ rawText = modifiedText;
+ // write file on disk
+ files.SaveStringToFile(file, rawText);
+ }
+
+ /**
+ * Whenever a given tag does not exist, there are cases when we need to add
+ * one. This method permits to add a tag right after one that already exists
+ * @param linePosition The tag used as anchor
+ * @param text the text to be included
+ */
+ public void addTag(int linePosition, String text) {
+ lines[linePosition] += "\n" + text;
+ }
}