1 17 package org.apache.geronimo.web.deployment; 18 19 import javax.xml.namespace.QName ; 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 32 public class GenericToSpecificPlanConverter { 33 34 private static final QName GENERIC_QNAME = GerWebAppDocument.type.getDocumentElementName(); 35 private static final String GENERIC_NAMESPACE = GENERIC_QNAME.getNamespaceURI(); 36 private static final String OLD_GENERIC_NAMESPACE = "http://geronimo.apache.org/xml/ns/web"; 37 38 private static final QName GENERIC_CONFIG_QNAME = new QName (GENERIC_NAMESPACE, "container-config"); 39 private static final QName OLD_GENERIC_CONFIG_QNAME = new QName (OLD_GENERIC_NAMESPACE, "container-config"); 40 private static final String SYSTEM_NAMESPACE = ModuleDocument.type.getDocumentElementName().getNamespaceURI(); 41 private static final QName SECURITY_QNAME = GerSecurityDocument.type.getDocumentElementName(); 42 private final String configNamespace; 43 private final String namespace; 44 private final String element; 45 46 public GenericToSpecificPlanConverter(String configNamespace, String namespace, String 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 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 (this.namespace, cursor.getName().getLocalPart())); 92 } 93 } 94 cursor.toNextToken(); 95 } 96 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 |