All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alipay.sofa.common.xmap.XAnnotatedMap Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alipay.sofa.common.xmap;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alipay.sofa.boot.log.SofaBootLoggerFactory;
import org.slf4j.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.alipay.sofa.common.xmap.annotation.XNodeMap;

/**
 *
 * @author Bogdan Stefanescu
 * @author [email protected]
 * @since 2.6.0
 */
public class XAnnotatedMap extends XAnnotatedList {

    protected static final ElementMapVisitor        elementMapVisitor = new ElementMapVisitor();
    protected static final ElementValueMapVisitor   elementVisitor    = new ElementValueMapVisitor();
    protected static final AttributeValueMapVisitor attributeVisitor  = new AttributeValueMapVisitor();

    protected Path                                  key;

    public XAnnotatedMap(XMap xmap, XSetter setter, XGetter getter, XNodeMap anno) {
        super(xmap, setter, getter);
        if (anno != null) {
            path = new Path(anno.value());
            trim = anno.trim();
            key = new Path(anno.key());
            type = anno.type();
            cdata = anno.cdata();
            componentType = anno.componentType();
            valueFactory = xmap.getValueFactory(componentType);
            xao = xmap.register(componentType);
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    protected Object getValue(Context ctx, Element base) throws IllegalAccessException,
                                                        InstantiationException {
        Map values = (Map) type.newInstance();
        if (xao != null) {
            DOMHelper.visitMapNodes(ctx, this, base, path, elementMapVisitor, values);
        } else {
            if (path.attribute != null) {
                // attribute list
                DOMHelper.visitMapNodes(ctx, this, base, path, attributeVisitor, values);
            } else {
                // element list
                DOMHelper.visitMapNodes(ctx, this, base, path, elementVisitor, values);
            }
        }

        return values;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void decode(Object instance, Node base, Document document, List filters)
                                                                                           throws Exception {
        if (!isFilter(filters)) {
            return;
        }

        Map values = (Map) getter.getValue(instance);

        Node node = base;
        int len = path.segments.length - 1;
        for (int i = 0; i < len; i++) {
            Node n = DOMHelper.getElementNode(node, path.segments[i]);
            if (n == null) {
                Element element = document.createElement(path.segments[i]);
                node = node.appendChild(element);
            } else {
                node = n;
            }
        }

        String name = path.segments[len];

        Node lastParentNode = node;

        Set entrys = values.entrySet();
        for (Map.Entry entry : entrys) {

            Element element = document.createElement(name);
            node = lastParentNode.appendChild(element);

            Object keyObj = entry.getKey();
            String keyValue = keyObj == null ? "" : keyObj.toString();

            Object object = entry.getValue();

            Attr attrKey = document.createAttribute(key.attribute);
            attrKey.setNodeValue(keyValue);
            ((Element) node).setAttributeNode(attrKey);

            if (xao != null) {
                xao.decode(object, node, document, filters);
            } else {
                String value = object == null ? "" : object.toString();

                if (path.attribute != null && path.attribute.length() > 0) {
                    Attr attrValue = document.createAttribute(path.attribute);
                    attrValue.setNodeValue(value);

                    ((Element) node).setAttributeNode(attrValue);
                } else {
                    if (cdata) {
                        CDATASection cdataSection = document.createCDATASection(value);
                        node.appendChild(cdataSection);
                    } else {
                        node.setTextContent(value);
                    }
                }
            }
        }
    }

}

/**
 * ElementMapNodeVisitor
 *
 * @author [email protected]
 * @since 2.6.0
 */
class ElementMapVisitor extends DOMHelper.NodeMapVisitor {

    private static final Logger LOGGER = SofaBootLoggerFactory.getLogger(ElementMapVisitor.class);

    @Override
    public void visitNode(Context ctx, XAnnotatedMember xam, Node node, String key,
                          Map result) {
        try {
            result.put(key, xam.xao.newInstance(ctx, (Element) node));
        } catch (Exception e) {
            LOGGER.error("visitNode error", e);
        }
    }
}

/**
 * ElementValueMapNodeVisitor
 *
 * @author [email protected]
 * @since 2.6.0
 */
class ElementValueMapVisitor extends DOMHelper.NodeMapVisitor {

    @Override
    public void visitNode(Context ctx, XAnnotatedMember xam, Node node, String key,
                          Map result) {
        String val = node.getTextContent();
        if (xam.trim) {
            val = val.trim();
        }
        if (xam.valueFactory != null) {
            result.put(key, xam.valueFactory.getValue(ctx, val));
        } else {
            // TODO: log warning?
            result.put(key, val);
        }
    }
}

/**
 * AttributeValueMapNodeVisitor
 *
 * @author [email protected]
 * @since 2.6.0
 */
class AttributeValueMapVisitor extends DOMHelper.NodeMapVisitor {

    @Override
    public void visitNode(Context ctx, XAnnotatedMember xam, Node node, String key,
                          Map result) {
        String val = node.getNodeValue();
        if (xam.valueFactory != null) {
            result.put(key, xam.valueFactory.getValue(ctx, val));
        } else {
            // TODO: log warning?
            result.put(key, val);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy