1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.web.deployment;
19
20 import javax.xml.namespace.QName;
21
22 import org.apache.geronimo.common.DeploymentException;
23 import org.apache.geronimo.deployment.xbeans.ModuleDocument;
24 import org.apache.geronimo.schema.SchemaConversionUtils;
25 import org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument;
26 import org.apache.geronimo.xbeans.geronimo.web.GerWebAppDocument;
27 import org.apache.xmlbeans.XmlCursor;
28 import org.apache.xmlbeans.XmlObject;
29
30 /**
31 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
32 */
33 public class GenericToSpecificPlanConverter {
34
35 private static final QName GENERIC_QNAME = GerWebAppDocument.type.getDocumentElementName();
36 private static final String GENERIC_NAMESPACE = GENERIC_QNAME.getNamespaceURI();
37 private static final String OLD_GENERIC_NAMESPACE = "http://geronimo.apache.org/xml/ns/web";
38
39 private static final QName GENERIC_CONFIG_QNAME = new QName(GENERIC_NAMESPACE, "container-config");
40 private static final QName OLD_GENERIC_CONFIG_QNAME = new QName(OLD_GENERIC_NAMESPACE, "container-config");
41 private static final String SYSTEM_NAMESPACE = ModuleDocument.type.getDocumentElementName().getNamespaceURI();
42 private static final QName SECURITY_QNAME = GerSecurityDocument.type.getDocumentElementName();
43 private final String configNamespace;
44 private final String namespace;
45 private final String element;
46
47 public GenericToSpecificPlanConverter(String configNamespace, String namespace, String element) {
48 this.configNamespace = configNamespace;
49 this.namespace = namespace;
50 this.element = element;
51 }
52
53 public XmlObject convertToSpecificPlan(XmlObject plan) throws DeploymentException {
54 XmlCursor rawCursor = plan.newCursor();
55 try {
56 if (SchemaConversionUtils.findNestedElement(rawCursor, "web-app")) {
57 XmlCursor temp = rawCursor.newCursor();
58 String namespace = temp.getName().getNamespaceURI();
59 temp.dispose();
60 if(!namespace.equals(GENERIC_NAMESPACE) && !namespace.equals(this.namespace) && !namespace.equals(OLD_GENERIC_NAMESPACE)) {
61 throw new DeploymentException("Cannot handle web plan with namespace "+namespace+" -- expecting "+GENERIC_NAMESPACE+" or "+this.namespace);
62 }
63
64 XmlObject webPlan = rawCursor.getObject().copy();
65
66 XmlCursor cursor = webPlan.newCursor();
67 XmlCursor end = cursor.newCursor();
68 try {
69 cursor.push();
70 if (cursor.toChild(GENERIC_CONFIG_QNAME) || cursor.toChild(OLD_GENERIC_CONFIG_QNAME)) {
71 XmlCursor source = cursor.newCursor();
72 cursor.push();
73 cursor.toEndToken();
74 cursor.toNextToken();
75 try {
76 if (source.toChild(configNamespace, element)) {
77 source.copyXmlContents(cursor);
78 }
79
80 } finally {
81 source.dispose();
82 }
83 cursor.pop();
84 cursor.removeXml();
85 }
86 cursor.pop();
87 cursor.push();
88 while (cursor.hasNextToken()) {
89 if (cursor.isStart()) {
90 if (!SchemaConversionUtils.convertSingleElementToGeronimoSubSchemas(cursor, end)
91 && !this.namespace.equals(cursor.getName().getNamespaceURI())) {
92 cursor.setName(new QName(this.namespace, cursor.getName().getLocalPart()));
93 }
94 }
95 cursor.toNextToken();
96 }
97
98
99 cursor.pop();
100 cursor.push();
101 if (cursor.toChild(this.namespace, "security-realm-name")) {
102 XmlCursor other = cursor.newCursor();
103 try {
104 other.toParent();
105 if (other.toChild(SYSTEM_NAMESPACE, "gbean")) {
106 other.toPrevToken();
107 } else {
108 other.toEndToken();
109 other.toPrevToken();
110 }
111 cursor.moveXml(other);
112 cursor.pop();
113 cursor.push();
114 if (cursor.toChild(SECURITY_QNAME)) {
115 cursor.moveXml(other);
116 }
117 } finally {
118 other.dispose();
119 }
120 }
121 cursor.pop();
122 return webPlan;
123 } finally {
124 cursor.dispose();
125 end.dispose();
126 }
127 } else {
128 throw new DeploymentException("No web-app element");
129 }
130 } finally {
131 rawCursor.dispose();
132 }
133 }
134
135 }