/** * Copy a source file to a target file. * * @param file the source file. * @param outputFile the target file. * @return the target file. */ public static File copyFile(final File file, final File outputFile) { try { FileUtils.copyFile(file, outputFile); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, e) .addContextValue(FileErrorContext.FILE, file) .addContextValue(FileErrorContext.FILE, outputFile); } return outputFile; }
/** * 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 is a {@link Path file} is empty. * * @param filePath the path to a file. * @return {@code true} if the file is empty; {@code false} otherwise. */ public static boolean isFileEmpty(final Path filePath) { try { return Files.size(filePath) == 0; } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, "Cannot read file", e) .addContextValue(FileErrorContext.FILE, filePath); } }
@Override public Object resolveVariable(final QName qName) { final String variableName = qName.getLocalPart(); if (!variables.containsKey(variableName)) { throw new ApplicationException(XPathErrorType.ERROR_XPATH_EVALUATION, "Cannot resolve variable for XPath evaluation") .addContextValue(XPathErrorContext.VARIABLE_NAME, variableName) .addContextValue(XPathErrorContext.LIST, ExceptionMessageUtils.printMap(variables, String::toString, String::toString)); } return variables.get(qName.getLocalPart()); } });
/** * Retrieve a file by name in a folder. * * @param name the name of the file * @param folder the folder * @return the requested file */ public static File retrieveFileInFolder(final String name, final File folder) { Validate.notNull(folder); Validate.notNull(name); File ret = new File(folder, name); if (!ret.exists() || !ret.isFile()) { throw new ApplicationException(FileErrorType.NO_FILE_FOUND, "No file found in the folder with given name") .addContextValue(FileErrorContext.FOLDER, folder) .addContextValue(FileErrorContext.FILENAME, name); } logger.info("File [{}] found in folder [{}]", ret, folder); return ret; }
/** * Build a new {@link InputSource} from a byte array. * * @param content the byte array * @return the input source */ public static InputSource newInputSource(final byte[] content) { try (InputStream is = new ByteArrayInputStream(content)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * 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); } }
/** * Copy a source file to a target file. * * @param filePath the path to the source file. * @param outputFilePath the path to the target file. * @return the path to the target file. */ public static Path copyFile(final Path filePath, final Path outputFilePath) { final Path ret; try { ret = Files.copy(filePath, outputFilePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_COPY_FILE, e) .addContextValue(FileErrorContext.FILE, filePath) .addContextValue(FileErrorContext.FILE, outputFilePath); } return ret; }
/** * 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); } }
public static byte[] readFiletoByteArray(final File file) { Validate.notNull(file); try { return FileUtils.readFileToByteArray(file); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, file); } } }
/** * Build a new {@link InputSource} from a {@link String}. * * @param content the content as {@link String} * @return the input source */ public static InputSource newInputSource(final String content) { try (InputStream is = IOUtils.toInputStream(content, StandardCharsets.UTF_8)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * Parse an XML file using a SAX parser, with a given SAX handler. * * @param xmlFile the XML file. * @param saxHandler the SAX handler. */ public static void parseXmlFile(final File xmlFile, final DefaultHandler saxHandler) { try { SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); saxParser.parse(xmlFile, saxHandler); } catch (ApplicationException e) { throw e; } catch (Exception e) { throw new SystemException(XmlErrorType.ERROR_PARSING_XML, "Cannot parse xml file with sax parser", e) .addContextValue(XmlErrorContext.XML_FILE, xmlFile) .addContextValue("saxHandler", saxHandler); } } }
/** * 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); } }
/** * Open an {@link InputStream} on a file {@link File}. * * @param file the file. * @return the input stream. */ public static InputStream openInputStream(final File file) { try { return new ByteArrayInputStream(FileUtils.readFileToByteArray(file)); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_OPENING_STREAM, e) .addContextValue(FileErrorContext.FILE, file); } }
/** * Provide {@code LocalDateTime.now()} as an {@link XMLGregorianCalendar}. * * @return the current moment as {@link XMLGregorianCalendar}. */ public static XMLGregorianCalendar nowAsGregorian() { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(LocalDateTime.now().toString()); } catch (DatatypeConfigurationException e) { throw new SystemException(XmlErrorType.ISSUE_WITH_DATE_XML, e); } }
/** * 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); } }
/** * Build a new {@link InputSource} from a {@link File}. * * @param file the file * @return the input source */ public static InputSource newInputSource(final File file) { try (InputStream is = FileIOUtils.openInputStream(file)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * 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); } } }