001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.url;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.cocoon.xml.XMLUtils;
022import org.xml.sax.ContentHandler;
023import org.xml.sax.SAXException;
024
025/**
026 * This bean represents a url preview with title, description, image and favico 
027 *
028 */
029public class UrlPreview
030{
031    private String _title;
032    private String _description;
033    private String _imageUrl;
034    private String _favico;
035    private String _url;
036    
037    /**
038     * Empty constructor
039     */
040    public UrlPreview()
041    {
042        // Nothing
043    }
044    
045    /**
046     * Set the title
047     * @param title the title
048     */
049    public void setTitle(String title)
050    {
051        _title = title;
052    }
053    
054    /**
055     * Get the title
056     * @return the title
057     */
058    public String getTitle()
059    {
060        return _title;
061    }
062    
063    /**
064     * Set the description
065     * @param description the description
066     */
067    public void setDescription(String description)
068    {
069        _description = description;
070    }
071    
072    /**
073     * Get the description
074     * @return the description
075     */
076    public String getDescription()
077    {
078        return _description;
079    }
080    
081    /**
082     * Set the absolute url of image
083     * @param imgUrl the url of image
084     */
085    public void setImageUrl(String imgUrl)
086    {
087        _imageUrl = imgUrl;
088    }
089    
090    /**
091     * Get the image url
092     * @return the absolute url of the image
093     */
094    public String getImageUrl()
095    {
096        return _imageUrl;
097    }
098    
099    /**
100     * Set the absolute url of favico
101     * @param favico the url of favico
102     */
103    public void setFavicon(String favico)
104    {
105        _favico = favico;
106    }
107    
108    /**
109     * Get the favico
110     * @return the favico absolute url
111     */
112    public String getFavicon()
113    {
114        return _favico;
115    }
116    
117    /**
118     * Set the url
119     * @param url the url
120     */
121    public void setUrl(String url)
122    {
123        _url = url;
124    }
125    
126    /**
127     * Get the url
128     * @return the url
129     */
130    public String getUrl()
131    {
132        return _url;
133    }
134    
135    /**
136     * Get the JSON representation of this url preview
137     * @return the properties as json
138     */
139    public Map<String, String> toJSON()
140    {
141        Map<String, String> preview = new HashMap<>();
142        
143        preview.put("title", _title);
144        preview.put("description", _description);
145        preview.put("imageUrl", _imageUrl);
146        preview.put("favico", _favico);
147        preview.put("url", _url);
148        
149        return preview;
150    }
151    
152    /**
153     * Generates sax events representing this url preview
154     * @param handler The content handler to sax into
155     * @param tagName the root tag name
156     * @throws SAXException if an error occurs while saxing
157     */
158    public void toSAX(ContentHandler handler, String tagName) throws SAXException
159    {
160        XMLUtils.startElement(handler, tagName);
161        
162        _saxNonNullElement(handler, "title", _title);
163        _saxNonNullElement(handler, "description", _description);
164        _saxNonNullElement(handler, "imageUrl", _imageUrl);
165        _saxNonNullElement(handler, "favico", _favico);
166        _saxNonNullElement(handler, "url", _url);
167        
168        XMLUtils.endElement(handler, tagName);
169    }
170    
171    private void _saxNonNullElement(ContentHandler handler, String tagName, String value) throws SAXException
172    {
173        if (value != null)
174        {
175            XMLUtils.createElement(handler, tagName, value);
176        }
177    }
178}