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 |
|
@link |
65 |
|
|
66 |
|
@Singleton |
|
|
| 94.9% |
Uncovered Elements: 2 (39) |
Complexity: 12 |
Complexity Density: 0.46 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
5 |
@Inject... |
82 |
|
public SiteMapXMLUtilImpl(Provider<SiteMapService> service) { |
83 |
5 |
this.service = service; |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
86 |
4 |
@Override... |
87 |
|
public String generateSiteMapXML(Node siteMapNode) throws RepositoryException, JAXBException { |
88 |
4 |
List<SiteMapEntry> entries = new ArrayList<SiteMapEntry>(); |
89 |
4 |
Iterator<SiteMapEntry> entryIt = service.get().getSiteMapBeans(siteMapNode); |
90 |
18 |
while (entryIt.hasNext()) { |
91 |
14 |
entries.add(entryIt.next()); |
92 |
|
} |
93 |
4 |
String type = SiteMapNodeTypes.SiteMap.getType(siteMapNode) != null ? SiteMapNodeTypes.SiteMap.getType(siteMapNode) : SiteMapType.Standard.name(); |
94 |
4 |
return marshalSiteMapEntries(entries, SiteMapType.valueOf(type)); |
95 |
|
} |
96 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.23 |
|
97 |
4 |
private String marshalSiteMapEntries(List<SiteMapEntry> entries, final SiteMapType type) throws JAXBException {... |
98 |
4 |
JAXBContext jaxbContext = JAXBContext.newInstance(SiteMapEntry.class, SiteMapEntryList.class); |
99 |
4 |
StringWriter sw = new StringWriter(); |
100 |
4 |
Marshaller marshaller = jaxbContext.createMarshaller(); |
101 |
4 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
102 |
4 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
103 |
|
|
104 |
4 |
try { |
105 |
4 |
SiteMapEntryList list = new SiteMapEntryList(entries); |
106 |
4 |
final XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw); |
107 |
4 |
final XMLStreamWriter delWriter = new DelegatingXMLStreamWriter(writer) { |
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
108 |
74 |
@Override... |
109 |
|
public void writeStartElement(String prefix, String namespace, String localName) throws XMLStreamException { |
110 |
74 |
super.writeStartElement(prefix, namespace, localName); |
111 |
74 |
if (URL_ELEMENT_NAME.equals(namespace) && SiteMapType.Mobile == type) { |
112 |
10 |
writeEmptyElement(MOBILE_PREFIX, MOBILE_ELEMENT_NAME, ""); |
113 |
|
} |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
116 |
4 |
@Override... |
117 |
|
public void writeNamespace(String prefix, String localName) throws XMLStreamException { |
118 |
4 |
super.writeNamespace(prefix, localName); |
119 |
4 |
if (type == SiteMapType.Mobile) { |
120 |
2 |
super.writeNamespace(MOBILE_PREFIX, MOBILE_SCHEMA_URL); |
121 |
|
} |
122 |
|
} |
123 |
|
}; |
124 |
4 |
marshaller.marshal(list, delWriter); |
125 |
|
} catch (PropertyException e) { |
126 |
0 |
log.error("Failed to rebind marshaller's namespace prefix mapper, did the implementation of JAXB change?", e); |
127 |
|
} catch (XMLStreamException e) { |
128 |
0 |
log.error("Failed to marshal sitemap", e); |
129 |
|
} |
130 |
4 |
return sw.toString(); |
131 |
|
} |
132 |
|
|
133 |
|
} |