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: */package org.apache.geronimo.web.deployment;
017:
018: import javax.xml.namespace.QName;
019:
020: import org.apache.geronimo.common.DeploymentException;
021: import org.apache.geronimo.deployment.xbeans.ModuleDocument;
022: import org.apache.geronimo.schema.SchemaConversionUtils;
023: import org.apache.geronimo.xbeans.geronimo.security.GerSecurityDocument;
024: import org.apache.geronimo.xbeans.geronimo.web.GerWebAppDocument;
025: import org.apache.xmlbeans.XmlCursor;
026: import org.apache.xmlbeans.XmlObject;
027:
028: /**
029: * @version $Rev: 495635 $ $Date: 2007-01-12 08:46:40 -0800 (Fri, 12 Jan 2007) $
030: */
031: public class GenericToSpecificPlanConverter {
032:
033: private static final QName GENERIC_QNAME = GerWebAppDocument.type
034: .getDocumentElementName();
035: private static final String GENERIC_NAMESPACE = GENERIC_QNAME
036: .getNamespaceURI();
037: private static final String OLD_GENERIC_NAMESPACE = "http://geronimo.apache.org/xml/ns/web";
038:
039: private static final QName GENERIC_CONFIG_QNAME = new QName(
040: GENERIC_NAMESPACE, "container-config");
041: private static final QName OLD_GENERIC_CONFIG_QNAME = new QName(
042: OLD_GENERIC_NAMESPACE, "container-config");
043: private static final String SYSTEM_NAMESPACE = ModuleDocument.type
044: .getDocumentElementName().getNamespaceURI();
045: private static final QName SECURITY_QNAME = GerSecurityDocument.type
046: .getDocumentElementName();
047: private final String configNamespace;
048: private final String namespace;
049: private final String element;
050:
051: public GenericToSpecificPlanConverter(String configNamespace,
052: String namespace, String element) {
053: this .configNamespace = configNamespace;
054: this .namespace = namespace;
055: this .element = element;
056: }
057:
058: public XmlObject convertToSpecificPlan(XmlObject plan)
059: throws DeploymentException {
060: XmlCursor rawCursor = plan.newCursor();
061: try {
062: if (SchemaConversionUtils.findNestedElement(rawCursor,
063: "web-app")) {
064: XmlCursor temp = rawCursor.newCursor();
065: String namespace = temp.getName().getNamespaceURI();
066: temp.dispose();
067: if (!namespace.equals(GENERIC_NAMESPACE)
068: && !namespace.equals(this .namespace)
069: && !namespace.equals(OLD_GENERIC_NAMESPACE)) {
070: throw new DeploymentException(
071: "Cannot handle web plan with namespace "
072: + namespace + " -- expecting "
073: + GENERIC_NAMESPACE + " or "
074: + this .namespace);
075: }
076:
077: XmlObject webPlan = rawCursor.getObject().copy();
078:
079: XmlCursor cursor = webPlan.newCursor();
080: XmlCursor end = cursor.newCursor();
081: try {
082: cursor.push();
083: if (cursor.toChild(GENERIC_CONFIG_QNAME)
084: || cursor.toChild(OLD_GENERIC_CONFIG_QNAME)) {
085: XmlCursor source = cursor.newCursor();
086: cursor.push();
087: cursor.toEndToken();
088: cursor.toNextToken();
089: try {
090: if (source
091: .toChild(configNamespace, element)) {
092: source.copyXmlContents(cursor);
093: }
094:
095: } finally {
096: source.dispose();
097: }
098: cursor.pop();
099: cursor.removeXml();
100: }
101: cursor.pop();
102: cursor.push();
103: while (cursor.hasNextToken()) {
104: if (cursor.isStart()) {
105: if (!SchemaConversionUtils
106: .convertSingleElementToGeronimoSubSchemas(
107: cursor, end)
108: && !this .namespace.equals(cursor
109: .getName()
110: .getNamespaceURI())) {
111: cursor.setName(new QName(
112: this .namespace, cursor
113: .getName()
114: .getLocalPart()));
115: }
116: }
117: cursor.toNextToken();
118: }
119: //move security elements after refs
120:
121: cursor.pop();
122: cursor.push();
123: if (cursor.toChild(this .namespace,
124: "security-realm-name")) {
125: XmlCursor other = cursor.newCursor();
126: try {
127: other.toParent();
128: if (other
129: .toChild(SYSTEM_NAMESPACE, "gbean")) {
130: other.toPrevToken();
131: } else {
132: other.toEndToken();
133: other.toPrevToken();
134: }
135: cursor.moveXml(other);
136: cursor.pop();
137: cursor.push();
138: if (cursor.toChild(SECURITY_QNAME)) {
139: cursor.moveXml(other);
140: }
141: } finally {
142: other.dispose();
143: }
144: }
145: cursor.pop();
146: return webPlan;
147: } finally {
148: cursor.dispose();
149: end.dispose();
150: }
151: } else {
152: throw new DeploymentException("No web-app element");
153: }
154: } finally {
155: rawCursor.dispose();
156: }
157: }
158:
159: }
|