Refine search
package com.example; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Unmarshaller; import javax.xml.bind.util.JAXBSource; import javax.xml.namespace.QName; public class Demo { public static void main(String[] args) throws Exception { com.home.Student studentA = new com.home.Student(); studentA.setName("Jane Doe"); studentA.setStatus(com.home.Status.FULL_TIME); JAXBContext contextA = JAXBContext.newInstance(com.home.Student.class); JAXBElement<com.home.Student> jaxbElementA = new JAXBElement(new QName("student"), com.home.Student.class, studentA); JAXBSource sourceA = new JAXBSource(contextA, jaxbElementA); JAXBContext contextB = JAXBContext.newInstance(com.school.Student.class); Unmarshaller unmarshallerB = contextB.createUnmarshaller(); JAXBElement<com.school.Student> jaxbElementB = unmarshallerB.unmarshal(sourceA, com.school.Student.class); com.school.Student studentB = jaxbElementB.getValue(); System.out.println(studentB.getName()); System.out.println(studentB.getStatus().getCode()); } }
/** * Creates a new {@link javax.xml.transform.Source} for the given content object. * * @param context * JAXBContext that was used to create * <code>contentObject</code>. This context is used * to create a new instance of marshaller and must not be null. * @param contentObject * An instance of a JAXB-generated class, which will be * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must * not be null. * @throws JAXBException if an error is encountered while creating the * JAXBSource or if either of the parameters are null. */ public JAXBSource( JAXBContext context, Object contentObject ) throws JAXBException { this( ( context == null ) ? assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) : context.createMarshaller(), ( contentObject == null ) ? assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) : contentObject); }
private static <T> T deepCopy(T object, Class<T> clazz, String packages) { try { JAXBContext jaxbContext = JAXBContext.newInstance(packages); // create marshaller which disable validation step Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setEventHandler(event -> true); // create unmarshaller which disable validation step Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setEventHandler(event -> true); JAXBElement<T> contentObject = new JAXBElement<>(new QName(clazz.getName()), clazz, object); return unmarshaller.unmarshal(new JAXBSource(marshaller, contentObject), clazz).getValue(); } catch (JAXBException e) { throw new ParseException("Time overlaps in <p> cannot be resolved.", e); } } }
public Source toSource() { JAXBContext context; try { context = JAXBContext.newInstance(getClass()); return new JAXBSource(context, this); } catch (JAXBException e) { throw new RuntimeException("Unable to marshal JAXB object to source", e); } }
protected void writeReport(SanityReport report) throws IOException { try { // marshall JAXBContext jc = JAXBContext.newInstance(SanityReport.class.getPackage().getName()); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Source source = new JAXBSource(marshaller, report); Result result = new StreamResult(writer); // transform TransformerFactory xformFactory = TransformerFactory.newInstance(); InputStream xslInputStream = HtmlReportWriter.class.getResourceAsStream(DEFAULT_XSL); Source xslSource = new StreamSource(xslInputStream); Transformer transformer = xformFactory.newTransformer(xslSource); transformer.transform(source, result); } catch (Exception e) { throw new SanityRuleException("Problem writing HTML", e); } } }
qname, clazz, wrapped); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz); Class<T> clazz = (Class<T>) value.getClass(); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, value); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz);
public JAXBSource(JAXBContext context, Object contentObject) throws JAXBException { if (context == null) { throw new JAXBException("context must not be null"); } if (contentObject == null) { throw new JAXBException("contentObject must not be null"); } setXMLReader(new Reader(context.createMarshaller(), contentObject)); setInputSource(new InputSource()); }
public JAXBSource getAsJAXBSource() throws JAXBException { return new JAXBSource(createMarshaller(), this); }
public static Cell copyCell(Cell cell, Unmarshaller unmarshaller, Marshaller marshaller) { try { JAXBElement<Cell> contentObject = new JAXBElement<>(new QName(cellName), Cell.class, cell); JAXBSource source = new JAXBSource(marshaller, contentObject); source.setXMLReader(new PseudoXMLReader(marshaller, contentObject)); JAXBElement<Cell> elem = unmarshaller.unmarshal(source, Cell.class); return elem.getValue(); } catch (JAXBException e) { throw new IllegalStateException(e); } }
@Override public Source build() { try { if (marshaller == null) { createDefaultMarshaller(); } final Object jaxbObject = getPreparedJaxbObject(); final JAXBSource jaxbSource = new JAXBSource(marshaller, jaxbObject); // the fake InputSource cannot be used (the Convert.java // will create a working one if it is null) jaxbSource.setInputSource(null); return jaxbSource; } catch (final JAXBException e) { throw new DataBindingException(e); } }
public JAXBSource(final Marshaller marshaller, final Object contentObject) throws JAXBException { if (marshaller == null) { throw new JAXBException("marshaller must not be null"); } if (contentObject == null) { throw new JAXBException("contentObject must not be null"); } setXMLReader(new Reader(marshaller, contentObject)); setInputSource(new InputSource()); }
public static <T> void xmlToXhtml(T catalog, StreamSource xslt, StreamResult result) { try { TransformerFactory.newInstance().newTransformer(xslt) .transform(new JAXBSource(JAXBContext.newInstance(catalog.getClass()), catalog), result); } catch (JAXBException | TransformerException e) { throw new IllegalStateException(e); } }
qname, clazz, wrapped); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz); Class<T> clazz = (Class<T>) value.getClass(); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, value); JAXBSource source = new JAXBSource(jc, contentObject); elem = jc.createUnmarshaller().unmarshal(source, clazz);
public JAXBSource(JAXBContext context, Object contentObject) throws JAXBException { if (context == null) { throw new JAXBException("context must not be null"); } if (contentObject == null) { throw new JAXBException("contentObject must not be null"); } setXMLReader(new Reader(context.createMarshaller(), contentObject)); setInputSource(new InputSource()); }
public JAXBSource getAsJAXBSource() throws JAXBException { return new JAXBSource(createMarshaller(), this); }
public static Cell copyCell(Cell cell, Unmarshaller unmarshaller, Marshaller marshaller) { try { JAXBElement<Cell> contentObject = new JAXBElement<>(new QName(cellName), Cell.class, cell); JAXBSource source = new JAXBSource(marshaller, contentObject); source.setXMLReader(new PseudoXMLReader(marshaller, contentObject)); JAXBElement<Cell> elem = unmarshaller.unmarshal(source, Cell.class); return elem.getValue(); } catch (JAXBException e) { throw new IllegalStateException(e); } }
public JAXBSource(final Marshaller marshaller, final Object contentObject) throws JAXBException { if (marshaller == null) { throw new JAXBException("marshaller must not be null"); } if (contentObject == null) { throw new JAXBException("contentObject must not be null"); } setXMLReader(new Reader(marshaller, contentObject)); setInputSource(new InputSource()); }
static public <T> T deepCopy(T object) { if (object == null) return null; try { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>)object.getClass(); JAXBContext context = JAXBContext.newInstance(clazz); JAXBElement<T> contentObject = new JAXBElement<T>(new QName(clazz.getSimpleName()), clazz, object); JAXBSource source = new JAXBSource(context, contentObject); return context.createUnmarshaller().unmarshal(source, clazz).getValue(); } catch (JAXBException e) { throw new RuntimeException(e); } }
import java.io.File; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.util.JAXBSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; public class Demo { public static void main(String[] args) throws Exception { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("customer.xsd")); JAXBContext jc = JAXBContext.newInstance(Customer.class); Customer customer = new Customer(); // populate the customer object JAXBSource source = new JAXBSource(jc, customer); schema.newValidator().validate(source); } }
/** * Creates a new {@link javax.xml.transform.Source} for the given content object. * * @param context * JAXBContext that was used to create * <code>contentObject</code>. This context is used * to create a new instance of marshaller and must not be null. * @param contentObject * An instance of a JAXB-generated class, which will be * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must * not be null. * @throws JAXBException if an error is encountered while creating the * JAXBSource or if either of the parameters are null. */ public JAXBSource( JAXBContext context, Object contentObject ) throws JAXBException { this( ( context == null ) ? assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) : context.createMarshaller(), ( contentObject == null ) ? assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) : contentObject); }