@@ -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 ) {
@@ -98,8 +106,9 @@ public boolean validate(String xmlPath, String version) {
98106 public boolean validate (Document xmlDocument , String version ) {
99107 try {
100108 if (!isSupportedVersion (version )) {
101- throw new RuntimeException ("Validator not support the version #" + version );
109+ throw new RuntimeException ("Validator does 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 does 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 | NullPointerException e ) {
142+ Log .e ("Could not validate xml: " + e .getMessage ());
143+ return false ;
144+ } catch (IllegalArgumentException e ) {
145+ throw new WatchFaceFormatValidationException ("No schema available" , 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