Search in sources :

Example 1 with XmlFormat

use of org.apache.juneau.xml.annotation.XmlFormat in project incubator-juneau by apache.

the class XmlParser method parseIntoBean.

private <T> BeanMap<T> parseIntoBean(XmlParserSession session, XMLStreamReader r, BeanMap<T> m) throws Exception {
    BeanMeta<?> bMeta = m.getMeta();
    XmlBeanMeta xmlMeta = bMeta.getExtendedMeta(XmlBeanMeta.class);
    for (int i = 0; i < r.getAttributeCount(); i++) {
        String key = session.getAttributeName(r, i);
        String val = r.getAttributeValue(i);
        BeanPropertyMeta bpm = xmlMeta.getPropertyMeta(key);
        if (bpm == null) {
            if (xmlMeta.getAttrsProperty() != null) {
                xmlMeta.getAttrsProperty().add(m, key, key, val);
            } else {
                Location l = r.getLocation();
                session.onUnknownProperty(key, m, l.getLineNumber(), l.getColumnNumber());
            }
        } else {
            bpm.set(m, key, val);
        }
    }
    BeanPropertyMeta cp = xmlMeta.getContentProperty();
    XmlFormat cpf = xmlMeta.getContentFormat();
    boolean trim = cp == null || !cpf.isOneOf(MIXED_PWS, TEXT_PWS);
    ClassMeta<?> cpcm = (cp == null ? session.object() : cp.getClassMeta());
    StringBuilder sb = null;
    BeanRegistry breg = cp == null ? null : cp.getBeanRegistry();
    LinkedList<Object> l = null;
    int depth = 0;
    do {
        int event = r.next();
        String currAttr;
        // Ignore if in ELEMENTS mode.
        if (event == CHARACTERS) {
            if (cp != null && cpf.isOneOf(MIXED, MIXED_PWS)) {
                if (cpcm.isCollectionOrArray()) {
                    if (l == null)
                        l = new LinkedList<Object>();
                    l.add(session.getText(r, false));
                } else {
                    cp.set(m, null, session.getText(r, trim));
                }
            } else if (cpf != ELEMENTS) {
                String s = session.getText(r, trim);
                if (s != null) {
                    if (sb == null)
                        sb = session.getStringBuilder();
                    sb.append(s);
                }
            } else {
            // Do nothing...we're in ELEMENTS mode.
            }
        } else if (event == START_ELEMENT) {
            if (cp != null && cpf.isOneOf(TEXT, TEXT_PWS)) {
                String s = session.parseText(r);
                if (s != null) {
                    if (sb == null)
                        sb = session.getStringBuilder();
                    sb.append(s);
                }
                depth--;
            } else if (cpf == XMLTEXT) {
                if (sb == null)
                    sb = session.getStringBuilder();
                sb.append(session.getElementAsString(r));
                depth++;
            } else if (cp != null && cpf.isOneOf(MIXED, MIXED_PWS)) {
                if (session.isWhitespaceElement(r) && (breg == null || !breg.hasName(r.getLocalName()))) {
                    if (cpcm.isCollectionOrArray()) {
                        if (l == null)
                            l = new LinkedList<Object>();
                        l.add(session.parseWhitespaceElement(r));
                    } else {
                        cp.set(m, null, session.parseWhitespaceElement(r));
                    }
                } else {
                    if (cpcm.isCollectionOrArray()) {
                        if (l == null)
                            l = new LinkedList<Object>();
                        l.add(parseAnything(session, cpcm.getElementType(), cp.getName(), r, m.getBean(false), false, cp));
                    } else {
                        cp.set(m, null, parseAnything(session, cpcm, cp.getName(), r, m.getBean(false), false, cp));
                    }
                }
            } else if (cp != null && cpf == ELEMENTS) {
                cp.add(m, null, parseAnything(session, cpcm.getElementType(), cp.getName(), r, m.getBean(false), false, cp));
            } else {
                currAttr = session.getElementName(r);
                BeanPropertyMeta pMeta = xmlMeta.getPropertyMeta(currAttr);
                if (pMeta == null) {
                    Location loc = r.getLocation();
                    session.onUnknownProperty(currAttr, m, loc.getLineNumber(), loc.getColumnNumber());
                    skipCurrentTag(r);
                } else {
                    session.setCurrentProperty(pMeta);
                    XmlFormat xf = pMeta.getExtendedMeta(XmlBeanPropertyMeta.class).getXmlFormat();
                    if (xf == COLLAPSED) {
                        ClassMeta<?> et = pMeta.getClassMeta().getElementType();
                        Object value = parseAnything(session, et, currAttr, r, m.getBean(false), false, pMeta);
                        setName(et, value, currAttr);
                        pMeta.add(m, currAttr, value);
                    } else if (xf == ATTR) {
                        pMeta.set(m, currAttr, session.getAttributeValue(r, 0));
                        r.nextTag();
                    } else {
                        ClassMeta<?> cm = pMeta.getClassMeta();
                        Object value = parseAnything(session, cm, currAttr, r, m.getBean(false), false, pMeta);
                        setName(cm, value, currAttr);
                        pMeta.set(m, currAttr, value);
                    }
                    session.setCurrentProperty(null);
                }
            }
        } else if (event == END_ELEMENT) {
            if (depth > 0) {
                if (cpf == XMLTEXT) {
                    if (sb == null)
                        sb = session.getStringBuilder();
                    sb.append(session.getElementAsString(r));
                } else
                    throw new ParseException("End element found where one was not expected.  {0}", XmlUtils.toReadableEvent(r));
            }
            depth--;
        } else {
            throw new ParseException("Unexpected event type: {0}", XmlUtils.toReadableEvent(r));
        }
    } while (depth >= 0);
    if (sb != null && cp != null)
        cp.set(m, null, sb.toString());
    else if (l != null && cp != null)
        cp.set(m, null, XmlUtils.collapseTextNodes(l));
    session.returnStringBuilder(sb);
    return m;
}
Also used : XmlFormat(org.apache.juneau.xml.annotation.XmlFormat) Location(javax.xml.stream.Location)

