org.dhatim.delivery.dom.serialize.TextSerializationUnit.java Source code

Java tutorial

Introduction

Here is the source code for org.dhatim.delivery.dom.serialize.TextSerializationUnit.java

Source

/*
   Milyn - Copyright (C) 2006 - 2010
    
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License (version 2.1) as published by the Free Software
   Foundation.
    
   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:
   http://www.gnu.org/licenses/lgpl.txt
*/
package org.dhatim.delivery.dom.serialize;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dhatim.SmooksException;
import org.dhatim.container.ExecutionContext;
import org.dhatim.delivery.sax.SAXElement;
import org.dhatim.delivery.sax.SAXElementVisitor;
import org.dhatim.delivery.sax.SAXText;
import org.dhatim.delivery.sax.SAXVisitBefore;
import org.dhatim.xml.DomUtils;
import org.dhatim.xml.Namespace;
import org.w3c.dom.*;

import java.io.IOException;
import java.io.Writer;

/**
 * Write a <text> element.
 * <p/>
 * Basically just drops the &lt;text&gt; tags. 
 *
 * @author <a href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
 */
public class TextSerializationUnit extends DefaultSerializationUnit implements SAXVisitBefore {

    public void writeElementStart(Element element, Writer writer, ExecutionContext executionContext)
            throws IOException {
    }

    public void writeElementEnd(Element element, Writer writer, ExecutionContext executionContext)
            throws IOException {
    }

    public void visitBefore(SAXElement element, ExecutionContext executionContext)
            throws SmooksException, IOException {
    }

    public static Element createTextElement(Element element, String templatingResult) {
        Document ownerDocument = element.getOwnerDocument();
        Element resultElement = ownerDocument.createElementNS(Namespace.SMOOKS_URI, "text");
        resultElement.appendChild(ownerDocument.createTextNode(templatingResult));
        return resultElement;
    }

    public static boolean isTextElement(Element element) {
        if (DomUtils.getName(element).equals("text") && Namespace.SMOOKS_URI.equals(element.getNamespaceURI())) {
            return true;
        }

        return false;
    }

    public static String getText(Element element) {
        return DomUtils.getAllText(element, false);
    }
}