Skip to content

Commit

Permalink
should be a working solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Niessner authored and Al Niessner committed Nov 21, 2023
1 parent b3990c9 commit 418aee4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public class FileReferencedMapList {
// Class to hold a map of every labels that referenced a physical file.
// If a file is referenced by more than one label, an error should be thrown.
class FileReferencedMap {
private URL url = null;
private List<String> labelNames = new ArrayList<>();

public FileReferencedMap(URL url) {
this.url = url;
public FileReferencedMap() {
}

public void setLabels(String labelName) {
Expand Down Expand Up @@ -86,7 +84,7 @@ public FileReferencedMap setLabels(URL url, String labelName) {
} else {
// If fileReferencedMaps does not contain an existing, url, create a new
// FileReferencedMap and add labelName to the new empty list.
fileReferencedMap = new FileReferencedMap(url);
fileReferencedMap = new FileReferencedMap();
fileReferencedMap.setLabels(labelName);
fileReferencedMaps.put(url, fileReferencedMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package gov.nasa.pds.tools.validate;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import gov.nasa.pds.tools.util.LabelUtil;

public class CrossLabelFileAreaReferenceChecker {
final private static HashMap<String,List<String>> knownRefs = new HashMap<String,List<String>>();
private static String resolve (String name, ValidationTarget target) {
if (!name.startsWith ("/")) {
name = Path.of(target.getUrl().getPath()).getParent().resolve(name).toString();
}
return name;
}
/**
* @param name - the file being referenced by the file area
* @param target - the label being validated
* @return true if name and target are unique and only known references and false otherwise
* All of these exceptions should not happen because they are getting the LID from the target.
* Would not have made it this far if that could not have happened already, So, just pass them
* back to the called and let them handle it with their own generic exception handler/message.
* @throws IOException
* @throws ParserConfigurationException
* @throws SAXException
*/
public static boolean add (String name, ValidationTarget target) throws IOException, ParserConfigurationException, SAXException {
boolean success = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document xml = dbf.newDocumentBuilder().parse(target.getUrl().openStream());
DOMSource domSource = new DOMSource(xml);
for (String lid : LabelUtil.getLogicalIdentifiers (domSource, target.getUrl())) {
if (lid.contains("::")) {
lid = lid.substring (0, lid.indexOf("::"));
}
if (!knownRefs.keySet().contains (resolve(name, target))) {
knownRefs.put(resolve(name, target), (List<String>)Arrays.asList(lid, target.getUrl().getPath()));
success = true;
} else {
success = false;
}
}
return success;
}
public static String getOtherId (String name, ValidationTarget target) {
return knownRefs.get(resolve(name, target)).get(0);
}
public static String getOtherFilename (String name, ValidationTarget target) {
return knownRefs.get(resolve(name, target)).get(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import gov.nasa.pds.tools.util.PDFUtil;
import gov.nasa.pds.tools.util.Utility;
import gov.nasa.pds.tools.util.XMLExtractor;
import gov.nasa.pds.tools.validate.CrossLabelFileAreaReferenceChecker;
import gov.nasa.pds.tools.validate.ProblemDefinition;
import gov.nasa.pds.tools.validate.ProblemType;
import gov.nasa.pds.tools.validate.ValidationProblem;
Expand Down Expand Up @@ -280,6 +281,15 @@ private boolean validate(NodeInfo xml) {
name = child.getStringValue();
this.fileMapping.put(name, "");
LOG.debug("FileReferenceValidationRule:validate:name {}", name);
if (!CrossLabelFileAreaReferenceChecker.add (name, target)) {
this.getListener().addProblem(
new ValidationProblem(
new ProblemDefinition(ExceptionType.ERROR, ProblemType.DUPLICATED_FILE_AREA_REFERENCE,
"This file area references " + name + " that is already used by label "
+ CrossLabelFileAreaReferenceChecker.getOtherId(name, target)
+ " in file " + CrossLabelFileAreaReferenceChecker.getOtherFilename(name, target)),
target, fileObject.getLineNumber(), -1));
};
} else if ("md5_checksum".equals(child.getLocalPart())) {
checksum = child.getStringValue();
LOG.debug("FileReferenceValidationRule:validate:checksum {}", checksum);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/features/developer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Scenario Outline: Execute validate command for tests below.
| testName | testDir | messageCount | messageText | problemEnum | resourceDir | reportDir | commandArgs | refOutputValue |

# Validate#755
|"NASA-PDS/validate#755 Detect and report file areas referencing same file" | "github755" | 1 | "1 errors expected" | "DUPLICATED_FILE_AREA_REFERENCE" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github755.json -s json -skip-content-validation -t {resourceDir}/github755/m221011.0013.xml {resourceDir}/github755/m221011.0014.xml {resourceDir}/github755/m221011.0015.xml {resourceDir}/github755/m221011.0030.xml" | "report_github755.json" |
|"NASA-PDS/validate#755 Detect and report file areas referencing same file" | "github755" | 2 | "2 errors expected" | "DUPLICATED_FILE_AREA_REFERENCE" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github755.json -s json -skip-content-validation -t {resourceDir}/github755/m221011.0013.xml {resourceDir}/github755/m221011.0014.xml {resourceDir}/github755/m221011.0015.xml {resourceDir}/github755/m221011.0030.xml" | "report_github755.json" |
|"NASA-PDS/validate#755 No common file referenced" | "github755" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github755.1.json -s json -skip-content-validation -t {resourceDir}/github755/m221011.0013.xml {resourceDir}/github755/m221011.0015.xml" | "report_github755.1.json" |

# Validate#754
Expand Down

0 comments on commit 418aee4

Please sign in to comment.