/** * Check that a {@link File file/folder} is writable. * * @param file the file/folder. */ public static void validateWritable(final File file) { if (!file.canWrite()) { throw new ValidationException(FileErrorType.NOT_WRITABLE) .addContextValue(FileErrorContext.FILE, file); } }
/** * Check that an {@link InputSource XML document} is well-formed. * * @param inputSource the input source for the XMl document. * @throws ValidationException if the XML document is not well-formed. */ public static void checkXmlIsWellFormed(final InputSource inputSource) { try { XmlDocumentBuilder.of(inputSource) .withNamespaceAwareness(false) .withValidationEnabled(false) .build(); } catch (ApplicationException e) { throw new ValidationException(XmlErrorType.XML_NOT_WELL_FORMED, "xml is not well-formed", e); } }
/** * Check that a {@link File file/folder} is readable. * * @param file the file/folder. */ public static void validateReadable(final File file) { if (!file.canRead()) { throw new ValidationException(FileErrorType.NOT_READABLE) .addContextValue(FileErrorContext.FILE, file); } }
/** * Check that a {@link File folder} is actually a folder. * * @param folder the folder. */ public static void validateFolder(final File folder) { if (!folder.isDirectory()) { throw new ValidationException(FileErrorType.NOT_A_FOLDER) .addContextValue(FileErrorContext.FOLDER, folder); } }
/** * Check that a {@link File file} is actually a file. * * @param file file */ public static void validateFile(final File file) { if (!file.isFile()) { throw new ValidationException(FileErrorType.NOT_A_FILE) .addContextValue(FileErrorContext.FILE, file); } }
/** * Check that a {@link File file/folder} is executable. * * @param file the file/folder. */ public static void validateExecutable(final File file) { if (!file.canWrite()) { throw new ValidationException(FileErrorType.NOT_EXECUTABLE) .addContextValue(FileErrorContext.FILE, file); } } }
public static void validateFolder(final Path folderPath) { if (!Files.isDirectory(folderPath)) { throw new ValidationException(FileErrorType.NOT_A_FOLDER) .addContextValue(FileErrorContext.FOLDER, folderPath); } }
public static void validateFile(final Path filePath) { if (!Files.isRegularFile(filePath)) { throw new ValidationException(FileErrorType.NOT_A_FILE) .addContextValue(FileErrorContext.FILE, filePath); } } }
/** * Validate a given XPath expression. * * @param xpathExpression the XPath expression. */ public static void validateXpathExpression(final String xpathExpression) { try { final XPath xpath = XPathFactory.newInstance().newXPath(); xpath.compile(xpathExpression); } catch (XPathExpressionException e) { throw new ValidationException(XPathErrorType.XPATH_INVALID, "Xpath is not valid.", e) .addContextValue(XPathErrorContext.XPATH, xpathExpression); } }
/** * Validate an {@link File XML file} against a given schema. * * @param xmlFile the XML file. * @param schemaUrl the URL of the remote schema. */ public static void validateXmlAgainstSchema(final File xmlFile, final URL schemaUrl) { Validate.notNull(xmlFile); Validate.notNull(schemaUrl); try (InputStream xmlIs = FileUtils.openInputStream(xmlFile)) { final SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory.newSchema(schemaUrl); final Validator validator = schema.newValidator(); validator.validate(new StreamSource(xmlIs)); } catch (SAXException e) { throw new ValidationException(XmlErrorType.XML_INVALID_AGAINST_SCHEMA, "xml invalid against its schema", e) .addContextValue(XmlErrorContext.XML_FILE, xmlFile); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, xmlFile); } }