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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.vineetmanohar.util;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;

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.xml.sax.SAXException;

/**
* Simple JAXB Wrapper
*
* @author Vineet Manohar
*/
public class JaxbWrapper<T> {
private JAXBContext jaxbContext = null;

private Schema schema;

/**
* @param schemaUrl
* @param clazz
* @throws JAXBException
* @throws SAXException
*/
public JaxbWrapper(URL schemaUrl, Class<?>... clazz) throws JAXBException, SAXException {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
this.schema = schemaFactory.newSchema(schemaUrl);
this.jaxbContext = JAXBContext.newInstance(clazz);
}

/**
* @param schemaFile
* @param clazz
* @throws JAXBException
* @throws SAXException
*/
public JaxbWrapper(File schemaFile, Class<?>... clazz) throws JAXBException, SAXException {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
this.schema = schemaFactory.newSchema(schemaFile);
this.jaxbContext = JAXBContext.newInstance(clazz);
}

/**
* Converts the given object to XML
*
* @param t
* @return
* @throws JAXBException
*
* @author Vineet Manohar
*/
public String objectToXml(T t) throws JAXBException {
StringWriter writer = new StringWriter();
objectToXml(t, writer);
return writer.toString();
}

/**
* Converts the given object to XML
*
* @param t
* the object to convert to xml
* @param writer
* the output will be written to this writer
*
* @throws JAXBException
*
* @author Vineet Manohar
*/
public void objectToXml(T t, Writer writer) throws JAXBException {
createMarshaller().marshal(t, writer);
}

/**
* Converts the given object to XML
*
* @param t
* the object to convert to xml
* @param outputStream
* the output will be written to this output stream
*
* @throws JAXBException
*
* @author Vineet Manohar
*/
public void objectToXml(T t, OutputStream outputStream) throws JAXBException {
createMarshaller().marshal(t, outputStream);
}

/**
* Converts the given object to XML
*
* @param t
* the object to convert to xml
* @param file
* the output will be written to this file
*
* @throws JAXBException
*
* @author Vineet Manohar
*/
public void objectToXml(T t, File file) throws JAXBException {
createMarshaller().marshal(t, file);
}

/**
* validates the object against the schema, throws an Exception if
* validation fails
*
* @param t
* the object to validate
* @throws JAXBException
* when schema validation fails
*
* @author Vineet Manohar
*/
public void validate(T t) throws JAXBException {
createMarshaller().marshal(t, new StringWriter());
}

/**
* converts xml form to a java object
*
* @param is
* InputStream which points to a valid XML content
* @return the Java object representing the xml
* @throws JAXBException
* if jaxb unmarshalling fails. Common reason is schema
* incompatibility
*
* @author Vineet Manohar
*/
@SuppressWarnings("unchecked")
public T xmlToObject(InputStream is) throws JAXBException {
return (T) createUnmarshaller().unmarshal(is);
}

/**
* converts xml form to a java object
*
* @param xml
* a valid XML string
* @return the Java object representing the xml
* @throws JAXBException
* if jaxb unmarshalling fails. Common reason is schema
* incompatibility
*
* @author Vineet Manohar
*/
@SuppressWarnings("unchecked")
public T xmlToObject(String xml) throws JAXBException {
StringReader stringReader = new StringReader(xml);
return (T) createUnmarshaller().unmarshal(stringReader);
}

/**
* Creates a marshaller. Clients generally don't need to call this method,
* hence the method is protected. If you want use features not already
* exposed, you can subclass and call this method.
*
* @return
* @throws JAXBException
* @throws PropertyException
*
* @author Vineet Manohar
*/
protected Marshaller createMarshaller() throws JAXBException, PropertyException {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setSchema(schema);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
return marshaller;
}

/**
* Creates a marshaller. Clients generally don't need to call this method,
* hence the method is protected. If you want use features not already
* exposed, you can subclass and call this method.
*
* @return
* @throws JAXBException
*
* @author Vineet Manohar
*/
protected Unmarshaller createUnmarshaller() throws JAXBException {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return unmarshaller;
}
}

Change log

r8 by vineet.manohar on Sep 2, 2009   Diff
added utility methods
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by vineet.manohar on Sep 2, 2009   Diff
generalized class using generics
All revisions of this file

File info

Size: 5845 bytes, 201 lines
Powered by Google Project Hosting