Skip to content

Commit

Permalink
code changes for new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Niessner authored and Al Niessner committed Oct 4, 2023
1 parent fc9231d commit 2c3a7dc
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/main/java/gov/nasa/pds/tools/label/LabelValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
import gov.nasa.pds.tools.validate.TargetExaminer;
import gov.nasa.pds.tools.validate.ValidationProblem;
import gov.nasa.pds.validate.constants.Constants;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.TreeInfo;
import net.sf.saxon.trans.XPathException;

/**
Expand Down Expand Up @@ -252,7 +252,7 @@ public void setCatalogs(String[] catalogFiles) {
resolver.setPreferPublic(true);
resolver.setCatalogList(catalogFiles);
useLabelSchematron = true;
LOG.debug("setCatalogs:catalogFiles {}", catalogFiles);
LOG.debug("setCatalogs:catalogFiles {}", (Object[])catalogFiles);
LOG.debug("setCatalogs:useLabelSchematron explitly set to true");
}

Expand Down Expand Up @@ -658,9 +658,9 @@ public synchronized Document parseAndValidate(ProblemHandler handler, URL url)

SAXSource saxSource = new SAXSource(Utility.getInputSourceByURL(url));
saxSource.setSystemId(url.toString());
DocumentInfo docInfo = LabelParser.parse(saxSource);
TreeInfo docInfo = LabelParser.parse(saxSource);
for (DocumentValidator dv : documentValidators) {
dv.validate(handler, docInfo);
dv.validate(handler, docInfo.getRootNode());
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/gov/nasa/pds/tools/util/LabelParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.xml.transform.TransformerException;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.ParseOptions;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.TreeInfo;
import net.sf.saxon.xpath.XPathEvaluator;

public class LabelParser {
Expand All @@ -29,15 +29,15 @@ public class LabelParser {
* @return a DocumentInfo object.
* @throws TransformerException
*/
public static DocumentInfo parse(Source source) throws TransformerException {
public static TreeInfo parse(Source source) throws TransformerException {
XPathEvaluator xpath = new XPathEvaluator();
Configuration configuration = xpath.getConfiguration();
configuration.setLineNumbering(true);
configuration.setXIncludeAware(Utility.supportXincludes());
ParseOptions options = new ParseOptions();
options.setErrorListener(new XMLErrorListener());
options.setLineNumbering(true);
options.setXIncludeAware(Utility.supportXincludes());
return configuration.buildDocument(source, options);
options.withErrorHandler(new XMLErrorListener());
options.withLineNumbering(true);
options.withXIncludeAware(Utility.supportXincludes());
return configuration.buildDocumentTree(source, options);
}
}
20 changes: 19 additions & 1 deletion src/main/java/gov/nasa/pds/tools/util/XMLErrorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

