View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.samples;
18  
19  import com.sun.syndication.feed.synd.*;
20  import com.sun.syndication.io.SyndFeedOutput;
21  
22  import java.io.FileWriter;
23  import java.io.Writer;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /***
30   * It creates a feed and writes it to a file.
31   * <p>
32   * @author Alejandro Abdelnur
33   *
34   */
35  public class FeedWriter {
36  
37      private static final String DATE_FORMAT = "yyyy-MM-dd";
38  
39      public static void main(String[] args) {
40          boolean ok = false;
41          if (args.length==2) {
42              try {
43                  String feedType = args[0];
44                  String fileName = args[1];
45  
46                  DateFormat dateParser = new SimpleDateFormat(DATE_FORMAT);
47  
48                  SyndFeed feed = new SyndFeedImpl();
49                  feed.setFeedType(feedType);
50  
51                  feed.setTitle("Sample Feed (created with Rome)");
52                  feed.setLink("http://rome.dev.java.net");
53                  feed.setDescription("This feed has been created using Rome (Java syndication utilities");
54  
55                  List entries = new ArrayList();
56                  SyndEntry entry;
57                  SyndContent description;
58  
59                  entry = new SyndEntryImpl();
60                  entry.setTitle("Rome v1.0");
61                  entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
62                  entry.setPublishedDate(dateParser.parse("2004-06-08"));
63                  description = new SyndContentImpl();
64                  description.setType("text/plain");
65                  description.setValue("Initial release of Rome");
66                  entry.setDescription(description);
67                  entries.add(entry);
68  
69                  entry = new SyndEntryImpl();
70                  entry.setTitle("Rome v2.0");
71                  entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome02");
72                  entry.setPublishedDate(dateParser.parse("2004-06-16"));
73                  description = new SyndContentImpl();
74                  description.setType("text/xml");
75                  description.setValue("Bug fixes, <xml>XML</xml> minor API changes and some new features");
76                  entry.setDescription(description);
77                  entries.add(entry);
78  
79                  entry = new SyndEntryImpl();
80                  entry.setTitle("Rome v3.0");
81                  entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome03");
82                  entry.setPublishedDate(dateParser.parse("2004-07-27"));
83                  description = new SyndContentImpl();
84                  description.setType("text/html");
85                  description.setValue("<p>More Bug fixes, mor API changes, some new features and some Unit testing</p>"+
86                                       "<p>For details check the <a href=\"http://wiki.java.net/bin/view/Javawsxml/RomeChangesLog#RomeV03\">Changes Log</a></p>");
87                  entry.setDescription(description);
88                  entries.add(entry);
89  
90                  feed.setEntries(entries);
91  
92                  Writer writer = new FileWriter(fileName);
93                  SyndFeedOutput output = new SyndFeedOutput();
94                  output.output(feed,writer);
95                  writer.close();
96  
97                  System.out.println("The feed has been written to the file ["+fileName+"]");
98  
99                  ok = true;
100             }
101             catch (Exception ex) {
102                 ex.printStackTrace();
103                 System.out.println("ERROR: "+ex.getMessage());
104             }
105         }
106 
107         if (!ok) {
108             System.out.println();
109             System.out.println("FeedWriter creates a RSS/Atom feed and writes it to a file.");
110             System.out.println("The first parameter must be the syndication format for the feed");
111             System.out.println("  (rss_0.90, rss_0.91, rss_0.92, rss_0.93, rss_0.94, rss_1.0 rss_2.0 or atom_0.3)");
112             System.out.println("The second parameter must be the file name for the feed");
113             System.out.println();
114         }
115     }
116 
117 }