Skip to content

Commit 0ace7ca

Browse files
Introduce a new method in the validation library that would throw in
case the schema was not created.
1 parent e139819 commit 0ace7ca

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

third_party/wff/specification/validator/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
apply plugin: 'java-library'
1717

18-
def baseVersion="1.0.0"
18+
def baseVersion="1.0.1"
1919
def baseArchivesName="wff-validator"
2020

2121
dependencies {
@@ -51,4 +51,4 @@ jar {
5151
java {
5252
sourceCompatibility = JavaVersion.VERSION_17
5353
targetCompatibility = JavaVersion.VERSION_17
54-
}
54+
}

third_party/wff/specification/validator/src/main/java/com/samsung/watchface/WatchFaceXmlValidator.java

+44-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public WatchFaceXmlValidator() {
4343
resourceManager = new ResourceManager();
4444
}
4545

46+
/** Exception thrown when the watch face format validation has failed to execute. */
47+
public static class WatchFaceFormatValidationException extends Exception {
48+
public WatchFaceFormatValidationException(String message, Throwable cause) {
49+
super(message, cause);
50+
}
51+
52+
}
53+
4654
/**
4755
* Check whether the validator support the version or not
4856
*
@@ -75,11 +83,11 @@ public boolean validate(String xmlPath, String version) {
7583
return true;
7684
} catch (SAXParseException e) {
7785
String errorMessage = String.format(
78-
"[Line %d:Column %d]: %s",
79-
e.getLineNumber(),
80-
e.getColumnNumber(),
81-
e.getMessage()
82-
);
86+
"[Line %d:Column %d]: %s",
87+
e.getLineNumber(),
88+
e.getColumnNumber(),
89+
e.getMessage()
90+
);
8391
Log.e(errorMessage);
8492
return false;
8593
} catch (Exception e) {
@@ -100,6 +108,7 @@ public boolean validate(Document xmlDocument, String version) {
100108
if (!isSupportedVersion(version)) {
101109
throw new RuntimeException("Validator not support the version #" + version);
102110
}
111+
103112
validateXMLSchema(
104113
resourceManager.getXsdFile(version).getCanonicalPath(), new DOMSource(xmlDocument));
105114
return true;
@@ -109,8 +118,37 @@ public boolean validate(Document xmlDocument, String version) {
109118
}
110119
}
111120

121+
/**
122+
* Validate watch face format xml via specified version of the watch face xsd file.
123+
*
124+
* @param xmlDocument the watchface layout document
125+
* @param version version of the watch face format
126+
* @return true if valid, else false
127+
* @throws WatchFaceFormatValidationException if the schema for the validation was not
128+
* available.
129+
*/
130+
public boolean validateOrThrow(Document xmlDocument, String version)
131+
throws WatchFaceFormatValidationException {
132+
if (!isSupportedVersion(version)) {
133+
Log.e("Validator not support the version #" + version);
134+
return false;
135+
}
136+
137+
try {
138+
validateXMLSchema(
139+
resourceManager.getXsdFile(version).getCanonicalPath(), new DOMSource(xmlDocument));
140+
return true;
141+
} catch (SAXException | IOException e) {
142+
Log.e(e.getMessage());
143+
return false;
144+
} catch (IllegalArgumentException e) {
145+
throw new WatchFaceFormatValidationException("No schema available: " + e.getMessage(), e);
146+
}
147+
}
148+
149+
112150
private static void validateXMLSchema(String xsdPath, Source xmlSource) throws
113-
IllegalArgumentException, SAXException, IOException, NullPointerException {
151+
IllegalArgumentException, SAXException, IOException, NullPointerException {
114152
// https://stackoverflow.com/questions/20807066/how-to-validate-xml-against-xsd-1-1-in-java
115153
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
116154
Schema schema = factory.newSchema(new File(xsdPath));

0 commit comments

Comments
 (0)