Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a new method to validate schema that throws #77

Merged
merged 3 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions third_party/wff/specification/validator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
apply plugin: 'java-library'

def baseVersion="1.3.0"
def baseVersion="1.4.0"
def baseArchivesName="wff-validator"

dependencies {
Expand Down Expand Up @@ -51,4 +51,4 @@ jar {
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public WatchFaceXmlValidator() {
resourceManager = new ResourceManager();
}

/** Exception thrown when the watch face format validation has failed to execute. */
public static class WatchFaceFormatValidationException extends Exception {
public WatchFaceFormatValidationException(String message, Throwable cause) {
super(message, cause);
}

}

/**
* Check whether the validator support the version or not
*
Expand Down Expand Up @@ -75,11 +83,11 @@ public boolean validate(String xmlPath, String version) {
return true;
} catch (SAXParseException e) {
String errorMessage = String.format(
"[Line %d:Column %d]: %s",
e.getLineNumber(),
e.getColumnNumber(),
e.getMessage()
);
"[Line %d:Column %d]: %s",
e.getLineNumber(),
e.getColumnNumber(),
e.getMessage()
);
Log.e(errorMessage);
return false;
} catch (Exception e) {
Expand All @@ -100,6 +108,7 @@ public boolean validate(Document xmlDocument, String version) {
if (!isSupportedVersion(version)) {
throw new RuntimeException("Validator not support the version #" + version);
}

validateXMLSchema(
resourceManager.getXsdFile(version).getCanonicalPath(), new DOMSource(xmlDocument));
return true;
Expand All @@ -109,8 +118,37 @@ public boolean validate(Document xmlDocument, String version) {
}
}

/**
* Validate watch face format xml via specified version of the watch face xsd file.
*
* @param xmlDocument the watchface layout document
* @param version version of the watch face format
* @return true if valid, else false
* @throws WatchFaceFormatValidationException if the schema for the validation was not
* available.
*/
public boolean validateOrThrow(Document xmlDocument, String version)
throws WatchFaceFormatValidationException {
if (!isSupportedVersion(version)) {
Log.e("Validator not support the version #" + version);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log.e("Validator does not support version #" + version);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return false;
}

try {
validateXMLSchema(
resourceManager.getXsdFile(version).getCanonicalPath(), new DOMSource(xmlDocument));
return true;
} catch (SAXException | IOException e) {
Log.e(e.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log.e("Could not validate xml", e);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return false;
} catch (IllegalArgumentException e) {
throw new WatchFaceFormatValidationException("No schema available: " + e.getMessage(), e);
}
}


private static void validateXMLSchema(String xsdPath, Source xmlSource) throws
IllegalArgumentException, SAXException, IOException, NullPointerException {
IllegalArgumentException, SAXException, IOException, NullPointerException {
// https://stackoverflow.com/questions/20807066/how-to-validate-xml-against-xsd-1-1-in-java
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
Schema schema = factory.newSchema(new File(xsdPath));
Expand Down
Loading