001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.web.deployment;
018    
019    import javax.xml.namespace.QName;
020    
021    import org.apache.geronimo.common.DeploymentException;
022    import org.apache.geronimo.deployment.xbeans.ModuleDocument;
023    import org.apache.geronimo.schema.SchemaConversionUtils;
024    import org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument;
025    import org.apache.geronimo.xbeans.geronimo.web.GerWebAppDocument;
026    import org.apache.xmlbeans.XmlCursor;
027    import org.apache.xmlbeans.XmlObject;
028    
029    /**
030     * @version $Rev: 495635 $ $Date: 2007-01-12 11:46:40 -0500 (Fri, 12 Jan 2007) $
031     */
032    public class GenericToSpecificPlanConverter {
033    
034        private static final QName GENERIC_QNAME = GerWebAppDocument.type.getDocumentElementName();
035        private static final String GENERIC_NAMESPACE = GENERIC_QNAME.getNamespaceURI();
036        private static final String OLD_GENERIC_NAMESPACE = "http://geronimo.apache.org/xml/ns/web";
037    
038        private static final QName GENERIC_CONFIG_QNAME = new QName(GENERIC_NAMESPACE, "container-config");
039        private static final QName OLD_GENERIC_CONFIG_QNAME = new QName(OLD_GENERIC_NAMESPACE, "container-config");
040        private static final String SYSTEM_NAMESPACE = ModuleDocument.type.getDocumentElementName().getNamespaceURI();
041        private static final QName SECURITY_QNAME = GerSecurityDocument.type.getDocumentElementName();
042        private final String configNamespace;
043        private final String namespace;
044        private final String element;
045    
046        public GenericToSpecificPlanConverter(String configNamespace, String namespace, String element) {
047            this.configNamespace = configNamespace;
048            this.namespace = namespace;
049            this.element = element;
050        }
051    
052        public XmlObject convertToSpecificPlan(XmlObject plan) throws DeploymentException {
053            XmlCursor rawCursor = plan.newCursor();
054            try {
055                if (SchemaConversionUtils.findNestedElement(rawCursor, "web-app")) {
056                    XmlCursor temp = rawCursor.newCursor();
057                    String namespace = temp.getName().getNamespaceURI();
058                    temp.dispose();
059                    if(!namespace.equals(GENERIC_NAMESPACE) && !namespace.equals(this.namespace) && !namespace.equals(OLD_GENERIC_NAMESPACE)) {
060                        throw new DeploymentException("Cannot handle web plan with namespace "+namespace+" -- expecting "+GENERIC_NAMESPACE+" or "+this.namespace);
061                    }
062    
063                    XmlObject webPlan = rawCursor.getObject().copy();
064    
065                    XmlCursor cursor = webPlan.newCursor();
066                    XmlCursor end = cursor.newCursor();
067                    try {
068                        cursor.push();
069                        if (cursor.toChild(GENERIC_CONFIG_QNAME) || cursor.toChild(OLD_GENERIC_CONFIG_QNAME)) {
070                            XmlCursor source = cursor.newCursor();
071                            cursor.push();
072                            cursor.toEndToken();
073                            cursor.toNextToken();
074                            try {
075                                if (source.toChild(configNamespace, element)) {
076                                    source.copyXmlContents(cursor);
077                                }
078    
079                            } finally {
080                                source.dispose();
081                            }
082                            cursor.pop();
083                            cursor.removeXml();
084                        }
085                        cursor.pop();
086                        cursor.push();
087                        while (cursor.hasNextToken()) {
088                            if (cursor.isStart()) {
089                                if (!SchemaConversionUtils.convertSingleElementToGeronimoSubSchemas(cursor, end)
090                                && !this.namespace.equals(cursor.getName().getNamespaceURI())) {
091                                    cursor.setName(new QName(this.namespace, cursor.getName().getLocalPart()));
092                                }
093                            }
094                            cursor.toNextToken();
095                        }
096                        //move security elements after refs
097    
098                        cursor.pop();
099                        cursor.push();
100                        if (cursor.toChild(this.namespace, "security-realm-name")) {
101                            XmlCursor other = cursor.newCursor();
102                            try {
103                                other.toParent();
104                                if (other.toChild(SYSTEM_NAMESPACE, "gbean")) {
105                                    other.toPrevToken();
106                                } else {
107                                    other.toEndToken();
108                                    other.toPrevToken();
109                                }
110                                cursor.moveXml(other);
111                                cursor.pop();
112                                cursor.push();
113                                if (cursor.toChild(SECURITY_QNAME)) {
114                                    cursor.moveXml(other);
115                                }
116                            } finally {
117                                other.dispose();
118                            }
119                        }
120                        cursor.pop();
121                        return webPlan;
122                    } finally {
123                        cursor.dispose();
124                        end.dispose();
125                    }
126                } else {
127                    throw new DeploymentException("No web-app element");
128                }
129            } finally {
130                rawCursor.dispose();
131            }
132        }
133    
134    }