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.schema;
018    
019    import javax.xml.namespace.QName;
020    
021    import org.apache.xmlbeans.XmlCursor;
022    
023    /**
024     * @version $Rev: 545781 $ $Date: 2007-06-09 13:44:02 -0400 (Sat, 09 Jun 2007) $
025     */
026    public class SecurityElementConverter implements ElementConverter {
027    
028        public static final String GERONIMO_SECURITY_NAMESPACE = "http://geronimo.apache.org/xml/ns/security-2.0";
029        private static final QName PRINCIPAL_QNAME = new QName(GERONIMO_SECURITY_NAMESPACE, "principal");
030        private static final QName REALM_NAME_QNAME = new QName("realm-name");
031        private static final QName DESIGNATED_RUN_AS = new QName("designated-run-as");
032    
033        public void convertElement(XmlCursor cursor, XmlCursor end) {
034            cursor.push();
035            end.toCursor(cursor);
036            end.toEndToken();
037            while (cursor.hasNextToken() && cursor.isLeftOf(end)) {
038                if (cursor.isStart()) {
039                    if (GERONIMO_SECURITY_NAMESPACE.equals(cursor.getName().getNamespaceURI())) {
040                        break;
041                    }
042                    cursor.setName(new QName(GERONIMO_SECURITY_NAMESPACE, cursor.getName().getLocalPart()));
043    
044                }
045                cursor.toNextToken();
046            }
047            cursor.pop();
048            XmlCursor source = null;
049            try {
050                while (cursor.hasNextToken() && cursor.isLeftOf(end)) {
051                    if (cursor.isStart()) {
052                        String localPart = cursor.getName().getLocalPart();
053                        if (localPart.equals("realm")) {
054                            if (source == null) {
055                                source = cursor.newCursor();
056                            } else {
057                                source.toCursor(cursor);
058                            }
059                            cursor.push();
060                            cursor.toEndToken();
061                            cursor.toNextToken();
062                            if (source.toChild(PRINCIPAL_QNAME)) {
063                                do {
064                                    source.removeAttribute(DESIGNATED_RUN_AS);
065                                    source.copyXml(cursor);
066                                } while (source.toNextSibling(PRINCIPAL_QNAME));
067                            }
068    
069                            cursor.pop();
070                            cursor.removeXml();
071                        } else if (localPart.equals("default-subject")) {
072    //                    cursor.removeAttribute(REALM_NAME_QNAME);
073                            cursor.toEndToken();
074                        } else if (localPart.equals("default-principal")) {
075                            cursor.removeXml();
076                        } else if (localPart.equals("principal")) {
077                            cursor.removeAttribute(DESIGNATED_RUN_AS);
078                        } else if (localPart.equals("run-as-subject")) {
079                            cursor.toEndToken();
080                        }
081                    }
082                    cursor.toNextToken();
083                }
084            } finally {
085                if (source != null) {
086                    source.dispose();
087                }
088            }
089        }
090    }