Example usage for org.jdom2 Element removeChild

List of usage examples for org.jdom2 Element removeChild

Introduction

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

Prototype

public boolean removeChild(final String cname) 

Source Link

Document

This removes the first child element (one level deep) with the given local name and belonging to no namespace.

Usage

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method removes wrong placed edit-tags in the xml-file. this error occured in the
 * {@link #changeEditTimeStamp(int) changeEditTimeStamp()} method, where the edit-element,
 * child-element of the timestamp-element, was set as child of the zettel-element (and not its
 * child "timestamp"). This method tries to fix this error...
 *//* w  w  w. j  a  v a 2s.c om*/
public void fixWrongEditTags() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
        // retrieve element
        Element zettel = retrieveZettel(cnt);
        // check for valid value
        if (zettel != null) {
            // check whether element has a child named "edited". if so, it either
            // has to be moved as sub-child to the child-element timestamp, or removed
            // if "timestamp" already has an edit-element
            Element edited = zettel.getChild("edited");
            // only proceed, if wrong placed edited element exists
            if (edited != null) {
                // retrieve timestamp-element
                Element timestamp = zettel.getChild("timestamp");
                // check for valid value
                if (timestamp != null) {
                    // retrieve edited-timestamp
                    Element timestampedit = timestamp.getChild("edited");
                    // check whether edited-element exists
                    if (null == timestampedit) {
                        // if timestampedit is null, the element has no edited-element
                        // so we add the content of the wrong placed element as new edited-element
                        // create new edited element
                        Element ed = new Element("edited");
                        // add to timestamp
                        timestamp.addContent(ed);
                        // set content
                        ed.setText(edited.getText());
                    } else {
                        // now we know that an edited-element already exists
                        // we now want to check whether the existing editing-timestamp
                        // is older than the value in the wrong placed edited-element,
                        // and if so, update the timestamp
                        if (timestamp.getText().compareTo(edited.getText()) < 0)
                            timestampedit.setText(edited.getText());
                    }
                }
                // and remove wrong edited element
                zettel.removeChild("edited");
            }
        }
    }
}

From source file:de.danielluedecke.zettelkasten.database.Daten.java

License:Open Source License

/**
 * This method updates the timestamp-attributes in the data base. The former
 * XML-elements (timestamp) with the children "created" and "edited" are from database
 * version 3.4 on simply stored as attribute of each element.
 *//*from  ww w.java2 s . c o  m*/
public void db_updateTimestampAttributes() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
        // retrieve element
        Element zettel = retrieveZettel(cnt);
        // check for valid value
        if (zettel != null) {
            // init strings with default values
            String created = Tools.getTimeStamp();
            String edited = "";
            // retrieve created-timestamp
            Element el = zettel.getChild("timestamp").getChild("created");
            if (el != null)
                created = el.getText();
            // retrieve edited-timestamp
            el = zettel.getChild("timestamp").getChild("edited");
            if (el != null)
                edited = el.getText();
            // remove old values
            zettel.removeChild("timestamp");
            // and set timestamp as attributes
            setTimestamp(zettel, created, edited);
        }
    }
}

From source file:org.artifactory.update.md.MetadataConverterUtils.java

License:Open Source License

public static List<Element> extractExtensionFields(Element rootElement) {
    String modifiedBy = rootElement.getChildText(MODIFIED_BY);
    List<Element> toMove = new ArrayList<>(EXTENSION_FIELDS.length);
    for (String tagName : EXTENSION_FIELDS) {
        Element element = rootElement.getChild(tagName);
        if (element != null) {
            toMove.add(element);//from w  ww.  j  ava  2s .co m
            rootElement.removeChild(tagName);
        } else {
            if (CREATED_BY.equals(tagName)) {
                toMove.add(new Element(CREATED_BY).setText(modifiedBy));
            }
        }
    }
    return toMove;
}

From source file:org.artifactory.update.md.MetadataConverterUtils.java

License:Open Source License

public static RepoPath extractRepoPath(Element rootElement) {
    String repoKey = rootElement.getChildText(REPO_KEY);
    String relPath = rootElement.getChildText(REL_PATH);
    RepoPath repoPath = InternalRepoPathFactory.create(repoKey, relPath);
    rootElement.removeChild(REPO_KEY);
    rootElement.removeChild(REL_PATH);//  ww  w.jav  a2  s. c  o m
    return repoPath;
}

From source file:org.artifactory.update.md.v125rc0.MdFileConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    rootElement.setName(getNewMetadataName());
    RepoPath repoPath = MetadataConverterUtils.extractRepoPath(rootElement);
    List<Element> toMove = MetadataConverterUtils.extractExtensionFields(rootElement);
    MetadataConverterUtils.addNewContent(rootElement, repoPath, toMove);
    // Not used anymore
    rootElement.removeChild(MdFolderConverter.ARTIFACTORY_NAME);
    rootElement.removeChild("xmlAware");
    rootElement.removeChild("downloadCount");
}

From source file:org.artifactory.update.md.v125rc0.MdFolderConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    rootElement.setName(getNewMetadataName());
    RepoPath repoPath = MetadataConverterUtils.extractRepoPath(rootElement);
    // In this version the relPath is the father and name need to be added
    if (rootElement.getChild(ARTIFACTORY_NAME) != null) {
        String name = rootElement.getChildText(ARTIFACTORY_NAME);
        repoPath = InternalRepoPathFactory.create(repoPath, name);
    }//from   ww w  .j a v  a2s  .com
    List<Element> toMove = MetadataConverterUtils.extractExtensionFields(rootElement);
    MetadataConverterUtils.addNewContent(rootElement, repoPath, toMove);
    // Not used anymore
    rootElement.removeChild(ARTIFACTORY_NAME);
}

