Example usage for org.jdom2 Element getParent

List of usage examples for org.jdom2 Element getParent

Introduction

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

Prototype

public Parent getParent() 

Source Link

Document

Return this child's parent, or null if this child is currently not attached.

Usage

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

License:Open Source License

/**
 * This method retrieves a bullet element from the given path {@code tp} and copies it into
 * a global Element-variable, so this element is stored for later use. Furthermore, the element
 * retrieve from the treepath {@code tp} is removed from the XML-document
 *
 * @param timestamp the timestamp of the bullet that should be copied to the "clipboard" and deleted afterwards
 * (cut-operation)/*from ww w  . ja  v  a  2 s . c  om*/
 */
public void cutBulletToClip(String timestamp) {
    // retrieve the entry that should be deleted
    Element bullet = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (bullet != null) {
        // get the entry's parent
        Parent p = bullet.getParent();
        // get the index from "bullet"
        int index = p.indexOf(bullet);
        // if we have a valid index, go on
        if (index != -1) {
            // remove the content and save it to the clipboard-element
            clipbullet = (Element) p.removeContent(index);
            // change modified state
            setModified(true);
        }
    }
}

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

License:Open Source License

/**
 * This method deletes the selected entry from the desktop.
 * //  w w w. j a  v  a 2 s. c  om
 * @param timestamp the timestamp of the to be deleted entry
 */
