@@ -43,6 +43,14 @@ public WatchFaceXmlValidator() {
43
43
resourceManager = new ResourceManager ();
44
44
}
45
45
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
+
46
54
/**
47
55
* Check whether the validator support the version or not
48
56
*
@@ -75,11 +83,11 @@ public boolean validate(String xmlPath, String version) {
75
83
return true ;
76
84
} catch (SAXParseException e ) {
77
85
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
+ );
83
91
Log .e (errorMessage );
84
92
return false ;
85
93
} catch (Exception e ) {
@@ -100,6 +108,7 @@ public boolean validate(Document xmlDocument, String version) {
100
108
if (!isSupportedVersion (version )) {
101
109
throw new RuntimeException ("Validator not support the version #" + version );
102
110
}
111
+
103
112
validateXMLSchema (
104
113
resourceManager .getXsdFile (version ).getCanonicalPath (), new DOMSource (xmlDocument ));
105
114
return true ;
@@ -109,8 +118,37 @@ public boolean validate(Document xmlDocument, String version) {
109
118
}
110
119
}
111
120
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
+
112
150
private static void validateXMLSchema (String xsdPath , Source xmlSource ) throws
113
- IllegalArgumentException , SAXException , IOException , NullPointerException {
151
+ IllegalArgumentException , SAXException , IOException , NullPointerException {
114
152
// https://stackoverflow.com/questions/20807066/how-to-validate-xml-against-xsd-1-1-in-java
115
153
SchemaFactory factory = SchemaFactory .newInstance ("http://www.w3.org/XML/XMLSchema/v1.1" );
116
154
Schema schema = factory .newSchema (new File (xsdPath ));
0 commit comments