import javax.xml.transform.ErrorListener;
import javax.xml.transform.TransformerException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
* Class that handles errors while parsing an XML file.
*
* @author mcayanan
*
*/
public class XMLErrorListener implements ErrorListener {
public class XMLErrorListener implements ErrorListener,ErrorHandler {

/**
* Method is called when an error is encountered.
Expand Down Expand Up @@ -61,4 +64,19 @@ public void warning(TransformerException exception) throws TransformerException
throw new TransformerException(exception);
}

@Override
public void warning(SAXParseException exception) throws SAXException {
throw exception;
}

@Override
public void error(SAXParseException exception) throws SAXException {
throw exception;
}

@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}

}
15 changes: 8 additions & 7 deletions src/main/java/gov/nasa/pds/tools/util/XMLExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.xml.sax.InputSource;
import net.sf.saxon.Configuration;
import net.sf.saxon.lib.ParseOptions;
import net.sf.saxon.om.NamespaceUri;
import net.sf.saxon.om.TreeInfo;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.tree.tiny.TinyNodeImpl;
Expand Down Expand Up @@ -66,7 +67,7 @@ public XMLExtractor(Source xml) throws XPathExpressionException, XPathException
configuration.setLineNumbering(true);
configuration.setXIncludeAware(Utility.supportXincludes());
String definedNamespace = getValueFromDoc("namespace-uri(/*)");
xpath.getStaticContext().setDefaultElementNamespace(definedNamespace);
xpath.getStaticContext().setDefaultElementNamespace(NamespaceUri.of (definedNamespace));
}

/**
Expand All @@ -83,11 +84,11 @@ public XMLExtractor(URL url) throws XPathException, XPathExpressionException {
configuration.setLineNumbering(true);
configuration.setXIncludeAware(Utility.supportXincludes());
ParseOptions options = new ParseOptions();
options.setErrorListener(new XMLErrorListener());
options.withErrorHandler(new XMLErrorListener());
try {
xml = configuration.buildDocument(new SAXSource(Utility.getInputSourceByURL(url)), options);
xml = configuration.buildDocumentTree(new SAXSource(Utility.getInputSourceByURL(url)), options).getRootNode();
String definedNamespace = getValueFromDoc("namespace-uri(/*)");
xpath.getStaticContext().setDefaultElementNamespace(definedNamespace);
xpath.getStaticContext().setDefaultElementNamespace(NamespaceUri.of (definedNamespace));
} catch (IOException io) {
throw new XPathException("Error while reading input: " + io.getMessage());
}
Expand All @@ -99,10 +100,10 @@ public XMLExtractor(InputSource source) throws XPathException, XPathExpressionEx
configuration.setLineNumbering(true);
configuration.setXIncludeAware(Utility.supportXincludes());
ParseOptions options = new ParseOptions();
options.setErrorListener(new XMLErrorListener());
xml = configuration.buildDocument(new SAXSource(source), options);
options.withErrorHandler(new XMLErrorListener());
xml = configuration.buildDocumentTree(new SAXSource(source), options).getRootNode();
String definedNamespace = getValueFromDoc("namespace-uri(/*)");
xpath.getStaticContext().setDefaultElementNamespace(definedNamespace);
xpath.getStaticContext().setDefaultElementNamespace(NamespaceUri.of(definedNamespace));
}

public XMLExtractor(File file)
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/gov/nasa/pds/tools/validate/TargetExaminer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import gov.nasa.pds.tools.util.LabelParser;
import gov.nasa.pds.tools.util.Utility;
import gov.nasa.pds.tools.util.XMLExtractor;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.TreeInfo;
import net.sf.saxon.tree.tiny.TinyNodeImpl;

/**
Expand Down Expand Up @@ -127,11 +127,11 @@ private static boolean tagMatches(URL url, String tagCheck, boolean ignoreErrors
InputSource source = Utility.getInputSourceByURL(url);
SAXSource saxSource = new SAXSource(source);
saxSource.setSystemId(url.toString());
DocumentInfo docInfo = LabelParser.parse(saxSource); // Parses a label.
TreeInfo docInfo = LabelParser.parse(saxSource); // Parses a label.
LOG.debug("tagMatches:docInfo {},{}", docInfo, docInfo.getClass());
List<TinyNodeImpl> xmlModels = new ArrayList<>();
try {
XMLExtractor extractor = new XMLExtractor(docInfo);
XMLExtractor extractor = new XMLExtractor(docInfo.getRootNode());
xmlModels = extractor.getNodesFromDoc(tagCheck);
LOG.debug("tagMatches:url,tagCheck,xmlModels.size() {},{},{}", url, tagCheck,
xmlModels.size());
Expand Down Expand Up @@ -181,11 +181,11 @@ public static ArrayList<String> getTargetContent(URL url, String nodeCheck, Stri
InputSource source = Utility.getInputSourceByURL(url);
SAXSource saxSource = new SAXSource(source);
saxSource.setSystemId(url.toString());
DocumentInfo docInfo = LabelParser.parse(saxSource); // Parses a label.
TreeInfo docInfo = LabelParser.parse(saxSource); // Parses a label.
LOG.debug("getTargetContent:docInfo {},{}", docInfo, docInfo.getClass());
List<TinyNodeImpl> xmlModels = new ArrayList<>();
try {
XMLExtractor extractor = new XMLExtractor(docInfo);
XMLExtractor extractor = new XMLExtractor(docInfo.getRootNode());
xmlModels = extractor.getNodesFromDoc(nodeCheck);
LOG.debug("getTargetContent:url,nodeCheck,xmlModels.size() {},{},{}", url, nodeCheck,
xmlModels.size());
Expand Down Expand Up @@ -226,10 +226,10 @@ public static boolean isTargetALabel(URL url) {
InputSource source = Utility.getInputSourceByURL(url);
SAXSource saxSource = new SAXSource(source);
saxSource.setSystemId(url.toString());
DocumentInfo docInfo = LabelParser.parse(saxSource); // Parses a label.
TreeInfo docInfo = LabelParser.parse(saxSource); // Parses a label.
@SuppressWarnings("unused")
List<TinyNodeImpl> xmlModels = new ArrayList<>();
XMLExtractor extractor = new XMLExtractor(docInfo);
XMLExtractor extractor = new XMLExtractor(docInfo.getRootNode());
xmlModels = extractor.getNodesFromDoc("logical_identifier");
return true;
} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
import gov.nasa.pds.tools.validate.content.AudioVideo;
import gov.nasa.pds.tools.validate.rule.AbstractValidationRule;
import gov.nasa.pds.tools.validate.rule.ValidationTest;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.TreeInfo;
import net.sf.saxon.tree.tiny.TinyNodeImpl;

/**
Expand Down Expand Up @@ -111,9 +112,9 @@ public void validateFileReferences() {
DOMSource source = new DOMSource(label);
source.setSystemId(uri.toString());
try {
DocumentInfo xml = LabelParser.parse(source);
TreeInfo xml = LabelParser.parse(source);
LOG.debug("FileReferenceValidationRule:validateFileReferences:uri {}", uri);
validate(xml);
validate(xml.getRootNode());
} catch (TransformerException te) {
ProblemDefinition pd =
new ProblemDefinition(ExceptionType.ERROR, ProblemType.INTERNAL_ERROR, te.getMessage());
Expand Down Expand Up @@ -153,7 +154,7 @@ private void checkExtension(String filename, String encoding) {
}
}

private boolean validate(DocumentInfo xml) {
private boolean validate(NodeInfo xml) {
this.target = new ValidationTarget(getTarget());

try {
Expand Down

0 comments on commit 2c3a7dc

Please sign in to comment.