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