KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > web > deployment > GenericToSpecificPlanConverter


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.web.deployment;
18
19 import javax.xml.namespace.QName JavaDoc;
20
21 import org.apache.geronimo.common.DeploymentException;
22 import org.apache.geronimo.deployment.xbeans.ModuleDocument;
23 import org.apache.geronimo.schema.SchemaConversionUtils;
24 import org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument;
25 import org.apache.geronimo.xbeans.geronimo.web.GerWebAppDocument;
26 import org.apache.xmlbeans.XmlCursor;
27 import org.apache.xmlbeans.XmlObject;
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class GenericToSpecificPlanConverter {
33
34     private static final QName JavaDoc GENERIC_QNAME = GerWebAppDocument.type.getDocumentElementName();
35     private static final String JavaDoc GENERIC_NAMESPACE = GENERIC_QNAME.getNamespaceURI();
36     private static final String JavaDoc OLD_GENERIC_NAMESPACE = "http://geronimo.apache.org/xml/ns/web";
37
38     private static final QName JavaDoc GENERIC_CONFIG_QNAME = new QName JavaDoc(GENERIC_NAMESPACE, "container-config");
39     private static final QName JavaDoc OLD_GENERIC_CONFIG_QNAME = new QName JavaDoc(OLD_GENERIC_NAMESPACE, "container-config");
40     private static final String JavaDoc SYSTEM_NAMESPACE = ModuleDocument.type.getDocumentElementName().getNamespaceURI();
41     private static final QName JavaDoc SECURITY_QNAME = GerSecurityDocument.type.getDocumentElementName();
42     private final String JavaDoc configNamespace;
43     private final String JavaDoc namespace;
44     private final String JavaDoc element;
45
46     public GenericToSpecificPlanConverter(String JavaDoc configNamespace, String JavaDoc namespace, String JavaDoc element) {
47         this.configNamespace = configNamespace;
48         this.namespace = namespace;
49         this.element = element;
50     }
51
52     public XmlObject convertToSpecificPlan(XmlObject plan) throws DeploymentException {
53         XmlCursor rawCursor = plan.newCursor();
54         try {
55             if (SchemaConversionUtils.findNestedElement(rawCursor, "web-app")) {
56                 XmlCursor temp = rawCursor.newCursor();
57                 String JavaDoc namespace = temp.getName().getNamespaceURI();
58                 temp.dispose();
59                 if(!namespace.equals(GENERIC_NAMESPACE) && !namespace.equals(this.namespace) && !namespace.equals(OLD_GENERIC_NAMESPACE)) {
60                     throw new DeploymentException("Cannot handle web plan with namespace "+namespace+" -- expecting "+GENERIC_NAMESPACE+" or "+this.namespace);
61                 }
62
63                 XmlObject webPlan = rawCursor.getObject().copy();
64
65                 XmlCursor cursor = webPlan.newCursor();
66                 XmlCursor end = cursor.newCursor();
67                 try {
68                     cursor.push();
69                     if (cursor.toChild(GENERIC_CONFIG_QNAME) || cursor.toChild(OLD_GENERIC_CONFIG_QNAME)) {
70                         XmlCursor source = cursor.newCursor();
71                         cursor.push();
72                         cursor.toEndToken();
73                         cursor.toNextToken();
74                         try {
75                             if (source.toChild(configNamespace, element)) {
76                                 source.copyXmlContents(cursor);
77                             }
78
79                         } finally {
80                             source.dispose();
81                         }
82                         cursor.pop();
83                         cursor.removeXml();
84                     }
85                     cursor.pop();
86                     cursor.push();
87                     while (cursor.hasNextToken()) {
88                         if (cursor.isStart()) {
89                             if (!SchemaConversionUtils.convertSingleElementToGeronimoSubSchemas(cursor, end)
90                             && !this.namespace.equals(cursor.getName().getNamespaceURI())) {
91                                 cursor.setName(new QName JavaDoc(this.namespace, cursor.getName().getLocalPart()));
92                             }
93                         }
94                         cursor.toNextToken();
95                     }
96                     //move security elements after refs
97

98                     cursor.pop();
99                     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 }
Popular Tags