001    package railo.runtime.text.feed;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.type.Array;
008    import railo.runtime.type.Collection;
009    import railo.runtime.type.Collection.Key;
010    import railo.runtime.type.util.ListUtil;
011    import railo.runtime.type.KeyImpl;
012    
013    public class FeedDeclaration {
014    
015    
016    
017    
018            private static Map<String,FeedDeclaration> declarations = new HashMap<String,FeedDeclaration>();
019            private static FeedDeclaration defaultDeclaration;
020            static {
021                    
022                    
023                    
024    // RSS 2.0
025                    Map<String,El> decl=new HashMap<String,El>();
026                    decl.put("rss",                 new El(El.QUANTITY_1,new Attr("version")));
027                    // rss.channel *
028                    decl.put("rss.channel.item",            new El(El.QUANTITY_0_N,true));
029                    decl.put("rss.channel.category",                new El(El.QUANTITY_0_N,new Attr("domain")));
030                    decl.put("rss.channel.cloud",           new El(El.QUANTITY_AUTO,new Attr[]{
031                                    new Attr("domain"),new Attr("port"),new Attr("path"),new Attr("registerProcedure"),new Attr("protocol")}));
032                    decl.put("rss.channel.image",           new El(El.QUANTITY_AUTO));
033                    
034                    // rss.channel.item *
035                    decl.put("rss.channel.item.author",             new El(El.QUANTITY_0_1));
036                    decl.put("rss.channel.item.category",   new El(El.QUANTITY_0_N,new Attr("domain")));
037                    decl.put("rss.channel.item.comments",   new El(El.QUANTITY_0_1));
038                    decl.put("rss.channel.item.enclosure",  new El(El.QUANTITY_0_N,new Attr[]{
039                                    new Attr("url"),new Attr("length"),new Attr("type")}));
040                    decl.put("rss.channel.item.guid",               new El(El.QUANTITY_0_1,new Attr("isPermaLink","true")));
041                    decl.put("rss.channel.item.pubDate",            new El(El.QUANTITY_0_1));
042                    decl.put("rss.channel.item.source",             new El(El.QUANTITY_0_1,new Attr("url")));
043                    
044                    // rss.channel.item +
045                    decl.put("rss.channel.item.title",      new El(El.QUANTITY_1));
046                    decl.put("rss.channel.item.description",        new El(El.QUANTITY_1,new Attr[]{}));// muss auch sein das er value anlegt
047                    
048                    // rss.channel.item ?
049                    decl.put("rss.channel.item.link",       new El(El.QUANTITY_0_1));// muss auch sein das er value anlegt
050                    
051                    // rss.channel.item 1
052                    decl.put("rss.channel.image.url",               new El(El.QUANTITY_1));
053                    decl.put("rss.channel.image.title",             new El(El.QUANTITY_1));
054                    decl.put("rss.channel.image.link",              new El(El.QUANTITY_1));
055    
056                    // rss.channel.item *
057                    decl.put("rss.channel.image.width",             new El(El.QUANTITY_0_N));
058                    decl.put("rss.channel.image.height",            new El(El.QUANTITY_0_N));
059                    decl.put("rss.channel.image.description", new El(El.QUANTITY_0_N));
060                    
061                    decl.put("rss.channel.textInput.title", new El(El.QUANTITY_1));
062                    decl.put("rss.channel.textInput.description", new El(El.QUANTITY_1));
063                    decl.put("rss.channel.textInput.name", new El(El.QUANTITY_1));
064                    decl.put("rss.channel.textInput.link", new El(El.QUANTITY_1));
065                    
066                    FeedDeclaration fd = new FeedDeclaration(decl,"rss.channel","rss_2.0");
067                    declarations.put("rss", fd);
068                    declarations.put("rss_2.0", fd);
069                    declarations.put("rss_2", fd);
070                    
071            
072            // RSS 0.92
073                            decl=new HashMap<String, El>();
074                            decl.put("rss",                 new El(El.QUANTITY_1,new Attr("version")));
075                            // rss.channel *
076                            decl.put("rss.channel.item",            new El(El.QUANTITY_0_N));
077                            decl.put("rss.channel.category",                new El(El.QUANTITY_0_N,new Attr("domain")));
078                            decl.put("rss.channel.cloud",           new El(El.QUANTITY_AUTO,new Attr[]{
079                                            new Attr("domain"),new Attr("port"),new Attr("path"),new Attr("registerProcedure"),new Attr("protocol")}));
080                            decl.put("rss.channel.image",           new El(El.QUANTITY_AUTO));
081                            
082                            // rss.channel.item *
083                            decl.put("rss.channel.item.author",             new El(El.QUANTITY_0_1));
084                            decl.put("rss.channel.item.category",   new El(El.QUANTITY_0_N,new Attr("domain","leer")));
085                            decl.put("rss.channel.item.comments",   new El(El.QUANTITY_0_1));
086                            decl.put("rss.channel.item.enclosure",  new El(El.QUANTITY_0_N,new Attr[]{
087                                            new Attr("url"),new Attr("length"),new Attr("type")}));
088                            decl.put("rss.channel.item.guid",               new El(El.QUANTITY_0_1,new Attr("isPermaLink","true")));
089                            decl.put("rss.channel.item.pubDate",            new El(El.QUANTITY_0_1));
090                            decl.put("rss.channel.item.source",             new El(El.QUANTITY_0_1,new Attr("url")));
091                            
092                            // rss.channel.item +
093                            decl.put("rss.channel.item.title",      new El(El.QUANTITY_1));
094                            
095                            // rss.channel.item ?
096                            decl.put("rss.channel.item.link",       new El(El.QUANTITY_0_1));// muss auch sein das er value anlegt
097                            
098                            // rss.channel.item 1
099                            decl.put("rss.channel.image.url",               new El(El.QUANTITY_1));
100                            decl.put("rss.channel.image.title",             new El(El.QUANTITY_1));
101                            decl.put("rss.channel.image.link",              new El(El.QUANTITY_1));
102    
103                            // rss.channel.item *
104                            decl.put("rss.channel.image.width",             new El(El.QUANTITY_0_N));
105                            decl.put("rss.channel.image.height",            new El(El.QUANTITY_0_N));
106                            decl.put("rss.channel.image.description", new El(El.QUANTITY_0_N));
107                            
108                            decl.put("rss.channel.textInput.title", new El(El.QUANTITY_1));
109                            decl.put("rss.channel.textInput.description", new El(El.QUANTITY_1));
110                            decl.put("rss.channel.textInput.name", new El(El.QUANTITY_1));
111                            decl.put("rss.channel.textInput.link", new El(El.QUANTITY_1));
112                            
113                            fd = new FeedDeclaration(decl,"rss.channel","rss_0.92");
114                            declarations.put("rss_0.92", fd);
115                            
116    
117            // RSS 0.91
118                            decl=new HashMap<String, El>();
119                            decl.put("rss",                 new El(El.QUANTITY_1,new Attr("version")));
120                            // rss.channel *
121                            decl.put("rss.channel.item",            new El(El.QUANTITY_0_N));
122                            decl.put("rss.channel.category",                new El(El.QUANTITY_0_N,new Attr("domain")));
123                            decl.put("rss.channel.cloud",           new El(El.QUANTITY_AUTO,new Attr[]{
124                                            new Attr("domain"),new Attr("port"),new Attr("path"),new Attr("registerProcedure"),new Attr("protocol")}));
125                            decl.put("rss.channel.image",           new El(El.QUANTITY_AUTO));
126                            
127                            // rss.channel.item *
128                            decl.put("rss.channel.item.author",             new El(El.QUANTITY_0_1));
129                            decl.put("rss.channel.item.category",   new El(El.QUANTITY_0_N,new Attr("domain","leer")));
130                            decl.put("rss.channel.item.comments",   new El(El.QUANTITY_0_1));
131                            decl.put("rss.channel.item.enclosure",  new El(El.QUANTITY_0_N,new Attr[]{
132                                            new Attr("url"),new Attr("length"),new Attr("type")}));
133                            decl.put("rss.channel.item.guid",               new El(El.QUANTITY_0_1,new Attr("isPermaLink","true")));
134                            decl.put("rss.channel.item.pubDate",            new El(El.QUANTITY_0_1));
135                            decl.put("rss.channel.item.source",             new El(El.QUANTITY_0_1,new Attr("url")));
136                            
137                            // rss.channel.item 1
138                            decl.put("rss.channel.image.url",               new El(El.QUANTITY_1));
139                            decl.put("rss.channel.image.title",             new El(El.QUANTITY_1));
140                            decl.put("rss.channel.image.link",              new El(El.QUANTITY_1));
141    
142                            // rss.channel.item *
143                            decl.put("rss.channel.image.width",             new El(El.QUANTITY_0_N));
144                            decl.put("rss.channel.image.height",            new El(El.QUANTITY_0_N));
145                            decl.put("rss.channel.image.description", new El(El.QUANTITY_0_N));
146                            
147                            decl.put("rss.channel.textInput.title", new El(El.QUANTITY_1));
148                            decl.put("rss.channel.textInput.description", new El(El.QUANTITY_1));
149                            decl.put("rss.channel.textInput.name", new El(El.QUANTITY_1));
150                            decl.put("rss.channel.textInput.link", new El(El.QUANTITY_1));
151                            
152                            fd = new FeedDeclaration(decl,"rss.channel","rss_0.91");
153                            declarations.put("rss_0.91", fd);
154                                            
155    
156    
157            // ATOM
158                            decl=new HashMap<String, El>();
159                            decl.put("feed",                new El(El.QUANTITY_1,new Attr("version")));
160                            
161                            decl.put("feed.author.name", new El(El.QUANTITY_1));
162                            decl.put("feed.title", new El(El.QUANTITY_1, new Attr[]{}));
163                            decl.put("feed.link", new El(El.QUANTITY_0_N));
164                            decl.put("feed.entry", new El(El.QUANTITY_0_N));
165                            decl.put("feed.entry.author", new El(El.QUANTITY_0_N));
166                            decl.put("feed.entry.contributor", new El(El.QUANTITY_0_N));
167                            decl.put("feed.entry.content", new El(El.QUANTITY_0_N));
168                            decl.put("feed.entry.link", new El(El.QUANTITY_0_N));
169                            decl.put("feed.entry.title", new El(El.QUANTITY_1,new Attr[]{}));
170                            decl.put("feed.entry.summary", new El(El.QUANTITY_1,new Attr[]{new Attr("type","text/plain"),new Attr("mode","xml")}));
171                            
172                            fd = new FeedDeclaration(decl,"feed","atom");
173                            declarations.put("atom", fd);
174    
175    
176                            
177                    defaultDeclaration=new FeedDeclaration(new HashMap<String, El>(),null,"custom");
178            }
179    
180    
181            private Map<String, El> declaration;
182            private Key[] entryLevel;
183            private String type;
184            
185            private FeedDeclaration(Map<String,El> declaration, String entryLevel,String type) {
186                    this.declaration=declaration;
187                    
188                    if(!StringUtil.isEmpty(entryLevel)) {
189                            Array array = ListUtil.listToArray(entryLevel, '.');
190                            this.entryLevel=new Collection.Key[array.size()];
191                    for(int i=0;i<this.entryLevel.length;i++) {
192                            this.entryLevel[i]=KeyImpl.toKey(array.get(i+1,null),null);
193                    }
194                    }
195                    else this.entryLevel=new Collection.Key[0];
196                    this.type=type;
197            }
198    
199    
200            public static FeedDeclaration getInstance(String decName) {
201                    FeedDeclaration fd= declarations.get(decName);
202                    if(fd!=null)return fd;
203                    
204                    if(StringUtil.startsWithIgnoreCase(decName, "rss"))
205                                    fd=declarations.get("rss");
206                    if(fd!=null)return fd;
207                    
208                    if(StringUtil.startsWithIgnoreCase(decName, "atom"))
209                            fd=declarations.get("atom");
210                    if(fd!=null)return fd;
211                    
212                    return defaultDeclaration;
213            }
214    
215            
216            class FeedDeclarationItem {
217                    
218            }
219    
220    
221            /**
222             * @return the declaration
223             */
224            public Map<String, El> getDeclaration() {
225                    return declaration;
226            }
227    
228    
229            /**
230             * @return the entryLevel
231             */
232            public Key[] getEntryLevel() {
233                    return entryLevel;
234            }
235    
236    
237            public String getType() {
238                    return type;
239            }
240    }