Example usage for org.jdom2 Element setText

List of usage examples for org.jdom2 Element setText

Introduction

In this page you can find the example usage for org.jdom2 Element setText.

Prototype

public Element setText(final String text) 

Source Link

Document

Sets the content of the element to be the text given.

Usage

From source file:org.artifactory.update.security.v2.RepoPathAclConverter.java

License:Open Source License

private void convertIdentifierToPermissionTarget(Element acl) {
    String identifier;//from   ww  w .j a  va  2 s  . c om
    try {
        identifier = URLDecoder.decode(acl.getChildText(IDENTIFIER), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Failed to decode identifier", e);
    }
    RepoPath repoPath = InternalRepoPathFactory.fromId(identifier);
    acl.removeChild(IDENTIFIER);
    Element permissionTarget = new Element("permissionTarget");

    Element nameEl = new Element("name");
    if (repoPath.getRepoKey().equalsIgnoreCase(PermissionTargetInfo.ANY_REPO)
            && repoPath.getPath().equalsIgnoreCase(PermissionTargetInfo.ANY_REPO)) {
        nameEl.setText(PermissionTargetInfo.ANY_PERMISSION_TARGET_NAME);
    } else {
        nameEl.setText(repoPath.getId());
    }
    permissionTarget.addContent(nameEl);

    Element repoKeyEl = new Element("repoKey");
    repoKeyEl.setText(repoPath.getRepoKey());
    permissionTarget.addContent(repoKeyEl);

    Element includesEl = new Element("includes");
    Element includeEl = new Element("string");
    if (repoPath.getPath().equalsIgnoreCase(PermissionTargetInfo.ANY_REPO)) {
        includeEl.setText(PermissionTargetInfo.ANY_PATH);
    } else {
        includeEl.setText(repoPath.getPath() + "/" + PermissionTargetInfo.ANY_PATH);
    }
    includesEl.addContent(includeEl);
    permissionTarget.addContent(includesEl);
    permissionTarget.addContent(new Element("excludes"));

    acl.addContent(permissionTarget);
}

From source file:org.artifactory.update.security.v3.AnyRemoteConverter.java

License:Open Source License

private Element newElement(String name, String value) {
    Element element = new Element(name);
    element.setText(value);
    return element;
}

From source file:org.artifactory.update.security.v6.LdapGroupSettingXmlConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element root = doc.getRootElement();
    Namespace namespace = root.getNamespace();
    Element child = root.getChild("users", namespace);
    List users = child.getChildren("user", namespace);
    if (users != null && !users.isEmpty()) {
        for (Object user : users) {
            Element userElement = (Element) user;
            Element groups = userElement.getChild("groups", namespace);
            if (groups != null) {
                List groupNames = groups.getChildren("string", namespace);
                List<String> listOfGroupNames = new ArrayList<>();
                for (Object groupName : groupNames) {
                    Element group = (Element) groupName;
                    listOfGroupNames.add(group.getText());
                }/*from w  w w  .j  a  v  a  2  s. c om*/
                groups.removeChildren("string", namespace);
                for (String name : listOfGroupNames) {
                    Element userGroup = new Element("userGroup", namespace);
                    userGroup.setText(name);
                    groups.addContent(userGroup);
                }
            }
        }
    }
}

From source file:org.artifactory.update.security.v6.LowercaseUsernameXmlConverter.java

License:Open Source License

private void mergeUsers(Element root, Namespace namespace) {
    Element child = root.getChild("users", namespace);
    List users = child.getChildren("user", namespace);
    Map<String, Element> usernameToGroups = Maps.newHashMap();
    if (users != null && !users.isEmpty()) {
        Iterator iterator = users.iterator();
        while (iterator.hasNext()) {
            Element userElement = (Element) iterator.next();
            Element userNameElement = userElement.getChild("username", namespace);
            String userName = userNameElement.getText();
            String lowerCaseUsername = userName.toLowerCase();
            userNameElement.setText(lowerCaseUsername);
            if (!usernameToGroups.containsKey(lowerCaseUsername)) {
                usernameToGroups.put(lowerCaseUsername, userElement);
                addGroupsToUser(userElement, userElement, namespace);
                copyEmails(namespace, usernameToGroups, userElement, lowerCaseUsername);
            } else {
                String isAdmin = userElement.getChild("admin").getText();
                Element existingUserElement = usernameToGroups.get(lowerCaseUsername);
                addGroupsToUser(existingUserElement, userElement, namespace);
                if (Boolean.parseBoolean(isAdmin)) {
                    usernameToGroups.put(lowerCaseUsername, userElement);
                }//w ww  . j  ava2 s .  c o  m
                copyEmails(namespace, usernameToGroups, existingUserElement, lowerCaseUsername);
                addGroupsToUser(userElement, existingUserElement, namespace);
                iterator.remove();
            }
        }
    }
    root.removeChildren("users", namespace);
    Element usersElement = new Element("users", namespace);
    root.addContent(usersElement);
    for (Map.Entry<String, Element> elementEntry : usernameToGroups.entrySet()) {
        Element newUser = elementEntry.getValue();
        Element userElement = new Element("user", namespace);
        userElement.setContent(newUser.cloneContent());
        usersElement.addContent(userElement);
    }
}

