Search in sources :

Example 26 with Text

use of org.jdom2.Text in project jspwiki by apache.

the class XHtmlElementToWikiTranslator method printChildren.

private void printChildren(Element base) throws IOException, JDOMException {
    for (Iterator i = base.getContent().iterator(); i.hasNext(); ) {
        Object c = i.next();
        if (c instanceof Element) {
            Element e = (Element) c;
            String n = e.getName().toLowerCase();
            if (n.equals("h1")) {
                m_out.print("\n!!! ");
                print(e);
                m_out.println();
            } else if (n.equals("h2")) {
                m_out.print("\n!!! ");
                print(e);
                m_out.println();
            } else if (n.equals("h3")) {
                m_out.print("\n!! ");
                print(e);
                m_out.println();
            } else if (n.equals("h4")) {
                m_out.print("\n! ");
                print(e);
                m_out.println();
            } else if (n.equals("p")) {
                if (// we don't want to print empty elements: <p></p>
                e.getContentSize() != 0) {
                    m_out.println();
                    print(e);
                    m_out.println();
                }
            } else if (n.equals("br")) {
                if (m_preStack.isPreMode()) {
                    m_out.println();
                } else {
                    String parentElementName = base.getName().toLowerCase();
                    // 
                    if (parentElementName.matches("p|div") && !base.getText().matches("(?s).*\\[\\{.*\\}\\].*")) {
                        m_out.print(" \\\\\n");
                    } else {
                        m_out.print(" \\\\");
                    }
                }
                print(e);
            } else if (n.equals("hr")) {
                m_out.println();
                print("----");
                print(e);
                m_out.println();
            } else if (n.equals("table")) {
                if (!m_outTimmer.isCurrentlyOnLineBegin()) {
                    m_out.println();
                }
                print(e);
            } else if (n.equals("tr")) {
                print(e);
                m_out.println();
            } else if (n.equals("td")) {
                m_out.print("| ");
                print(e);
                if (!m_preStack.isPreMode()) {
                    print(" ");
                }
            } else if (n.equals("th")) {
                m_out.print("|| ");
                print(e);
                if (!m_preStack.isPreMode()) {
                    print(" ");
                }
            } else if (n.equals("a")) {
                if (!isIgnorableWikiMarkupLink(e)) {
                    if (e.getChild("IMG") != null) {
                        printImage(e);
                    } else {
                        String ref = e.getAttributeValue("href");
                        if (ref == null) {
                            if (isUndefinedPageLink(e)) {
                                m_out.print("[");
                                print(e);
                                m_out.print("]");
                            } else {
                                print(e);
                            }
                        } else {
                            ref = trimLink(ref);
                            if (ref != null) {
                                if (// This is a link to a footnote.
                                ref.startsWith("#")) {
                                    // convert "#ref-PageName-1" to just "1"
                                    String href = ref.replaceFirst("#ref-.+-(\\d+)", "$1");
                                    // remove the brackets around "[1]"
                                    String textValue = e.getValue().substring(1, (e.getValue().length() - 1));
                                    if (href.equals(textValue)) {
                                        // handles the simplest case. Example: [1]
                                        print(e);
                                    } else {
                                        // handles the case where the link text is different from the href. Example: [something|1]
                                        m_out.print("[" + textValue + "|" + href + "]");
                                    }
                                } else {
                                    Map augmentedWikiLinkAttributes = getAugmentedWikiLinkAttributes(e);
                                    m_out.print("[");
                                    print(e);
                                    if (!e.getTextTrim().equalsIgnoreCase(ref)) {
                                        m_out.print("|");
                                        print(ref);
                                        if (!augmentedWikiLinkAttributes.isEmpty()) {
                                            m_out.print("|");
                                            String augmentedWikiLink = augmentedWikiLinkMapToString(augmentedWikiLinkAttributes);
                                            m_out.print(augmentedWikiLink);
                                        }
                                    } else if (!augmentedWikiLinkAttributes.isEmpty()) {
                                        // If the ref has the same value as the text and also if there
                                        // are attributes, then just print: [ref|ref|attributes] .
                                        m_out.print("|" + ref + "|");
                                        String augmentedWikiLink = augmentedWikiLinkMapToString(augmentedWikiLinkAttributes);
                                        m_out.print(augmentedWikiLink);
                                    }
                                    m_out.print("]");
                                }
                            }
                        }
                    }
                }
            } else if (n.equals("b") || n.equals("strong")) {
                m_out.print("__");
                print(e);
                m_out.print("__");
            } else if (n.equals("i") || n.equals("em") || n.equals("address")) {
                m_out.print("''");
                print(e);
                m_out.print("''");
            } else if (n.equals("u")) {
                m_out.print("%%( text-decoration:underline; )");
                print(e);
                m_out.print("/%");
            } else if (n.equals("strike")) {
                m_out.print("%%strike ");
                print(e);
                m_out.print("/%");
            // NOTE: don't print a space before or after the double percents because that can break words into two.
            // For example: %%(color:red)ABC%%%%(color:green)DEF%% is different from %%(color:red)ABC%% %%(color:green)DEF%%
            } else if (n.equals("sup")) {
                m_out.print("%%sup ");
                print(e);
                m_out.print("/%");
            } else if (n.equals("sub")) {
                m_out.print("%%sub ");
                print(e);
                m_out.print("/%");
            } else if (n.equals("dl")) {
                m_out.print("\n");
                print(e);
                // print a newline after the definition list. If we don't,
                // it may cause problems for the subsequent element.
                m_out.print("\n");
            } else if (n.equals("dt")) {
                m_out.print(";");
                print(e);
            } else if (n.equals("dd")) {
                m_out.print(":");
                print(e);
            } else if (n.equals("ul")) {
                m_out.println();
                m_liStack.push("*");
                print(e);
                m_liStack.pop();
            } else if (n.equals("ol")) {
                m_out.println();
                m_liStack.push("#");
                print(e);
                m_liStack.pop();
            } else if (n.equals("li")) {
                m_out.print(m_liStack + " ");
                print(e);
                // The following line assumes that the XHTML has been "pretty-printed"
                // (newlines separate child elements from their parents).
                boolean lastListItem = base.indexOf(e) == (base.getContentSize() - 2);
                boolean sublistItem = m_liStack.toString().length() > 1;
                // only print a newline if this <li> element is not the last item within a sublist.
                if (!sublistItem || !lastListItem) {
                    m_out.println();
                }
            } else if (n.equals("pre")) {
                // start JSPWiki "code blocks" on its own line
                m_out.print("\n{{{");
                m_preStack.push();
                print(e);
                m_preStack.pop();
                // print a newline after the closing braces
                // to avoid breaking any subsequent wiki markup that follows.
                m_out.print("}}}\n");
            } else if (n.equals("code") || n.equals("tt")) {
                m_out.print("{{");
                m_preStack.push();
                print(e);
                m_preStack.pop();
                m_out.print("}}");
            // NOTE: don't print a newline after the closing brackets because if the Text is inside
            // a table or list, it would break it if there was a subsequent row or list item.
            } else if (n.equals("img")) {
                if (!isIgnorableWikiMarkupLink(e)) {
                    m_out.print("[");
                    print(trimLink(e.getAttributeValue("src")));
                    m_out.print("]");
                }
            } else if (n.equals("form")) {
                // remove the hidden input where name="formname" since a new one will be generated again when the xhtml is rendered.
                Element formName = (Element) XPath.selectSingleNode(e, "INPUT[@name='formname']");
                if (formName != null) {
                    formName.detach();
                }
                String name = e.getAttributeValue("name");
                m_out.print("\n[{FormOpen");
                if (name != null) {
                    m_out.print(" form='" + name + "'");
                }
                m_out.print("}]\n");
                print(e);
                m_out.print("\n[{FormClose}]\n");
            } else if (n.equals("input")) {
                String type = e.getAttributeValue("type");
                String name = e.getAttributeValue("name");
                String value = e.getAttributeValue("value");
                String checked = e.getAttributeValue("checked");
                m_out.print("[{FormInput");
                if (type != null) {
                    m_out.print(" type='" + type + "'");
                }
                if (name != null) {
                    // remove the "nbf_" that was prepended since new one will be generated again when the xhtml is rendered.
                    if (name.startsWith("nbf_")) {
                        name = name.substring(4, name.length());
                    }
                    m_out.print(" name='" + name + "'");
                }
                if (value != null && !value.equals("")) {
                    m_out.print(" value='" + value + "'");
                }
                if (checked != null) {
                    m_out.print(" checked='" + checked + "'");
                }
                m_out.print("}]");
                print(e);
            } else if (n.equals("textarea")) {
                String name = e.getAttributeValue("name");
                String rows = e.getAttributeValue("rows");
                String cols = e.getAttributeValue("cols");
                m_out.print("[{FormTextarea");
                if (name != null) {
                    if (name.startsWith("nbf_")) {
                        name = name.substring(4, name.length());
                    }
                    m_out.print(" name='" + name + "'");
                }
                if (rows != null) {
                    m_out.print(" rows='" + rows + "'");
                }
                if (cols != null) {
                    m_out.print(" cols='" + cols + "'");
                }
                m_out.print("}]");
                print(e);
            } else if (n.equals("select")) {
                String name = e.getAttributeValue("name");
                m_out.print("[{FormSelect");
                if (name != null) {
                    if (name.startsWith("nbf_")) {
                        name = name.substring(4, name.length());
                    }
                    m_out.print(" name='" + name + "'");
                }
                m_out.print(" value='");
                print(e);
                m_out.print("'}]");
            } else if (n.equals("option")) {
                // is expected to be a newline character which is at index of 0).
                if (base.indexOf(e) != 1) {
                    m_out.print(";");
                }
                Attribute selected = e.getAttribute("selected");
                if (selected != null) {
                    m_out.print("*");
                }
                String value = e.getAttributeValue("value");
                if (value != null) {
                    m_out.print(value);
                } else {
                    print(e);
                }
            } else {
                print(e);
            }
        } else {
            print(c);
        }
    }
}
Also used : Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 27 with Text

