public Object unmarshal0(XMLStreamReader reader, JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
      throw new IllegalArgumentException(Messages.format(Messages.NULL_READER));
    }

    int eventType = reader.getEventType();
    if (eventType != XMLStreamConstants.START_ELEMENT
        && eventType != XMLStreamConstants.START_DOCUMENT) {
      // TODO: convert eventType into event name
      throw new IllegalStateException(Messages.format(Messages.ILLEGAL_READER_STATE, eventType));
    }

    XmlVisitor h = createUnmarshallerHandler(null, false, expectedType);
    StAXConnector connector = StAXStreamConnector.create(reader, h);

    try {
      connector.bridge();
    } catch (XMLStreamException e) {
      throw handleStreamException(e);
    }

    Object retVal = h.getContext().getResult();
    h.getContext().clearResult();
    return retVal;
  }
Example #2
0
  private Object unmarshal0(XMLEventReader reader, JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
      throw new IllegalArgumentException(Messages.format(Messages.NULL_READER));
    }

    try {
      XMLEvent event = reader.peek();

      if (!event.isStartElement() && !event.isStartDocument()) {
        // TODO: convert event into event name
        throw new IllegalStateException(
            Messages.format(Messages.ILLEGAL_READER_STATE, event.getEventType()));
      }

      // Quick hack until SJSXP fixes 6270116
      boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.stream.XMLReaderImpl");
      XmlVisitor h = createUnmarshallerHandler(null, false, expectedType);
      if (!isZephyr) {
        h = new InterningXmlVisitor(h);
      }
      new StAXEventConnector(reader, h).bridge();
      return h.getContext().getResult();
    } catch (XMLStreamException e) {
      throw handleStreamException(e);
    }
  }
 protected final void unexpectedLeaveChild() throws SAXException {
   // I believe this is really a bug of the compiler,
   // since when an object spawns a child object, it must be "prepared"
   // to receive this event.
   dump();
   throw new JAXBAssertionError(Messages.format(Messages.UNEXPECTED_LEAVE_CHILD));
 }
  public void startElement(String uri, String local, String qname, Attributes atts)
      throws SAXException {

    // work gracefully with misconfigured parsers that don't support namespaces
    if (uri == null) uri = "";
    if (local == null || local.length() == 0) local = qname;
    if (qname == null || qname.length() == 0) qname = local;

    if (result == null) {
      // this is the root element.
      // create a root object and start unmarshalling
      UnmarshallingEventHandler unmarshaller = grammarInfo.createUnmarshaller(uri, local, this);
      if (unmarshaller == null) {
        // the registry doesn't know about this element.
        //
        // the no.1 cause of this problem is that your application is configuring
        // an XML parser by your self and you forgot to call
        // the SAXParserFactory.setNamespaceAware(true). When this happens, you see
        // the namespace URI is reported as empty whereas you expect something else.
        throw new SAXParseException(
            Messages.format(
                Messages.UNEXPECTED_ROOT_ELEMENT2, uri, local, computeExpectedRootElements()),
            getLocator());
      }
      result = unmarshaller.owner();

      pushContentHandler(unmarshaller, 0);
    }

    processText(true);

    getCurrentHandler().enterElement(uri, local, qname, atts);
  }
 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);
 }
  protected final void unexpectedText(String str) throws SAXException {
    // make str printable
    str = str.replace('\r', ' ').replace('\n', ' ').replace('\t', ' ').trim();

    reportError(Messages.format(Messages.UNEXPECTED_TEXT, str), true);
  }
 protected final void unexpectedLeaveAttribute(String uri, String local, String qname)
     throws SAXException {
   reportError(Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local), false);
 }
 protected final void unexpectedLeaveElement(String uri, String local, String qname)
     throws SAXException {
   reportError(Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local), false);
 }
 /** Called when a RuntimeException is thrown during unmarshalling a text. */
 protected void handleUnexpectedTextException(String text, RuntimeException e)
     throws SAXException {
   // report this as an error
   reportError(Messages.format(Messages.UNEXPECTED_TEXT, text), e, true);
 }