From source file:org.artifactory.update.security.v6.LowercaseUsernameXmlConverter.java

License:Open Source License

private void copyEmails(Namespace namespace, Map<String, Element> usernameToGroups, Element userElement,
        String lowerCaseUsername) {
    Element userElementFromMap = usernameToGroups.get(lowerCaseUsername);
    Element emailFromXml = userElementFromMap.getChild("email");
    if (emailFromXml == null) {
        Element emailElement = userElement.getChild("email");
        if (emailElement != null) {
            String email = emailElement.getText();
            if (StringUtils.isNotBlank(email)) {
                Element newEmailElement = new Element("email", namespace);
                newEmailElement.setText(email);
                userElementFromMap.addContent(newEmailElement);
            }//w w  w  .  j  a  v a  2  s  .  com
        }
    }
}

From source file:org.artifactory.update.security.v6.LowercaseUsernameXmlConverter.java

License:Open Source License

private Map<String, Element> mergeGroups(Element root, Namespace namespace) {
    Map<String, Element> foundGroupNames = Maps.newHashMap();
    Element groupsChildren = root.getChild("groups", namespace);
    if (groupsChildren != null) {
        List groups = groupsChildren.getChildren("group", namespace);
        if (groups != null && !groups.isEmpty()) {
            Iterator groupsIterator = groups.iterator();
            while (groupsIterator.hasNext()) {
                Element groupElement = (Element) groupsIterator.next();
                Element groupNameElement = groupElement.getChild("groupName");
                String groupName = groupNameElement.getText();
                String lowerCaseGroupName = groupName.toLowerCase();
                if (!foundGroupNames.containsKey(lowerCaseGroupName)) {
                    groupNameElement.setText(lowerCaseGroupName);
                    foundGroupNames.put(lowerCaseGroupName, groupElement);
                } else {
                    Element autoJoinElement = groupElement.getChild("newUserDefault");
                    if (autoJoinElement != null) {
                        boolean isNewUserDefault = Boolean.parseBoolean(autoJoinElement.getText());
                        if (isNewUserDefault) {
                            Element existingGroupElement = foundGroupNames.get(lowerCaseGroupName);
                            Element existingNewUserDefault = existingGroupElement.getChild("newUserDefault");
                            if (existingNewUserDefault != null) {
                                existingNewUserDefault.setText("true");
                            } else {
                                Element newUserDefault = new Element("newUserDefault");
                                newUserDefault.setText("true");
                                existingGroupElement.addContent(newUserDefault);
                            }/* ww  w. j a va  2  s. c  o  m*/
                        }
                    }
                    groupsIterator.remove();
                }
            }
        }
    }
    return foundGroupNames;
}

From source file:org.artifactory.update.security.v6.LowercaseUsernameXmlConverter.java

License:Open Source License

private void addGroupsToUser(Element userElement, Element userToGetGroupsFrom, Namespace namespace) {
    Set<String> existingGroupNames = getUserGroups(userElement);
    Set<String> newGroupNames = getUserGroups(userToGetGroupsFrom);
    existingGroupNames.addAll(newGroupNames);
    Element groupsElement = userElement.getChild("groups");
    if (groupsElement != null) {
        groupsElement.removeChildren("userGroup");
        for (String groupName : existingGroupNames) {
            Element userGroup = new Element("userGroup", namespace);
            userGroup.setText(groupName.toLowerCase());
            groupsElement.addContent(userGroup);
        }/*w  w  w  .  ja v  a  2s.c o m*/
    }
}

From source file:org.artifactory.update.security.v6.LowercaseUsernameXmlConverter.java

License:Open Source License

