001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.tomcat.cluster.wadi.builder;
021    
022    import javax.xml.namespace.QName;
023    
024    import org.apache.geronimo.schema.ElementConverter;
025    import org.apache.geronimo.xbeans.geronimo.GerTomcatClusteringWadiDocument;
026    import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
027    import org.apache.xmlbeans.XmlCursor;
028    
029    /**
030     *
031     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
032     */
033    public class TomcatClusteringWADIConverter implements ElementConverter {
034        private static final String TOMCAT_CLUSTERING_WADI_NS = GerTomcatClusteringWadiDocument.type.getDocumentElementName().getNamespaceURI();
035        private static final String NAMING_NS = GerPatternType.type.getName().getNamespaceURI();
036        private static final String CLUSTER_ELEMENT_NAME = "cluster";
037        private static final String BACKING_STRATEGY_FACTORY_ELEMENT_NAME = "backing-strategy-factory";
038    
039        public void convertElement(XmlCursor cursor, XmlCursor end) {
040            end.toCursor(cursor);
041            end.toEndToken();
042    
043            while (cursor.hasNextToken() && cursor.isLeftOf(end)) {
044                if (cursor.isStart()) {
045                    String localPart = cursor.getName().getLocalPart();
046                    cursor.setName(new QName(TOMCAT_CLUSTERING_WADI_NS, localPart));
047                    if (localPart.equals(CLUSTER_ELEMENT_NAME) || localPart.equals(BACKING_STRATEGY_FACTORY_ELEMENT_NAME)) {
048                        convertChildrenToNamingNS(cursor);
049                        cursor.toEndToken();
050                    }
051                }
052                cursor.toNextToken();
053            }
054        }
055    
056        protected void convertChildrenToNamingNS(XmlCursor cursor) {
057            XmlCursor namingCursor = cursor.newCursor();
058            try {
059                if (namingCursor.toFirstChild()) {
060                    XmlCursor endNamingCursor = namingCursor.newCursor();
061                    try {
062                        convertToNamingNS(namingCursor, endNamingCursor);
063                    } finally {
064                        endNamingCursor.dispose();
065                    }
066                }
067            } finally {
068                namingCursor.dispose();
069            }
070        }
071    
072        protected void convertToNamingNS(XmlCursor cursor, XmlCursor end) {
073            end.toCursor(cursor);
074            end.toEndToken();
075            while (cursor.hasNextToken() && cursor.isLeftOf(end)) {
076                if (cursor.isStart()) {
077                    String localPart = cursor.getName().getLocalPart();
078                    cursor.setName(new QName(NAMING_NS, localPart));
079                }
080                cursor.toNextToken();
081            }
082        }
083    
084    }