/** * 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); } }
/** * 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); } }
/** * 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); } }
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 collection of{@link String}. * * @param content the content as a collection {@link String} * @return the input source */ public static InputSource newInputSource(final Collection<String> content) { try (InputStream is = IOUtils.toInputStream(StringUtils.join(content, " "), StandardCharsets.UTF_8)) { return new InputSource(is); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_BUILDING_INPUT_SOURCE, e); } }
/** * 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); } }
private void init() { this.prefixNamespaceMap = XmlNamespaceUtils.extractNamespaces(this.inputSource); this.prefixNamespaceMap.put("fn", "http://www.w3.org/2005/xpath-functions"); this.prefixNamespaceMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); this.prefixNamespaceMap.put("op", "http://www.w3.org/2002/08/xquery-operators"); try { inputSource.getByteStream().reset(); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_RESETTING_INPUT_SOURCE, e); } }
/** * Read the content of a {@link File} as a collection of {@link String} lines. * * @param file the file. * @return the collection if {@link String} lines. */ public static Collection<String> readLines(final File file) { Validate.notNull(file); try { return FileUtils.readLines(file, StandardCharsets.UTF_8); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, 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 if a file is empty. * * @param file the file * @return {@code true} if the file is empty; {@code false} otherwise. */ public static boolean isFileEmpty(final File file) { try { final List<String> lines = FileUtils.readLines(file, StandardCharsets.UTF_8); return lines.isEmpty(); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, "Cannot read file", e) .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); } }
/** * Read the content of a {@link Path file} as a collection {@link String lines}. * * @param filePath the path to a file. * @return the collection of lines. */ public static Collection<String> readLines(final Path filePath) { Validate.notNull(filePath); try { return Files.readAllLines(filePath, StandardCharsets.UTF_8); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FILE, filePath); } } }
/** * Convert a {@link LocalDateTime} to {@link XMLGregorianCalendar}. * * @param localDateTime the date time to convert. * @return the date time converted as XML Gregorian date. */ public static XMLGregorianCalendar toXmlGregorian(final LocalDateTime localDateTime) { Validate.notNull(localDateTime); try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDateTime.toString()); } catch (DatatypeConfigurationException e) { throw new SystemException(XmlErrorType.ISSUE_WITH_DATE_XML, e); } }
/** * Check if the content of a folder is empty (i.e. doesn't contain any folder nor file) * * @param folderPath the folder * @return true if the folder is empty, false otherwise */ public static boolean isFolderEmpty(final Path folderPath) { Validate.notNull(folderPath); try { return Files.list(folderPath).count() <= 0; } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_READING_FILE, e) .addContextValue(FileErrorContext.FOLDER, folderPath); } }
/** * Build the {@link Document XML document}. * * @return the built XML document. */ public Document build() { final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(this.namespaceAware); dbf.setValidating(this.validationEnabled); final DocumentBuilder documentBuilder; try { documentBuilder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new SystemException(XmlErrorType.DOCUMENT_BUILDER_INITIALIZATION_ISSUE, e); } documentBuilder.setErrorHandler(this.errorHandler); final Document ret; try { ret = documentBuilder.parse(this.inputSource); } catch (SAXException e) { throw new ApplicationException(XmlErrorType.ERROR_PARSING_XML, e); } catch (IOException e) { throw new SystemException(XmlErrorType.ERROR_PARSING_XML, e); } return ret; }
/** * Clean a folder from its content (file and sub-folders). * * @param folder the folder. */ public static void cleanDirectory(final File folder) { if (!folder.exists()) { logger.info("Folder [{}] does not exist. No need to clean.", folder); return; } try { FileUtils.cleanDirectory(folder); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_CLEANING_FOLDER, e) .addContextValue(FileErrorContext.FOLDER, folder); } }
/** * Extract the namespaces defined in an XML document. * * @param inputSource the XML document (as an {@link InputSource}). * @return the map of prefixes/namespaces. */ public static Map<String, String> extractNamespaces(final InputSource inputSource) { Map<String, String> ret = new HashMap<>(); try (InputStream is = inputSource.getByteStream()) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader reader = inputFactory.createXMLStreamReader(is, StandardCharsets.UTF_8.displayName()); while (reader.hasNext()) { int evt = reader.next(); if (evt == XMLStreamConstants.START_ELEMENT) { QName qName = reader.getName(); if (qName != null) { ret.put(qName.getPrefix(), qName.getNamespaceURI()); } } } ret.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); } catch (XMLStreamException | IOException e) { throw new SystemException(XmlErrorType.ERROR_PARSING_XML, e); } return ret; }
/** * Return or create a folder. * * @param folder the folder * @return the already existing / created folder */ public static File provideFolder(final File folder) { Validate.notNull(folder); if (!folder.exists()) { try { FileUtils.forceMkdir(folder); logger.info("Folder created [{}]", folder); } catch (IOException e) { throw new SystemException(FileErrorType.ERROR_CREATING_FOLDER, e) .addContextValue(FileErrorContext.FOLDER, folder); } } return folder; }
/** * Add namespaces to a {@link InputSource XML document}. * * @param inputSource the input source for the XML document. * @param namespaces the map of namespaces prefixes and URIs. * @return the modified document. */ public static Document addNamespacesToRootNode(final InputSource inputSource, final Map<String, String> namespaces) { final Document ret; try { ret = XmlDocumentBuilder.of(inputSource).build(); Element element = ret.getDocumentElement(); ret.setStrictErrorChecking(false); for (Map.Entry<String, String> elm : namespaces.entrySet()) { if (StringUtils.isNotBlank(elm.getValue())) { element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + elm.getKey(), elm.getValue()); } } } catch (final Exception exc) { throw new SystemException(XmlErrorType.ERROR_PARSING_XML, exc); } return ret; } }
/** * 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; }