static String format( String property ) { return format( property, null ); }
public void parse(InputSource input) throws SAXException { parse(); }
/** * 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); }
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()); } }
public boolean handleEvent( ValidationEvent event ) { events.add(event); boolean retVal = true; switch( event.getSeverity() ) { case ValidationEvent.WARNING: retVal = true; // continue validation break; case ValidationEvent.ERROR: retVal = true; // continue validation break; case ValidationEvent.FATAL_ERROR: retVal = false; // halt validation break; default: _assert( false, Messages.format( Messages.UNRECOGNIZED_SEVERITY, event.getSeverity() ) ); break; } return retVal; }
public Word2003XmlConverter(Source source) throws JAXBException, Docx4JException { // Use 2003-import.xsl to convert to a Transition03To06 object java.lang.ClassLoader classLoader = Word2003XmlConverter.class.getClassLoader(); JAXBResult result = new JAXBResult( JAXBContext.newInstance("org.docx4j.convert.in.word2003xml", classLoader) ); XmlUtils.transform(source, xslt, null, result); // set the unmarshalled content tree transitionContainer = (Transition03To06)result.getResult(); }
/** * Creates a new instance that uses the specified * JAXBContext to unmarshal. * * @param context The JAXBContext that will be used to create the * necessary Unmarshaller. This parameter must not be null. * @exception JAXBException if an error is encountered while creating the * JAXBResult or if the context parameter is null. */ public JAXBResult( JAXBContext context ) throws JAXBException { this( ( context == null ) ? assertionFailed() : context.createUnmarshaller() ); }
public void setJaxbElement(JAXBResult result) throws JAXBException { setJaxbElement((E)result.getResult()); }
/** * Prepare a JAXB transformation result for some given context. * @param context The JAXB context. * @return The result data structure created. * @throws Docx4JException In case of configuration errors. */ public static JAXBResult prepareJAXBResult(final JAXBContext context) throws Docx4JException { final JAXBResult result; try { final Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setEventHandler(new JaxbValidationEventHandler()); result = new JAXBResult(unmarshaller); } catch (JAXBException e) { throw new Docx4JException("Error preparing empty JAXB result", e); } return result; }
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);
static String format( String property, Object arg1, Object arg2 ) { return format( property, new Object[]{arg1,arg2} ); }
JAXBResult result = new JAXBResult(Context.jc ); java.util.HashMap<String, Object> settings = new java.util.HashMap<String, Object>(); Object ddJaxb = result.getResult(); DiagramDataPart.setFriendlyIds(XmlUtils.unwrap(ddJaxb));
public void parse() throws SAXException { // parses a content object by using the given marshaller // SAX events will be sent to the repeater, and the repeater // will further forward it to an appropriate component. try { marshaller.marshal( contentObject, (XMLFilterImpl)repeater ); } catch( JAXBException e ) { // wrap it to a SAXException SAXParseException se = new SAXParseException( e.getMessage(), null, null, -1, -1, e ); // if the consumer sets an error handler, it is our responsibility // to notify it. if(errorHandler!=null) errorHandler.fatalError(se); // this is a fatal error. Even if the error handler // returns, we will abort anyway. throw se; } } };
XmlUtils.transform(doc, mcPreprocessorXslt, null, result); jaxbElement = (E) XmlUtils.unwrap( result.getResult() ); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e);
/** * Convert any w:fldSimple in this part to complex field. * @param part * @throws Docx4JException */ public static void complexifyFields(JaxbXmlPart part) throws Docx4JException { org.w3c.dom.Document doc = XmlUtils.marshaltoW3CDomDocument( part.getJaxbElement() ); // XPathsPart xPathsPart = null; JAXBContext jc = Context.jc; try { // Use constructor which takes Unmarshaller, rather than JAXBContext, // so we can set JaxbValidationEventHandler Unmarshaller u = jc.createUnmarshaller(); u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); javax.xml.bind.util.JAXBResult result = new javax.xml.bind.util.JAXBResult(u ); org.docx4j.XmlUtils.transform(doc, xslt, null, result); part.setJaxbElement(result); } catch (Exception e) { throw new Docx4JException("Problems transforming fields", e); } }
static String format( String property, Object arg1 ) { return format( property, new Object[]{arg1} ); }
static String format( String property, Object arg1, Object arg2, Object arg3 ) { return format( property, new Object[]{arg1,arg2,arg3} ); }
/** * Hook to throw exception from the middle of a contructor chained call * to this */ private static Unmarshaller assertionFailed() throws JAXBException { throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) ); } }
/** * Creates a new instance that uses the specified * Unmarshaller to unmarshal an object. * * <p> * This JAXBResult object will use the specified Unmarshaller * instance. It is the caller's responsibility not to use the * same Unmarshaller for other purposes while it is being * used by this object. * * <p> * The primary purpose of this method is to allow the client * to configure Unmarshaller. Unless you know what you are doing, * it's easier and safer to pass a JAXBContext. * * @param _unmarshaller the unmarshaller. This parameter must not be null. * @throws JAXBException if an error is encountered while creating the * JAXBResult or the Unmarshaller parameter is null. */ public JAXBResult( Unmarshaller _unmarshaller ) throws JAXBException { if( _unmarshaller == null ) throw new JAXBException( Messages.format( Messages.RESULT_NULL_UNMARSHALLER ) ); this.unmarshallerHandler = _unmarshaller.getUnmarshallerHandler(); super.setHandler(unmarshallerHandler); }