/*
* FeedDestination.java
*
* Created on May 22, 2007, 9:42 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.atomojo.app.client;
import java.net.URI;
import java.util.logging.Logger;
import org.infoset.xml.Document;
import org.infoset.xml.Element;
import org.infoset.xml.Item;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.Location;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.DocumentDestination;
/**
*
* @author alex
*/
public class FeedDestination implements ItemDestination
{
int level;
DocumentDestination docDest = null;
boolean feedSent = false;
/** Creates a new instance of FeedDestination */
public FeedDestination()
{
int level = 0;
}
public void send(Item item)
throws XMLException
{
switch (item.getType()) {
case DocumentItem:
break;
case ElementItem:
{
level++;
Element e = (Element)item;
switch (level) {
case 1:
if (!e.getName().equals(XML.FEED_NAME)) {
throw new XMLException("Feed does not start with an Atom 'feed' element: "+e.getName(),(Location)e);
}
docDest = new DocumentDestination();
//System.out.println("Feed: "+e.isBaseURIInherited()+", "+e.getBaseURI());
docDest.send(e.getInfoset().createItemConstructor().createDocument(e.getBaseURI()));
docDest.send(e);
break;
case 2:
if (!feedSent) {
if (e.getName().equals(XML.ENTRY_NAME)) {
// send the feed document
this.onFeed(docDest.getDocument());
feedSent = true;
// Now collect the first entry
//System.out.println("Entry: "+e.isBaseURIInherited()+", "+e.getBaseURI());
docDest = new DocumentDestination();
docDest.send(e.getInfoset().createItemConstructor().createDocument(e.getBaseURI()));
e.localizeNamespaceDeclarations();
docDest.send(e);
} else {
docDest.send(e);
}
} else {
if (!e.getName().equals(XML.ENTRY_NAME)) {
throw new XMLException("Non-entry element after the first entry: "+e.getName(),(Location)e);
}
// collect the entry
//System.out.println("Entry: "+e.isBaseURIInherited()+", "+e.getBaseURI());
docDest = new DocumentDestination();
docDest.send(e.getInfoset().createItemConstructor().createDocument(e.getBaseURI()));
docDest.send(e);
}
break;
default:
if (docDest!=null) {
docDest.send(item);
}
}
}
break;
case ElementEndItem:
if (docDest!=null) {
docDest.send(item);
}
level--;
switch (level) {
case 0:
if (!feedSent) {
this.onFeed(docDest.getDocument());
docDest = null;
}
break;
case 1:
if (feedSent && docDest!=null) {
this.onEntry(docDest.getDocument());
docDest = null;
}
}
break;
case DocumentEndItem:
onEnd();
break;
default:
if (docDest!=null) {
docDest.send(item);
}
}
}
public void onFeed(Document feedDoc)
throws XMLException
{
}
public void onEntry(Document entryDoc)
throws XMLException
{
}
public void onEnd()
throws XMLException
{
}
}