use of javax.xml.bind.Marshaller 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.Marshaller 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);
}
use of javax.xml.bind.Marshaller in project quickstarts by jboss-switchyard.
the class JaxbTransformationTest method testJaxbOrderToXML.
@Test
public void testJaxbOrderToXML() throws Exception {
Order order = new Order();
order.setItemId("BUTTER");
order.setOrderId("PO-19838-XYZ");
order.setQuantity(200);
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] { Order.class });
StringWriter resultWriter = new StringWriter();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(order, resultWriter);
_testKit.compareXMLToResource(resultWriter.toString(), ORDER_XML);
}
use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.
the class ConfigParser method marshal.
/**
* This method saves List of Request objects into xml file
*
* @param requests
* object to be saved
* @param xmlFileLocation
* file object to save the object into
*/
@SuppressWarnings("resource")
public void marshal(List<Request> requests, File xmlFileLocation) throws StiebelHeatPumpException {
JAXBContext context;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFileLocation), "UTF-8"));
} catch (IOException e) {
throw new StiebelHeatPumpException(e.toString());
}
try {
context = JAXBContext.newInstance(Requests.class);
} catch (JAXBException e) {
throw new StiebelHeatPumpException(e.toString());
}
Marshaller m;
try {
m = context.createMarshaller();
} catch (JAXBException e) {
throw new StiebelHeatPumpException(e.toString());
}
try {
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (PropertyException e) {
throw new StiebelHeatPumpException(e.toString());
}
try {
m.marshal(new Requests(requests), writer);
} catch (JAXBException e) {
throw new StiebelHeatPumpException(e.toString());
}
try {
writer.close();
} catch (IOException e) {
throw new StiebelHeatPumpException(e.toString());
}
}
use of javax.xml.bind.Marshaller in project openhab1-addons by openhab.
the class LgTvChannelSet method savetofile.
/**
* Save Channel List to File f
*
* @param f
*/
public void savetofile(String f) {
Writer writer = null;
JAXBContext jc;
try {
jc = JAXBContext.newInstance(envelope.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
marshaller.marshal(envel, writer);
} catch (PropertyException e) {
logger.error("error in savetofile", e);
} catch (JAXBException e) {
logger.error("error in savetofile", e);
} catch (IOException ex) {
logger.error("error in savetofile", ex);
} finally {
try {
writer.close();
} catch (Exception ex) {
}
}
}
Aggregations