private Optional<Element> findChildById(Element parent, String id) { for (Element child : XML.getChildren(parent)) if (id.equals(child.getAttribute("id"))) return Optional.of(child); return Optional.empty(); }
private List<Node> readNodes(Element providerElement) { Element nodesSpec = XML.getChild(providerElement, "nodes"); if (nodesSpec == null) { return null; } List<Node> nodes = new ArrayList<>(); for (Element nodeSpec : XML.getChildren(nodesSpec, "node")) { nodes.add(readNode(nodeSpec)); } return nodes; }
/** * Returns the Document of the string XML payload */ public static Document getDocument(String xmlString) { return getDocument(new StringReader(xmlString)); }
private Map<String, String> getServletConfig(Element servletElement) { Map<String, String> servletConfig = new HashMap<>(); Element servletConfigElement = XML.getChild(servletElement, "servlet-config"); XML.getChildren(servletConfigElement).forEach( parameter -> servletConfig.put(parameter.getTagName(), XML.getValue(parameter)) ); return servletConfig; } }
public static Collection<String> valuesFromElements(Element parent, String elementName) { List<String> symbols = new ArrayList<>(); for (Element symbol : XML.getChildren(parent, elementName)) { symbols.add(XML.getValue(symbol).trim()); } return symbols; }
private boolean readSourceSet(Element searcherSpec) { return XML.getChild(searcherSpec, "source-set") != null; }
/** * Returns a ValidationOverrides instance with the content of the given XML string. * An empty ValidationOverrides is returned if the argument is empty. * * @param xmlForm the string which optionally contains a validation-overrides XML structure * @return a ValidationOverrides from the argument * @throws IllegalArgumentException if the validation-allows.xml file exists but is invalid */ public static ValidationOverrides fromXml(String xmlForm) { if ( xmlForm.isEmpty()) return ValidationOverrides.empty; try { // Assume valid structure is ensured by schema validation Element root = XML.getDocument(xmlForm).getDocumentElement(); List<ValidationOverrides.Allow> overrides = new ArrayList<>(); for (Element allow : XML.getChildren(root, "allow")) { Instant until = LocalDate.parse(allow.getAttribute("until"), DateTimeFormatter.ISO_DATE) .atStartOfDay().atZone(ZoneOffset.UTC).toInstant() .plus(Duration.ofDays(1)); // Make the override valid *on* the "until" date Optional<ValidationId> validationId = ValidationId.from(XML.getValue(allow)); if (validationId.isPresent()) // skip unknown ids as they may be valid for other model versions overrides.add(new ValidationOverrides.Allow(validationId.get(), until)); } return new ValidationOverrides(overrides, xmlForm); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("validation-overrides is invalid", e); } }
public List<Element> createQueryProfiles(List<NamedReader> queryProfileReaders, QueryProfileRegistry registry) { List<Element> queryProfileElements = new ArrayList<>(queryProfileReaders.size()); for (NamedReader reader : queryProfileReaders) { Element root = XML.getDocument(reader).getDocumentElement(); if ( ! root.getNodeName().equals("query-profile")) { throw new IllegalArgumentException("Root tag in '" + reader.getName() + Element dimensions = XML.getChild(root,"dimensions"); if (dimensions != null) queryProfile.setDimensions(toArray(XML.getValue(dimensions)));
/** * Replaces the following: * <ul> * <li>all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if * escapeLowAscii is <code>true</code> * <li>ampersand (&) * <li>less than (<) * <li>larger than (>) * <li>double quotes (") if isAttribute is <code>true</code> * </ul> * with character entities. * */ public static String xmlEscape(String string, boolean isAttribute, boolean escapeLowAscii, StringBuilder buffer) { return xmlEscape(string, isAttribute, escapeLowAscii, buffer, -1); }
List<Step> steps = new ArrayList<>(); Optional<String> globalServiceId = Optional.empty(); Element root = XML.getDocument(xmlForm).getDocumentElement(); if (validate) validateTagOrder(root); for (Element environmentTag : XML.getChildren(root)) { if ( ! isEnvironmentName(environmentTag.getTagName())) continue; for (Element stepTag : XML.getChildren(environmentTag)) { Optional<AthenzService> athenzService = stringAttribute("athenz-service", environmentTag).map(AthenzService::from); if (stepTag.getTagName().equals("delay")) { } else if (stepTag.getTagName().equals("parallel")) { List<DeclaredZone> zones = new ArrayList<>(); for (Element regionTag : XML.getChildren(stepTag)) { zones.add(readDeclaredZone(environment, athenzService, regionTag));
public List<Element> createQueryProfileTypes(List<NamedReader> queryProfileTypeReaders, QueryProfileTypeRegistry registry) { List<Element> queryProfileTypeElements = new ArrayList<>(queryProfileTypeReaders.size()); for (NamedReader reader : queryProfileTypeReaders) { Element root = XML.getDocument(reader).getDocumentElement(); if ( ! root.getNodeName().equals("query-profile-type")) { throw new IllegalArgumentException("Root tag in '" + reader.getName() + "' must be 'query-profile-type', not '" + root.getNodeName() + "'"); } String idString=root.getAttribute("id"); if (idString == null || idString.equals("")) throw new IllegalArgumentException("'" + reader.getName() + "' has no 'id' attribute in the root element"); ComponentId id = new ComponentId(idString); validateFileNameToId(reader.getName(),id,"query profile type"); QueryProfileType type = new QueryProfileType(id); type.setMatchAsPath(XML.getChild(root,"match") != null); type.setStrict(XML.getChild(root,"strict") != null); registry.register(type); queryProfileTypeElements.add(root); } return queryProfileTypeElements; }
private static String getBinding(Element e) { String binding = XML.getValue(e); if (! binding.endsWith("/")) { log.warning("Adding a trailing '/' to the document-api binding: " + binding + " -> " + binding + "/"); binding = binding + "/"; } return binding; }
/** * Creates a new XML DocumentBuilder * * @return a DocumentBuilder * @throws RuntimeException if we fail to create one */ public static DocumentBuilder getDocumentBuilder() { return getDocumentBuilder("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", null); }
private Map<String,String> readParameters(Element containingElement) { List<Element> parameterElements=XML.getChildren(containingElement,"parameter"); if (parameterElements.size()==0) return Collections.emptyMap(); // Shortcut Map<String,String> parameters=new LinkedHashMap<>(); for (Element parameter : parameterElements) { String key=parameter.getAttribute("name"); String value=XML.getValue(parameter); parameters.put(key,value); } return parameters; }
private static String getCleanValue(Element spec, String name) { Element elem = XML.getChild(spec, name); if (elem == null) { return null; } String value = elem.getFirstChild().getNodeValue(); if (value == null) { return null; } value = value.trim(); return value.isEmpty() ? null : value; }
private void addRoutingAliases(ContainerCluster cluster, Element spec, Environment environment) { if (environment != Environment.prod) return; Element aliases = XML.getChild(spec, "aliases"); for (Element alias : XML.getChildren(aliases, "service-alias")) { cluster.serviceAliases().add(XML.getValue(alias)); } for (Element alias : XML.getChildren(aliases, "endpoint-alias")) { cluster.endpointAliases().add(XML.getValue(alias)); } }
/** * Replaces the following: * <ul> * <li>all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) * <li>ampersand (&) * <li>less than (<) * <li>larger than (>) * <li>double quotes (") if isAttribute is <code>true</code> * </ul> * with character entities. * */ public static String xmlEscape(String string, boolean isAttribute, StringBuilder buffer) { return xmlEscape(string, isAttribute, true, buffer, -1); }
private Object readFieldValue(Element field, String name, String targetDescription, QueryProfileRegistry registry) { Element ref = XML.getChild(field,"ref"); if (ref != null) { String referencedName = XML.getValue(ref); QueryProfile referenced = registry.getComponent(referencedName); if (referenced == null) throw new IllegalArgumentException("Could not find query profile '" + referencedName + "' referenced as '" + name + "' in " + targetDescription); return referenced; } else { return XML.getValue(field); } }
private DeclaredZone readDeclaredZone(Environment environment, Optional<AthenzService> athenzService, Element regionTag) { return new DeclaredZone(environment, Optional.of(RegionName.from(XML.getValue(regionTag).trim())), readActive(regionTag), athenzService); }