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.jcr.util.PropertyUtil; |
37 |
|
import info.magnolia.module.googlesitemap.GoogleSiteMapConfiguration; |
38 |
|
import info.magnolia.module.googlesitemap.bean.SiteMapEntry; |
39 |
|
import info.magnolia.module.googlesitemap.bean.SiteMapEntryList; |
40 |
|
import info.magnolia.module.googlesitemap.config.SiteMapType; |
41 |
|
|
42 |
|
import java.io.StringWriter; |
43 |
|
import java.util.ArrayList; |
44 |
|
import java.util.Iterator; |
45 |
|
import java.util.List; |
46 |
|
|
47 |
|
import javax.inject.Provider; |
48 |
|
import javax.inject.Singleton; |
49 |
|
import javax.jcr.Node; |
50 |
|
import javax.jcr.RepositoryException; |
51 |
|
import javax.xml.bind.JAXBContext; |
52 |
|
import javax.xml.bind.JAXBException; |
53 |
|
import javax.xml.bind.Marshaller; |
54 |
|
import javax.xml.bind.PropertyException; |
55 |
|
import javax.xml.stream.XMLOutputFactory; |
56 |
|
import javax.xml.stream.XMLStreamException; |
57 |
|
import javax.xml.stream.XMLStreamWriter; |
58 |
|
|
59 |
|
import org.slf4j.Logger; |
60 |
|
import org.slf4j.LoggerFactory; |
61 |
|
|
62 |
|
import com.google.inject.Inject; |
63 |
|
|
64 |
|
|
65 |
|
@link |
66 |
|
|
67 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 37 (37) |
Complexity: 11 |
Complexity Density: 0.42 |
|
68 |
|
public class SiteMapXMLUtilImpl implements SiteMapXMLUtil { |
69 |
|
|
70 |
|
public static final String MOBILE_SCHEMA_URL = "http://www.google.com/schemas/sitemap-mobile/1.0"; |
71 |
|
|
72 |
|
public static final String MOBILE_ELEMENT_NAME = "mobile"; |
73 |
|
|
74 |
|
public static final String MOBILE_PREFIX = "mobile"; |
75 |
|
|
76 |
|
public static final String URL_ELEMENT_NAME = "url"; |
77 |
|
|
78 |
|
private Logger log = LoggerFactory.getLogger(getClass()); |
79 |
|
|
80 |
|
private Provider<SiteMapService> service; |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
0
|
@Inject... |
83 |
|
public SiteMapXMLUtilImpl(Provider<SiteMapService> service) { |
84 |
0
|
this.service = service; |
85 |
|
} |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
87 |
0
|
@Override... |
88 |
|
public String generateSiteMapXML(Node siteMapNode) throws RepositoryException, JAXBException { |
89 |
0
|
List<SiteMapEntry> entries = new ArrayList<SiteMapEntry>(); |
90 |
0
|
Iterator<SiteMapEntry> entryIt = service.get().getSiteMapBeans(siteMapNode); |
91 |
0
|
while (entryIt.hasNext()) { |
92 |
0
|
entries.add(entryIt.next()); |
93 |
|
} |
94 |
0
|
SiteMapType type = SiteMapType.valueOf(PropertyUtil.getString(siteMapNode, GoogleSiteMapConfiguration.TYPE_PROPERTY, SiteMapType.Standard.name())); |
95 |
0
|
return marshalSiteMapEntries(entries, type); |
96 |
|
} |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.23 |
|
98 |
0
|
private String marshalSiteMapEntries(List<SiteMapEntry> entries, final SiteMapType type) throws JAXBException {... |
99 |
0
|
JAXBContext jaxbContext = JAXBContext.newInstance(SiteMapEntry.class, SiteMapEntryList.class); |
100 |
0
|
StringWriter sw = new StringWriter(); |
101 |
0
|
Marshaller marshaller = jaxbContext.createMarshaller(); |
102 |
0
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
103 |
0
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
104 |
|
|
105 |
0
|
try { |
106 |
0
|
SiteMapEntryList list = new SiteMapEntryList(entries); |
107 |
0
|
final XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); |
108 |
0
|
final XMLStreamWriter delWriter = new DelegatingXMLStreamWriter(writer) { |
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
109 |
0
|
@Override... |
110 |
|
public void writeStartElement(String prefix, String namespace, String localName) throws XMLStreamException { |
111 |
0
|
super.writeStartElement(prefix, namespace, localName); |
112 |
0
|
if (URL_ELEMENT_NAME.equals(namespace) && SiteMapType.Mobile == type) { |
113 |
0
|
writeEmptyElement(MOBILE_PREFIX, MOBILE_ELEMENT_NAME, ""); |
114 |
|
} |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
117 |
0
|
@Override... |
118 |
|
public void writeNamespace(String prefix, String localName) throws XMLStreamException { |
119 |
0
|
super.writeNamespace(prefix, localName); |
120 |
0
|
if (type == SiteMapType.Mobile) { |
121 |
0
|
super.writeNamespace(MOBILE_PREFIX, MOBILE_SCHEMA_URL); |
122 |
|
} |
123 |
|
} |
124 |
|
}; |
125 |
0
|
marshaller.marshal(list, delWriter); |
126 |
|
} catch (PropertyException e) { |
127 |
0
|
log.error("Failed to rebind marshaller's namespace prefix mapper, did the implementation of JAXB change?", e); |
128 |
|
} catch (XMLStreamException e) { |
129 |
0
|
log.error("Failed to marshal sitemap", e); |
130 |
|
} |
131 |
0
|
return sw.toString(); |
132 |
|
} |
133 |
|
|
134 |
|
} |