View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.googlesitemap.service;
35  
36  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
37  import info.magnolia.module.googlesitemap.bean.SiteMapEntry;
38  import info.magnolia.module.googlesitemap.bean.SiteMapEntryList;
39  import info.magnolia.module.googlesitemap.config.SiteMapType;
40  
41  import java.io.StringWriter;
42  import java.util.ArrayList;
43  import java.util.Iterator;
44  import java.util.List;
45  
46  import javax.inject.Provider;
47  import javax.inject.Singleton;
48  import javax.jcr.Node;
49  import javax.jcr.RepositoryException;
50  import javax.xml.bind.JAXBContext;
51  import javax.xml.bind.JAXBException;
52  import javax.xml.bind.Marshaller;
53  import javax.xml.bind.PropertyException;
54  import javax.xml.stream.XMLOutputFactory;
55  import javax.xml.stream.XMLStreamException;
56  import javax.xml.stream.XMLStreamWriter;
57  
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  import com.google.inject.Inject;
62  
63  /**
64   * Implementation of {@link SiteMapXMLUtil}.
65   */
66  @Singleton
67  public class SiteMapXMLUtilImpl implements SiteMapXMLUtil {
68  
69      public static final String MOBILE_SCHEMA_URL = "http://www.google.com/schemas/sitemap-mobile/1.0";
70  
71      public static final String MOBILE_ELEMENT_NAME = "mobile";
72  
73      public static final String MOBILE_PREFIX = "mobile";
74  
75      public static final String URL_ELEMENT_NAME = "url";
76  
77      private Logger log = LoggerFactory.getLogger(getClass());
78  
79      private Provider<SiteMapService> service;
80  
81      @Inject
82      public SiteMapXMLUtilImpl(Provider<SiteMapService> service) {
83          this.service = service;
84      }
85  
86      @Override
87      public String generateSiteMapXML(Node siteMapNode) throws RepositoryException, JAXBException {
88          List<SiteMapEntry> entries = new ArrayList<SiteMapEntry>();
89          Iterator<SiteMapEntry> entryIt = service.get().getSiteMapBeans(siteMapNode);
90          while (entryIt.hasNext()) {
91              entries.add(entryIt.next());
92          }
93          String type = SiteMapNodeTypes.SiteMap.getType(siteMapNode) != null ? SiteMapNodeTypes.SiteMap.getType(siteMapNode) : SiteMapType.Standard.name();
94          return marshalSiteMapEntries(entries, SiteMapType.valueOf(type));
95      }
96  
97      private String marshalSiteMapEntries(List<SiteMapEntry> entries, final SiteMapType type) throws JAXBException {
98          JAXBContext jaxbContext = JAXBContext.newInstance(SiteMapEntry.class, SiteMapEntryList.class);
99          StringWriter sw = new StringWriter();
100         Marshaller marshaller = jaxbContext.createMarshaller();
101         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
102         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
103 
104         try {
105             SiteMapEntryList list = new SiteMapEntryList(entries);
106             final XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
107             final XMLStreamWriter delWriter = new DelegatingXMLStreamWriter(writer) {
108                 @Override
109                 public void writeStartElement(String prefix, String namespace, String localName) throws XMLStreamException {
110                     super.writeStartElement(prefix, namespace, localName);
111                     if (URL_ELEMENT_NAME.equals(namespace) && SiteMapType.Mobile == type) {
112                         writeEmptyElement(MOBILE_PREFIX, MOBILE_ELEMENT_NAME, "");
113                     }
114                 }
115 
116                 @Override
117                 public void writeNamespace(String prefix, String localName) throws XMLStreamException {
118                     super.writeNamespace(prefix, localName);
119                     if (type == SiteMapType.Mobile) {
120                         super.writeNamespace(MOBILE_PREFIX, MOBILE_SCHEMA_URL);
121                     }
122                 }
123             };
124             marshaller.marshal(list, delWriter);
125         } catch (PropertyException e) {
126             log.error("Failed to rebind marshaller's namespace prefix mapper, did the implementation of JAXB change?", e);
127         } catch (XMLStreamException e) {
128             log.error("Failed to marshal sitemap", e);
129         }
130         return sw.toString();
131     }
132 
133 }