My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.vineetmanohar.sitemap;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

import com.vineetmanohar.sitemap.jaxb.Urlset;

/**
* Simple JAXB Wrapper for Sitemap
*
* @author Vineet Manohar
*/
public class SitemapJAXBWrapper {
private static boolean initialized = false;
private static JAXBContext jaxbContext = null;
private static Log log = LogFactory.getLog(SitemapJAXBWrapper.class);

private static Schema schema;

private static SchemaFactory schemaFactory = null;

static {
try {
init();
} catch (JAXBException e) {
throw new RuntimeException("Could not intialize class", e);
} catch (SAXException e) {
throw new RuntimeException("Could not intialize class", e);
}
}

private static Marshaller createMarshaller() throws JAXBException {
return createMarshaller(schema);
}

private static Marshaller createMarshaller(Schema schema)
throws JAXBException, PropertyException {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setSchema(schema);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(
true));
return marshaller;
}

private static Unmarshaller createUnmarshaller() throws JAXBException {
return createUnmarshaller(schema);
}

private static Unmarshaller createUnmarshaller(Schema schema)
throws JAXBException {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return unmarshaller;
}

private static void init() throws JAXBException, SAXException {
if (initialized) {
log.info("jaxb context already initialized");
return;
}

jaxbContext = JAXBContext.newInstance(Urlset.class);
schemaFactory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");

schema = schemaFactory.newSchema(SitemapJAXBWrapper.class
.getResource("/sitemap.xsd"));

log.info("jaxb context initialized");
initialized = true;
}

public static String toXml(Urlset urlset) throws JAXBException {
StringWriter writer = new StringWriter();
createMarshaller().marshal(urlset, writer);
return writer.toString();
}

public static void validate(Urlset urlSet) throws JAXBException {
createMarshaller().marshal(urlSet, new StringWriter());
}

public static Urlset xmlToObject(String xml) throws JAXBException {
StringReader stringReader = new StringReader(xml);
return (Urlset) createUnmarshaller().unmarshal(stringReader);
}
}

Change log

r7 by vineet.manohar on Sep 2, 2009   Diff
sorted members
Go to: 
Project members, sign in to write a code review

Older revisions

r5 by vineet.manohar on Sep 1, 2009   Diff
simple jaxb wrapper for validating,
xml to java and java to xml for
sitemap files
All revisions of this file

File info

Size: 2804 bytes, 99 lines
Powered by Google Project Hosting