List of usage examples for javax.xml.bind JAXBContext createMarshaller
public abstract Marshaller createMarshaller() throws JAXBException;
From source file:org.springframework.hateoas.ResourceIntegrationTest.java
/** * @see #124/*from w ww.j a v a2 s . c om*/ * @see #154 */ @Test public void marshalsResourceToXml() throws Exception { Person person = new Person(); person.firstname = "Dave"; person.lastname = "Matthews"; PersonResource resource = new PersonResource(person); resource.add(new Link("/foo", "bar")); JAXBContext context = JAXBContext.newInstance(PersonResource.class, Person.class); StringWriter writer = new StringWriter(); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(resource, writer); assertThat(new Diff(XML_REFERENCE, writer.toString()).similar(), is(true)); }
From source file:dk.dbc.rawrepo.oai.ResumptionTokenTest.java
@Test public void testXmlExpiration() throws Exception { ObjectNode jsonOriginal = (ObjectNode) new ObjectMapper().readTree("{\"foo\":\"bar\"}"); long now = Instant.now().getEpochSecond(); ResumptionTokenType token = ResumptionToken.toToken(jsonOriginal, 0); OAIPMH oaipmh = OBJECT_FACTORY.createOAIPMH(); ListRecordsType getRecord = OBJECT_FACTORY.createListRecordsType(); oaipmh.setListRecords(getRecord);/*from www . j a v a 2s .c o m*/ getRecord.setResumptionToken(token); JAXBContext context = JAXBContext.newInstance(OAIPMH.class); StringWriter writer = new StringWriter(); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(oaipmh, writer); String xml = writer.getBuffer().toString(); System.out.println("XML is:\n" + xml); int start = xml.indexOf("expirationDate=\"") + "expirationDate=\"".length(); int end = xml.indexOf("\"", start); String timestamp = xml.substring(start, end); System.out.println("timestamp = " + timestamp); assertTrue("Timestamp should be in ISO_INSTANT ending with Z", timestamp.endsWith("Z")); TemporalAccessor parse = DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault()).parse(timestamp); long epochSecond = Instant.from(parse).getEpochSecond(); long diff = Math.abs(now - epochSecond); System.out.println("diff = " + diff); assertTrue("Difference between expirationdate and now should be 10 sec or less", diff <= 10); }
From source file:com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlistParser.java
private void marshallPlist(Plist plist, Writer projectFile) throws JAXBException { JAXBContext ctx = JAXBContext.newInstance(com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlist.class); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); try {/*from w ww.j ava 2 s . co m*/ marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", xmlHeaders); } catch (PropertyException ex) { marshaller.setProperty("com.sun.xml.bind.xmlHeaders", xmlHeaders); } marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.marshal(plist, projectFile); }
From source file:eu.impress.impressplatform.IntegrationLayer.ResourcesMgmt.BedAvailabilityServiceBean.java
@Override public String createBedAvailabilityDE() throws DatatypeConfigurationException { String DEmessageenvelope = ""; String DEmessage = ""; EDXLDistribution ed = EDXLlib.createEDXLEnvelope(); try {/*w w w. ja v a 2 s. c om*/ JAXBContext jaxbContext = JAXBContext.newInstance(EDXLDistribution.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); //jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter sw = new StringWriter(); //marshal the envelope jaxbMarshaller.marshal(ed, sw); DEmessageenvelope = sw.toString(); //could not unescape characters no matter what! //encapsulate the edxl have message into DE by avoiding jaxb //DEmessage = EDXLlib.DEEncapsulation(DEmessageenvelope, edxlhave); DEmessage = DEmessageenvelope; } catch (JAXBException e) { e.printStackTrace(); return "Error Marshalling XML Object" + HttpStatus.INTERNAL_SERVER_ERROR; } return DEmessage; }
From source file:org.javelin.sws.ext.bind.internal.model.ClassHierarchyTest.java
@Test public void handleXmlAccessTypePropertyWithBase() throws Exception { // f3, f4, p1, p2, p3, p4 - properties and annotated fields System.out.println("\nD2"); JAXBContext context = JAXBContext.newInstance(D2.class); context.createMarshaller().marshal(new JAXBElement<D2>(new QName("", "r"), D2.class, new D2()), System.out); System.out.println();/*from ww w .j a va 2 s . c o m*/ final List<DOMResult> results = new LinkedList<DOMResult>(); context.generateSchema(new SchemaOutputResolver() { @Override public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException { DOMResult result = new DOMResult(); results.add(result); result.setSystemId(suggestedFileName); return result; } }); for (DOMResult dr : results) { javax.xml.transform.TransformerFactory.newInstance().newTransformer().transform( new javax.xml.transform.dom.DOMSource(dr.getNode()), new javax.xml.transform.stream.StreamResult(new java.io.PrintWriter(System.out))); } JAXBContext ctx = SweJaxbContextFactory.createContext(new Class[] { D2.class }, null); Map<Class<?>, TypedPattern<?>> patterns = (Map<Class<?>, TypedPattern<?>>) ReflectionTestUtils.getField(ctx, "patterns"); ComplexTypePattern<D2> pattern = (ComplexTypePattern<D2>) patterns.get(D2.class); Map<QName, PropertyMetadata<D2, ?>> elements = (Map<QName, PropertyMetadata<D2, ?>>) ReflectionTestUtils .getField(pattern, "elements"); assertThat(elements.size(), equalTo(8)); assertTrue(elements.containsKey(new QName("", "f3"))); assertTrue(elements.containsKey(new QName("", "f4"))); assertTrue(elements.containsKey(new QName("", "fd3"))); assertTrue(elements.containsKey(new QName("", "fd4"))); assertTrue(elements.containsKey(new QName("", "p1"))); assertTrue(elements.containsKey(new QName("", "p2"))); assertTrue(elements.containsKey(new QName("", "p3"))); assertTrue(elements.containsKey(new QName("", "p4"))); }
From source file:org.apache.cxf.systest.jaxrs.jaxws.BookStoreSoapRestImpl.java
public Book getBook(Long id) throws BookNotFoundFault { if (books.get(id) == null) { if (id == 0) { try { OutputStream os = jaxrsContext.getHttpServletResponse().getOutputStream(); JAXBContext c = JAXBContext.newInstance(new Class[] { Book.class }); Marshaller m = c.createMarshaller(); m.marshal(books.get(123L), os); os.flush();//ww w.j ava 2s . c o m return null; } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(); } } int returnCode = 404; if (id == 321) { returnCode = 525; } else if (id == 322) { BookNotFoundDetails details = new BookNotFoundDetails(); details.setId(id); throw new BookNotFoundFault(details); } String msg = "No Book with id " + id + " is available"; ResponseBuilder builder = Response.status(returnCode).header("BOOK-HEADER", msg); if (returnCode == 404) { builder.type("text/plain").entity(msg); } throw new WebApplicationException(builder.build()); } if (!ignoreJaxrsClient) { if (!invocationInProcess) { invocationInProcess = true; return webClient.getBook(id); } invocationInProcess = false; } return books.get(id); }
From source file:com.onespatial.jrc.tns.oml_to_rif.fixture.DomBasedUnitTest.java
/** * @param rifDocument/*from w w w . j a v a 2 s .co m*/ * {@link org.w3._2007.rif.Document} * @return {@link Document} * @throws JAXBException * if any errors occurred trying to marshall the RIF content to * an DOM document * @throws ParserConfigurationException */ protected org.w3c.dom.Document getDomFromRif(org.w3._2007.rif.Document rifDocument) throws JAXBException { org.w3c.dom.Document domDocument = builder.newDocument(); JAXBContext jc = JAXBContext.newInstance("org.w3._2007.rif", getClass().getClassLoader()); //$NON-NLS-1$ jc.createMarshaller() .marshal(new JAXBElement<org.w3._2007.rif.Document>(new QName("http://www.w3.org/2007/rif#", //$NON-NLS-1$ "Document", "rif"), org.w3._2007.rif.Document.class, rifDocument), //$NON-NLS-1$ //$NON-NLS-2$ domDocument); return domDocument; }
From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java
@Override public String getBedTypeAllAvailablityHAVE() { String hospitalstatushave;/*from www. j a v a 2 s .c om*/ List<eu.impress.repository.model.BedStats> bedStatsList = bedService.getHospitalAllAvailableBedTypes(); HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList); try { JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(hospitalStatus, sw); hospitalstatushave = sw.toString(); } catch (JAXBException e) { e.printStackTrace(); return "Error Marshalling XML Object"; } return hospitalstatushave; }
From source file:com.healthcit.cacure.xforms.XModuleModel.java
public XModuleModel(Module module) { this.moduleRoot = module; try {/* w w w.java 2 s . co m*/ JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.metadata.module"); jaxbMarshaller = jc.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); } catch (JAXBException e) { log.error("Error creating module metadata via jaxb", e); } }
From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java
@Override public String getBedTypeAvailablityHAVE(String hospitalname) { String hospitalstatushave;//from w w w. ja v a 2 s .c o m List<eu.impress.repository.model.BedStats> bedStatsList = bedService .getHospitalAvailableBedTypes(hospitalname); HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList); try { JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter sw = new StringWriter(); jaxbMarshaller.marshal(hospitalStatus, sw); hospitalstatushave = sw.toString(); } catch (JAXBException e) { e.printStackTrace(); return "Error Marshalling XML Object"; } return hospitalstatushave; }