public static boolean directWriteMessage(MESSAGE message, OutputStream os) throws SocketException { try { javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(message.getClass().getPackage().getName()); javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); synchronized (os) { marshaller.marshal(message, os); try { os.write("\n".getBytes(DEFAULTENCODING)); } catch (IOException ex) { log.debug(ex); } } } catch (javax.xml.bind.JAXBException ex) { // XXXTODO Handle exception log.error(ex); // NOI18N throw new SocketException(); } return false; }
public static String messageToStringFormatted(MESSAGE message) { String toReturn = ""; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(message.getClass().getPackage().getName()); javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(message, baos); // marshaller.marshal(message, System.out); } catch (javax.xml.bind.JAXBException ex) { // XXXTODO Handle exception log.error(ex.getMessage()); // NOI18N } try { String xml = baos.toString("UTF-8"); toReturn = "\n" + xml; // log.debug("xml to send : " + toReturn); return toReturn; } catch (UnsupportedEncodingException ex) { log.error(ex); return null; } }
public static MESSAGE parseXML(String xml) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE toReturn = objectFactory.createMESSAGE(); try { javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(toReturn.getClass().getPackage().getName()); javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); toReturn = (MESSAGE) unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); // NOI18N return toReturn; } catch (javax.xml.bind.JAXBException ex) { // XXXTODO Handle exception log.error(ex); // NOI18N return null; } }