Beispiel #1
0
  public String readAsXmlString() throws IOException, XMLStreamException {
    StringBuilder sb = new StringBuilder();
    int depth = 0;

    while (true) {
      if (XMLStreamReader.START_ELEMENT == getEventType()) {
        depth++;

        String prefix = getPrefix();

        sb.append("<");

        if (!"".equals(prefix)) {
          sb.append(prefix);
          sb.append(":");
        }

        sb.append(getLocalName());

        if (getNamespaceURI() != null) {
          if ("".equals(prefix)) sb.append(" xmlns");
          else sb.append(" xmlns:").append(prefix);

          sb.append("=\"");
          sb.append(getNamespaceURI()).append("\"");
        }

        for (int i = 0; i < getAttributeCount(); i++) {
          sb.append(" ");
          sb.append(getAttributeLocalName(i));
          sb.append("=\"");
          sb.append(getAttributeValue(i));
          sb.append("\"");
        }
        sb.append(">");

        log.finest(this + " " + sb);
      } else if (XMLStreamReader.END_ELEMENT == getEventType()) {
        depth--;

        sb.append("</");

        String prefix = getPrefix();
        if (!"".equals(prefix)) sb.append(prefix).append(":");

        sb.append(getLocalName());
        sb.append(">");

        if (depth == 0) return sb.toString();
      } else if (XMLStreamReader.CHARACTERS == getEventType()) {
        sb.append(getText());
      } else {
        log.finer(this + " tag=" + getEventType());

        return sb.toString();
      }

      if (next() < 0) {
        log.finer(this + " unexpected end of file");

        return sb.toString();
      }
    }
  }
Beispiel #2
0
/** Marshals from an xmpp request to and from a serialized class */
public class XmppStreamReaderImpl extends XMLStreamReaderImpl implements XmppStreamReader {
  private static final L10N L = new L10N(XmppStreamReaderImpl.class);
  private static final Logger log = Logger.getLogger(XmppStreamReaderImpl.class.getName());

  private XmppMarshalFactory _marshalFactory;

  XmppStreamReaderImpl(ReadStream is, XmppMarshalFactory factory) throws XMLStreamException {
    super(is);

    _marshalFactory = factory;
  }

  public Serializable readValue() throws IOException, XMLStreamException {
    QName name = getName();

    Serializable query = null;

    XmppMarshal marshal = _marshalFactory.getUnserialize(name);

    if (marshal != null) return marshal.fromXml(this);
    else return readAsXmlString();
  }

  public String readAsXmlString() throws IOException, XMLStreamException {
    StringBuilder sb = new StringBuilder();
    int depth = 0;

    while (true) {
      if (XMLStreamReader.START_ELEMENT == getEventType()) {
        depth++;

        String prefix = getPrefix();

        sb.append("<");

        if (!"".equals(prefix)) {
          sb.append(prefix);
          sb.append(":");
        }

        sb.append(getLocalName());

        if (getNamespaceURI() != null) {
          if ("".equals(prefix)) sb.append(" xmlns");
          else sb.append(" xmlns:").append(prefix);

          sb.append("=\"");
          sb.append(getNamespaceURI()).append("\"");
        }

        for (int i = 0; i < getAttributeCount(); i++) {
          sb.append(" ");
          sb.append(getAttributeLocalName(i));
          sb.append("=\"");
          sb.append(getAttributeValue(i));
          sb.append("\"");
        }
        sb.append(">");

        log.finest(this + " " + sb);
      } else if (XMLStreamReader.END_ELEMENT == getEventType()) {
        depth--;

        sb.append("</");

        String prefix = getPrefix();
        if (!"".equals(prefix)) sb.append(prefix).append(":");

        sb.append(getLocalName());
        sb.append(">");

        if (depth == 0) return sb.toString();
      } else if (XMLStreamReader.CHARACTERS == getEventType()) {
        sb.append(getText());
      } else {
        log.finer(this + " tag=" + getEventType());

        return sb.toString();
      }

      if (next() < 0) {
        log.finer(this + " unexpected end of file");

        return sb.toString();
      }
    }
  }
}