001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.configuration.test;
029
030import java.util.function.Consumer;
031
032import javax.xml.bind.JAXBContext;
033import javax.xml.parsers.DocumentBuilder;
034import javax.xml.parsers.DocumentBuilderFactory;
035
036import org.apache.commons.digester3.NodeCreateRule;
037import org.apache.commons.digester3.Rule;
038
039import org.dom4j.Element;
040import org.dom4j.io.DOMReader;
041import org.w3c.dom.Document;
042
043/**
044 * Helper class for using JAXB to convert between beans and OpenCms config file XML elements.
045 *
046 * <p>To use this, first create a JAXB-compatible bean type with the correct annotations, then
047 * create an instance of this class as a member in the OpenCms configuration class (e.g.CmsSystemConfiguration..
048 * Add the rule returned by getDigesterRule in the config object's addXmlDigesterRules, and call
049 * the appendToXml rule in the config object's generateXml method.
050 *
051 * @param <T> the bean type to convert to/from XML
052 */
053public class CmsJaxbConfigHelper<T> {
054
055    /** Document builder factory. */
056    private static DocumentBuilderFactory DBF = DocumentBuilderFactory.newInstance();
057
058    /** The JAXB context. */
059    private JAXBContext m_jaxb;
060
061    /** Document builder used for serialization. */
062    private DocumentBuilder m_docBuilder;
063
064    /**
065     * Creates a new instance.
066     *
067     * @param cls the bean class
068     */
069    public CmsJaxbConfigHelper(Class<T> cls) {
070
071        try {
072            m_jaxb = JAXBContext.newInstance(cls);
073            m_docBuilder = DBF.newDocumentBuilder();
074        } catch (Exception e) {
075            throw new RuntimeException(e);
076        }
077    }
078
079    /**
080     * Appends the XML element for the bean to the given parent element.
081     *
082     * @param parent the parent element
083     * @param bean the bean to convert to an XML element and append to the parent
084     */
085    public void appendToXml(Element parent, T bean) {
086
087        if (bean != null) {
088            try {
089                Document doc = m_docBuilder.newDocument();
090                m_jaxb.createMarshaller().marshal(bean, doc);
091                DOMReader rd = new DOMReader();
092                Element result = rd.read(doc).getRootElement();
093                result.detach();
094                parent.add(result);
095            } catch (Exception e) {
096                throw new RuntimeException(e);
097            }
098        }
099
100    }
101
102    /**
103     * Gets a rule which uses JAXB to unmarshal a bean from the XML.
104     *
105     * @param beanCallback the callback to call with the finished bean
106     * @return the digester rule
107     */
108    public Rule getDigesterRule(Consumer<T> beanCallback) {
109
110        try {
111            return new NodeCreateRule() {
112
113                @SuppressWarnings("synthetic-access")
114                @Override
115                public void end(String namespace, String name) throws Exception {
116
117                    org.w3c.dom.Node root = (org.w3c.dom.Node)(getDigester().pop());
118                    T bean = (T)(m_jaxb.createUnmarshaller().unmarshal(root));
119                    beanCallback.accept(bean);
120                }
121            };
122        } catch (Exception e) {
123            throw new RuntimeException(e);
124        }
125
126    }
127
128}