Example usage for org.jdom2 Element cloneContent

List of usage examples for org.jdom2 Element cloneContent

Introduction

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

Prototype

@Override
    public List<Content> cloneContent() 

Source Link

Usage

From source file:cz.muni.fi.mir.mathmlcanonicalization.modules.MrowNormalizer.java

License:Apache License

private static void removeElement(final Element element, final Element parent) {
    assert element != null && parent != null;
    parent.addContent(parent.indexOf(element), element.cloneContent());
    element.detach();/*from  w ww.  j av a2 s  .c o m*/
}

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

License:Open Source License

/**
 * This method archives the desktop-data of the desktop with the name {@code name} to a
 * zipped xml-file. The file contains the desktop-data, the modifed-entries-data for those entries
 * that appear on the desktop and the saved desktop-notes.
 *
 * @param name the name of the desktop that should be archived.
 * @return the archived document as XML-focument, or {@code null} if an error occured.
 *//*from   w w w . j a  v  a 2  s  . co  m*/
public Document archiveDesktop(String name) {
    // create new document
    Document archive = new Document(new Element("archivedDesktop"));
    // add desktop-element of desktop that should be archived
    Element deskel = getDesktopElement(name);
    // if we found a valid value, go on
    if (deskel != null) {
        try {
            // set name attribute
            archive.getRootElement().setAttribute("name", name);
            // create desktop-element
            Element content_desktop = new Element("desktop");
            // clone content from current desktop
            content_desktop.addContent(deskel.cloneContent());
            // add element to archive-file
            archive.getRootElement().addContent(content_desktop);
            // now retrieve desktop-notes
            Element noteel = getDesktopNotes(name);
            // if we found a valid value, go on
            if (noteel != null) {
                // create notes-element
                Element content_notes = new Element("desktopNotes");
                // clone content from current desktop-notes
                content_notes.addContent(noteel.cloneContent());
                // add content
                archive.getRootElement().addContent(content_notes);
            }
            // now retrieve all timestamps from the archived desktop
            // and look for modified entries...
            // create new list that will contain the timestamps
            timestampList = new ArrayList<String>();
            // fill list with all entry-numbers. since we have sub-bullets/levels, we
            // recursevly go through the desktop-data
            retrieveDesktopEntriesTimestamps(deskel);
            // if we have any results, go on...
            if (timestampList.size() > 0) {
                // create base element
                Element modifiedel = new Element("modifiedEntries");
                // add all modified entries that appear on the archived desktop
                String[] timestamps = timestampList.toArray(new String[timestampList.size()]);
                for (String ts : timestamps) {
                    // retrieve modifed entry
                    Element me_dummy = retrieveModifiedEntryElementFromTimestamp(ts);
                    // check for valid value
                    if (me_dummy != null) {
                        // crate new modified-entry-element
                        Element me = new Element(ELEMENT_ENTRY);
                        // set timestamp-attribute
                        me.setAttribute(ATTR_TIMESTAMP, ts);
                        // and add modified text
                        me.setText(me_dummy.getText());
                        // and add content
                        modifiedel.addContent(me);
                    }
                }
                archive.getRootElement().addContent(modifiedel);
            }
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        } catch (IllegalAddException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        }
    }
    return archive;
}

From source file:io.wcm.handler.richtext.impl.RichTextHandlerImpl.java

License:Apache License

private List<Content> processRichText(String text, UrlMode urlMode, MediaArgs mediaArgs) {
    if (isEmpty(text)) {
        return ImmutableList.of();
    }/* w w  w .j  a  v a2  s. c  o m*/

    // Parse text
    try {
        Element contentParent = RichTextUtil.parseText(text, true);

        // Rewrite content (e.g. anchor tags)
        RichTextUtil.rewriteContent(contentParent, rewriteContentHandler);

        // return xhtml elements
        return ImmutableList.copyOf(contentParent.cloneContent());
    } catch (JDOMException ex) {
        log.debug("Unable to parse XHTML text."
                + (currentPage != null ? " Current page is " + currentPage.getPath() + "." : ""), ex);
        return ImmutableList.of();
    }
}

From source file:io.wcm.handler.richtext.impl.RichTextRewriteContentHandlerImpl.java

License:Apache License

/**
 * Checks if the given anchor element has to be rewritten.
 * @param element Element to check/*from w ww .  j a  v a2 s  .c o m*/
 * @return null if nothing is to do with this element.
 *         Return empty list to remove this element.
 *         Return list with other content to replace element with new content.
 */
private List<Content> rewriteAnchor(Element element) {

    // detect empty anchor elements and insert at least an empty string to avoid "self-closing" elements
    // that are not handled correctly by most browsers
    if (element.getContent().isEmpty()) {
        element.setText("");
    }

    // resolve link metadata from DOM element
    Link link = getAnchorLink(element);

    // build anchor for link metadata
    Element anchorElement = buildAnchorElement(link, element);

    // Replace anchor tag or remove anchor tag if invalid - add any sub-content in every case
    List<Content> content = new ArrayList<Content>();
    if (anchorElement != null) {
        anchorElement.addContent(element.cloneContent());
        content.add(anchorElement);
    } else {
        content.addAll(element.getContent());
    }
    return content;
}

