Example usage for org.jdom2 Text getTextTrim

List of usage examples for org.jdom2 Text getTextTrim

Introduction

In this page you can find the example usage for org.jdom2 Text getTextTrim.

Prototype

public String getTextTrim() 

Source Link

Document

This returns the textual content with all surrounding whitespace removed.

Usage

From source file:ditatools.translate.DitaTranslator.java

License:Apache License

private void handleText(Text text) {
    if (text.getTextTrim().length() > 0) {
        try {//from  w  ww.  j  a v  a  2 s.c  om
            Translation fromEnglish = translator.translate(text.getText(), DOC_LANGUAGE, language);

            // String translatedValue = fromEnglish.getTranslatedText();
            // System.out.println(translatedValue);
            // printCharacters(translatedValue);
            // text.setText(translatedValue);

            // XXX better handling of API errors; in particular, wait
            //  and retry when hitting a short term API rate limit.

            text.setText(fromEnglish.getTranslatedText());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (TranslatorException e) {
            e.printStackTrace();
        }

    }

}

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

License:Apache License

/**
 * Method updateElement./*  www .java  2  s .  com*/
 * 
 * @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.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method insertAtPreferredLocation.//from w w  w  . ja  v a2s. c  om
 * 
 * @param parent
 * @param counter
 * @param child
 */
public static void insertAtPreferredLocation(final Element parent, final Element child,
        final IndentationCounter counter) {
    int contentIndex = 0;
    int elementCounter = 0;
    final Iterator it = parent.getContent().iterator();
    Text lastText = null;
    int offset = 0;
    while (it.hasNext() && elementCounter <= counter.getCurrentIndex()) {
        final Object next = it.next();
        offset = offset + 1;
        if (next instanceof Element) {
            elementCounter = elementCounter + 1;
            contentIndex = contentIndex + offset;
            offset = 0;
        }
        if (next instanceof Text && it.hasNext()) {
            lastText = (Text) next;
        }
    }
    if (lastText != null && lastText.getTextTrim().length() == 0) {
        lastText = lastText.clone();
    } else {
        String starter = lineSeparator;
        for (int i = 0; i < counter.getDepth(); i++) {
            starter = starter + INDENT; // TODO make settable?
        }
        lastText = factory.text(starter);
    }
    if (parent.getContentSize() == 0) {
        final Text finalText = lastText.clone();
        finalText.setText(finalText.getText().substring(0, finalText.getText().length() - INDENT.length()));
        parent.addContent(contentIndex, finalText);
    }
    parent.addContent(contentIndex, child);
    parent.addContent(contentIndex, lastText);
}

From source file:org.apache.maven.toolchain.model.io.jdom.MavenToolchainsJDOMWriter.java

License:Apache License

/**
 * Method insertAtPreferredLocation.// ww  w  . jav a  2  s.c o m
 * 
 * @param parent
 * @param counter
 * @param child
 */
protected void insertAtPreferredLocation(final Element parent, final Element child,
        final IndentationCounter counter) {
    int contentIndex = 0;
    int elementCounter = 0;
    final Iterator it = parent.getContent().iterator();
    Text lastText = null;
    int offset = 0;
    while (it.hasNext() && elementCounter <= counter.getCurrentIndex()) {
        final Object next = it.next();
        offset = offset + 1;
        if (next instanceof Element) {
            elementCounter = elementCounter + 1;
            contentIndex = contentIndex + offset;
            offset = 0;
        }
        if (next instanceof Text && it.hasNext()) {
            lastText = (Text) next;
        }
    }
    if (lastText != null && lastText.getTextTrim().length() == 0) {
        lastText = lastText.clone();
    } else {
        String starter = lineSeparator;
        for (int i = 0; i < counter.getDepth(); i++) {
            starter = starter + INDENT; // TODO make settable?
        }
        lastText = factory.text(starter);
    }
    if (parent.getContentSize() == 0) {
        final Text finalText = lastText.clone();
        finalText.setText(finalText.getText().substring(0, finalText.getText().length() - INDENT.length()));
        parent.addContent(contentIndex, finalText);
    }
    parent.addContent(contentIndex, child);
    parent.addContent(contentIndex, lastText);
}

From source file:org.rascalmpl.library.lang.xml.DOM.java

License:Open Source License

private IString getString(boolean trim, Text text) throws Skip {
    if (trim) {//from w ww. ja  v a 2s .  co  m
        java.lang.String s = text.getTextTrim();
        if ("".equals(s)) {
            throw new Skip();
        }
        return vf.string(s);
    }
    return vf.string(text.getText());
}

From source file:recparser.idmef.IdmefParser.java

License:Open Source License

public void listChildren(Element current, Object o, Element parent) {

    if (o != null) {
        Object object;/*ww w  .  jav a 2s.  c  om*/
        String contentName = current.getName();
        Address address = null;
        //Si es Address generamos una nueva instancia
        if (contentName.equals("Address")) {
            address = new Address();
            object = address;
        } else {
            object = o;
        }
        //get the attributes of the current element and add them to the corresponding object of idmefModel package
        List attribute_list = current.getAttributes();
        Iterator it_attribute = attribute_list.iterator();
        while (it_attribute.hasNext()) {
            Attribute attribute = (Attribute) it_attribute.next();
            addAttribute(current, attribute, object);
        }
        //get the contents of the current element and add them to the corresponding object of idmefModel package
        List contents = current.getContent();
        Iterator it_contents = contents.iterator();

        while (it_contents.hasNext()) {
            Object child = it_contents.next();
            if (child instanceof Text) {
                Text t = (Text) child;
                String contentValue = t.getTextTrim();

                if (!("".equals(contentValue))) {
                    addContent(parent, contentName, contentValue, object);
                }
            }
        }
        if (contentName.equals("Address")) {
            if (o instanceof IntrusionTarget) {
                IntrusionTarget target = (IntrusionTarget) o;
                target.setNode(address);
            } else if (o instanceof IntrusionSource) {
                IntrusionSource source = (IntrusionSource) o;
                source.setNode(address);
            }
        }

        //get the children of the current element and call the method recursively.
        List children = current.getChildren();
        Iterator iterator = children.iterator();

        while (iterator.hasNext()) {
            Element child = (Element) iterator.next();
            ;
            listChildren(child, object, current);
        }
    }
}