
nuggets.JavaXMLReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-nuggets Show documentation
Show all versions of jadex-nuggets Show documentation
The Jadex nuggets is a fast Java bean to
XML and back converter.
The newest version!
/*
* JavaXMLWriter.java
* Copyright (c) 2005 by University of Hamburg. All Rights Reserved.
* Departament of Informatics.
* Distributed Systems and Information Systems.
*
* Created by walczak on Jan 11, 2006.
* Last revision $Revision: 6926 $ by:
* $Author: braubach $ on $Date: 2008-09-28 22:16:58 +0200 (So, 28 Sep 2008) $.
*/
package nuggets;
import java.io.IOException;
import java.io.Reader;
import nuggets.util.Base64;
import nuggets.util.CharStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
/**
* JavaXMLWriter - writes a raw XML with java packages for ontology purpose.
* This can be used only with java.
*
* @author walczak
* @since Jan 11, 2006
*/
public class JavaXMLReader implements IReader
{
private XmlPullParser xpp;
private final CharStream cs = new CharStream();
/** Initializes the Reader
* Constructor for JavaXMLReader.
*/
public JavaXMLReader() {
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
xpp = factory.newPullParser();
}
catch(XmlPullParserException e)
{
throw new PersistenceException(e);
}
}
/**
* @param rdr
* @see nuggets.IReader#start(java.io.Reader)
*/
public void start(Reader rdr)
{
try
{
xpp.setInput(rdr);
int i;
while((i = xpp.next())!=XmlPullParser.START_TAG) {
if (i==XmlPullParser.END_DOCUMENT) throw new PersistenceException("Input ended befor the first element");
}
String name;
if (!"n:root".equals(name = xpp.getName())) {
throw new PersistenceException("Encountered wrong element: "+name);
}
attributes=readAttributes();
}
catch(XmlPullParserException e)
{
throw new PersistenceException(e);
}
catch(IOException e)
{
throw new PersistenceException(e);
}
}
final Buffer buffer = new Buffer();
final int[] oAndL = new int[2];
private String[] attributes;
/**
* @return a
* @see nuggets.IReader#getText()
*/
public Buffer getText()
{
int i;
try
{
while((i = xpp.next())!=XmlPullParser.TEXT) {
if (i==XmlPullParser.END_DOCUMENT || i==XmlPullParser.END_TAG) {
buffer.set(null, 0, 0);
return buffer;
}
}
buffer.set(xpp.getTextCharacters(oAndL), oAndL[0], oAndL[1]);
return buffer;
}
catch(XmlPullParserException e)
{
throw new PersistenceException(e);
}
catch(IOException e)
{
throw new PersistenceException(e);
}
}
/**
* @return byte array from the text element encoded in Base64
* @see nuggets.IReader#getData()
*/
public byte[] getData()
{
int i;
try
{
while((i = xpp.next())!=XmlPullParser.TEXT) {
if (i==XmlPullParser.END_DOCUMENT) {
return null;
}
}
buffer.set(xpp.getTextCharacters(oAndL), oAndL[0], oAndL[1]);
return Base64.decode(buffer.chars, buffer.start, buffer.len);
}
catch(XmlPullParserException e)
{
throw new PersistenceException(e);
}
catch(IOException e)
{
throw new PersistenceException(e);
}
}
/**
* @return the name of the next element or null
* @see nuggets.IReader#nextElement()
*/
public String nextElement()
{
int i;
try
{
while((i = xpp.next())!=XmlPullParser.START_TAG) {
if (i==XmlPullParser.END_DOCUMENT) return null;
}
attributes=readAttributes();
return decodeTag(xpp.getName());
}
catch(XmlPullParserException e)
{
throw new PersistenceException(e);
}
catch(IOException e)
{
throw new PersistenceException(e);
}
}
/**
* @return the attributes decoded
*/
private String[] readAttributes()
{
int count=xpp.getAttributeCount();
String[] attr=new String[count];
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy