private String extractFilePath(Node fileNode) { String password = nullSafeGetStringAttribute(fileNode, "password"); String value = nullSafeGetStringAttribute(fileNode, "value"); return value + (password == null ? "" : PdfFileSourceAdapter.PASSWORD_SEPARATOR_CHARACTER + password); }
/** * @param node * @param attributeName * @return the boolean value of the given attribute. false if the attribute is not found. */ public static boolean nullSafeGetBooleanAttribute(Node node, String attributeName) { return nullSafeGetBooleanAttribute(node, attributeName, false); }
protected List<String> doParseFileNames(File file) throws IOException, SAXException, ParserConfigurationException, XPathException { DocumentBuilderFactory domFactory = XMLUtils.getDocumentBuilderFactory(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); builder.setErrorHandler(XMLUtils.ERROR_HANDLER); Document doc = builder.parse(file); List<String> result = new ArrayList<>(); result.addAll(parseSingleFiles(doc)); result.addAll(parseFileSets(doc, file)); return result; }
private void initializeFromInputStream(InputStream input) throws ConfigurationException { try { DocumentBuilderFactory factory = XMLUtils.getDocumentBuilderFactory(); initializeSchemaValidation(factory); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(XMLUtils.ERROR_HANDLER); Document document = builder.parse(input); notificationStrategy = getNotificationStrategy(document); tasks = getTasksMap(document); validation = getValidation(document); ignoreXmlConfig = getIgnoreXmlConfig(document); } catch (IOException | SAXException e) { throw new ConfigurationException(e); } catch (ParserConfigurationException | XPathExpressionException e) { throw new ConfigurationException("Unable to create DocumentBuilder.", e); } }
/** * @param node * @param attributeName * @param defaultValue * @return the boolean value of the given attribute. the defaultValue if the attribute is not found. */ public static boolean nullSafeGetBooleanAttribute(Node node, String attributeName, boolean defaultValue) { String value = nullSafeGetStringAttribute(node, attributeName); if (StringUtils.isNotBlank(value)) { return Boolean.parseBoolean(value); } return defaultValue; }
/** * @param node * @param attributeName * @return the boolean value of the given attribute. false if the attribute is not found. */ public static boolean nullSafeGetBooleanAttribute(Node node, String attributeName) { return nullSafeGetBooleanAttribute(node, attributeName, false); }
private void initializeFromInputStream(InputStream input) throws ConfigurationException { try { DocumentBuilderFactory factory = XMLUtils.getDocumentBuilderFactory(); initializeSchemaValidation(factory); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(XMLUtils.ERROR_HANDLER); Document document = builder.parse(input); notificationStrategy = getNotificationStrategy(document); tasks = getTasksMap(document); validation = getValidation(document); ignoreXmlConfig = getIgnoreXmlConfig(document); } catch (IOException | SAXException e) { throw new ConfigurationException(e); } catch (ParserConfigurationException | XPathExpressionException e) { throw new ConfigurationException("Unable to create DocumentBuilder.", e); } }
/** * @param node * @param attributeName * @param defaultValue * @return the boolean value of the given attribute. the defaultValue if the attribute is not found. */ public static boolean nullSafeGetBooleanAttribute(Node node, String attributeName, boolean defaultValue) { String value = nullSafeGetStringAttribute(node, attributeName); if (StringUtils.isNotBlank(value)) { return Boolean.parseBoolean(value); } return defaultValue; }
private boolean getValidation(Document document) throws XPathExpressionException { Node node = (Node) xpathFactory.newXPath().evaluate(ROOT_NODE, document, XPathConstants.NODE); return nullSafeGetBooleanAttribute(node, VALIDATION_ATTRIBUTENAME); }
/** * Parse fileset definitions <filelist><fileset>[...]</fileset></filelist> ignoring the rest of the document * * @param doc * @return a list of string matching the contents of the <filelist><fileset> tags in the document * @throws XPathExpressionException */ private List<String> parseFileSets(Document doc, File configFile) throws XPathExpressionException { List<String> result = new ArrayList<>(); NodeList nodeList = getNodeListMatchingXpath("//filelist/fileset/file", doc); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); Node fileSet = node.getParentNode(); String parentDirPath = nullSafeGetStringAttribute(fileSet, "dir"); if (parentDirPath == null) { parentDirPath = configFile.getAbsoluteFile().getParent(); } String filePath = extractFilePath(node); // warn if file in fileset is using absolute path mode if (FilenameUtils.getPrefixLength(filePath) > 0) { LOG.warn("File " + filePath + " in fileset " + StringUtils.defaultIfBlank(nullSafeGetStringAttribute(fileSet, "dir"), "") + " seems to be an absolute path. Will _not_ be resolved relative to the <fileset>, but as an absolute path. Normally you would want to use relative paths in a //filelist/fileset/file, and absolute paths in a //filelist/file."); } result.add(FilenameUtils.concat(parentDirPath, filePath)); } return result; }
private boolean getIgnoreXmlConfig(Document document) throws XPathExpressionException { Node node = (Node) xpathFactory.newXPath().evaluate(ROOT_NODE, document, XPathConstants.NODE); return nullSafeGetBooleanAttribute(node, IGNORE_XML_CONFIG_VALIDATION_ATTRIBUTENAME, true); }
/** * Retrieves the value of the input xpath in the given node, creates a Class object and performs a check to ensure that the input assignableInterface is assignable by the * created Class object. * * @param <T> * * @param node * @param attributeName * @param assignableInterface * @return the retrieved class. * @throws ConfigurationException */ private <T> Class<? extends T> getClassFromNode(Node node, String attributeName, Class<T> assignableInterface) throws ConfigurationException { String attributeValue = nullSafeGetStringAttribute(node, attributeName); if (isNotBlank(attributeValue)) { Class<?> clazz; try { clazz = Class.forName(attributeValue.trim()); } catch (ClassNotFoundException e) { throw new ConfigurationException(String.format("Unable to find the configured %s", attributeValue), e); } if (assignableInterface.isAssignableFrom(clazz)) { return clazz.asSubclass(assignableInterface); } throw new ConfigurationException( String.format("The configured %s is not a subtype of %s", clazz, assignableInterface)); } throw new ConfigurationException(String.format("Missing %s configuration parameter.", attributeName)); }
private boolean getIgnoreXmlConfig(Document document) throws XPathExpressionException { Node node = (Node) xpathFactory.newXPath().evaluate(ROOT_NODE, document, XPathConstants.NODE); return nullSafeGetBooleanAttribute(node, IGNORE_XML_CONFIG_VALIDATION_ATTRIBUTENAME, true); }
/** * Retrieves the value of the input xpath in the given node, creates a Class object and performs a check to ensure that the input assignableInterface is assignable by the * created Class object. * * @param <T> * * @param node * @param attributeName * @param assignableInterface * @return the retrieved class. * @throws ConfigurationException */ private <T> Class<? extends T> getClassFromNode(Node node, String attributeName, Class<T> assignableInterface) throws ConfigurationException { String attributeValue = nullSafeGetStringAttribute(node, attributeName); if (isNotBlank(attributeValue)) { Class<?> clazz; try { clazz = Class.forName(attributeValue.trim()); } catch (ClassNotFoundException e) { throw new ConfigurationException(String.format("Unable to find the configured %s", attributeValue), e); } if (assignableInterface.isAssignableFrom(clazz)) { return clazz.asSubclass(assignableInterface); } throw new ConfigurationException( String.format("The configured %s is not a subtype of %s", clazz, assignableInterface)); } throw new ConfigurationException(String.format("Missing %s configuration parameter.", attributeName)); }
private boolean getValidation(Document document) throws XPathExpressionException { Node node = (Node) xpathFactory.newXPath().evaluate(ROOT_NODE, document, XPathConstants.NODE); return nullSafeGetBooleanAttribute(node, VALIDATION_ATTRIBUTENAME); }
@Test public void nullSafeGetStringAttribute() throws XPathExpressionException { Node node = (Node) XPathFactory.newInstance().newXPath().evaluate("/test", doc, XPathConstants.NODE); assertEquals("stringValue", XMLUtils.nullSafeGetStringAttribute(node, "stringAttr")); assertNull(XMLUtils.nullSafeGetStringAttribute(node, "chuckNorris")); Node node2 = (Node) XPathFactory.newInstance().newXPath().evaluate("/chuckNorris", doc, XPathConstants.NODE); assertNull(node2); assertNull(XMLUtils.nullSafeGetStringAttribute(node2, "kick")); }
/** * Given a document, search for the notification strategy configuration and returns the configured strategy or the default one if nothing is configured. * * @param document * @return the class extending {@link NotificationStrategy} configured. * @throws XPathExpressionException */ private Class<? extends NotificationStrategy> getNotificationStrategy(Document document) throws XPathExpressionException { Node node = (Node) xpathFactory.newXPath().evaluate(ROOT_NODE + NOTIFICATION_XPATH, document, XPathConstants.NODE); if (nullSafeGetBooleanAttribute(node, NOTIFICATION_ASYNC_ATTRIBUTENAME)) { return AsyncNotificationStrategy.class; } return SyncNotificationStrategy.class; }
/** * Given a document, search for the notification strategy configuration and returns the configured strategy or the default one if nothing is configured. * * @param document * @return the class extending {@link NotificationStrategy} configured. * @throws XPathExpressionException */ private Class<? extends NotificationStrategy> getNotificationStrategy(Document document) throws XPathExpressionException { Node node = (Node) xpathFactory.newXPath().evaluate(ROOT_NODE + NOTIFICATION_XPATH, document, XPathConstants.NODE); if (nullSafeGetBooleanAttribute(node, NOTIFICATION_ASYNC_ATTRIBUTENAME)) { return AsyncNotificationStrategy.class; } return SyncNotificationStrategy.class; }
@Test public void nullSafeGetBooleanAttribute() throws XPathExpressionException { Node node = (Node) XPathFactory.newInstance().newXPath().evaluate("/test", doc, XPathConstants.NODE); assertTrue(XMLUtils.nullSafeGetBooleanAttribute(node, "boolVal")); assertFalse(XMLUtils.nullSafeGetBooleanAttribute(node, "chuckNorris")); }
@Test public void nullSafeGetBooleanAttributeWithDefault() throws XPathExpressionException { Node node = (Node) XPathFactory.newInstance().newXPath().evaluate("/test", doc, XPathConstants.NODE); assertTrue(XMLUtils.nullSafeGetBooleanAttribute(node, "chuckNorris", true)); } }