use of javax.xml.bind.JAXBContext in project quickstarts by jboss-switchyard.
the class Library method toString.
public String toString(boolean detailed) {
StringWriter sw = new StringWriter();
synchronized (librarian) {
try {
if (detailed) {
JAXBContext ctx = JAXBContext.newInstance("org.switchyard.quickstarts.demos.library.types");
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
ObjectFactory of = new ObjectFactory();
for (Book book : isbns_to_books.values()) {
int quantity = isbns_to_quantities.get(book.getIsbn());
sw.write("\nBook (quantity=" + quantity + ")\n");
m.marshal(of.createBook(book), sw);
sw.write('\n');
}
} else {
for (Book book : isbns_to_books.values()) {
int quantity = isbns_to_quantities.get(book.getIsbn());
sw.write(book.getTitle() + " (" + quantity + ")\n");
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
return sw.toString().trim();
}
use of javax.xml.bind.JAXBContext in project quickstarts by jboss-switchyard.
the class LibraryClient method wrapRequest.
private <T> String wrapRequest(QName name, Class<T> declaredType, T value, String pid) throws Exception {
JAXBElement<T> e = new JAXBElement<T>(name, declaredType, null, value);
JAXBContext ctx = JAXBContext.newInstance("org.switchyard.quickstarts.demos.library.types");
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
StringWriter sw = new StringWriter();
String processInstanceId = pid != null ? "<bpm:processInstanceId xmlns:bpm='urn:switchyard-component-bpm:bpm:1.0'>" + pid + "</bpm:processInstanceId>" : "";
sw.write(SOAP_REQUEST_PREFIX.replaceFirst("PID", processInstanceId));
m.marshal(e, sw);
sw.write(SOAP_REQUEST_SUFFIX);
return sw.toString();
}
use of javax.xml.bind.JAXBContext in project jersey by jersey.
the class WadlGeneratorResourceDocSupportTest method wadlIsGeneratedWithUnknownCustomParameterAnnotation.
@Test
public void wadlIsGeneratedWithUnknownCustomParameterAnnotation() throws JAXBException {
/* Set up a ClassDocType that has something for a custom-annotated parameter */
ClassDocType cdt = new ClassDocType();
cdt.setClassName(TestResource.class.getName());
MethodDocType mdt = new MethodDocType();
mdt.setMethodName("method");
cdt.getMethodDocs().add(mdt);
ParamDocType pdt = new ParamDocType("x", "comment about x");
mdt.getParamDocs().add(pdt);
AnnotationDocType adt = new AnnotationDocType();
adt.setAnnotationTypeName(CustomParam.class.getName());
adt.getAttributeDocs().add(new NamedValueType("value", "x"));
pdt.getAnnotationDocs().add(adt);
ResourceDocType rdt = new ResourceDocType();
rdt.getDocs().add(cdt);
/* Generate WADL for that class */
WadlGenerator wg = new WadlGeneratorResourceDocSupport(new WadlGeneratorImpl(), rdt);
WadlBuilder wb = new WadlBuilder(wg, false, null);
Resource resource = Resource.from(TestResource.class);
ApplicationDescription app = wb.generate(Collections.singletonList(resource));
/* Confirm that it can be marshalled without error */
StringWriter sw = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Application.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(app.getApplication(), sw);
}
use of javax.xml.bind.JAXBContext in project jersey by jersey.
the class ResourceDoclet method getSerializedLinkFromTag.
private static String getSerializedLinkFromTag(final SeeTag linkTag) {
final MemberDoc referencedMember = linkTag.referencedMember();
if (referencedMember == null) {
throw new NullPointerException("Referenced member of @link " + print(linkTag) + " cannot be resolved.");
}
if (!referencedMember.isStatic()) {
LOG.warning("Referenced member of @link " + print(linkTag) + " is not static." + " Right now only references to static members are supported.");
return null;
}
/* Get referenced example bean
*/
final ClassDoc containingClass = referencedMember.containingClass();
final Object object;
try {
final Field declaredField = Class.forName(containingClass.qualifiedName(), false, Thread.currentThread().getContextClassLoader()).getDeclaredField(referencedMember.name());
if (referencedMember.isFinal()) {
declaredField.setAccessible(true);
}
object = declaredField.get(null);
LOG.log(Level.FINE, "Got object " + object);
} catch (final Exception e) {
LOG.info("Have classloader: " + ResourceDoclet.class.getClassLoader().getClass());
LOG.info("Have thread classloader " + Thread.currentThread().getContextClassLoader().getClass());
LOG.info("Have system classloader " + ClassLoader.getSystemClassLoader().getClass());
LOG.log(Level.SEVERE, "Could not get field " + referencedMember.qualifiedName(), e);
return null;
}
/* marshal the bean to xml
*/
try {
final JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
final StringWriter stringWriter = new StringWriter();
final Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, stringWriter);
final String result = stringWriter.getBuffer().toString();
LOG.log(Level.FINE, "Got marshalled output:\n" + result);
return result;
} catch (final Exception e) {
LOG.log(Level.SEVERE, "Could serialize bean to xml: " + object, e);
return null;
}
}
use of javax.xml.bind.JAXBContext in project quickstarts by jboss-switchyard.
the class JaxbTransformationTest method testJaxbOrderAckToXML.
@Test
public void testJaxbOrderAckToXML() throws Exception {
OrderAck orderAck = new OrderAck();
orderAck.setAccepted(Boolean.TRUE);
orderAck.setOrderId("PO-19838-XYZ");
orderAck.setStatus("Order Accepted");
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { OrderAck.class });
StringWriter resultWriter = new StringWriter();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(orderAck, resultWriter);
_testKit.compareXMLToResource(resultWriter.toString(), ORDER_ACK_XML);
}
Aggregations