use of org.jdom2.Text in project jspwiki by apache.

the class CleanTextRenderer method getString.

/**
 *  {@inheritDoc}
 */
public String getString() throws IOException {
    StringBuilder sb = new StringBuilder();
    try {
        XPath xp = XPath.newInstance(ALL_TEXT_NODES);
        List nodes = xp.selectNodes(m_document.getDocument());
        for (Iterator i = nodes.iterator(); i.hasNext(); ) {
            Object el = i.next();
            if (el instanceof Text) {
                sb.append(((Text) el).getValue());
            }
        }
    } catch (JDOMException e) {
        log.error("Could not parse XPATH expression");
        throw new IOException(e.getMessage());
    }
    return sb.toString();
}
Also used : XPath(org.jdom2.xpath.XPath) Iterator(java.util.Iterator) List(java.util.List) Text(org.jdom2.Text) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException)

Example 28 with Text

use of org.jdom2.Text in project jspwiki by apache.

the class CreoleRenderer method renderElement.

/**
 * Renders an element into the StringBuilder given
 * @param ce
 * @param sb
 */
private void renderElement(Element ce, StringBuilder sb) {
    String endEl = EMPTY_STRING;
    for (int i = 0; i < ELEMENTS.length; i += 3) {
        if (ELEMENTS[i].equals(ce.getName())) {
            sb.append(ELEMENTS[i + 1]);
            endEl = ELEMENTS[i + 2];
        }
    }
    if (UL.equals(ce.getName())) {
        m_listCount++;
        m_listChar = '*';
    } else if (OL.equals(ce.getName())) {
        m_listCount++;
        m_listChar = '#';
    } else if (LI.equals(ce.getName())) {
        for (int i = 0; i < m_listCount; i++) sb.append(m_listChar);
        sb.append(ONE_SPACE);
    } else if (A.equals(ce.getName())) {
        String href = ce.getAttributeValue(HREF_ATTRIBUTE);
        String text = ce.getText();
        if (href.equals(text)) {
            sb.append(HREF_START + href + HREF_END);
        } else {
            sb.append(HREF_START + href + HREF_DELIMITER + text + HREF_END);
        }
        // Do not render anything else
        return;
    } else if (PRE.equals(ce.getName())) {
        sb.append(PRE_START);
        sb.append(ce.getText());
        sb.append(PRE_END);
        return;
    }
    // 
    for (Iterator<Content> i = ce.getContent().iterator(); i.hasNext(); ) {
        Content c = i.next();
        if (c instanceof PluginContent) {
            PluginContent pc = (PluginContent) c;
            if (pc.getPluginName().equals(PLUGIN_IMAGE)) {
                sb.append(IMG_START + pc.getParameter(PARAM_SRC) + IMG_END);
            } else {
                m_plugins.add(pc);
                sb.append(PLUGIN_START + pc.getPluginName() + ONE_SPACE + m_plugins.size() + PLUGIN_END);
            }
        } else if (c instanceof Text) {
            sb.append(((Text) c).getText());
        } else if (c instanceof Element) {
            renderElement((Element) c, sb);
        }
    }
    if (UL.equals(ce.getName()) || OL.equals(ce.getName())) {
        m_listCount--;
    } else if (P.equals(ce.getName())) {
        sb.append(LINEBREAK);
    }
    sb.append(endEl);
}
Also used : PluginContent(org.apache.wiki.parser.PluginContent) Content(org.jdom2.Content) Element(org.jdom2.Element) Text(org.jdom2.Text) PluginContent(org.apache.wiki.parser.PluginContent)

