/** * Retrieve among a collection of {@link String} the first one that is not a processing instruction. * * @param lines the lines. * @return the matching line. */ public static String retrieveFirstNonProcessingInstructionLine(final Collection<String> lines) { Validate.notNull(lines); for (String line : lines) { if (line.matches(ProcessingInstructionsConstants.PROCESSING_INSTRUCTION_START)) { continue; } return line; } throw new ApplicationException(XmlErrorType.XML_NO_NON_PI_LINE, "Found no non-processing-instruction line") .addContextValue(XmlErrorContext.LINES, lines); } }
/** * Evaluate the XPath. * * @param <T> the return type. * @return the value. */ public <T> T evaluate() { final XPath xPath = XPathFactoryUtils.newXPath(this.inputSource, variables); try { xPath.compile(this.xpathExpression); } catch (XPathExpressionException e) { throw new ApplicationException(XPathErrorType.ERROR_XPATH_EVALUATION, e) .addContextValue(XPathErrorContext.XPATH, this.xpathExpression); } try { return (T) xPath.evaluate(xpathExpression, this.inputSource, this.returnType); } catch (XPathExpressionException e) { throw new ApplicationException(XPathErrorType.ERROR_XPATH_EVALUATION, e); } }
@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; }
/** * Retrieve a file by name in a folder. * * @param filename the name of the file * @param folderPath the folder * @return the requested file */ public static Path retrieveFileInFolder(final String filename, final Path folderPath) { Validate.notNull(folderPath); Validate.notNull(filename); Path ret = folderPath.resolve(filename); if (Files.notExists(ret) || !Files.isRegularFile(ret)) { throw new ApplicationException(FileErrorType.NO_FILE_FOUND, "No file found in the folder with given name") .addContextValue(FileErrorContext.FOLDER, folderPath) .addContextValue(FileErrorContext.FILENAME, filename); } logger.info("File [{}] found in folder [{}]", ret, folderPath); return ret; }
/** * 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; }
/** * Validate an {@link File XML file} against the schema declared in the XML file. * * <p>The schema must be found in the given schem folder</p> * * @param xmlFile the XML file. * @param schemaFolder the folder where to search for schemas. * @throws ApplicationException if no schema is declared in the XML file. * @see #validateXmlAgainstSchema(File, URL) */ public static void validateXml(final File xmlFile, final File schemaFolder) { final URL schemaUrl = XmlSchemaUtils.extractSchemaUrl(xmlFile, schemaFolder); // schema location is defined in XML ? if (schemaUrl == null) { throw new ApplicationException(XmlErrorType.NO_SCHEMA_FOUND, "Cannot find internal schema URL") .addContextValue(XmlErrorContext.XML_FILE, xmlFile); } validateXmlAgainstSchema(xmlFile, schemaUrl); logger.info("XML file [{}] is valid against its schema [{}]", xmlFile, schemaUrl); }