use of javax.xml.bind.JAXBContext 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.JAXBContext in project hibernate-orm by hibernate.
the class XmlParserHelper method getJaxbRoot.
public <T> T getJaxbRoot(InputStream stream, Class<T> clazz, Schema schema) throws XmlParsingException {
XMLEventReader staxEventReader;
try {
staxEventReader = createXmlEventReader(stream);
} catch (XMLStreamException e) {
throw new XmlParsingException("Unable to create stax reader", e);
}
ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
try {
staxEventReader = new JpaNamespaceTransformingEventReader(staxEventReader);
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
unmarshaller.setEventHandler(handler);
return clazz.cast(unmarshaller.unmarshal(staxEventReader));
} catch (JAXBException e) {
StringBuilder builder = new StringBuilder();
builder.append("Unable to perform unmarshalling at line number ");
builder.append(handler.getLineNumber());
builder.append(" and column ");
builder.append(handler.getColumnNumber());
builder.append(". Message: ");
builder.append(handler.getMessage());
throw new XmlParsingException(builder.toString(), e);
}
}
use of javax.xml.bind.JAXBContext 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.JAXBContext in project openhab1-addons by openhab.
the class FritzahaWebserviceUpdateXmlCallback method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(int status, String response) {
super.execute(status, response);
if (validRequest) {
logger.trace("Received State response " + response + " for item " + itemName);
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DevicelistModel.class);
Unmarshaller jaxbUM = jaxbContext.createUnmarshaller();
DevicelistModel model = (DevicelistModel) jaxbUM.unmarshal(new StringReader(response));
ArrayList<DeviceModel> list = model.getDevicelist();
for (DeviceModel device : list) {
if (device.getIdentifier().equals(this.deviceAin)) {
BigDecimal meterValueScaled = new BigDecimal(0);
switch(type) {
case POWER:
meterValueScaled = device.getPowermeter().getPower().scaleByPowerOfTen(-3);
break;
case ENERGY:
meterValueScaled = device.getPowermeter().getEnergy();
break;
case TEMPERATURE:
meterValueScaled = device.getTemperature().getCelsius().scaleByPowerOfTen(-1);
break;
default:
logger.warn("unknown meter type: " + type);
break;
}
logger.debug(device.toString());
webIface.postUpdate(itemName, new DecimalType(meterValueScaled));
} else {
logger.trace("device " + device.getIdentifier() + " was not requested");
}
}
} catch (JAXBException e) {
logger.error(e.getLocalizedMessage(), e);
}
}
}
use of javax.xml.bind.JAXBContext in project openhab1-addons by openhab.
the class SmarthomaticBinding method activate.
/**
* activate binding
*
*/
@Override
public void activate() {
// log activate of binding
if (baseStation != null) {
logger.info("Smarthomatic Binding activated. BaseStation= {}", baseStation.toString());
}
Bundle bundle = SmarthomaticActivator.getContext().getBundle();
URL fileURL = bundle.getEntry("packet_layout.xml");
Packet packet = null;
try {
InputStream inputStream = fileURL.openConnection().getInputStream();
JAXBContext jaxbContext = JAXBContext.newInstance(Packet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
packet = (Packet) jaxbUnmarshaller.unmarshal(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
this.packet = packet;
}
Aggregations