List of usage examples for javax.xml.bind Marshaller setEventHandler
public void setEventHandler(ValidationEventHandler handler) throws JAXBException;
From source file:MarshalValidation.java
public static void main(String[] args) throws Exception { Person p = new Person(); p.setFirstName("B"); p.setLastName("H"); JAXBContext context = JAXBContext.newInstance(Person.class); Marshaller marshaller = context.createMarshaller(); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File("person.xsd")); marshaller.setSchema(schema);// w ww. ja v a2 s . c o m marshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(ValidationEvent event) { System.out.println(event); return false; } }); marshaller.marshal(p, System.out); }
From source file:Main.java
public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException { Marshaller m = CTX.createMarshaller(); m.setEventHandler(new DefaultValidationEventHandler()); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); m.marshal(o, os);//from w ww . j a v a2 s. c o m }
From source file:Main.java
public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException { String pkg = o.getClass().getPackage().getName(); JAXBContext jc = getCachedContext(pkg); Marshaller m = jc.createMarshaller(); m.setEventHandler(new DefaultValidationEventHandler()); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); m.marshal(o, os);/*from w w w. j a v a 2 s.c o m*/ }
From source file:com.netflix.subtitles.ttml.TtmlParagraphResolver.java
private static <T> T deepCopy(T object, Class<T> clazz, String packages) { try {/*w ww. j a v a 2 s.c o m*/ 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); } }
From source file:Main.java
@SuppressWarnings("rawtypes") public static void marshal(Object object, Writer w) throws JAXBException { Class clazz = object.getClass(); JAXBContext context = s_contexts.get(clazz); if (context == null) { context = JAXBContext.newInstance(clazz); s_contexts.put(clazz, context);// w w w .j a va 2 s .c o m } ValidationEventCollector valEventHndlr = new ValidationEventCollector(); Marshaller marshaller = context.createMarshaller(); marshaller.setSchema(null); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setEventHandler(valEventHndlr); try { marshaller.marshal(object, w); } catch (Exception e) { if (e instanceof JAXBException) { throw (JAXBException) e; } else { throw new MarshalException(e.getMessage(), e); } } if (valEventHndlr.hasEvents()) { for (ValidationEvent valEvent : valEventHndlr.getEvents()) { if (valEvent.getSeverity() != ValidationEvent.WARNING) { // throw a new Marshall Exception if there is a parsing error throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException()); } } } }
From source file:org.vincibean.salestaxes.jaxb.JaxbFactory.java
/** * Factory method, creates a {@link Marshaller} from the context given in the constructor; moreover, ensure that * the marshalled XML data is formatted with linefeeds and indentation. * @return an {@link Optional} object which may or may not contain a {@link Marshaller} *//*from www . j a va2 s . c o m*/ public static Optional<Marshaller> createMarshaller(final Class<?> context) { try { Marshaller marshaller = JAXBContext.newInstance(context).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new ClassPathResource("receipt/Poiuyt.xsd").getFile())); marshaller.setEventHandler(new FoobarValidationEventHandler()); return Optional.fromNullable(marshaller); } catch (JAXBException | SAXException | IOException e) { logger.warn("Exception on jaxb factory creation: ", e); return Optional.absent(); } }
From source file:com.savoirtech.jaxb.engine.MarshallerPool.java
@Override public Object makeObject() throws Exception { LOG.debug("Creating a new marshaller"); Marshaller marshaller = context.createMarshaller(); marshaller.setEventHandler(new ValidationChecker()); marshaller.setProperty("jaxb.encoding", "UTF-8"); return marshaller; }
From source file:com.mijao.poc.jaxb.MarshallerPool.java
@Override public Object makeObject() throws Exception { logger.debug("Creating a new marshaller"); Marshaller marshaller = context.createMarshaller(); marshaller.setEventHandler(new ValidationChecker()); marshaller.setProperty("jaxb.encoding", "UTF-8"); return marshaller; }
From source file:org.apache.cxf.jaxbplus.io.DataWriterImpl.java
public Marshaller createMarshaller(Object elValue, MessagePartInfo part) { Class<?> cls = null;/*w ww. j a v a2s. co m*/ if (part != null) { cls = part.getTypeClass(); } if (cls == null) { cls = null != elValue ? elValue.getClass() : null; } if (cls != null && cls.isArray() && elValue instanceof Collection) { Collection<?> col = (Collection<?>) elValue; elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size())); } Marshaller marshaller; try { marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); marshaller.setListener(databinding.getMarshallerListener()); if (databinding.getValidationEventHandler() != null) { marshaller.setEventHandler(databinding.getValidationEventHandler()); } final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings(); if (nspref != null) { JAXBUtils.setNamespaceWrapper(nspref, marshaller); } if (databinding.getMarshallerProperties() != null) { for (Map.Entry<String, Object> propEntry : databinding.getMarshallerProperties().entrySet()) { try { marshaller.setProperty(propEntry.getKey(), propEntry.getValue()); } catch (PropertyException pe) { LOG.log(Level.INFO, "PropertyException setting Marshaller properties", pe); } } } marshaller.setSchema(schema); AttachmentMarshaller atmarsh = getAttachmentMarshaller(); marshaller.setAttachmentMarshaller(atmarsh); if (schema != null && atmarsh instanceof JAXBAttachmentMarshaller) { //we need a special even handler for XOP attachments marshaller.setEventHandler(new MtomValidationHandler(marshaller.getEventHandler(), (JAXBAttachmentMarshaller) atmarsh)); } } catch (JAXBException ex) { if (ex instanceof javax.xml.bind.MarshalException) { javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex; Message faultMessage = new Message("MARSHAL_ERROR", LOG, marshalEx.getLinkedException().getMessage()); throw new Fault(faultMessage, ex); } else { throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex); } } return marshaller; }
From source file:org.codice.ddf.parser.xml.XmlParser.java
private void marshal(ParserConfigurator configurator, Consumer<Marshaller> marshallerConsumer) throws ParserException { JAXBContext jaxbContext = getContext(configurator.getContextPath(), configurator.getClassLoader()); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); try {/*from w w w . j av a 2 s . c o m*/ Thread.currentThread().setContextClassLoader(configurator.getClassLoader()); Marshaller marshaller = jaxbContext.createMarshaller(); if (configurator.getAdapter() != null) { marshaller.setAdapter(configurator.getAdapter()); } if (configurator.getHandler() != null) { marshaller.setEventHandler(configurator.getHandler()); } for (Map.Entry<String, Object> propRow : configurator.getProperties().entrySet()) { marshaller.setProperty(propRow.getKey(), propRow.getValue()); } marshallerConsumer.accept(marshaller); } catch (RuntimeException e) { LOGGER.error("Error marshalling ", e); throw new ParserException("Error marshalling ", e); } catch (JAXBException e) { LOGGER.error("Error marshalling ", e); throw new ParserException("Error marshalling", e); } finally { Thread.currentThread().setContextClassLoader(tccl); } }