private static final String W3C_XML_SCHEMA_NS_URI = "http://www.w3.org/2001/XMLSchema";
<<<<<<< HEAD
/** Assure that only one loading/jaxb operation is active. */
private static final Object SYNC_OBJECT = new Object();
public final T load(StreamSource source, Class clazz, Class> factory, URL xsdFile)
throws JAXBException,
SAXException {
T result;
synchronized (SYNC_OBJECT) {
// TODO creating a JaxbContext is expensive, consider pooling.
Unmarshaller unmarshaller = createUnmarshaller(factory, xsdFile);
unmarshaller.setEventHandler(new XmlValidationEventHandler());
result = unmarshaller.unmarshal(source, clazz).getValue();
}
return result;
}
public final T load(File file, Class clazz, Class> factory, URL xsdFile)
throws JAXBException, SAXException {
Preconditions.checkNotNull(xsdFile);
return load(new StreamSource(file), clazz, factory, xsdFile);
}
private final Unmarshaller createUnmarshaller(final Class> objectFactoryClass, final URL xsdFile)
throws JAXBException, SAXException {
JAXBContext context = JAXBContext.newInstance(objectFactoryClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
if (unmarshaller == null) {
throw new JAXBException("Created unmarshaller is null.");
}
unmarshaller.setSchema(getSchema(xsdFile));
return unmarshaller;
}
private static Schema getSchema(final URL xsdFile) throws SAXException {
// TODO schema factory could be created once, put into pool if expensive
SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
return sf.newSchema(xsdFile);
=======
/** Assure that only one loading/jaxb operation is active. */
private static final Object SYNC_OBJECT = new Object();
public final T load(StreamSource source, Class clazz, Class> factory, URL xsdFile) throws JAXBException, SAXException {
T result;
synchronized (SYNC_OBJECT) {
// TODO check if could work
// SAXParserFactory spf = SAXParserFactory.newInstance();
// spf.setXIncludeAware(true);
// spf.setNamespaceAware(true);
// XMLReader xr = spf.newSAXParser().getXMLReader();
// SAXSource src = new SAXSource(xr, new InputSource("test.xml"));
// TODO creating a JaxbContext is expensive, consider pooling.
Unmarshaller unmarshaller = createUnmarshaller(factory, xsdFile);
unmarshaller.setEventHandler(new XmlValidationEventHandler());
result = unmarshaller.unmarshal(source, clazz).getValue();
}
return result;
}
public final T load(File file, Class clazz, Class> factory, URL xsdFile) throws JAXBException, SAXException {
Preconditions.checkNotNull(xsdFile);
return load(new StreamSource(file), clazz, factory, xsdFile);
}
private final Unmarshaller createUnmarshaller(final Class> objectFactoryClass, final URL xsdFile) throws JAXBException,
SAXException {
JAXBContext context = JAXBContext.newInstance(objectFactoryClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
if (unmarshaller == null) {
throw new JAXBException("Created unmarshaller is null.");
}
unmarshaller.setSchema(getSchema(xsdFile));
return unmarshaller;
}
private static Schema getSchema(final URL xsdFile) throws SAXException {
SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
return sf.newSchema(xsdFile);
>>>>>>> 53a92a10ef0671a29018b950f0b18b9c1c011349
}
} |