private void mergeAcls(Element root, Namespace namespace) {
    List acls = root.getChildren("acls", namespace);
    if (acls != null && !acls.isEmpty()) {
        for (Object acl : acls) {
            Element aclElement = (Element) acl;
            List aces = aclElement.getChildren("acl", namespace);
            if (aces != null && !aces.isEmpty()) {
                for (Object ace : aces) {
                    Element aceElement = (Element) ace;
                    List childAces = aceElement.getChildren("aces", namespace);
                    if (childAces != null && !childAces.isEmpty()) {
                        for (Object childAce : childAces) {
                            Map<String, Element> aclMap = Maps.newHashMap();

                            Element childAceElement = (Element) childAce;
                            List aceName = childAceElement.getChildren("ace");
                            if (aceName != null && !aceName.isEmpty()) {
                                Iterator iterator = aceName.iterator();
                                while (iterator.hasNext()) {
                                    Element child = (Element) iterator.next();
                                    Element principalElement = child.getChild("principal");
                                    boolean isGroup = Boolean.parseBoolean(child.getChild("group").getText());
                                    String principal = principalElement.getText();
                                    String principalLowerCase = principal.toLowerCase();
                                    String finalGroupName = principalLowerCase + " " + isGroup;
                                    principalElement.setText(principalLowerCase);
                                    if (!aclMap.containsKey(finalGroupName)) {
                                        aclMap.put(finalGroupName, child);
                                    } else {
                                        Element element = aclMap.get(finalGroupName);
                                        int newMask = Integer.parseInt(child.getChild("mask").getText());
                                        Element originalElementMask = element.getChild("mask");
                                        int originalMask = Integer.parseInt(originalElementMask.getText());
                                        newMask |= originalMask;
                                        originalElementMask.setText(String.valueOf(newMask));
                                        iterator.remove();
                                    }//  w  ww .  jav  a 2  s .c  o m
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:org.artifactory.version.converter.SnapshotUniqueVersionConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element root = doc.getRootElement();
    Namespace ns = root.getNamespace();

    List localRepos = root.getChild("localRepositories", ns).getChildren();
    for (Object localRepo1 : localRepos) {
        Element localRepo = (Element) localRepo1;
        Element snapshotBehavior = localRepo.getChild("useSnapshotUniqueVersions", ns);
        if (snapshotBehavior != null) {
            // rename the element
            snapshotBehavior.setName("snapshotVersionBehavior");
            String repoKey = localRepo.getChildText("key", ns);
            log.debug(//ww w.  j a v a 2  s . c  o  m
                    "Renamed element 'useSnapshotUniqueVersions' to " + "'snapshotVersionBehavior' for repo {}",
                    repoKey);

            // change the element value
            if (snapshotBehavior.getText().equals("true")) {
                log.debug("Changed value 'true' to 'deployer' for repo {}", repoKey);
                snapshotBehavior.setText("deployer");
            } else if (snapshotBehavior.getText().equals("false")) {
                log.debug("Changed value 'false' to 'non-unique' for repo {}", repoKey);
                snapshotBehavior.setText("non-unique");
            }
        }
    }
}

From source file:org.artifactory.version.converter.v100.RepositoriesKeysConverter.java

License:Open Source License

@Override
@SuppressWarnings({ "unchecked" })
public void convert(Document doc) {
    Element root = doc.getRootElement();
    Namespace ns = root.getNamespace();

    // get all local repositories
    Element localReposWrapper = root.getChild("localRepositories", ns);
    List localRepos = localReposWrapper.getChildren();
    List repos = new ArrayList(localRepos);

    // and add all remote repositories if any
    Element remoteReposWrapper = root.getChild("remoteRepositories", ns);
    if (remoteReposWrapper != null) {
        List remoteRepos = remoteReposWrapper.getChildren();
        if (remoteRepos != null) {
            repos.addAll(remoteRepos);/*from  w w w . j a  v  a 2 s.  c o  m*/
        }
    }

    for (Object repo : repos) {
        Element localRepo = (Element) repo;
        Element keyElement = localRepo.getChild("key", ns);
        String key = keyElement.getText();
        Map<String, String> keys = ArtifactoryHome.get().getArtifactoryProperties().getSubstituteRepoKeys();
        if (keys.containsKey(key)) {
            String newKey = keys.get(key);
            log.debug("Changing repository key from {} to {}", key, newKey);
            keyElement.setText(newKey);
        }
    }
}