use of javax.xml.bind.JAXBContext in project jOOQ by jOOQ.
the class GenerationTool method load.
/**
* Load a jOOQ codegen configuration file from an input stream
*/
public static Configuration load(InputStream in) throws IOException {
// [#1149] If there is no namespace defined, add the default one
ByteArrayOutputStream out = new ByteArrayOutputStream();
copyLarge(in, out);
String xml = out.toString();
// TODO [#1201] Add better error handling here
xml = xml.replaceAll("<(\\w+:)?configuration xmlns(:\\w+)?=\"http://www.jooq.org/xsd/jooq-codegen-\\d+\\.\\d+\\.\\d+.xsd\">", "<$1configuration xmlns$2=\"" + Constants.NS_CODEGEN + "\">");
xml = xml.replace("<configuration>", "<configuration xmlns=\"" + Constants.NS_CODEGEN + "\">");
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
javax.xml.validation.Schema schema = sf.newSchema(GenerationTool.class.getResource("/xsd/" + Constants.XSD_CODEGEN));
JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(new ValidationEventHandler() {
@Override
public boolean handleEvent(ValidationEvent event) {
log.warn("Unmarshal warning", event.getMessage());
return true;
}
});
return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
throw new GeneratorException("Error while reading XML configuration", e);
}
}
use of javax.xml.bind.JAXBContext in project jersey by jersey.
the class AbstractJaxbProvider method getUnmarshaller.
/**
* Get the JAXB unmarshaller for the given class and media type.
* <p>
* In case this provider instance has been {@link #AbstractJaxbProvider(Providers, MediaType)
* created with a fixed resolver media type}, the supplied media type argument will be ignored.
* </p>
*
* @param type Java type to be unmarshalled.
* @param mediaType entity media type.
* @return JAXB unmarshaller for the requested Java type, media type combination.
* @throws JAXBException in case retrieving the unmarshaller fails with a JAXB exception.
*/
protected final Unmarshaller getUnmarshaller(Class type, MediaType mediaType) throws JAXBException {
if (fixedResolverMediaType) {
return getUnmarshaller(type);
}
final ContextResolver<Unmarshaller> unmarshallerResolver = jaxrsProviders.getContextResolver(Unmarshaller.class, mediaType);
if (unmarshallerResolver != null) {
Unmarshaller u = unmarshallerResolver.getContext(type);
if (u != null) {
return u;
}
}
final JAXBContext ctx = getJAXBContext(type, mediaType);
return (ctx == null) ? null : ctx.createUnmarshaller();
}
use of javax.xml.bind.JAXBContext in project jersey by jersey.
the class AbstractJaxbProvider method getMarshaller.
/**
* Get the JAXB marshaller for the given class and media type.
* <p>
* In case this provider instance has been {@link #AbstractJaxbProvider(Providers, MediaType)
* created with a fixed resolver media type}, the supplied media type argument will be ignored.
* </p>
*
* @param type Java type to be marshalled.
* @param mediaType entity media type.
* @return JAXB marshaller for the requested Java type, media type combination.
* @throws JAXBException in case retrieving the marshaller fails with a JAXB exception.
*/
protected final Marshaller getMarshaller(Class type, MediaType mediaType) throws JAXBException {
if (fixedResolverMediaType) {
return getMarshaller(type);
}
final ContextResolver<Marshaller> mcr = jaxrsProviders.getContextResolver(Marshaller.class, mediaType);
if (mcr != null) {
Marshaller m = mcr.getContext(type);
if (m != null) {
return m;
}
}
final JAXBContext ctx = getJAXBContext(type, mediaType);
if (ctx == null) {
return null;
}
Marshaller m = ctx.createMarshaller();
if (formattedOutput.get()) {
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput.get());
}
return m;
}
use of javax.xml.bind.JAXBContext in project jersey by jersey.
the class AbstractJaxbProvider method getMarshaller.
private Marshaller getMarshaller(Class type) throws JAXBException {
final ContextResolver<Marshaller> resolver = mtMarshaller.get();
if (resolver != null) {
Marshaller u = resolver.getContext(type);
if (u != null) {
return u;
}
}
final JAXBContext ctx = getJAXBContext(type);
if (ctx == null) {
return null;
}
Marshaller m = ctx.createMarshaller();
if (formattedOutput.get()) {
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput.get());
}
return m;
}
use of javax.xml.bind.JAXBContext in project jersey by jersey.
the class JaxbStringReaderProvider method getStoredJAXBContext.
/**
* Get the stored JAXB context supporting the Java type.
*
* @param type Java type supported by the stored JAXB context.
* @return stored JAXB context supporting the Java type.
* @throws JAXBException in case JAXB context retrieval fails.
*/
protected JAXBContext getStoredJAXBContext(Class type) throws JAXBException {
synchronized (jaxbContexts) {
JAXBContext c = jaxbContexts.get(type);
if (c == null) {
c = JAXBContext.newInstance(type);
jaxbContexts.put(type, c);
}
return c;
}
}
Aggregations