@Override public <C> ContentHandle<C> newHandle(Class<C> type) { ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new JAXBHandle<C>(factoryContext) : null; return handle; } }
/** * Assigns the root object of the JAXB structure for the content * and returns the handle as a fluent convenience. * @param content the root JAXB object * @return this handle */ public JAXBHandle<C> with(C content) { set(content); return this; }
/** * Returns the JAXB structure as an XML string. */ @Override public String toString() { try { return new String(toBuffer(),"UTF-8"); } catch (UnsupportedEncodingException e) { throw new MarkLogicIOException(e); } }
@Override public JobInstance getJobInstance(Long instanceId) { String uri = SPRING_BATCH_DIR + instanceId.toString() + ".xml"; XMLDocumentManager xmlDocMgr = databaseClient.newXMLDocumentManager(); DocumentDescriptor desc = xmlDocMgr.exists(uri); if (desc == null) { return null; } else { JAXBHandle<MarkLogicJobInstance> jaxbHandle = xmlDocMgr.read(uri, new JAXBHandle<MarkLogicJobInstance>(jaxbContext())); MarkLogicJobInstance mji = jaxbHandle.get(); return mji.getJobInstance(); } }
JAXBHandle objectHandle = new JAXBHandle(context); docMgr.write(docId, objectHandle.with(refers)); Refers refers2 = (Refers) docMgr.read(docId, objectHandle).get(); assertTrue("Failed to read JAXB root", refers2 != null); assertEquals("JAXB document with different root", refers.name, refers2.name); JAXBHandle<Refers> objectHandle2 = new JAXBHandle<>(context); docMgr.write(docId, objectHandle2.with(refers)); refers2 = docMgr.read(docId, objectHandle2).get(); assertTrue("Failed to read JAXB root", refers2 != null); assertEquals("JAXB document with different root", refers.name, refers2.name);
JAXBHandle jaxbH = new JAXBHandle(jaxbContext).with(refers); before = jaxbH.toBuffer(); beforeStr = new String(before); jaxbH.fromBuffer(before); after = jaxbH.toBuffer(); assertNotNull("JAXB after",after); assertXMLEqual("JAXB buffering",beforeStr,new String(after));
/** * Write document using JAXBHandle with metadata * * @param client * @param product * @param uri * @param metadataHandle * @param type * @throws JAXBException */ public void writeDocumentUsingJAXBHandle(DatabaseClient client, Product product, String uri, DocumentMetadataHandle metadataHandle, String type) throws JAXBException { // set jaxb context JAXBContext context = JAXBContext.newInstance(Product.class); // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // create an identifier for the document String docId = uri + product.getName() + ".xml"; // create a handle on the content JAXBHandle contentHandle = new JAXBHandle(context); contentHandle.set(product); // write the doc docMgr.write(docId, metadataHandle, contentHandle); System.out.println("Write " + docId + " to the database"); }
writeset.add(docId[0], new JAXBHandle<Product>(context).with(product1)); writeset.add(docId[1], new JAXBHandle<Product>(context).with(product2)); writeset.add(docId[2], new JAXBHandle<Product>(context).with(product3));
/** * Returns the root object of the JAXB structure for the content * cast to a more specific class. * @param as the class of the object * @return the root JAXB object */ public <T> T get(Class<T> as) { if (content == null) { return null; } if (as == null) { throw new IllegalArgumentException("Cannot cast content to null class"); } if (!as.isAssignableFrom(content.getClass())) { throw new IllegalArgumentException( "Cannot cast "+content.getClass().getName()+" to "+as.getName() ); } @SuppressWarnings("unchecked") T content = (T) get(); return content; } /**
/** * Returns the marshaller that converts a tree data structure * from Java objects to XML, reusing any existing marshaller. * @return the marshaller for the JAXB context */ public Marshaller getMarshaller() throws JAXBException { return getMarshaller(true); } /**
/** * Returns the unmarshaller that converts a tree data structure * from XML to Java objects, reusing any existing unmarshaller. * @return the unmarshaller for the JAXB context */ public Unmarshaller getUnmarshaller() throws JAXBException { return getUnmarshaller(true); } /**
/** * Specifies the mime type of the content and returns the handle * as a fluent convenience. * @param mimetype the mime type of the content * @return this handle */ public JAXBHandle<C> withMimetype(String mimetype) { setMimetype(mimetype); return this; }
/** * fromBuffer() unmarshals a JAXB POJO from a byte array * buffer. The buffer must store the marshaled XML for the * JAXB POJO in UTF-8 encoding. JAXB cannot unmarshal arbitrary XML. */ @Override public void fromBuffer(byte[] buffer) { if (buffer == null || buffer.length == 0) content = null; else receiveContent(new ByteArrayInputStream(buffer)); } @Override
/** * Initializes the JAXB handle with the JAXB context for the classes * of the marshalled or unmarshalled structure. * @param context the JAXB context */ public JAXBHandle(JAXBContext context) { super(); if (context == null) { throw new IllegalArgumentException( "null JAXB context for converting classes" ); } super.setFormat(Format.XML); setResendable(true); this.context = context; }
@Override public List<JobExecution> findJobExecutions(JobInstance jobInstance) { String uri = SPRING_BATCH_DIR + jobInstance.getId().toString() + ".xml"; XMLDocumentManager xmlDocMgr = databaseClient.newXMLDocumentManager(); JAXBHandle<MarkLogicJobInstance> handle = new JAXBHandle<>(jaxbContext()); MarkLogicJobInstance mji = xmlDocMgr.read(uri, handle).get(); List<JobExecution> jobExecutions = mji.getJobExecutions(); Collections.reverse(mji.getJobExecutions()); return jobExecutions; }
DocumentWriteSet writeset = docMgr.newWriteSet(); writeset.add(docId[0], new JAXBHandle<Product>(context).with(product1)); writeset.add(docId[1], new JAXBHandle<Product>(context).with(product2)); writeset.add(docId[2], new JAXBHandle<Product>(context).with(product3));
/** * Returns the root object of the JAXB structure for the content * cast to a more specific class. * @param as the class of the object * @param <T> the type to return * @return the root JAXB object */ public <T> T get(Class<T> as) { if (content == null) { return null; } if (as == null) { throw new IllegalArgumentException("Cannot cast content to null class"); } if (!as.isAssignableFrom(content.getClass())) { throw new IllegalArgumentException( "Cannot cast "+content.getClass().getName()+" to "+as.getName() ); } @SuppressWarnings("unchecked") T content = (T) get(); return content; } /**
/** * Returns the marshaller that converts a tree data structure * from Java objects to XML, reusing any existing marshaller. * @return the marshaller for the JAXB context * @throws JAXBException if marshaller initialization fails */ public Marshaller getMarshaller() throws JAXBException { return getMarshaller(true); } /**
/** * Returns the unmarshaller that converts a tree data structure * from XML to Java objects, reusing any existing unmarshaller. * @return the unmarshaller for the JAXB context * @throws JAXBException if unmarshaller initialization fails */ public Unmarshaller getUnmarshaller() throws JAXBException { return getUnmarshaller(true); } /**
/** * Specifies the mime type of the content and returns the handle * as a fluent convenience. * @param mimetype the mime type of the content * @return this handle */ public JAXBHandle<C> withMimetype(String mimetype) { setMimetype(mimetype); return this; }