| 1 |
/** |
|---|
| 2 |
* ActionScript Syndication Library |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (c) 2008 Takanobu Izukawa (humming.via-kitchen.com) and |
|---|
| 5 |
* Spark project (www.libspark.org) |
|---|
| 6 |
* |
|---|
| 7 |
* Dual licensed under the MIT (MIT-LICENSE.txt) |
|---|
| 8 |
* and GPL (GPL-LICENSE.txt) licenses. |
|---|
| 9 |
* |
|---|
| 10 |
*/ |
|---|
| 11 |
package org.libspark.syndication |
|---|
| 12 |
{ |
|---|
| 13 |
import flash.errors.IllegalOperationError; |
|---|
| 14 |
import flash.utils.getQualifiedClassName; |
|---|
| 15 |
import org.libspark.syndication.errors.UnknownFeedError; |
|---|
| 16 |
import org.libspark.syndication.namespaces.rss; |
|---|
| 17 |
import org.libspark.syndication.namespaces.atom; |
|---|
| 18 |
import org.libspark.syndication.namespaces.atom03; |
|---|
| 19 |
|
|---|
| 20 |
/** |
|---|
| 21 |
* Feed. |
|---|
| 22 |
*/ |
|---|
| 23 |
public class Feed implements IFeed |
|---|
| 24 |
{ |
|---|
| 25 |
/** |
|---|
| 26 |
* a reference of feed adapter object. |
|---|
| 27 |
*/ |
|---|
| 28 |
private var _data:IFeed; |
|---|
| 29 |
|
|---|
| 30 |
/** |
|---|
| 31 |
* @inheritDoc |
|---|
| 32 |
*/ |
|---|
| 33 |
public function get title():String |
|---|
| 34 |
{ |
|---|
| 35 |
return _data.title; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
/** |
|---|
| 39 |
* @inheritDoc |
|---|
| 40 |
*/ |
|---|
| 41 |
public function get link():String |
|---|
| 42 |
{ |
|---|
| 43 |
return _data.link; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
/** |
|---|
| 47 |
* @inheritDoc |
|---|
| 48 |
*/ |
|---|
| 49 |
public function get entries():Array |
|---|
| 50 |
{ |
|---|
| 51 |
return _data.entries; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
/** |
|---|
| 55 |
* Constructor. |
|---|
| 56 |
*/ |
|---|
| 57 |
public function Feed(data:IFeed) |
|---|
| 58 |
{ |
|---|
| 59 |
_data = data; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
/** |
|---|
| 63 |
* @inheritDoc |
|---|
| 64 |
*/ |
|---|
| 65 |
public function toString():String |
|---|
| 66 |
{ |
|---|
| 67 |
return '[Feed data="'+getQualifiedClassName(_data)+'"]'; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
/** |
|---|
| 71 |
* フィードをパースする |
|---|
| 72 |
* |
|---|
| 73 |
* @param data Feedの文字列かXML |
|---|
| 74 |
* @return Feedオブジェクト |
|---|
| 75 |
* @throws |
|---|
| 76 |
* IllegalOperationError |
|---|
| 77 |
* UnknownFeedError |
|---|
| 78 |
*/ |
|---|
| 79 |
public static function parse(data:*):Feed |
|---|
| 80 |
{ |
|---|
| 81 |
// 文字列ならばXMLへキャスト |
|---|
| 82 |
if (data is String) |
|---|
| 83 |
{ |
|---|
| 84 |
data = XML(data); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
// dataがXMLで無ければExceptionを投げる |
|---|
| 88 |
if (data is XML === false) |
|---|
| 89 |
{ |
|---|
| 90 |
throw new IllegalOperationError('first argument must be String or XML.'); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
var localName:String = data.localName(); |
|---|
| 94 |
var version:String = data.@version.toString(); |
|---|
| 95 |
var nsList:Array = data.namespaceDeclarations(); |
|---|
| 96 |
nsList = nsList.map(function (...a):String { return a[0].uri; }); |
|---|
| 97 |
|
|---|
| 98 |
var adapter:IFeed; |
|---|
| 99 |
if (localName == 'rss' && !!version) |
|---|
| 100 |
{ |
|---|
| 101 |
adapter = new RSS2Feed(data); |
|---|
| 102 |
} |
|---|
| 103 |
else if (nsList.indexOf(rss.uri) !== -1) |
|---|
| 104 |
{ |
|---|
| 105 |
adapter = new RSSFeed(data); |
|---|
| 106 |
} |
|---|
| 107 |
else if (nsList.indexOf(atom.uri) !== -1) |
|---|
| 108 |
{ |
|---|
| 109 |
adapter = new AtomFeed(data); |
|---|
| 110 |
} |
|---|
| 111 |
else if (nsList.indexOf(atom03.uri) !== -1) |
|---|
| 112 |
{ |
|---|
| 113 |
adapter = new Atom03Feed(data); |
|---|
| 114 |
} |
|---|
| 115 |
else |
|---|
| 116 |
{ |
|---|
| 117 |
throw new UnknownFeedError("Unable to determine feed type."); |
|---|
| 118 |
} |
|---|
| 119 |
return new Feed(adapter); |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|