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 GmbH & Co. KG, 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.setup.comptest;
029
030import org.opencms.i18n.CmsEncoder;
031import org.opencms.setup.CmsSetupBean;
032
033import java.io.ByteArrayInputStream;
034
035import org.apache.xerces.impl.Version;
036import org.apache.xerces.parsers.DOMParser;
037
038import org.w3c.dom.Document;
039import org.xml.sax.InputSource;
040
041/**
042 * Test for the Xerces version.<p>
043 *
044 * @since 6.1.8
045 */
046public class CmsSetupTestXercesVersion implements I_CmsSetupTest {
047
048    /** The test name. */
049    public static final String TEST_NAME = "Xerces Version";
050
051    /**
052     * @see org.opencms.setup.comptest.I_CmsSetupTest#getName()
053     */
054    public String getName() {
055
056        return TEST_NAME;
057    }
058
059    /**
060     * @see org.opencms.setup.comptest.I_CmsSetupTest#execute(org.opencms.setup.CmsSetupBean)
061     */
062    public CmsSetupTestResult execute(CmsSetupBean setupBean) throws Exception {
063
064        CmsSetupTestResult testResult = new CmsSetupTestResult(this);
065
066        DOMParser parser = new DOMParser();
067        String document = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test>test</test>\n";
068        parser.parse(new InputSource(new ByteArrayInputStream(document.getBytes(CmsEncoder.ENCODING_UTF_8))));
069        Document doc = parser.getDocument();
070
071        // Xerces 1 and 2 APIs are different, let's see what we have...
072        String versionStr = null;
073        int xercesVersion = 0;
074
075        try {
076            doc.getClass().getMethod("getXmlEncoding", new Class[] {}).invoke(doc, new Object[] {});
077            versionStr = Version.getVersion();
078            xercesVersion = 2;
079        } catch (Throwable t) {
080            // noop
081        }
082        if (versionStr == null) {
083            try {
084                doc.getClass().getMethod("getEncoding", new Class[] {}).invoke(doc, new Object[] {});
085                versionStr = "Xerces version 1";
086                xercesVersion = 1;
087            } catch (Throwable t) {
088                // noop
089            }
090        }
091
092        switch (xercesVersion) {
093            case 2:
094                testResult.setResult(versionStr);
095                testResult.setHelp(
096                    "OpenCms requires Xerces version 2 to run. Usually this should be available as part of the servlet environment.");
097                testResult.setGreen();
098                break;
099            case 1:
100                testResult.setResult(versionStr);
101                testResult.setRed();
102                testResult.setInfo(
103                    "OpenCms requires Xerces version 2 to run, your Xerces version is 1. "
104                        + "Usually Xerces 2 should be installed by default as part of the servlet environment.");
105                testResult.setHelp(testResult.getInfo());
106                break;
107            default:
108                if (versionStr == null) {
109                    versionStr = "Unknown version";
110                }
111                testResult.setResult(versionStr);
112                testResult.setRed();
113                testResult.setInfo(
114                    "OpenCms requires Xerces version 2 to run. "
115                        + "Usually Xerces 2 should be installed by default as part of the servlet environment.");
116                testResult.setHelp(testResult.getInfo());
117        }
118        return testResult;
119    }
120}