Informa的core包中定義了所有基本的標(biāo)記接口,實(shí)體接口,行為接口。對(duì)應(yīng)的實(shí)現(xiàn)有2種:
★In-memory實(shí)現(xiàn):de.nava.informa.impl.basic
★hibernate實(shí)現(xiàn):de.nava.informa.impl.hibernate
前者在內(nèi)存中實(shí)現(xiàn)了Channel和Item的創(chuàng)建,訪問,存儲(chǔ)(內(nèi)存數(shù)據(jù)庫),后者通過Hibernate來持久化,訪問。根據(jù)Informa的文檔,最簡(jiǎn)單的是basic實(shí)現(xiàn)。先從這個(gè)入手:

可以看到在basic包中,類并不是很多。其中幾個(gè)在core包出現(xiàn)的接口如:ChannelParser, ChannelExporter, ChannelObservable, ChannelObserver都沒有在basic中實(shí)現(xiàn)。它們分別在各自的parser, exporter包中被實(shí)現(xiàn)了。而一系列的標(biāo)記接口則分別被basic包中的一眾類實(shí)現(xiàn)了。
basic包其實(shí)非常簡(jiǎn)單,幾乎就是一系列的Java Bean,經(jīng)過幾個(gè)小時(shí)的學(xué)習(xí),總結(jié)了里面幾個(gè)比較重要的類:
★Channel類
Channel類構(gòu)造方法如下:

public Channel(Element channelElement, String title)
{
this.id = IdGenerator.getInstance().getId();
this.channelElement = channelElement;
this.title = title;
this.items = Collections.synchronizedMap((new LinkedHashMap<Long, ItemIF>()));
this.categories = new ArrayList<CategoryIF>();
this.observers = new ArrayList<ChannelObserverIF>();
this.format = ChannelFormat.UNKNOWN_CHANNEL_FORMAT;
this.lastUpdated = new Date();
}
在Channel內(nèi)部了定義了RSS規(guī)范中所需的屬性,其中比較重要的就是items:即該channel下的所有item。其次是categories:標(biāo)明該channel屬于哪個(gè)(些)分類。除了直接地從外部接收參數(shù)構(gòu)造,Channel類還提供了一個(gè)“cheap-copy”方法,叫做setAllProperties。該方法的實(shí)現(xiàn)如下:

/** *//**
* setAllProperties - Set all the properties in this Channel by copying them
* from a source Channel.
* N.B. This includes all properties, but not children such as items etc.
* N.B. Location and Format are also not copied.
*
* @param sourceChan - ChannelIF that will supply new values
*/

public void setAllProperties(ChannelIF sourceChan)
{
setTitle(sourceChan.getTitle());
setDescription(sourceChan.getDescription());
setSite(sourceChan.getSite());
setCreator(sourceChan.getCreator());
setCopyright(sourceChan.getCopyright());
setPublisher(sourceChan.getPublisher());
setLanguage(sourceChan.getLanguage());
setImage(sourceChan.getImage());

}
為什么說這是一個(gè)“cheap-copy”呢?從API中就可以看出:它只拷貝頂層的屬性和元素,但對(duì)于嵌套的子元素不會(huì)拷貝。至于為什么location和format不拷貝我想是因?yàn)檫@個(gè)做法通常用于RSS Feed之間的共享,此時(shí)location肯定是不同的,而format也可能不同(大家使用相同的內(nèi)容,但可能有不同格式協(xié)議。如RSS 1.0和RSS 2.0)。
注意在Channel中有一個(gè)觀察者列表:observers---由于ChannelIF繼承了ChannelObservable接口,所以Channel實(shí)現(xiàn)類必須包含,維護(hù)一組對(duì)其感興趣的觀察者列表。

public void addObserver(ChannelObserverIF o)
{
observers.add(o);
}


public void removeObserver(ChannelObserverIF o)
{
observers.remove(o);
}
當(dāng)channel中新增了item的時(shí)候,channel必須及時(shí)通知這些觀察者有對(duì)應(yīng)的事件發(fā)生。在ChannelObserverIF接口中定義了兩種觀察者感興趣的事件:

public interface ChannelObserverIF
{


/** *//**
* Called when a new Item is added to this Channel
*
* @param newItem - Item that was added.
*/
void itemAdded(ItemIF newItem);

/** *//**
* Called when a new Channel is added
*
* @param channel - Channel that was added
*/
void channelRetrieved(ChannelIF channel);
}
可以看到觀察者只對(duì)新增channel和item感興趣,實(shí)際上當(dāng)channel本身有信心更新時(shí),也會(huì)調(diào)用channelRetrieved方法。但是對(duì)于channel和item的刪除事件并不在這里定義,而是在另外一個(gè)包:cleaner中定義。
channel中通過addItem,removeItem,getItem來增加,移除,訪問其下的item信息,只有當(dāng)addItem發(fā)生時(shí)才會(huì)通知觀察者:

public void addItem(ItemIF item)
{
items.put(new Long(item.getId()), item);
item.setChannel(this);
notifyObserversItemAdded(item);
}


public void removeItem(ItemIF item)
{
items.remove(new Long(item.getId()));
}


public ItemIF getItem(long anId)
{
return (ItemIF) items.get(new Long(anId));
}
當(dāng)channel自身發(fā)生更新時(shí),也會(huì)通知觀察者

public void setLastUpdated(Date lastUpdated)
{
this.lastUpdated = lastUpdated;
notifyObserversChannelUpdated();
}
請(qǐng)看下面這兩個(gè)notify的過程:

private void notifyObserversItemAdded(ItemIF newItem)
{
Iterator it = observers.iterator();

while (it.hasNext())
{
ChannelObserverIF o = (ChannelObserverIF) it.next();
o.itemAdded(newItem);
}
}


/** *//**
* Loops through and notifies each observer if a new item was
* detected.
*/

private void notifyObserversChannelUpdated()
{
Iterator it = observers.iterator();

while (it.hasNext())
{
ChannelObserverIF o = (ChannelObserverIF) it.next();
o.channelRetrieved(this);
}
}
總結(jié)起來,channel的創(chuàng)建,更新過程如下:
①接收經(jīng)過ChannelParser解析后的XML Element元素,和其他一系列屬性,調(diào)用構(gòu)造方法
②構(gòu)造方法為channel生成一個(gè)全局唯一ID
③構(gòu)造方法創(chuàng)建item列表,觀察者列表,設(shè)置好分類(Category),首次更新時(shí)間
④如果有新item到來,則添加到item列表,同時(shí)更新item的channel歸屬
⑤如果現(xiàn)有channel更新,則設(shè)置最新更新時(shí)間
⑥通知每一個(gè)已注冊(cè)的觀察者有事件發(fā)生
在Channel類里面提供了若干訪問channel子元素的方法,它們分別是:
public String getElementValue(final String path)

public String[] getElementValues(final String path, final String[] elements)

public String getAttributeValue(final String path, final String attribute)

public String[] getAttributeValues(final String path, final String[] attributes)
在這些方法的內(nèi)部,都是使用了一個(gè)叫做XmlPathUtils的類提供的靜態(tài)方法,至于XPATH訪問的Element就是在構(gòu)造方法中緩存的經(jīng)ChannelParser解析后的XML Element。
-------------------------------------------------------------
生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
posted on 2009-12-22 23:31
Paul Lin 閱讀(1257)
評(píng)論(0) 編輯 收藏 所屬分類:
J2SE