edu.jdomengine.core.node.JDomText.java Source code

Java tutorial

Introduction

Here is the source code for edu.jdomengine.core.node.JDomText.java

Source

/* 
 *   Copyright (C) 2014 Arnaud PETIT <arnaud.petit@minesdedouai.fr>
 *   
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *   
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *   
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 *   MA 02110-1301 USA
 */

package edu.jdomengine.core.node;

import org.apache.commons.lang.StringEscapeUtils;

import edu.jdomengine.core.JDomBaseNode;
import edu.jdomengine.interfaces.IJDom;
import edu.jdomengine.interfaces.IJDomNode;
import edu.jdomengine.interfaces.accept.node.IJDomParentText;
import edu.jdomengine.interfaces.node.IJDomText;

/**
 * A Text node represents some text content. The inner string of this node
 * will be html-escaped during rendering.
 */
public class JDomText extends JDomBaseNode implements IJDomText {
    private static final long serialVersionUID = 1L;

    /**
     * Factory method for the {@link JDomText} node.
     * @param node a {@link IJDomParentText} the parent node of this one
     * @return a new {@link JDomText} node
     */
    public static JDomText createNode(IJDomParentText node) {
        return new JDomText(node);
    }

    private JDomText(IJDomNode node) {
        super(node);
    }

    private String text = IJDom.EMPTY_STRING;

    @Override
    public String getText() {
        return this.text;
    }

    @Override
    public void setText(String string) {
        this.text = StringEscapeUtils.unescapeHtml(string);
    }

    @Override
    public String getName() {
        return NODE_TEXT;
    }

    @Override
    public StringBuilder toHtml5(StringBuilder str) {
        return str.append(StringEscapeUtils.escapeHtml(getText()));
    }

    @Override
    public StringBuilder renderHtml5(StringBuilder str) {
        if (needsRender()) {
            toHtml5(str);
        }
        return str;
    }

    @Override
    public boolean needsRender() {
        return !IJDom.EMPTY_STRING.equals(getText());
    }

}