Example 2 with XmlFormat

use of org.apache.juneau.xml.annotation.XmlFormat in project incubator-juneau by apache.

the class XmlSerializer method serializeBeanMap.

private ContentResult serializeBeanMap(XmlSerializerSession session, XmlWriter out, BeanMap<?> m, Namespace elementNs, boolean isCollapsed, boolean isMixed) throws Exception {
    boolean hasChildren = false;
    BeanMeta<?> bm = m.getMeta();
    List<BeanPropertyValue> lp = m.getValues(session.isTrimNulls());
    XmlBeanMeta xbm = bm.getExtendedMeta(XmlBeanMeta.class);
    Set<String> attrs = xbm.getAttrPropertyNames(), elements = xbm.getElementPropertyNames(), collapsedElements = xbm.getCollapsedPropertyNames();
    String attrsProperty = xbm.getAttrsPropertyName(), contentProperty = xbm.getContentPropertyName();
    XmlFormat cf = null;
    Object content = null;
    ClassMeta<?> contentType = null;
    for (BeanPropertyValue p : lp) {
        String n = p.getName();
        if (attrs.contains(n) || attrs.contains("*") || n.equals(attrsProperty)) {
            BeanPropertyMeta pMeta = p.getMeta();
            ClassMeta<?> cMeta = p.getClassMeta();
            String key = p.getName();
            Object value = p.getValue();
            Throwable t = p.getThrown();
            if (t != null)
                session.onBeanGetterException(pMeta, t);
            if (session.canIgnoreValue(cMeta, key, value))
                continue;
            Namespace ns = (session.isEnableNamespaces() && pMeta.getExtendedMeta(XmlBeanPropertyMeta.class).getNamespace() != elementNs ? pMeta.getExtendedMeta(XmlBeanPropertyMeta.class).getNamespace() : null);
            if (pMeta.isUri()) {
                out.attrUri(ns, key, value);
            } else if (n.equals(attrsProperty)) {
                if (value instanceof BeanMap) {
                    BeanMap<?> bm2 = (BeanMap) value;
                    for (BeanPropertyValue p2 : bm2.getValues(true)) {
                        String key2 = p2.getName();
                        Object value2 = p2.getValue();
                        Throwable t2 = p2.getThrown();
                        if (t2 != null)
                            session.onBeanGetterException(pMeta, t);
                        out.attr(ns, key2, value2);
                    }
                } else /* Map */
                {
                    Map m2 = (Map) value;
                    for (Map.Entry e : (Set<Map.Entry>) (m2.entrySet())) {
                        out.attr(ns, session.toString(e.getKey()), e.getValue());
                    }
                }
            } else {
                out.attr(ns, key, value);
            }
        }
    }
    boolean hasContent = false, preserveWhitespace = false, isVoidElement = xbm.getContentFormat() == VOID;
    for (BeanPropertyValue p : lp) {
        BeanPropertyMeta pMeta = p.getMeta();
        ClassMeta<?> cMeta = p.getClassMeta();
        String n = p.getName();
        if (n.equals(contentProperty)) {
            content = p.getValue();
            contentType = p.getClassMeta();
            hasContent = true;
            cf = xbm.getContentFormat();
            if (cf.isOneOf(MIXED, MIXED_PWS, TEXT, TEXT_PWS, XMLTEXT))
                isMixed = true;
            if (cf.isOneOf(MIXED_PWS, TEXT_PWS))
                preserveWhitespace = true;
            if (contentType.isCollection() && ((Collection) content).isEmpty())
                hasContent = false;
            else if (contentType.isArray() && Array.getLength(content) == 0)
                hasContent = false;
        } else if (elements.contains(n) || collapsedElements.contains(n) || elements.contains("*") || collapsedElements.contains("*")) {
            String key = p.getName();
            Object value = p.getValue();
            Throwable t = p.getThrown();
            if (t != null)
                session.onBeanGetterException(pMeta, t);
            if (session.canIgnoreValue(cMeta, key, value))
                continue;
            if (!hasChildren) {
                hasChildren = true;
                out.appendIf(!isCollapsed, '>').nlIf(!isMixed);
            }
            XmlBeanPropertyMeta xbpm = pMeta.getExtendedMeta(XmlBeanPropertyMeta.class);
            serializeAnything(session, out, value, cMeta, key, xbpm.getNamespace(), false, xbpm.getXmlFormat(), isMixed, false, pMeta);
        }
    }
    if (!hasContent)
        return (hasChildren ? CR_ELEMENTS : isVoidElement ? CR_VOID : CR_EMPTY);
    out.append('>').nlIf(!isMixed);
    // Serialize XML content.
    if (content != null) {
        if (contentType == null) {
        } else if (contentType.isCollection()) {
            Collection c = (Collection) content;
            for (Iterator i = c.iterator(); i.hasNext(); ) {
                Object value = i.next();
                serializeAnything(session, out, value, contentType.getElementType(), null, null, false, cf, isMixed, preserveWhitespace, null);
            }
        } else if (contentType.isArray()) {
            Collection c = toList(Object[].class, content);
            for (Iterator i = c.iterator(); i.hasNext(); ) {
                Object value = i.next();
                serializeAnything(session, out, value, contentType.getElementType(), null, null, false, cf, isMixed, preserveWhitespace, null);
            }
        } else {
            serializeAnything(session, out, content, contentType, null, null, false, cf, isMixed, preserveWhitespace, null);
        }
    } else {
        if (!session.isTrimNulls()) {
            if (!isMixed)
                out.i(session.indent);
            out.text(content);
            if (!isMixed)
                out.nl();
        }
    }
    return isMixed ? CR_MIXED : CR_ELEMENTS;
}
Also used : XmlFormat(org.apache.juneau.xml.annotation.XmlFormat)

Aggregations

XmlFormat (org.apache.juneau.xml.annotation.XmlFormat)2 Location (javax.xml.stream.Location)1