public static Document buildXmlDocument(InputStream inputStream, URL resource, String xsds) throws Exception { return buildXmlDocument(inputStream, new ValidatingSaxBuilder(resource, xsds)); }
public static Document buildXmlDocument(String xmlContent, URL resource) throws Exception { return buildXmlDocument(new ByteArrayInputStream(xmlContent.getBytes()), new ValidatingSaxBuilder(resource)); }
public void verifyXsdValid(Document document) throws Exception { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); XmlUtils.writeXml(document, buffer); InputStream content = new ByteArrayInputStream(buffer.toByteArray()); buildXmlDocument(content, GoConfigSchema.getCurrentSchema(), registry.xsds()); }
private void validate(String content) { int currentVersion = getCurrentSchemaVersion(content); try { buildXmlDocument(new ByteArrayInputStream(content.getBytes()), GoConfigSchema.getResource(currentVersion), registry.xsds()); } catch (Exception e) { throw bomb("Cruise config file with version " + currentVersion + " is invalid. Unable to upgrade.", e); } }
public CommandSnippet parse(String xmlContent, String fileName, String relativeFilePath) { try { Document document = buildXmlDocument(xmlContent, CommandSnippet.class.getResource("command-snippet.xsd")); CommandSnippetComment comment = getComment(document); Element execTag = document.getRootElement(); String commandName = execTag.getAttributeValue("command"); List<String> arguments = new ArrayList<>(); for (Object child : execTag.getChildren()) { Element element = (Element) child; arguments.add(element.getValue()); } return new CommandSnippet(commandName, arguments, comment, fileName, relativeFilePath); } catch (Exception e) { String errorMessage = String.format("Reason: %s", e.getMessage()); LOGGER.info("Could not load command '{}'. {}", fileName, errorMessage); return CommandSnippet.invalid(fileName, errorMessage, new EmptySnippetComment()); } }
private Element parseInputStream(InputStream inputStream) throws Exception { Element rootElement = buildXmlDocument(inputStream, GoConfigSchema.getCurrentSchema(), registry.xsds()).getRootElement(); validateDom(rootElement, registry); return rootElement; }
@Test public void shouldThrowExceptionWithTranslatedErrorMessage() throws Exception { String xmlContent = "<foo name='invalid'/>"; InputStream inputStream = new ByteArrayInputStream(xmlContent.getBytes()); try { buildXmlDocument(inputStream, GoConfigSchema.getCurrentSchema(), configElementImplementationRegistry.xsds()); fail("Should throw a XsdValidationException"); } catch (Exception e) { assertThat(e, is(instanceOf(XsdValidationException.class))); } }
@Test public void shouldThrowExceptionWhenXmlIsMalformed() throws Exception { expectedException.expect(JDOMParseException.class); expectedException.expectMessage(containsString("Error on line 1: XML document structures must start and end within the same entity")); String xmlContent = "<foo name='invalid'"; buildXmlDocument(xmlContent, GoConfigSchema.getCurrentSchema()); }
@Test public void shouldDisableDocTypeDeclarationsWhenValidatingXmlDocumentsWithExternalXsds() throws Exception { expectDOCTYPEDisallowedException(); buildXmlDocument(new ByteArrayInputStream(xxeFileContent().getBytes()), GoConfigSchema.getCurrentSchema(), configElementImplementationRegistry.xsds()); }
@Test public void shouldDisableDocTypeDeclarationsWhenValidatingXmlDocuments() throws Exception { expectDOCTYPEDisallowedException(); buildXmlDocument(xxeFileContent(), GoConfigSchema.getCurrentSchema()); }