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.descriptor;
016    
017    import com.liferay.util.xml.ElementIdentifier;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.dom4j.Document;
023    import org.dom4j.Element;
024    
025    /**
026     * @author Jorge Ferrer
027     * @author Brian Wing Shun Chan
028     */
029    public class WebXML24Descriptor extends SimpleXMLDescriptor {
030    
031            public WebXML24Descriptor() {
032                    _orderedChildren.put(
033                            "servlet",
034                            new String[] {
035                                    "icon", "servlet-name", "display-name", "description",
036                                    "servlet-class", "jsp-file", "init-param", "load-on-startup",
037                                    "run-as", "security-role-ref"
038                            });
039                    _orderedChildren.put(
040                            "jsp-config", new String[] {"taglib", "jsp-property-group"});
041            }
042    
043            @Override
044            public boolean canHandleType(String doctype, Document root) {
045                    return doctype.contains("web-app");
046            }
047    
048            @Override
049            public String[] getChildrenOrder(Element parentElement) {
050                    String parentName = parentElement.getQName().getName();
051    
052                    String[] childrenOrder = _orderedChildren.get(parentName);
053    
054                    if (childrenOrder == null) {
055                            childrenOrder = new String[0];
056                    }
057    
058                    return childrenOrder;
059            }
060    
061            @Override
062            public ElementIdentifier[] getElementsIdentifiedByAttribute() {
063                    return _ELEMENTS_IDENTIFIED_BY_ATTR;
064            }
065    
066            @Override
067            public ElementIdentifier[] getElementsIdentifiedByChild() {
068                    return _ELEMENTS_IDENTIFIED_BY_CHILD;
069            }
070    
071            @Override
072            public String[] getJoinableElements() {
073                    return _JOINABLE_ELEMENTS;
074            }
075    
076            @Override
077            public String[] getRootChildrenOrder() {
078                    return _ROOT_ORDERED_CHILDREN;
079            }
080    
081            @Override
082            public String[] getUniqueElements() {
083                    return _UNIQUE_ELEMENTS;
084            }
085    
086            private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_ATTR = {
087            };
088    
089            private static final ElementIdentifier[] _ELEMENTS_IDENTIFIED_BY_CHILD = {
090                    new ElementIdentifier("context-param", "param-name"),
091                    new ElementIdentifier("filter", "filter-name"),
092                    //new ElementIdentifier("filter-mapping", "filter-name"),
093                    new ElementIdentifier("servlet", "servlet-name"),
094                    //new ElementIdentifier("servlet-mapping", "servlet-name"),
095                    new ElementIdentifier("init-param", "param-name"),
096                    new ElementIdentifier("taglib", "taglib-uri"),
097                    new ElementIdentifier("resource-env-ref", "res-env-ref-name"),
098                    new ElementIdentifier("resource-ref", "res-ref-name"),
099                    new ElementIdentifier("ejb-local-ref", "ejb-ref-name")
100            };
101    
102            private static final String[] _JOINABLE_ELEMENTS = {
103                    "welcome-file-list", "jsp-config"
104            };
105    
106            private static final String[] _ROOT_ORDERED_CHILDREN = {
107                    "icon", "display-name", "description", "distributable", "context-param",
108                    "filter", "filter-mapping", "listener", "servlet", "servlet-mapping",
109                    "session-config", "mime-mapping", "welcome-file-list", "error-page",
110                    "jsp-config", "resource-env-ref", "resource-ref", "security-constraint",
111                    "login-config", "security-role", "env-entry", "ejb-ref", "ejb-local-ref"
112            };
113    
114            private static final String[] _UNIQUE_ELEMENTS = {
115                    "icon", "display-name", "description", "distributable",
116                    "session-config", "welcome-file-list", "jsp-config", "login-config"
117            };
118    
119            private Map<String, String[]> _orderedChildren =
120                    new HashMap<String, String[]>();
121    
122    }