A
download Atom03Generator.java
Language: Java
License: AL20
Copyright: Copyright 2004 Sun Microsystems, Inc.
LOC: 272
Project Info
Rome, Atom/RSS Java utilities(rome)
Server: java.net
Type: cvs
...om\sun\syndication\io\impl\
   Atom03Generator.java
   Atom03Parser.java
   Atom10Generator.java
   Atom10Parser.java
   Base64.java
   BaseWireFeedGenerator.java
   BaseWireFeedParser.java
   DateParser.java
   DCModuleGenerator.java
   DCModuleParser.java
   FeedGenerators.java
   FeedParsers.java
   ModuleGenerators.java
   ModuleParsers.java
   PluginManager.java
   PropertiesLoader.java
   RSS090Generator.java
   RSS090Parser.java
   ...1NetscapeGenerator.java
   RSS091NetscapeParser.java
   ...1UserlandGenerator.java
   RSS091UserlandParser.java
   RSS092Generator.java
   RSS092Parser.java
   RSS093Generator.java
   RSS093Parser.java
   RSS094Generator.java
   RSS094Parser.java
   RSS10Generator.java
   RSS10Parser.java
   RSS20Generator.java
   RSS20Parser.java
   RSS20wNSParser.java
   SyModuleGenerator.java
   SyModuleParser.java
   XmlFixerReader.java

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright 2004 Sun Microsystems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.sun.syndication.io.impl;

import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.*;
import com.sun.syndication.feed.synd.SyndPerson;
import com.sun.syndication.io.FeedException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import java.io.StringReader;
import java.util.List;

/**
 * Feed Generator for Atom
 * <p/>
 *
 * @author Elaine Chien
 *
 */

