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 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
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 }