From source file:org.artifactory.update.md.v130beta3.ArtifactoryFileConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element rootElement = doc.getRootElement();
    if (rootElement.getName().equals(getNewMetadataName())) {
        // Already done
        return;//from   w  w w.j a  va 2 s.co m
    }
    rootElement.setName(getNewMetadataName());
    RepoPath repoPath = MetadataConverterUtils.extractRepoPath(rootElement);
    List<Element> toMove = MetadataConverterUtils.extractExtensionFields(rootElement);
    MetadataConverterUtils.addNewContent(rootElement, repoPath, toMove);
    MimeType ct = NamingUtils.getMimeType(repoPath.getName());
    rootElement.removeChild("mimeType");
    rootElement.addContent(new Element("mimeType").setText(ct.getType()));
}

From source file:org.artifactory.update.security.v1.AclsConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element root = doc.getRootElement();
    root.removeChild("repoPaths");

    Element aclsElement = root.getChild("acls");
    @SuppressWarnings({ "unchecked" })
    List<Element> acls = aclsElement.getChildren();
    if (acls == null) {
        log.warn("No acls detected");
        return;//from   w ww  . j a  v a  2  s .co m
    }

    // remove all acls - will create new ones
    root.removeChild("acls");
    Element newAclsElement = new Element("acls");
    root.addContent(newAclsElement);

    Map<Integer, String> objectIdentitiesMap = buildObjectIdentitiesMap(acls);
    HashMap<String, Element> newAclsByIdentity = new HashMap<>();

    for (Element oldAcl : acls) {
        if ("org.acegisecurity.acl.basic.SimpleAclEntry".equals(oldAcl.getName())) {
            Element objectIdentity = oldAcl.getChild("aclObjectIdentity");
            if (hasIdentity(objectIdentity)) {
                // add new acl
                Element newAcl = new Element("org.artifactory.security.RepoPathAcl");
                String newIdentity = getNewIdentity(objectIdentity);
                newAcl.addContent(new Element("identifier").setText(newIdentity));
                Element aces = new Element("aces");
                newAcl.addContent(aces);
                aces.addContent(new Element("list"));
                addAceToAcl(oldAcl, newAcl);
                newAclsByIdentity.put(newIdentity, newAcl);
                newAclsElement.addContent(newAcl);
            } else {
                // just an ace - add to existing acl
                // get the identifier index from the reference attribute
                // sample value: ../../org.acegisecurity.acl.basic.SimpleAclEntry[2]/aclObjectIdentity
                String reference = objectIdentity.getAttributeValue("reference");
                Pattern pattern = Pattern.compile(".*org.acegisecurity.acl.basic.SimpleAclEntry\\[(.*)\\].*");
                Matcher matcher = pattern.matcher(reference);
                if (matcher.matches()) {
                    String indexStr = matcher.group(1);
                    int identityIndex = Integer.parseInt(indexStr);
                    String identity = objectIdentitiesMap.get(identityIndex);
                    Element newAcl = newAclsByIdentity.get(identity);
                    addAceToAcl(oldAcl, newAcl);
                } else {
                    log.warn("Couldn't match identity reference {}", reference);
                }
            }
        } else {
            log.warn("Acl tag " + oldAcl + " under acls is not a SimpleAclEntry!");
        }
    }
}

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

License:Open Source License

@Override
@SuppressWarnings({ "unchecked" })
public void convert(Document doc) {
    Element aclsTag = doc.getRootElement().getChild("acls");
    List<Element> acls = aclsTag.getChildren();
    for (Element acl : acls) {
        if (acl.getName().contains("RepoPathAcl")) {
            acl.setName("acl");
            convertIdentifierToPermissionTarget(acl);
            Element acesTag = acl.getChild(ACES);
            Element aceListTag = acesTag.getChild("list");
            List<Element> aces = aceListTag.getChildren("org.artifactory.security.RepoPathAce");
            Element newAces = new Element(ACES);
            Element aceTemplate = new Element("ace");
            Element groupEl = new Element("group");
            groupEl.setText("false");
            aceTemplate.addContent(new Element(PRINCIPAL)).addContent(groupEl).addContent(new Element(MASK));
            for (Element ace : aces) {
                Element newAce = (Element) aceTemplate.clone();
                newAce.getChild(PRINCIPAL).setText(ace.getChildText(PRINCIPAL));
                Element maskEl = ace.getChild(MASK);
                int mask = Integer.parseInt(maskEl.getText());
                if ((mask & (ArtifactoryPermission.MANAGE.getMask()
                        | ArtifactoryPermission.DEPLOY.getMask())) > 0) {
                    mask |= ArtifactoryPermission.DELETE.getMask();
                }//from www. jav a 2s .  c  o m
                newAce.getChild(MASK).setText("" + mask);
                newAces.addContent(newAce);
            }
            acl.removeChild(ACES);
            acl.addContent(newAces);
        } else {
            log.warn("Acl tag " + acl + " under acls is not a RepoPAthAcl!");
        }
    }
}

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

License:Open Source License

private void convertIdentifierToPermissionTarget(Element acl) {
    String identifier;/*w w w .ja  va  2s  .  c o  m*/
    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);
}