Example 29 with Text

use of org.jdom2.Text in project ldapchai by ldapchai.

the class ChaiResponseSet method challengeToXml.

private static Element challengeToXml(final Challenge loopChallenge, final Answer answer, final String elementName) throws ChaiOperationException {
    final Element responseElement = new Element(elementName);
    responseElement.addContent(new Element(XML_NODE_CHALLENGE).addContent(new Text(loopChallenge.getChallengeText())));
    final Element answerElement = answer.toXml();
    responseElement.addContent(answerElement);
    responseElement.setAttribute(XML_ATTRIBUTE_ADMIN_DEFINED, String.valueOf(loopChallenge.isAdminDefined()));
    responseElement.setAttribute(XML_ATTRIBUTE_REQUIRED, String.valueOf(loopChallenge.isRequired()));
    responseElement.setAttribute(XNL_ATTRIBUTE_MIN_LENGTH, String.valueOf(loopChallenge.getMinLength()));
    responseElement.setAttribute(XNL_ATTRIBUTE_MAX_LENGTH, String.valueOf(loopChallenge.getMaxLength()));
    return responseElement;
}
Also used : Element(org.jdom2.Element) Text(org.jdom2.Text)

Example 30 with Text

use of org.jdom2.Text in project incubator-sdap-mudrod by apache.

