/** * Removes characters that are illegal in XML text nodes or attribute * values. * * @param text * the String that should be used in XML elements or attributes * @return the String with all illegal characters removed */ public static String removeIllegalChars(String text) { if (text == null || text.trim().length() == 0) { return text; } if (org.jdom2.Verifier.checkCharacterData(text) == null) { return text; } // It seems we have to filter out invalid XML characters... StringBuilder sb = new StringBuilder(); for (int i = 0; i < text.length(); i++) { if (Verifier.isXMLCharacter(text.charAt(i))) { sb.append(text.charAt(i)); } } return sb.toString(); }
private void removeIllegalVariables() { for (Iterator<Entry<String, Object>> entries = variables.entrySet().iterator(); entries.hasNext();) { String name = entries.next().getKey(); String result = Verifier.checkXMLName(name); if (result != null) { LOGGER.warn("Illegally named transformation parameter, removing {}", name); entries.remove(); } } }
public void validateRepoName(String repoKey) throws RepoConfigException { if (StringUtils.isBlank(repoKey)) { throw new RepoConfigException("Repository key cannot be empty", SC_BAD_REQUEST); } if (StringUtils.length(repoKey) > REPO_KEY_MAX_LENGTH) { throw new RepoConfigException("Repository key '" + repoKey + "' exceed maximum length", SC_BAD_REQUEST); } if (UiRequestUtils.isReservedName(repoKey) || VirtualRepoDescriptor.GLOBAL_VIRTUAL_REPO_KEY.equalsIgnoreCase(repoKey)) { throw new RepoConfigException("Repository key '" + repoKey + "' is a reserved name", SC_BAD_REQUEST); } if (repoKey.equals(".") || repoKey.equals("..") || repoKey.equals("&")) { throw new RepoConfigException("Invalid Repository name", SC_BAD_REQUEST); } // TODO: [by dan] make this stream-ey char[] nameChars = repoKey.toCharArray(); for (char c : nameChars) { for (char fc : forbiddenChars) { if (c == fc) { throw new RepoConfigException("Illegal character: '" + c + "'", SC_BAD_REQUEST); } } } String error = Verifier.checkXMLName(repoKey); if (StringUtils.isNotBlank(error)) { throw new RepoConfigException("Invalid Repository name: " + error, SC_BAD_REQUEST); } if (!centralConfig.getMutableDescriptor().isKeyAvailable(repoKey)) { throw new RepoConfigException("Repository name is already in use", SC_BAD_REQUEST); } }
@Override public void execute(ArtifactoryRestRequest request, RestResponse response) { String name = request.getQueryParamByKey("xmlName"); String result = Verifier.checkXMLName(name); if (result != null) { response.error("Invalid XML name"); } else { // successful validation response.info("XML name validated"); } }
@Override protected void onValidate(IValidatable validatable) { String value = (String) validatable.getValue(); String result = Verifier.checkXMLName(value); if (result != null) { ValidationError error = new ValidationError(); String message = errorMessage == null ? result : String.format(errorMessage, value); error.setMessage(message); validatable.error(error); } }