WebBox Klassenbibliothek
ulrich
2020-12-28 828ffa35841a585ddeeac9d934e3a4951e1702c0
commit | author | age
c583a8 1 package de.uhilger.wbx.web;
U 2
3 import java.io.FileOutputStream;
4
5 import javax.xml.stream.XMLEventFactory;
6 import javax.xml.stream.XMLEventWriter;
7 import javax.xml.stream.XMLOutputFactory;
8 import javax.xml.stream.XMLStreamException;
9 import javax.xml.stream.events.Characters;
10 import javax.xml.stream.events.EndElement;
11 import javax.xml.stream.events.StartDocument;
12 import javax.xml.stream.events.StartElement;
13 import javax.xml.stream.events.XMLEvent;
14
15 import de.vogella.rss.model.Feed;
16 import de.vogella.rss.model.FeedMessage;
17 import java.io.OutputStream;
18
19 public class FeedStreamWriter {
20
21     //private String outputFile;
22     private Feed rssfeed;
23     private OutputStream out;
24
25     public FeedStreamWriter(Feed rssfeed, OutputStream s) {
26         this.rssfeed = rssfeed;
27         this.out = s;
28     }
29
30     public void write() throws Exception {
31
32         // create a XMLOutputFactory
33         XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
34
35         // create XMLEventWriter
36         XMLEventWriter eventWriter = outputFactory
37                 .createXMLEventWriter(out);
38
39         // create a EventFactory
40
41         XMLEventFactory eventFactory = XMLEventFactory.newInstance();
42         XMLEvent end = eventFactory.createDTD("\n");
43
44         // create and write Start Tag
45
46         StartDocument startDocument = eventFactory.createStartDocument();
47
48         eventWriter.add(startDocument);
49
50         // create open tag
51         eventWriter.add(end);
52
53         StartElement rssStart = eventFactory.createStartElement("", "", "rss");
54         eventWriter.add(rssStart);
55         eventWriter.add(eventFactory.createAttribute("version", "2.0"));
56         eventWriter.add(end);
57
58         eventWriter.add(eventFactory.createStartElement("", "", "channel"));
59         eventWriter.add(end);
60
61         // Write the different nodes
62
63         createNode(eventWriter, "title", rssfeed.getTitle());
64
65         createNode(eventWriter, "link", rssfeed.getLink());
66
67         createNode(eventWriter, "description", rssfeed.getDescription());
68
69         createNode(eventWriter, "language", rssfeed.getLanguage());
70
71         createNode(eventWriter, "copyright", rssfeed.getCopyright());
72
73         createNode(eventWriter, "pubdate", rssfeed.getPubDate());
74
75         for (FeedMessage entry : rssfeed.getMessages()) {
76             eventWriter.add(eventFactory.createStartElement("", "", "item"));
77             eventWriter.add(end);
78             createNode(eventWriter, "title", entry.getTitle());
79             createNode(eventWriter, "description", entry.getDescription());
80             createNode(eventWriter, "link", entry.getLink());
81             createNode(eventWriter, "author", entry.getAuthor());
82             createNode(eventWriter, "guid", entry.getGuid());
83             eventWriter.add(end);
84             eventWriter.add(eventFactory.createEndElement("", "", "item"));
85             eventWriter.add(end);
86
87         }
88
89         eventWriter.add(end);
90         eventWriter.add(eventFactory.createEndElement("", "", "channel"));
91         eventWriter.add(end);
92         eventWriter.add(eventFactory.createEndElement("", "", "rss"));
93
94         eventWriter.add(end);
95
96         eventWriter.add(eventFactory.createEndDocument());
97
98         eventWriter.close();
99     }
100
101     private void createNode(XMLEventWriter eventWriter, String name,
102
103     String value) throws XMLStreamException {
104         XMLEventFactory eventFactory = XMLEventFactory.newInstance();
105         XMLEvent end = eventFactory.createDTD("\n");
106         XMLEvent tab = eventFactory.createDTD("\t");
107         // create Start node
108         StartElement sElement = eventFactory.createStartElement("", "", name);
109         eventWriter.add(tab);
110         eventWriter.add(sElement);
111         // create Content
112         Characters characters = eventFactory.createCharacters(value);
113         eventWriter.add(characters);
114         // create End node
115         EndElement eElement = eventFactory.createEndElement("", "", name);
116         eventWriter.add(eElement);
117         eventWriter.add(end);
118     }
119 }