public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { State s = (State) state.target; UnmarshallingContext context = state.getContext(); try { s.handler.endElement(ea.uri, ea.local, ea.getQname()); s.undeclarePrefixes(context.getNewlyDeclaredPrefixes()); } catch (SAXException e) { context.handleError(e); throw e; } if ((--s.depth) == 0) { // emulate the end of the document try { s.undeclarePrefixes(context.getAllDeclaredPrefixes()); s.handler.endDocument(); } catch (SAXException e) { context.handleError(e); throw e; } // we are done state.target = s.getElement(); } }
protected final void unexpectedEnterElement( String uri, String local, String qname, Attributes atts) throws SAXException { // notify the error reportError(Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri, local), true); // then recover by ignoring the whole element. context.pushContentHandler(new Discarder(context), state); context.getCurrentHandler().enterElement(uri, local, qname, atts); }
@Override public <A extends XmlAdapter> A getAdapter(Class<A> type) { if (type == null) throw new IllegalArgumentException(); if (coordinator.containsAdapter(type)) // so as not to create a new instance when this method is called return coordinator.getAdapter(type); else return null; }
private void reportError(String msg, Exception nested, boolean canRecover) throws SAXException { context.handleEvent( new ValidationEventImpl( canRecover ? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, msg, new ValidationEventLocatorImpl(context.getLocator()), nested), canRecover); }
@Override public <A extends XmlAdapter> A getAdapter(Class<A> type) { if (type == null) { throw new IllegalArgumentException(); } if (coordinator.containsAdapter(type)) { return coordinator.getAdapter(type); } else { return null; } }
public static void reportError(String msg, Exception nested, boolean canRecover) throws SAXException { UnmarshallingContext context = UnmarshallingContext.getInstance(); context.handleEvent( new ValidationEventImpl( canRecover ? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, msg, context.getLocator().getLocation(), nested), canRecover); }
public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { UnmarshallingContext context = state.getContext(); if (state.target == null) state.target = new State(context); State s = (State) state.target; 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; } }
/** * This method is called by the generated derived class when a datatype parse method throws an * exception. */ protected void handleParseConversionException(Exception e) throws SAXException { if (e instanceof RuntimeException) throw (RuntimeException) e; // don't catch the runtime exception. just let it go. // wrap it into a ParseConversionEvent and report it ParseConversionEvent pce = new ParseConversionEventImpl( ValidationEvent.ERROR, e.getMessage(), new ValidationEventLocatorImpl(context.getLocator()), e); context.handleEvent(pce, true); }
public State(UnmarshallingContext context) throws SAXException { result = dom.createUnmarshaller(context); handler.setResult(result); // emulate the start of documents try { handler.setDocumentLocator(context.getLocator()); handler.startDocument(); declarePrefixes(context, context.getAllDeclaredPrefixes()); } catch (SAXException e) { context.handleError(e); throw e; } }
protected final void spawnHandlerFromLeaveAttribute( UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) throws SAXException { context.pushContentHandler(unm, memento); unm.leaveAttribute(uri, local, qname); }
@Override public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) { if (type == null) { throw new IllegalArgumentException(); } coordinator.putAdapter(type, adapter); }
protected final Element spawnWildcard( int memento, String uri, String local, String qname, Attributes atts) throws SAXException { UnmarshallingEventHandler ueh = context.getGrammarInfo().createUnmarshaller(uri, local, context); if (ueh != null) { context.pushContentHandler(ueh, memento); ueh.enterElement(uri, local, qname, atts); return (Element) ueh.owner(); } else { // if no class is available to unmarshal this element, discard // the sub-tree by feeding events to discarder. context.pushContentHandler(new Discarder(context), memento); context.getCurrentHandler().enterElement(uri, local, qname, atts); return null; // return null so that the discarder will be ignored } }
private void declarePrefixes(UnmarshallingContext context, String[] prefixes) throws SAXException { for (int i = prefixes.length - 1; i >= 0; i--) { String nsUri = context.getNamespaceURI(prefixes[i]); if (nsUri == null) throw new IllegalStateException("prefix \'" + prefixes[i] + "\' isn't bound"); handler.startPrefixMapping(prefixes[i], nsUri); } }
@Override public void setProperty(String name, Object value) throws PropertyException { if (name.equals(FACTORY)) { coordinator.setFactories(value); return; } if (name.equals(IDResolver.class.getName())) { idResolver = (IDResolver) value; return; } if (name.equals(ClassResolver.class.getName())) { coordinator.classResolver = (ClassResolver) value; return; } if (name.equals(ClassLoader.class.getName())) { coordinator.classLoader = (ClassLoader) value; return; } super.setProperty(name, value); }
protected final void spawnHandlerFromEnterElement( UnmarshallingEventHandler unm, int memento, String uri, String local, String qname, Attributes atts) throws SAXException { context.pushContentHandler(unm, memento); unm.enterElement(uri, local, qname, atts); }
/* */ static JaxBeanInfo parseXsiType( UnmarshallingContext.State state, TagName ea, @Nullable JaxBeanInfo defaultBeanInfo) throws SAXException { /* 69 */ UnmarshallingContext context = state.getContext(); /* 70 */ JaxBeanInfo beanInfo = null; /* */ /* 73 */ Attributes atts = ea.atts; /* 74 */ int idx = atts.getIndex("http://www.w3.org/2001/XMLSchema-instance", "type"); /* */ /* 76 */ if (idx >= 0) /* */ { /* 79 */ String value = atts.getValue(idx); /* */ /* 81 */ QName type = DatatypeConverterImpl._parseQName(value, context); /* 82 */ if (type == null) { /* 83 */ reportError(Messages.NOT_A_QNAME.format(new Object[] {value}), true); /* */ } else { /* 85 */ if ((defaultBeanInfo != null) && (defaultBeanInfo.getTypeNames().contains(type))) /* */ { /* 92 */ return defaultBeanInfo; /* */ } /* 94 */ beanInfo = context.getJAXBContext().getGlobalType(type); /* 95 */ if (beanInfo == null) { /* 96 */ String nearest = context.getJAXBContext().getNearestTypeName(type); /* 97 */ if (nearest != null) /* 98 */ reportError( Messages.UNRECOGNIZED_TYPE_NAME_MAYBE.format(new Object[] {type, nearest}), true); /* */ else { /* 100 */ reportError( Messages.UNRECOGNIZED_TYPE_NAME.format(new Object[] {type}), true); /* */ } /* */ /* */ } /* */ /* */ } /* */ /* */ } /* */ /* 112 */ return beanInfo; /* */ }
private Object unmarshal0(XMLReader reader, InputSource source, JaxBeanInfo expectedType) throws JAXBException { SAXConnector connector = getUnmarshallerHandler(needsInterning(reader), expectedType); reader.setContentHandler(connector); // saxErrorHandler will be set by the getUnmarshallerHandler method. // configure XMLReader so that the error will be sent to it. // This is essential for the UnmarshallerHandler to be able to abort // unmarshalling when an error is found. // // Note that when this XMLReader is provided by the client code, // it might be already configured to call a client error handler. // This will clobber such handler, if any. // // Ryan noted that we might want to report errors to such a client // error handler as well. reader.setErrorHandler(coordinator); try { reader.parse(source); } catch (IOException e) { coordinator.clearStates(); throw new UnmarshalException(e); } catch (SAXException e) { coordinator.clearStates(); throw createUnmarshalException(e); } Object result = connector.getResult(); // avoid keeping unnecessary references too long to let the GC // reclaim more memory. // setting null upsets some parsers, so use a dummy instance instead. reader.setContentHandler(dummyHandler); reader.setErrorHandler(dummyHandler); return result; }
/** * Creates and configures a new unmarshalling pipe line. Depending on the setting, we put a * validator as a filter. * * @return A component that implements both {@link UnmarshallerHandler} and {@link * ValidationEventHandler}. All the parsing errors should be reported to this error handler * for the unmarshalling process to work correctly. * <p>Also, returned handler expects all the XML names to be interned. */ public final XmlVisitor createUnmarshallerHandler( InfosetScanner scanner, boolean inplace, JaxBeanInfo expectedType) { coordinator.reset(scanner, inplace, expectedType, idResolver); XmlVisitor unmarshaller = coordinator; // delegate to JAXP 1.3 for validation if the client provided a schema if (schema != null) unmarshaller = new ValidatingUnmarshaller(schema, unmarshaller); // if(attachmentUnmarshaller!=null && attachmentUnmarshaller.isXOPPackage()) // unmarshaller = new MTOMDecorator(this,unmarshaller,attachmentUnmarshaller); return unmarshaller; }
// // // spawn a new child object // // private UnmarshallingEventHandler spawnChild(Class clazz, int memento) { UnmarshallableObject child; try { child = (UnmarshallableObject) clazz.newInstance(); } catch (InstantiationException e) { throw new InstantiationError(e.getMessage()); } catch (IllegalAccessException e) { throw new IllegalAccessError(e.getMessage()); } UnmarshallingEventHandler handler = child.createUnmarshaller(context); context.pushContentHandler(handler, memento); return handler; }
@SuppressWarnings({"StringEquality"}) protected final void reportUnexpectedChildElement(TagName ea, boolean canRecover) throws SAXException { if (canRecover && !UnmarshallingContext.getInstance().parent.hasEventHandler()) // this error happens particurly often (when input documents contain a lot of unexpected // elements to be ignored), // so don't bother computing all the messages and etc if we know that // there's no event handler to receive the error in the end. See #286 return; if (ea.uri != ea.uri.intern() || ea.local != ea.local.intern()) reportError(Messages.UNINTERNED_STRINGS.format(), canRecover); else reportError( Messages.UNEXPECTED_ELEMENT.format(ea.uri, ea.local, computeExpectedElements()), canRecover); }
public final void text(String text) throws SAXException { if (isListState()) { // in list state, we don't need to care about whitespaces. // if the text is all whitespace, this won't generate a text event, // so it would be just fine. StringTokenizer tokens = new StringTokenizer(text); if (tokens.countTokens() == 1) { handleText(text); } else { while (tokens.hasMoreTokens()) // the handler can be switched during the text processing, // so the current handler has to be obtained inside the loop context.getCurrentHandler().text(tokens.nextToken()); } } else { // otherwise process this token handleText(text); } }
protected final void revertToParentFromText(String value) throws SAXException { context.popContentHandler(); context.getCurrentHandler().text(value); }
protected final void revertToParentFromLeaveAttribute(String uri, String local, String qname) throws SAXException { context.popContentHandler(); context.getCurrentHandler().leaveAttribute(uri, local, qname); }
// // // revert to parent // // protected final void revertToParentFromEnterElement( String uri, String local, String qname, Attributes atts) throws SAXException { context.popContentHandler(); context.getCurrentHandler().enterElement(uri, local, qname, atts); }
protected final void spawnHandlerFromText(UnmarshallingEventHandler unm, int memento, String text) throws SAXException { context.pushContentHandler(unm, memento); unm.text(text); }
public void leaveElement(String uri, String local, String qname) throws SAXException { depth--; if (depth == 0) context.popContentHandler(); }
public Object getResult() throws JAXBException, IllegalStateException { return context.getResult(); }