All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openfuxml.transform.xhtml.emitter.SimpleEmitter Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package org.openfuxml.transform.xhtml.emitter;

import java.util.Map;
import java.util.TreeMap;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.openfuxml.transform.xhtml.EmitterFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;

public class SimpleEmitter extends NestingEmitter
{
	final static Logger logger = LoggerFactory.getLogger(SimpleEmitter.class);
	
	private final String[] tags;

	private Map attributes;

	public SimpleEmitter(EmitterFactory ef, String... ofxTagNames)
	{
		super(ef);
		tags = ofxTagNames;
	}

	public void setAttribute(String name, String value)
	{
		if (attributes == null)
		{
			attributes = new TreeMap();
		}
		attributes.put(name, value);
	}

	@Override
	protected boolean localStart(XMLStreamWriter writer, String htmlElementName, Attributes atts) throws XMLStreamException
	{
		for (String tag : tags)
		{
			writer.writeStartElement(tag);
		}
		boolean hasId = false;
		if (attributes != null)
		{
			for (Map.Entry attr : attributes.entrySet())
			{
				writer.writeAttribute(attr.getKey(), attr.getValue());
			}
			if (attributes.containsKey("id")) {hasId = true;}
		}
		if(!hasId)
		{
			String elementId = atts.getValue("id");
			if (elementId != null)
			{
				writer.writeAttribute("id", elementId);
			}
		}
		return true;
	}
	
	@Override
	protected boolean localEnd(XMLStreamWriter writer, String htmlElementName) throws XMLStreamException
	{
		logger.trace("localEnd "+tags.length);
		for (int x = 0; x < tags.length; ++x)
		{
			writer.writeEndElement();
		}
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy