@Override public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { if(text.length()==0) return; // there's no point in creating an empty Text node in DOM. try { State s = (State) state.getTarget(); s.handler.characters(text.toString().toCharArray(),0,text.length()); } catch( SAXException e ) { state.getContext().handleError(e); throw e; } }
public void receive(UnmarshallingContext.State state, Object o) { ((List)state.getTarget()).add(o); } }
protected Loader selectLoader(UnmarshallingContext.State state, TagName ea) throws SAXException { int idx = ea.atts.getIndex(WellKnownNamespace.XML_SCHEMA_INSTANCE,"nil"); if (idx!=-1) { Boolean b = DatatypeConverterImpl._parseBoolean(ea.atts.getValue(idx)); if (b != null && b) { onNil(state); boolean hasOtherAttributes = (ea.atts.getLength() - 1) > 0; // see issues 6759703 and 565 - need to preserve attributes even if the element is nil; only when the type is stored in JAXBElement if (!(hasOtherAttributes && (state.getPrev().getTarget() instanceof JAXBElement))) { return Discarder.INSTANCE; } } } return defaultLoader; }
/** * Starts the packing scope, without adding any item. * * This allows us to return an empty pack, thereby allowing the user * to distinguish empty array vs null array. */ public void start( Accessor<BeanT,PropT> acc, Lister<BeanT,PropT,ItemT,PackT> lister) throws SAXException{ try { if(!hasStarted()) { this.bean = (BeanT)context.getCurrentState().getTarget(); this.acc = acc; this.lister = lister; this.pack = lister.startPacking(bean,acc); } } catch (AccessorException e) { Loader.handleGenericException(e,true); // recover from this error by ignoring future items. this.lister = Lister.getErrorInstance(); this.acc = Accessor.getErrorInstance(); } } }
/** * Adds a new item to this packing scope. */ public void add( Accessor<BeanT,PropT> acc, Lister<BeanT,PropT,ItemT,PackT> lister, ItemT value) throws SAXException{ try { if(!hasStarted()) { this.bean = (BeanT)context.getCurrentState().getTarget(); this.acc = acc; this.lister = lister; this.pack = lister.startPacking(bean,acc); } lister.addToPack(pack,value); } catch (AccessorException e) { Loader.handleGenericException(e,true); // recover from this error by ignoring future items. this.lister = Lister.getErrorInstance(); this.acc = Accessor.getErrorInstance(); } }
@Override public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { // create or obtain the Map object try { BeanT target = (BeanT) state.getPrev().getTarget(); ValueT mapValue = acc.get(target); if(mapValue == null) mapValue = ClassFactory.create(mapImplClass); else mapValue.clear(); Stack.push(this.target, target); Stack.push(map, mapValue); state.setTarget(mapValue); } catch (AccessorException e) { // recover from error by setting a dummy Map that receives and discards the values handleGenericException(e,true); state.setTarget(new HashMap()); } }
public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { try { xacc.parse(state.getTarget(),text); } catch (AccessorException e) { handleGenericException(e,true); } catch (RuntimeException e) { if(state.getPrev() != null) { if (!(state.getPrev().getTarget() instanceof JAXBElement)) handleParseConversionException(state,e); // else // do nothing - issue 601 - don't report exceptions like // NumberFormatException when unmarshalling "nillable" element // (I suppose JAXBElement indicates this } else { handleParseConversionException(state,e); } } } }
/** * Fires the afterUnmarshal event if necessary. * * @param state * state of the parent object */ protected final void fireAfterUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { // fire the event callback if(beanInfo.lookForLifecycleMethods()) { UnmarshallingContext context = state.getContext(); Unmarshaller.Listener listener = context.parent.getListener(); if(beanInfo.hasAfterUnmarshalMethod()) { beanInfo.invokeAfterUnmarshalMethod(context.parent, child, state.getTarget()); } if(listener!=null) listener.afterUnmarshal(child, state.getTarget()); } }
/** * Fires the beforeUnmarshal event if necessary. * * @param state * state of the newly create child object. */ protected final void fireBeforeUnmarshal(JaxBeanInfo beanInfo, Object child, UnmarshallingContext.State state) throws SAXException { if(beanInfo.lookForLifecycleMethods()) { UnmarshallingContext context = state.getContext(); Unmarshaller.Listener listener = context.parent.getListener(); if(beanInfo.hasBeforeUnmarshalMethod()) { beanInfo.invokeBeforeUnmarshalMethod(context.parent, child, state.getPrev().getTarget()); } if(listener!=null) { listener.beforeUnmarshal(child, state.getPrev().getTarget()); } } }
public Object intercept(UnmarshallingContext.State state, Object o) throws SAXException { JAXBElement e = (JAXBElement)state.getTarget(); state.setTarget(state.getBackup()); state.setBackup(null); if (state.isNil()) { e.setNil(true); state.setNil(false); } if(o!=null) // if the value is a leaf type, it's often already set to the element // through Accessor. e.setValue(o); fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state); return e; } }
@Override public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { UnmarshallingContext context = state.getContext(); if (state.getTarget() == null) state.setTarget(new State(context)); State s = (State) state.getTarget(); try { s.declarePrefixes(context, context.getNewlyDeclaredPrefixes()); s.handler.startElement(ea.uri, ea.local, ea.getQname(), ea.atts); } catch (SAXException e) { context.handleError(e); throw e; } }
@Override public void leaveElement(UnmarshallingContext.State state, TagName ea) { state.setTarget(toArray((List)state.getTarget())); }
public void receive(UnmarshallingContext.State state, Object o) { ((Object[])state.getTarget())[index] = o; } }
@Override protected void onNil(UnmarshallingContext.State state) throws SAXException { try { acc.set(state.getPrev().getTarget(),null); state.getPrev().setNil(true); } catch (AccessorException e) { handleGenericException(e,true); } }
@Override public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { UnmarshallingContext context = state.getContext(); context.startScope(1); // inherit the target so that our children can access its target state.setTarget(state.getPrev().getTarget()); // start it now, so that even if there's no children we can still return empty collection context.getScope(0).start(acc,lister); }
public void receive(UnmarshallingContext.State state, Object o) throws SAXException { try { set((BeanT) state.getTarget(), (ValueT) o); } catch (AccessorException e) { Loader.handleGenericException(e, true); } catch (IllegalAccessError iae) { // throw UnmarshalException instead IllegalAccesssError | Issue 475 Loader.handleGenericError(iae); } }
@Override public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { state.getContext().endScope(frameSize); fireAfterUnmarshal(beanInfo, state.getTarget(), state.getPrev()); }
public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { try { xacc.parse(state.getPrev().getTarget(), text); } catch (AccessorException e) { handleGenericException(e,true); } catch (RuntimeException e) { handleParseConversionException(state,e); } } }
@Override public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { state.setLoader(this); State s = (State) state.getPrev().getTarget(); s.depth++; state.setTarget(s); }