public void deleteEntry(String timestamp) {
    // retrieve the entry that should be deleted
    Element entry = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (entry != null) {
        // check whether we have any modified entry. if so, delete it, since
        // we no longer need it...
        deleteModifiedEntry(timestamp);
        // get the entry's parent
        Parent p = entry.getParent();
        // remove entry from parent
        p.removeContent(entry);
        // change modified state
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method deletes the selected entry from the desktop.
 * //from  w  ww. j  a  v  a  2  s .co m
 * @param timestamp the timestamp of the to the selected entry.
 */
public void deleteBullet(String timestamp) {
    // retrieve the entry that should be deleted
    Element bullet = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (bullet != null) {
        // retrieve all timestamps of those entries which are children of the to be
        // deleted bullet
        String[] timestamps = retrieveBulletTimestamps(bullet);
        // get the entry's parent
        Parent p = bullet.getParent();
        // remove entry from parent
        p.removeContent(bullet);
        // now remove all possible modified entries of that list
        if (timestamps != null && timestamps.length > 0) {
            // iterate all timestamps
            for (String singlets : timestamps) {
                // delete modified entries that already have been deleted due
                // to the removal of the bullet-point
                deleteModifiedEntry(singlets);
            }
        }
        // change modified state
        setModified(true);
    }
}

From source file:jodtemplate.pptx.ImageService.java

License:Apache License

public void insertImage(final ImageField imageField, final Slide slide, final Resources resources,
        final Element pic) throws JODTemplateException {
    try {//from   ww w. jav  a2s . com
        final InputStream is = imageField.getInputStream();
        if (is != null) {
            try (final InputStream bis = new BufferedInputStream(is)) {

                final byte[] imageContents = IOUtils.toByteArray(bis);

                final Image image = getImage(imageContents, slide.getPresentation(), resources);

                final Relationship imageRel = getImageRelationship(image, slide);

                final Attribute embed = pic
                        .getChild(PPTXDocument.BLIPFILL_ELEMENT, getPresentationmlNamespace())
                        .getChild(PPTXDocument.BLIP_ELEMENT, getDrawingmlNamespace())
                        .getAttribute(PPTXDocument.EMBED_ATTR, getRelationshipsNamespace());
                embed.setValue(imageRel.getId());

                setPicSize(pic, image);
            }
        } else {
            pic.getParent().removeContent(pic);
        }
    } catch (IOException | DataConversionException e) {
        throw new JODTemplateException(e);
    }
}

From source file:jodtemplate.pptx.preprocessor.ShortListPreprocessor.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) throws JODTemplateException {
    final IteratorIterable<Element> parentElements = document
            .getDescendants(Filters.element(parentElement, getNamespace()));
    final List<Element> parentElementsList = new ArrayList<>();
    while (parentElements.hasNext()) {
        parentElementsList.add(parentElements.next());
    }/*from  ww  w .  j  av a 2s  .  com*/

    for (final Element parent : parentElementsList) {
        final IteratorIterable<Element> atElements = parent
                .getDescendants(Filters.element(PPTXDocument.T_ELEMENT, getNamespace()));
        final List<Element> atElementsList = new ArrayList<>();
        while (atElements.hasNext()) {
            atElementsList.add(atElements.next());
        }

        final ExpressionHandler expressionHandler = configuration.getExpressionHandler();
        boolean isLoop = false;
        InlineListExpression expression = null;
        for (final Element at : atElementsList) {
            final String text = at.getText();
            if (configuration.getExpressionHandler().isInlineList(text)) {
                expression = expressionHandler.createInlineListExpression(text);
                at.setText(expressionHandler.createVariable(expression.getVariable()));
                isLoop = true;
            }
        }
        if (isLoop) {
            int apIndex = parent.getParent().indexOf(parent);
            final String beginList = expressionHandler.createBeginList(expression.getWhat(),
                    expression.getAs());
            final String endList = expressionHandler.createEndList();
            parent.getParent().addContent(apIndex, new Comment(beginList));
            apIndex++;
            parent.getParent().addContent(apIndex + 1, new Comment(endList));
        }
    }
    return document;
}

From source file:mil.tatrc.physiology.datamodel.doxygen.XSDToDoxygen.java

License:Open Source License

/**
 * Obtains any XML comments sibling content that immediately preceed this
 * element/*from   w  w  w. ja v a 2s  .c o m*/
 * @param element
 * @return
 */
private String getComment(Element element) {
    Parent parent = element.getParent();
    int i = parent.indexOf(element);

    StringBuffer comments = new StringBuffer();

    // Search backward from this position for preceding comments
    for (int x = i - 1; x >= 0; x--) {
        Content c = parent.getContent(x);
        if (c.getCType() == Content.CType.Comment) {
            Comment comment = (Comment) c;
            String text = comment.getText().trim();
            if (isValidComment(text)) {
                // This comment pertains to the preceding element
                if (text.startsWith("<<")) {
                    break;
                }
                if (comments.length() > 0) {
                    comments.insert(0, '\n');
                }
                comments.insert(0, text);
            }
        } else if (c.getCType() == Content.CType.Element) {
            // Stop looking when we hit another element 
            break;
        }
    }

    // Search forward from this position for inline commentx
    for (int x = i + 1; x < parent.getContentSize(); x++) {
        Content c = parent.getContent(x);
        if (c.getCType() == Content.CType.Comment) {
            Comment comment = (Comment) c;
            String text = comment.getText().trim();
            if (isValidComment(text)) {
                // This comment pertains to the preceding element
                if (text.startsWith("<<")) {
                    // Strip the "<<"
                    text = text.substring(2);
                    if (comments.length() > 0) {
                        comments.insert(0, '\n');
                    }
                    comments.insert(0, text);
                } else {
                    break;
                }
            }
        } else if (c.getCType() == Content.CType.Element) {
            // Stop looking when we hit another element 
            break;
        }
    }
    return comments.toString();
}

From source file:org.apache.marmotta.ldclient.provider.mediawiki.MediawikiProvider.java

License:Apache License

protected List<String> parseRevision(URI resource, String requestUrl, Model model, ValueFactory valueFactory,
        Element revisions, Context context) throws RepositoryException {
    List<String> followUp = Collections.emptyList();
    if (revisions == null)
        return followUp;
    final Element rev = revisions.getChild("rev");
    if (rev == null)
        return followUp;
    final Resource subject = resource;

    if (context == Context.META && "0".equals(rev.getAttributeValue("parentid"))) {
        // This is the first revision, so we use the creation date
        addLiteralTriple(subject, Namespaces.NS_DC_TERMS + "created", rev.getAttributeValue("timestamp"),
                Namespaces.NS_XSD + "dateTime", model, valueFactory);
    }//from w  w w  .  j a  v a 2  s .  co m
    if (context == Context.CONTENT && rev.getValue() != null && rev.getValue().trim().length() > 0) {
        final String content = rev.getValue().trim();
        final Matcher m = REDIRECT_PATTERN.matcher(content);
        if (((Element) revisions.getParent()).getAttribute("redirect") != null && m.find()) {
            followUp = Collections.singletonList(buildApiPropQueryUrl(requestUrl, m.group(1), "info",
                    getDefaultParams("info", null), Context.REDIRECT));
        } else {
            addLiteralTriple(subject, Namespaces.NS_RSS_CONTENT + "encoded", content,
                    Namespaces.NS_XSD + "string", model, valueFactory);
        }
    }
    return followUp;
}

From source file:org.kdp.word.transformer.FootnodeTransformer.java

License:Apache License

@Override
public void transform(Context context) {

    Map<String, Footnode> footnodes = new LinkedHashMap<>();

    Element root = context.getSourceRoot();
    for (Element el : root.getChildren()) {
        findFootnodes(context, el, footnodes);
    }//  w w  w.j a va  2 s.com

    JDOMFactory factory = context.getJDOMFactory();
    for (Footnode fn : footnodes.values()) {

        // Footnode Ref
        Element fnref = fn.fnref;
        String text = getFootnodeText(fnref);
        Parent parent = fnref.getParent();
        int index = parent.indexOf(fnref);
        parent.removeContent(index);
        Element span = factory.element("span");
        span.setAttribute("class", "MsoFootnoteReference");
        Element a = fnref.clone();
        a.removeContent();
        a.setText(text);
        //a.setAttribute("type", "noteref", OPFTransformer.NS_OPF);
        span.addContent(a);
        parent.addContent(index, span);

        /* Footnode Text
        Element fntxt = fn.fntxt;
        Element p = findMsoFootnoteText(fntxt); 
        text = getFootnodeText(p);
        p.getAttributes().clear();
        p.removeContent();
        p.setText(text);
        String divid = fn.id.substring(1);
        Element div = JDOMUtils.findElement(root, "div", "id", divid);
        IllegalStateAssertion.assertSame(p.getParentElement(), div, "Unexpected parent: " + div);
        Parent divparent = div.getParent();
        Element aside = factory.element("aside");
        aside.setAttribute("type", "footnote", OPFTransformer.NS_OPF);
        aside.setAttribute("id", fn.id);
        index = divparent.indexOf(div);
        divparent.removeContent(div);
        aside.addContent(p.clone());
        divparent.addContent(index, aside);
        */
    }
}

From source file:org.kdp.word.transformer.ListParagraphTransformer.java

License:Apache License

private boolean processItemMarker(Element liItem) {
    String first = "";
    int cosize = liItem.getContentSize();
    Content co = liItem.getContent(0);//from w w  w  .j a va2 s. co m
    if (co.getCType() == CType.Text) {
        Text text = (Text) co;
        String value = text.getText();
        int index = value.indexOf(" ");
        first = value.substring(0, index);
        value = value.substring(index + 1);
        text.setText(value);
    } else if (cosize > 1 && co.getCType() == CType.Element) {
        Element el = (Element) co;
        first = el.getText();
        el.getParent().removeContent(co);
    }
    return first.endsWith(".");
}

From source file:org.kdp.word.transformer.SectionTransformer.java

License:Apache License

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

    Sections sections = new Sections();
    context.putAttribute(Sections.class, sections);

    Element root = context.getSourceRoot();
    for (Element el : root.getChildren()) {
        findWordSections(context, sections, el);
    }//from www  .j  a v a 2  s  .c  o  m

    boolean navfound = false;
    Iterator<Section> itsec = sections.iterator();
    while (itsec.hasNext()) {
        Section section = itsec.next();

        if (navfound) {
            itsec.remove();
            continue;
        }
        navfound = section.isnav;

        // Remove the section from the original document
        Element element = section.element;
        Parent parent = element.getParent();
        parent.removeContent(element);

        // Build the target document 
        Element rootClone = root.clone();
        Element bodyClone = JDOMUtils.findElement(rootClone, "body");
        bodyClone.removeContent();
        bodyClone.addContent(element.clone());

        // Write the section document
        Document doc = factory.document(rootClone);
        File outfile = section.target.toFile();
        try {
            outfile.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(outfile);
            IOUtils.writeDocument(context, doc, fos);
            fos.close();
        } catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }
}