Example usage for org.jdom2 Element indexOf

List of usage examples for org.jdom2 Element indexOf

Introduction

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

Prototype

@Override
    public int indexOf(final Content child) 

Source Link

Usage

From source file:edu.unc.lib.dl.xml.DepartmentOntologyUtil.java

License:Apache License

/**
 * Compares the affiliation values in the given MODS document against the ontology. If a preferred term(s) is found,
 * then it will replace the original. Only the first and last terms in a single hierarchy are kept if there are more
 * than two levels//from w  w w .  jav  a 2 s.  co  m
 *
 * @param modsDoc
 * @return Returns true if the mods document was modified by adding or changing affiliations
 * @throws JDOMException
 */
@Override
public boolean updateDocumentTerms(Element docElement) throws JDOMException {
    List<?> nameObjs = namePath.evaluate(docElement);
    boolean modified = false;

    for (Object nameObj : nameObjs) {
        Element nameEl = (Element) nameObj;

        List<?> affiliationObjs = nameEl.getChildren("affiliation", MODS_V3_NS);
        if (affiliationObjs.size() == 0)
            continue;

        // Collect the set of all affiliations for this name so that it can be used to detect duplicates
        Set<String> affiliationSet = new HashSet<>();
        for (Object affiliationObj : affiliationObjs) {
            Element affiliationEl = (Element) affiliationObj;

            affiliationSet.add(affiliationEl.getTextNormalize());
        }

        // Make a snapshot of the list of affiliations so that the original can be modified
        List<?> affilList = new ArrayList<>(affiliationObjs);
        // Get the authoritative department path for each affiliation and overwrite the original
        for (Object affiliationObj : affilList) {
            Element affiliationEl = (Element) affiliationObj;
            String affiliation = affiliationEl.getTextNormalize();

            List<List<String>> departments = getAuthoritativeForm(affiliation);
            if (departments != null && departments.size() > 0) {

                Element parentEl = affiliationEl.getParentElement();
                int affilIndex = parentEl.indexOf(affiliationEl);

                boolean removeOriginal = true;
                // Add each path that matched the affiliation. There can be multiple if there were multiple parents
                for (List<String> deptPath : departments) {
                    String baseDept = deptPath.size() > 1 ? deptPath.get(0) : null;
                    String topDept = deptPath.get(deptPath.size() - 1);

                    // No need to remove the original if it is in the set of departments being added
                    if (affiliation.equals(topDept))
                        removeOriginal = false;

                    modified = addAffiliation(baseDept, parentEl, affilIndex, affiliationSet) || modified;
                    modified = addAffiliation(topDept, parentEl, affilIndex, affiliationSet) || modified;
                }

                // Remove the old affiliation unless it was already in the vocabulary
                if (removeOriginal)
                    parentEl.removeContent(affiliationEl);

            }
        }
    }

    return modified;
}

From source file:jodtemplate.pptx.postprocessor.StylePostprocessor.java

License:Apache License

private void processComment(final Comment comment, final Element at, final Slide slide,
        final Configuration configuration) throws JODTemplateException {
    String commentText = comment.getText();
    if (commentText.startsWith(STYLIZED_KEYWORD)) {
        commentText = StringUtils.removeStart(commentText, STYLIZED_KEYWORD);
        final String className = StringUtils.substringBefore(commentText, ":");
        commentText = StringUtils.removeStart(commentText, className + ": ");
        final Stylizer stylizer = configuration.getStylizer(className);
        if (stylizer == null) {
            throw new JODTemplateException("Unable to find stylizer");
        }/*w  w  w  .ja v a  2  s  .c o  m*/
        final String text = StringUtils.removeStart(commentText, " stylized: ");
        final Element ar = at.getParentElement();
        final Element ap = ar.getParentElement();
        final int arIndex = ap.indexOf(ar);
        final Element arPr = getArPrElement(ar);
        final Element apPr = getApPrElement(ap);
        final Element sourceApPr = ObjectUtils.clone(apPr);
        cleanApPrElement(apPr);

        final List<Element> stylizedElements = stylizer.stylize(text, arPr, apPr, slide);

        ap.removeContent(ar);
        final List<Element> remains = getRemainingElements(arIndex, ap);
        for (Element el : remains) {
            ap.removeContent(el);
        }

        final int currentApIndex = injectElementsInDocument(stylizedElements, ap, apPr, arIndex);
        injectRemainsInDocument(remains, ap, sourceApPr, currentApIndex);
    }
}

From source file:jodtemplate.pptx.postprocessor.StylePostprocessor.java

License:Apache License