public class Atom03Generator extends BaseWireFeedGenerator {
    private static final String ATOM_03_URI = "http://purl.org/atom/ns#";
    private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_03_URI);

    private String _version;

    public Atom03Generator() {
        this("atom_0.3","0.3");
    }

    protected Atom03Generator(String type,String version) {
        super(type);
        _version = version;
    }

    protected String getVersion() {
        return _version;
    }

    protected Namespace getFeedNamespace() {
        return ATOM_NS;
    }

    public Document generate(WireFeed wFeed) throws FeedException {
        Feed feed = (Feed) wFeed;
        Element root = createRootElement(feed);
        populateFeed(feed,root);
        return createDocument(root);
    }

    protected Document createDocument(Element root) {
        return new Document(root);
    }

    protected Element createRootElement(Feed feed) {
        Element root = new Element("feed",getFeedNamespace());
        root.addNamespaceDeclaration(getFeedNamespace());
        Attribute version = new Attribute("version", getVersion());
        root.setAttribute(version);
        generateModuleNamespaceDefs(root);
        return root;
    }

    protected void populateFeed(Feed feed,Element parent) throws FeedException  {
        addFeed(feed,parent);
        addEntries(feed,parent);
    }

    protected void addFeed(Feed feed, Element parent) throws FeedException {
        Element eFeed = parent;
        populateFeedHeader(feed,eFeed);
        checkFeedHeaderConstraints(eFeed);
        generateFeedModules(feed.getModules(),eFeed);
        generateForeignMarkup(eFeed, (List)feed.getForeignMarkup()); 
    }

    protected void addEntries(Feed feed,Element parent) throws FeedException {
        List items = feed.getEntries();
        for (int i=0;i<items.size();i++) {
            addEntry((Entry)items.get(i),parent);
        }
        checkEntriesConstraints(parent);
    }

    protected void addEntry(Entry entry,Element parent) throws FeedException {
        Element eEntry = new Element("entry", getFeedNamespace());
        populateEntry(entry,eEntry);
        checkEntryConstraints(eEntry);
        generateItemModules(entry.getModules(),eEntry);
        parent.addContent(eEntry);
    }

    protected void populateFeedHeader(Feed feed, Element eFeed) throws FeedException {
        if (feed.getTitle() != null) {
            eFeed.addContent(generateSimpleElement("title", feed.getTitle()));
        }

        List links = feed.getAlternateLinks();
        for (int i = 0; i < links.size(); i++) {
            eFeed.addContent(generateLinkElement((Link)links.get(i)));
        }

        links = feed.getOtherLinks();
        for (int i = 0; i < links.size(); i++) {
            eFeed.addContent(generateLinkElement((Link)links.get(i)));
        }
        if (feed.getAuthors()!=null && feed.getAuthors().size() > 0) {
            Element authorElement = new Element("author", getFeedNamespace());
            fillPersonElement(authorElement, (Person)feed.getAuthors().get(0));
            eFeed.addContent(authorElement);
        }

        List contributors = feed.getContributors();
        for (int i = 0; i < contributors.size(); i++) {
            Element contributorElement = new Element("contributor", getFeedNamespace());
            fillPersonElement(contributorElement, (Person)contributors.get(i));
            eFeed.addContent(contributorElement);
        }

        if (feed.getTagline() != null) {
            Element taglineElement = new Element("tagline", getFeedNamespace());
            fillContentElement(taglineElement, feed.getTagline());
            eFeed.addContent(taglineElement);
        }

        if (feed.getId() != null) {
            eFeed.addContent(generateSimpleElement("id", feed.getId()));
        }

        if (feed.getGenerator() != null) {
            eFeed.addContent(generateGeneratorElement(feed.getGenerator()));
        }

        if (feed.getCopyright() != null) {
            eFeed.addContent(generateSimpleElement("copyright", feed.getCopyright()));
        }

        if (feed.getInfo() != null) {
            Element infoElement = new Element("info", getFeedNamespace());
            fillContentElement(infoElement, feed.getInfo());
            eFeed.addContent(infoElement);
        }

        if (feed.getModified() != null) {
            Element modifiedElement = new Element("modified", getFeedNamespace());
            modifiedElement.addContent(DateParser.formatW3CDateTime(feed.getModified()));
            eFeed.addContent(modifiedElement);
        }
    }

    protected void populateEntry(Entry entry, Element eEntry) throws FeedException {
        if (entry.getTitle() != null) {
            eEntry.addContent(generateSimpleElement("title", entry.getTitle()));
        }
        List links = entry.getAlternateLinks();
        for (int i = 0; i < links.size(); i++) {
            eEntry.addContent(generateLinkElement((Link)links.get(i)));
        }

        links = entry.getOtherLinks();
        for (int i = 0; i < links.size(); i++) {
            eEntry.addContent(generateLinkElement((Link)links.get(i)));
        }

        if (entry.getAuthors()!=null && entry.getAuthors().size() > 0) {
            Element authorElement = new Element("author", getFeedNamespace());
            fillPersonElement(authorElement, (Person)entry.getAuthors().get(0));
            eEntry.addContent(authorElement);
        }

        List contributors = entry.getContributors();
        for (int i = 0; i < contributors.size(); i++) {
            Element contributorElement = new Element("contributor", getFeedNamespace());
            fillPersonElement(contributorElement, (Person)contributors.get(i));
            eEntry.addContent(contributorElement);
        }
        if (entry.getId() != null) {
            eEntry.addContent(generateSimpleElement("id", entry.getId()));
        }

        if (entry.getModified() != null) {
            Element modifiedElement = new Element("modified", getFeedNamespace());
            modifiedElement.addContent(DateParser.formatW3CDateTime(entry.getModified()));
            eEntry.addContent(modifiedElement);
        }

        if (entry.getIssued() != null) {
            Element issuedElement = new Element("issued", getFeedNamespace());
            issuedElement.addContent(DateParser.formatW3CDateTime(entry.getIssued()));
            eEntry.addContent(issuedElement);
        }

        if (entry.getCreated() != null) {
            Element createdElement = new Element("created", getFeedNamespace());
            createdElement.addContent(DateParser.formatW3CDateTime(entry.getCreated()));
            eEntry.addContent(createdElement);
        }

        if (entry.getSummary() != null) {
            Element summaryElement = new Element("summary", getFeedNamespace());
            fillContentElement(summaryElement, entry.getSummary());
            eEntry.addContent(summaryElement);
        }

        List contents = entry.getContents();
        for (int i = 0; i < contents.size(); i++) {
            Element contentElement = new Element("content", getFeedNamespace());
            fillContentElement(contentElement, (Content)contents.get(i));
            eEntry.addContent(contentElement);
        }
        
        generateForeignMarkup(eEntry, (List)entry.getForeignMarkup());
    }

    protected void checkFeedHeaderConstraints(Element eFeed) throws FeedException {
    }

    protected void checkEntriesConstraints(Element parent) throws FeedException {
    }

    protected void checkEntryConstraints(Element eEntry) throws FeedException {
    }


    protected Element generateLinkElement(Link link) {
        Element linkElement = new Element("link", getFeedNamespace());

        if (link.getRel() != null) {
            Attribute relAttribute = new Attribute("rel", link.getRel().toString());
            linkElement.setAttribute(relAttribute);
        }

        if (link.getType() != null) {
            Attribute typeAttribute = new Attribute("type", link.getType());
            linkElement.setAttribute(typeAttribute);
        }

        if (link.getHref() != null) {
            Attribute hrefAttribute = new Attribute("href", link.getHref());
            linkElement.setAttribute(hrefAttribute);
        }
        return linkElement;
    }


    protected void fillPersonElement(Element element, Person person) {
        if (person.getName() != null) {
            element.addContent(generateSimpleElement("name", person.getName()));
        }
        if (person.getUrl() != null) {
            element.addContent(generateSimpleElement("url", person.getUrl()));
        }

        if (person.getEmail() != null) {
            element.addContent(generateSimpleElement("email", person.getEmail()));
        }
    }

    protected Element generateTagLineElement(Content tagline) {
        Element taglineElement = new Element("tagline", getFeedNamespace());

        if (tagline.getType() != null) {
            Attribute typeAttribute = new Attribute("type", tagline.getType());
            taglineElement.setAttribute(typeAttribute);
        }

        if (tagline.getValue() != null) {
            taglineElement.addContent(tagline.getValue());
        }
        return taglineElement;
    }

    protected void fillContentElement(Element contentElement, Content content)
        throws FeedException {

        if (content.getType() != null) {
            Attribute typeAttribute = new Attribute("type", content.getType());
            contentElement.setAttribute(typeAttribute);
        }

        String mode = content.getMode();
        if (mode != null) {
            Attribute modeAttribute = new Attribute("mode", content.getMode().toString());
            contentElement.setAttribute(modeAttribute);
        }

        if (content.getValue() != null) {

            if (mode == null || mode.equals(Content.ESCAPED)) {
                contentElement.addContent(content.getValue());
            } else if (mode.equals(Content.BASE64)) {
                contentElement.addContent(Base64.encode(content.getValue()));
            } else if (mode.equals(Content.XML)) {

                StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
                tmpDocString.append(content.getValue());
                tmpDocString.append("</tmpdoc>");
                StringReader tmpDocReader = new StringReader(tmpDocString.toString());
                Document tmpDoc;

                try {
                    SAXBuilder saxBuilder = new SAXBuilder();
                    tmpDoc = saxBuilder.build(tmpDocReader);
                }
                catch (Exception ex) {
                    throw new FeedException("Invalid XML",ex);
                }

                List children = tmpDoc.getRootElement().removeContent();
                contentElement.addContent(children);
            }
        }
    }

    protected Element generateGeneratorElement(Generator generator) {
        Element generatorElement = new Element("generator", getFeedNamespace());

        if (generator.getUrl() != null) {
            Attribute urlAttribute = new Attribute("url", generator.getUrl());
            generatorElement.setAttribute(urlAttribute);
        }

        if (generator.getVersion() != null) {
            Attribute versionAttribute = new Attribute("version", generator.getVersion());
            generatorElement.setAttribute(versionAttribute);
        }

        if (generator.getValue() != null) {
            generatorElement.addContent(generator.getValue());
        }

        return generatorElement;

    }

    protected Element generateSimpleElement(String name, String value) {
        Element element = new Element(name, getFeedNamespace());
        element.addContent(value);
        return element;
    }

}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us