the class AggregateTriples method loopxml.

/**
 * Method of going through OWL structure
 */
public void loopxml() {
    Iterator<?> processDescendants = rootNode.getDescendants(new ElementFilter());
    String text = "";
    while (processDescendants.hasNext()) {
        Element e = (Element) processDescendants.next();
        String currentName = e.getName();
        text = e.getTextTrim();
        if ("".equals(text)) {
            LOG.info(currentName);
        } else {
            LOG.info("{} : {}", currentName, text);
        }
    }
}
Also used : ElementFilter(org.jdom2.filter.ElementFilter) Element(org.jdom2.Element)

Aggregations

Element (org.jdom2.Element)83 Document (org.jdom2.Document)36 File (java.io.File)21 ProcessingInstruction (org.jdom2.ProcessingInstruction)17 IOException (java.io.IOException)14 Text (org.jdom2.Text)12 Attribute (org.jdom2.Attribute)11 XMLOutputter (org.jdom2.output.XMLOutputter)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 XmlFile (jmri.jmrit.XmlFile)9 ArrayList (java.util.ArrayList)6 JDOMException (org.jdom2.JDOMException)6 StringWriter (java.io.StringWriter)5 NamedIcon (jmri.jmrit.catalog.NamedIcon)5 FileOutputStream (java.io.FileOutputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 Locale (java.util.Locale)3