001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.xml;
016    
017    import com.liferay.util.xml.descriptor.PortletAppDescriptor;
018    import com.liferay.util.xml.descriptor.StrictXMLDescriptor;
019    import com.liferay.util.xml.descriptor.StrutsConfigDescriptor;
020    import com.liferay.util.xml.descriptor.TilesDefsDescriptor;
021    import com.liferay.util.xml.descriptor.WebXML23Descriptor;
022    import com.liferay.util.xml.descriptor.WebXML24Descriptor;
023    import com.liferay.util.xml.descriptor.XMLDescriptor;
024    
025    import org.dom4j.Document;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    public class XMLTypeDetector {
031    
032            public static final XMLDescriptor[] REGISTERED_DESCRIPTORS = {
033                    new PortletAppDescriptor(), new StrutsConfigDescriptor(),
034                    new TilesDefsDescriptor(), new WebXML23Descriptor(),
035                    new WebXML24Descriptor()
036            };
037    
038            public static XMLDescriptor determineType(String doctype, Document root) {
039                    for (int i = 0; i < REGISTERED_DESCRIPTORS.length; i++) {
040                            XMLDescriptor descriptor = REGISTERED_DESCRIPTORS[i];
041    
042                            if (descriptor.canHandleType(doctype, root)) {
043                                    return descriptor;
044                            }
045                    }
046    
047                    return new StrictXMLDescriptor();
048            }
049    
050    }