From source file:io.wcm.handler.richtext.util.RichTextUtil.java

License:Apache License

/**
 * Parses XHTML text string, and adds to parsed content to the given parent element.
 * @param parent Parent element to add parsed content to
 * @param text XHTML text string (root element not needed)
 * @param xhtmlEntities If set to true, Resolving of XHtml entities in XHtml fragment is supported.
 * @throws JDOMException Is thrown if the text could not be parsed as XHTML
 *///  w ww  .  j  a  v a2 s .c o  m
public static void addParsedText(Element parent, String text, boolean xhtmlEntities) throws JDOMException {
    Element root = parseText(text, xhtmlEntities);
    parent.addContent(root.cloneContent());
}

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);
                }/*from   w  w  w  . j a v a2  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.kdp.word.transformer.TOCTransformer.java

License:Apache License

@Override
public void transform(Context context) {
    JDOMFactory factory = context.getJDOMFactory();

    Element root = context.getSourceRoot();
    for (Element el : root.getChildren()) {
        transformInternal(context, el);//from   w w  w .  j  a  va  2  s  .c  o  m
    }

    Element first = JDOMUtils.findElement(root, "p", "class", "MsoToc1");
    if (first != null) {
        Element parent = first.getParentElement();
        List<Element> children = parent.getChildren();

        // Add the nav element
        Element nav = factory.element("nav");
        nav.getAttributes().add(factory.attribute("type", "toc", OPFTransformer.NS_OPF));
        int index = children.indexOf(first);
        children.add(index, nav);

        // Add the ol element
        Element ol = factory.element("ol");
        ol.setAttribute("class", "Toc");
        nav.getChildren().add(ol);

        Iterator<Element> itel = children.iterator();
        while (itel.hasNext()) {
            Element el = itel.next();
            if (JDOMUtils.isElement(el, "p", "class", "MsoToc1")) {
                Element li = factory.element("li");
                li.getAttributes().add(factory.attribute("class", "MsoToc1"));
                li.addContent(el.cloneContent());
                ol.getChildren().add(li);
                itel.remove();
            }
        }
    }
}

From source file:org.mycore.datamodel.metadata.MCRMetaXML.java

License:Open Source License

/**
 * This method read the XML input stream part from a DOM part for the
 * metadata of the document.//from w  w w  .  j a v a  2 s.  c o m
 * 
 * @param element
 *            a relevant JDOM element for the metadata
 */
@Override
public void setFromDOM(org.jdom2.Element element) {
    super.setFromDOM(element);
    content = element.cloneContent();
}

From source file:org.mycore.frontend.editor.MCREditorDefReader.java

License:Open Source License

/**
 * Recursively removes include elements that are direct or indirect children
 * of the given container element and replaces them with the included
 * resource. Includes that may be contained in included resources are
 * recursively resolved, too./*from ww  w.  j av  a 2  s .  co m*/
 *
 * @param element
 *            The element where to start resolving includes
 */
private boolean resolveIncludes(Element element) {
    boolean replaced = false;

    String ref = element.getAttributeValue("ref", "");
    ref = tokenSubstitutor.substituteTokens(ref);

    if (element.getName().equals("include")) {
        String uri = element.getAttributeValue("uri");
        if (uri != null) {
            uri = tokenSubstitutor.substituteTokens(uri);
            LOGGER.info("Including " + uri + (ref.length() > 0 ? "#" + ref : ""));
            Element parent = element.getParentElement();
            int pos = parent.indexOf(element);

            Element container = MCRURIResolver.instance().resolve(uri);
            List<Content> found;

            if (ref.length() == 0) {
                found = container.cloneContent();
            } else {
                found = findContent(container, ref);
                ref = "";
            }
            replaced = true;
            parent.addContent(pos, found);
            element.detach();
        }
    } else {
        String id = element.getAttributeValue("id", "");
        if (id.length() > 0) {
            id2component.put(id, element);
        }

        setDefaultAttributes(element);
        resolveChildren(element);
    }

    if (ref.length() > 0) {
        referencing2ref.put(element, ref);
    }
    return replaced;
}

From source file:org.mycore.frontend.editor.MCREditorDefReader.java

License:Open Source License

private List<Content> findContent(Element candidate, String id) {
    if (id.equals(candidate.getAttributeValue("id"))) {
        return candidate.cloneContent();
    } else {/*from  w ww.  j ava2 s  .  co  m*/
        return ((List<Element>) candidate.getChildren()).stream().map(child -> findContent(child, id))
                .filter(Objects::nonNull).findFirst().orElse(null);
    }
}