View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.atom;
20  
21  import java.io.StringReader;
22  import java.util.Map;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.apache.abdera.model.Content;
27  import org.apache.abdera.model.Element;
28  import org.apache.abdera.model.Entry;
29  import org.apache.abdera.model.Feed;
30  import org.apache.abdera.model.Service;
31  import org.apache.abdera.model.Content.Type;
32  import org.springframework.extensions.webscripts.Format;
33  
34  /**
35   * Atom Model
36   * 
37   * @author davidc
38   */
39  public class AtomService
40  {
41      private AbderaService abderaService;
42      
43      /**
44       * Sets the Abdera Service
45       * 
46       * @param abderaService
47       */
48      public void setAbderaService(AbderaService abderaService)
49      {
50         this.abderaService = abderaService; 
51      }
52      
53      /**
54       * Construct an empty Feed
55       * 
56       * @return  feed
57       */
58      public Feed createFeed()
59      {
60          return abderaService.createFeed();
61      }
62      
63      /**
64       * Construct an empty Entry
65       * 
66       * @return  entry
67       */
68      public Entry createEntry()
69      {
70          return abderaService.createEntry();
71      }
72      
73      /**
74       * Gets pre-configured Atom Extensions (QNames)
75       * 
76       * @return  map of QNames by alias
77       */
78      public Map<String, QName> getNames()
79      {
80          return abderaService.getNames();
81      }
82      
83      /**
84       * Creates a QName
85       * 
86       * @param uri
87       * @param localName
88       * @return  qname
89       */
90      public QName createQName(String uri, String localName)
91      {
92          return new QName(uri, localName);
93      }
94      
95      /**
96       * Establish mimetype of atom content
97       * 
98       * @param content  atom content
99       * @return  mimetype (or null, if it could not be established)
100      */
101     public String toMimeType(Entry entry)
102     {
103         if (entry == null || entry.getContentElement() == null)
104         {
105             return null;
106         }
107         
108         Content content = entry.getContentElement();
109         String mimetype = (content.getMimeType() == null ? null : content.getMimeType().toString());
110         if (mimetype == null)
111         {
112             Content.Type type = content.getContentType();
113             if (type != null)
114             {
115                 if (type == Type.HTML)
116                 {
117                     mimetype = Format.HTML.mimetype();
118                 }
119                 else if (type == Type.XHTML)
120                 {
121                     mimetype = Format.XHTML.mimetype();
122                 }
123                 else if (type == Type.TEXT)
124                 {
125                     mimetype = Format.TEXT.mimetype();
126                 }
127             }
128         }
129         return mimetype;
130     }
131 
132     /**
133      * Parse an Atom element
134      * 
135      * @param entry
136      * @return
137      */
138     public Element toAtom(org.springframework.extensions.surf.util.Content atom)
139     {
140         return abderaService.parse(atom.getInputStream(), null);
141     }
142 
143     /**
144      * Parse an Atom element
145      * 
146      * @param entry
147      * @return
148      */
149     public Element toAtom(String atom)
150     {
151         return abderaService.parse(new StringReader(atom), null);
152     }
153     
154     /**
155      * Parse an Atom Service
156      * 
157      * @param entry
158      * @return
159      */
160     public Service toService(org.springframework.extensions.surf.util.Content entry)
161     {
162         return abderaService.parseService(entry.getInputStream(), null);
163     }
164 
165     /**
166      * Parse an Atom Service
167      * 
168      * @param entry
169      * @return
170      */
171     public Service toService(String entry)
172     {
173         return abderaService.parseService(new StringReader(entry), null);
174     }
175     
176     /**
177      * Parse an Atom Entry
178      * 
179      * @param entry
180      * @return
181      */
182     public Entry toEntry(org.springframework.extensions.surf.util.Content entry)
183     {
184         return abderaService.parseEntry(entry.getInputStream(), null);
185     }
186 
187     /**
188      * Parse an Atom Entry
189      * 
190      * @param entry
191      * @return
192      */
193     public Entry toEntry(String entry)
194     {
195         return abderaService.parseEntry(new StringReader(entry), null);
196     }
197 
198     /**
199      * Parse an Atom Feed
200      * 
201      * @param feed
202      * @return
203      */
204     public Feed toFeed(org.springframework.extensions.surf.util.Content feed)
205     {
206         return abderaService.parseFeed(feed.getInputStream(), null);
207     }
208 
209     /**
210      * Parse an Atom Feed
211      * 
212      * @param feed
213      * @return
214      */
215     public Feed toFeed(String feed)
216     {
217         return abderaService.parseFeed(new StringReader(feed), null);
218     }
219     
220 }