Created
January 26, 2020 23:57
-
-
Save EmmanuelOga/91b106b4209ca71f59c15a17176dfc58 to your computer and use it in GitHub Desktop.
RelaxNG validation with error messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sardina; | |
| import com.thaiopensource.util.PropertyMapBuilder; | |
| import com.thaiopensource.validate.Flag; | |
| import com.thaiopensource.validate.ValidateProperty; | |
| import com.thaiopensource.validate.ValidationDriver; | |
| import com.thaiopensource.validate.prop.rng.RngProperty; | |
| import com.thaiopensource.validate.rng.CompactSchemaReader; | |
| import org.xml.sax.ErrorHandler; | |
| import org.xml.sax.InputSource; | |
| import org.xml.sax.SAXException; | |
| import org.xml.sax.SAXParseException; | |
| import java.io.IOException; | |
| import java.net.URI; | |
| import java.net.URISyntaxException; | |
| class MyErrorHandler implements ErrorHandler { | |
| public void warning(SAXParseException e) throws SAXParseException { | |
| printException(e); | |
| } | |
| public void error(SAXParseException e) { | |
| printException(e); | |
| } | |
| public void fatalError(SAXParseException e) throws SAXParseException { | |
| printException(e); | |
| throw e; | |
| } | |
| private void printException(SAXParseException e) { | |
| System.out.println("message: " + e.getMessage()); | |
| System.out.println("systemId: " + e.getSystemId()); | |
| System.out.println("line: " + e.getLineNumber()); | |
| System.out.println("column: " + e.getColumnNumber()); | |
| Exception sub = e.getException(); | |
| if (sub != null) { | |
| System.out.println("exception: " + sub.getMessage()); | |
| } | |
| } | |
| } | |
| public class App { | |
| public static void main(String[] args) throws URISyntaxException, IOException, SAXException { | |
| URI xmlFile = ClassLoader.getSystemClassLoader().getResource("file.xml").toURI(); | |
| PropertyMapBuilder pmb = new PropertyMapBuilder(); | |
| pmb.put(RngProperty.CHECK_ID_IDREF, Flag.PRESENT); | |
| pmb.put(ValidateProperty.ERROR_HANDLER, new MyErrorHandler()); | |
| URI rngFile = ClassLoader.getSystemClassLoader().getResource("topic.rnc").toURI(); | |
| InputSource inputSource = ValidationDriver.uriOrFileInputSource(rngFile.toString()); | |
| inputSource.setEncoding("UTF-8"); | |
| ValidationDriver driver = new ValidationDriver(pmb.toPropertyMap(), CompactSchemaReader.getInstance()); | |
| driver.loadSchema(inputSource); | |
| boolean result = driver.validate(ValidationDriver.uriOrFileInputSource(xmlFile.toString())); | |
| System.out.println(result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment