001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.common.xmap;
023
024import java.io.IOException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.w3c.dom.Element;
029import org.w3c.dom.NodeList;
030
031public class XMLBuilder {
032
033    private static final Log log = LogFactory.getLog(XMLBuilder.class);
034
035    private XMLBuilder() {
036    }
037
038    public static String saveToXML(Object object, Element root, XAnnotatedObject xao) {
039        try {
040            toXML(object, root, xao);
041            return DOMSerializer.toString(root);
042        } catch (IOException e) {
043            log.error(e, e);
044        }
045        return null;
046    }
047
048    public static void toXML(Object o, Element parent, XAnnotatedObject xao) {
049        // XPath xpath = XPathFactory.newInstance().newXPath();
050        Element currentNode = parent;
051        String path = xao.getPath().toString();
052        if (path.length() > 0) {
053            currentNode = parent.getOwnerDocument().createElement(path);
054            parent.appendChild(currentNode);
055        }
056        // process annotated members
057        for (XAnnotatedMember m : xao.members) {
058            if (m instanceof XAnnotatedMap) {
059                m.toXML(o, currentNode);
060            } else if (m instanceof XAnnotatedList) {
061                m.toXML(o, currentNode);
062            } else if (m instanceof XAnnotatedContent) {
063                m.toXML(o, currentNode);
064            } else if (m instanceof XAnnotatedParent) {
065
066            } else {
067                m.toXML(o, currentNode);
068            }
069        }
070    }
071
072    // TODO use xpath for that ?
073    public static Element getOrCreateElement(Element root, Path path) {
074        Element e = root;
075        for (String segment : path.segments) {
076            e = getOrCreateElement(e, segment);
077        }
078        return e;
079    }
080
081    public static Element addElement(Element root, Path path) {
082        Element e = root;
083        int len = path.segments.length - 1;
084        for (int i = 0; i < len; i++) {
085            e = getOrCreateElement(e, path.segments[i]);
086        }
087        return addElement(e, path.segments[len]);
088    }
089
090    private static Element getOrCreateElement(Element parent, String segment) {
091        NodeList list = parent.getChildNodes();
092        for (int i = 0, len = list.getLength(); i < len; i++) {
093            Element e = (Element) list.item(i);
094            if (segment.equals(e.getNodeName())) {
095                return e;
096            }
097        }
098        // node not found, create one
099        return addElement(parent, segment);
100    }
101
102    public static Element addElement(Element parent, String segment) {
103        Element e = parent.getOwnerDocument().createElement(segment);
104        parent.appendChild(e);
105        return e;
106    }
107
108    public static void fillField(Element element, String value, String attribute) {
109        if (attribute != null) {
110            element.setAttribute(attribute, value);
111        } else {
112            element.setTextContent(value);
113        }
114    }
115
116}