private int injectElementsInDocument(final List<Element> stylizedElements, final Element ap, final Element apPr,
        final int arIndex) {
    int index = arIndex;
    final Element txBody = ap.getParentElement();
    int apIndex = txBody.indexOf(ap) + 1;
    boolean createNewAp = false;
    Element currentAp = ap;//from  w ww. j  a  v a 2 s  .co  m
    for (Element element : stylizedElements) {
        if (element.getName().equals(PPTXDocument.P_ELEMENT)) {
            currentAp = element;
            txBody.addContent(apIndex, currentAp);
            apIndex++;
            createNewAp = true;
        } else {
            if (createNewAp) {
                currentAp = new Element(PPTXDocument.P_ELEMENT, getNamespace());
                Element apPrToAdd = ObjectUtils.clone(apPr);
                if (apPrToAdd == null) {
                    apPrToAdd = new Element(PPTXDocument.PPR_ELEMENT, getNamespace());
                }
                final Element abuNone = new Element(PPTXDocument.BUNONE_ELEMENT, getNamespace());
                apPrToAdd.addContent(abuNone);
                currentAp.addContent(apPrToAdd);
                txBody.addContent(apIndex, currentAp);
                apIndex++;
                createNewAp = false;
            }
            if (currentAp == ap) {
                currentAp.addContent(index, element);
                index++;
            } else {
                currentAp.addContent(element);
            }
        }
    }
    return apIndex;
}

From source file:jodtemplate.pptx.preprocessor.FormatTagsPreprocessor.java

License:Apache License

@Override
public Document process(final Map<String, Object> context, final Document document, final Slide slide,
        final Resources resources, final Configuration configuration) {
    final IteratorIterable<Element> apElements = document
            .getDescendants(Filters.element(PPTXDocument.P_ELEMENT, getNamespace()));
    final List<Element> apElementsList = new ArrayList<>();
    while (apElements.hasNext()) {
        apElementsList.add(apElements.next());
    }//w  w  w.  j  a v a2  s. c  om
    for (Element ap : apElementsList) {
        final List<Element> apChildrenList = ap.getChildren();
        if (apChildrenList.size() != 0) {
            final List<Element> arabrElementsListResult = processArAndABrElements(apChildrenList,
                    configuration.getParserFactory().createParser());
            int firstArElementIndex = ap.indexOf(ap.getChild(PPTXDocument.R_ELEMENT, getNamespace()));
            if (firstArElementIndex < 0) {
                firstArElementIndex = 0;
            }
            ap.removeChildren(PPTXDocument.R_ELEMENT, getNamespace());
            ap.removeChildren(PPTXDocument.BR_ELEMENT, getNamespace());
            ap.addContent(firstArElementIndex, arabrElementsListResult);
        }
    }
    return document;
}

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method updateElement./*w  w w.  j  a v a  2 s .  co  m*/
 * 
 * @param counter
 * @param shouldExist
 * @param name
 * @param parent
 * @return Element
 */
public static Element updateElement(final IndentationCounter counter, final Element parent, final String name,
        final boolean shouldExist) {
    Element element = parent.getChild(name, parent.getNamespace());
    if (shouldExist) {
        if (element == null) {
            element = factory.element(name, parent.getNamespace());
            insertAtPreferredLocation(parent, element, counter);
        }
        counter.increaseCount();
    } else if (element != null) {
        final int index = parent.indexOf(element);
        if (index > 0) {
            final Content previous = parent.getContent(index - 1);
            if (previous instanceof Text) {
                final Text txt = (Text) previous;
                if (txt.getTextTrim().length() == 0) {
                    parent.removeContent(txt);
                }
            }
        }
        parent.removeContent(element);
    }
    return element;
}

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

License:Open Source License

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

    Element backupDir = root.getChild("backupDir", ns);
    if (backupDir != null) {
        root.removeContent(backupDir);/* ww w  .  j a va 2  s  .  c o m*/
        backupDir.setName("dir");
        log.debug("Renamed 'backupDir' to 'dir'");
    }

    Element backupCron = root.getChild("backupCronExp", ns);
    if (backupCron != null) {
        root.removeContent(backupCron);
        backupCron.setName("cronExp");
        log.debug("Renamed 'backupCronExp' to 'cronExp'");
    }

    if (backupDir != null && backupCron != null) {
        // create the new <backup> element and place before the localRepositories
        Element backup = new Element("backup", ns);
        backup.addContent(backupDir);
        backup.addContent(backupCron);
        int localReposLocation = root.indexOf(root.getChild("localRepositories", ns));
        root.addContent(localReposLocation, backup);
    } else {
        log.debug("No backup elements found");
    }
}

From source file:org.artifactory.version.converter.v130.AnnonAccessUnderSecurityConverter.java

License:Open Source License

private int findLastLocation(Element parent, String... elements) {
    for (int i = elements.length - 1; i >= 0; i--) {
        Element child = parent.getChild(elements[i], parent.getNamespace());
        if (child != null) {
            return parent.indexOf(child);
        }//w w  w.  j av a  2 s.c o m
    }
    return -1;
}

From source file:org.artifactory.version.converter.v130.BackupListConverter.java

License:Open Source License

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

    Element backup = root.getChild("backup", ns);
    if (backup != null) {
        int location = root.indexOf(backup);
        root.removeContent(location);//from www.  ja v  a 2  s . co  m
        Element backups = new Element("backups", ns);
        backups.addContent(backup);
        root.addContent(location, backups);
    }
}

From source file:org.artifactory.version.converter.v131.LdapAuthenticationPatternsConverter.java

License:Open Source License

@Override
public void convert(Document doc) {
    Element root = doc.getRootElement();
    Namespace ns = root.getNamespace();
    Element security = root.getChild("security", ns);
    if (security != null) {
        Element ldapSettings = security.getChild("ldapSettings", ns);
        if (ldapSettings != null) {
            Element userDn = ldapSettings.getChild("userDnPattern", ns);
            if (userDn != null) {
                log.debug("Moving userDnPattern under authenticationPatterns");
                int location = ldapSettings.indexOf(userDn);
                ldapSettings.removeContent(userDn);
                Element authPatterns = new Element("authenticationPatterns", ns);
                Element authPattern = new Element("authenticationPattern", ns);
                authPattern.addContent(userDn);
                authPatterns.addContent(authPattern);
                ldapSettings.addContent(location, authPatterns);
            }/* w w  w  .  j a v a2s.c  o  m*/
        } else {
            log.debug("No ldap settings found");
        }
    }
}

From source file:org.artifactory.version.converter.v132.LdapListConverter.java

License:Open Source License

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

    Element security = root.getChild("security", ns);
    if (security == null) {
        log.debug("No security settings defned");
        return;//from  ww w  .  ja  v a  2 s . c  om
    }

    Element oldLdapSettings = security.getChild("ldapSettings", ns);
    if (oldLdapSettings == null) {
        log.debug("No ldap settings configured");
        return;
    }

    int location = security.indexOf(oldLdapSettings);
    security.removeContent(oldLdapSettings);

    Element ldapSettings = new Element("ldapSettings", ns);
    security.addContent(location, ldapSettings);

    String ldapUrl = oldLdapSettings.getChildText("ldapUrl", ns);

    // manager dn and password only relevant for search based authentications
    String managerDn = oldLdapSettings.getChildText("managerDn", ns);
    String managerPassword = oldLdapSettings.getChildText("managerPassword", ns);

    // convert authentication patterns
    Element authPatternsElement = oldLdapSettings.getChild("authenticationPatterns", ns);
    List authPatterns = authPatternsElement.getChildren("authenticationPattern", ns);
    log.debug("Found {} patterns to convert" + authPatterns.size());

    // create new ldap setting for each authentication pattern
    int ldapKeyIndex = 1;
    for (Object pattern : authPatterns) {
        Element authPattern = (Element) pattern;

        Element ldapSetting = new Element("ldapSetting", ns);
        ldapSettings.addContent(ldapSetting);

        // add the key
        ldapSetting.addContent(createTextElement("key", ns, "ldap" + ldapKeyIndex++));

        // set enabled true
        ldapSetting.addContent(createTextElement("enabled", ns, "true"));

        // add the ldap url
        ldapSetting.addContent(createTextElement("ldapUrl", ns, ldapUrl));

        // add user dn if not empty
        String userDn = authPattern.getChildText("userDnPattern", ns);
        if (userDn != null) {
            ldapSetting.addContent(createTextElement("userDnPattern", ns, userDn));
        }

        // create and add search element if search filter exists
        String searchFilter = authPattern.getChildText("searchFilter", ns);
        if (searchFilter != null) {
            Element search = new Element("search", ns);
            ldapSetting.addContent(search);

            search.addContent(createTextElement("searchFilter", ns, searchFilter));

            String searchBase = authPattern.getChildText("searchBase", ns);
            if (searchBase != null) {
                search.addContent(createTextElement("searchBase", ns, searchBase));
            }

            String searchSubTree = authPattern.getChildText("searchSubTree", ns);
            if (searchSubTree != null) {
                search.addContent(createTextElement("searchSubTree", ns, searchSubTree));
            }

            if (managerDn != null) {
                search.addContent(createTextElement("managerDn", ns, managerDn));
            }

            if (managerPassword != null) {
                search.addContent(createTextElement("managerPassword", ns, managerPassword));
            }
        }
    }
}