001 /* 002 * Common usable utilities 003 * 004 * Copyright (c) 2006 Petr Hadraba <hadrabap@gmail.com> 005 * 006 * Author: Petr Hadraba 007 * 008 * -- 009 * 010 * XML Utilities 011 */ 012 013 package global.sandbox.xmlutilities.demo; 014 015 import global.sandbox.xmlutilities.XmlTools; 016 import global.sandbox.xmlutilities.XmlUtilitiesException; 017 import javax.xml.transform.Source; 018 import org.w3c.dom.Document; 019 020 public class SchemaValidator { 021 022 public static void validateXml(Source doc, Source schema) throws SchemaValidatorException, ValidationException { 023 String result; 024 025 try { 026 result = XmlTools.validateXmlUsingSchema(doc, schema); 027 } catch (XmlUtilitiesException ex) { 028 throw new SchemaValidatorException(ex); 029 } 030 031 if (result != null) { 032 throw new ValidationException(result); 033 } 034 } 035 036 public static void validateXml(Document doc, Document schema) throws SchemaValidatorException, ValidationException { 037 SchemaValidator.validateXml(XmlTools.documentToDomSource(doc), XmlTools.documentToDomSource(schema)); 038 } 039 040 public static class SchemaValidatorException extends Exception { 041 042 private static final long serialVersionUID = 1L; 043 044 public SchemaValidatorException(Throwable cause) { 045 super(cause); 046 } 047 048 } 049 050 public static class ValidationException extends Exception { 051 052 private static final long serialVersionUID = 1L; 053 054 public ValidationException(String message) { 055 super(message); 056 } 057 058 } 059 060 }