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

jadex.xml.writer.PullParserWriter Maven / Gradle / Ivy

package jadex.xml.writer;

import jadex.commons.collection.Tree;
import jadex.commons.collection.TreeNode;
import jadex.xml.IPreProcessor;
import jadex.xml.SXML;
import jadex.xml.StackElement;
import jadex.xml.TypeInfo;
import jadex.xml.stax.QName;

import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.XMLConstants;

import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

public class PullParserWriter extends AWriter
{
	
	public static final String NULL_NS_URI = "";


	// -------- static part --------

	/** The xml output factory. */
	protected static XmlPullParserFactory FACTORY;
	
	static {
		try
		{
			FACTORY = XmlPullParserFactory.newInstance();
		} catch (XmlPullParserException e)
		{
			e.printStackTrace();
		}
	}
	
	// -------- attributes --------
	/** Control flag for generating ids. */
	protected boolean genids;	
	
	/** Control flag for generating indention. */
	protected boolean indent;


	public PullParserWriter(boolean genIds, boolean indent)
	{
		this.genids = genIds;
		this.indent = indent;
	}

	@Override
	public void write(IObjectWriterHandler handler, Object object, String encoding, OutputStream out, ClassLoader classloader,
			Object context) throws Exception
	{
		XmlSerializer writer;
		synchronized (FACTORY)
		{
			writer = FACTORY.newSerializer();
			writer.setOutput(out, encoding);
		}

		writer.startDocument(encoding, false);
		writer.text(SXML.lf);
		WriteContextAndroid wc = new WriteContextAndroid(handler, writer, context, object, classloader);
		writeObject(wc, object, null);
		writer.endDocument();
//		writer.flush();
	}

	private void writeObject(WriteContextAndroid wc, Object object, QName tag) throws Exception
	{
		XmlSerializer writer = wc.getWriter();
		List stack = wc.getStack();
		
		// Special case null
		if(object==null)
		{
//			writeStartObject(writer, tag==null? SXML.NULL: tag, stack.size());
			writeStartObject(writer, SXML.NULL, stack.size());
			writeEndObject(writer, stack.size());
			return;
		}
		
//		if(tagname!=null)
//			System.out.println("tagname: "+tagname);
//		if(object.getClass().getName().indexOf("ComponentIdentifier")!=-1)
//			System.out.println("cfs");
		
		TypeInfo typeinfo = wc.getHandler().getTypeInfo(object, getXMLPath(stack), wc); 
		QName[] path = new QName[0];
		
		// Preprocess object.
		IPreProcessor[] preprocs = wc.getHandler().getPreProcessors(object, typeinfo);
		if(preprocs!=null && preprocs.length>0)
		{
//			System.out.println("found: "+object);
			for(int i=0; i or &
					// Must not use cdata when content is already cdata (nested cdata are not allowed)
					if((content.indexOf("<")!=-1 || content.indexOf(">")!=-1 || content.indexOf("&")!=-1)
						&& content.indexOf("




© 2015 - 2025 Weber Informatics LLC | Privacy Policy