1 17 18 package org.apache.geronimo.schema; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.xml.namespace.QName ; 24 25 import org.apache.geronimo.xbeans.j2ee.ApplicationClientDocument; 26 import org.apache.geronimo.xbeans.j2ee.ApplicationDocument; 27 import org.apache.geronimo.xbeans.j2ee.ConnectorDocument; 28 import org.apache.geronimo.xbeans.j2ee.EjbJarDocument; 29 import org.apache.geronimo.xbeans.j2ee.WebAppDocument; 30 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil; 31 import org.apache.xmlbeans.SchemaType; 32 import org.apache.xmlbeans.XmlCursor; 33 import org.apache.xmlbeans.XmlDocumentProperties; 34 import org.apache.xmlbeans.XmlException; 35 import org.apache.xmlbeans.XmlObject; 36 37 40 public class SchemaConversionUtils { 41 public static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee"; 42 public static final String JAVAEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee"; 43 44 static final String GERONIMO_NAMING_NAMESPACE = "http://geronimo.apache.org/xml/ns/naming-1.2"; 45 private static final String GERONIMO_SECURITY_NAMESPACE = "http://geronimo.apache.org/xml/ns/security-1.2"; 46 private static final String GERONIMO_SERVICE_NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment-1.2"; 47 48 private static final Map GERONIMO_SCHEMA_CONVERSIONS = new HashMap (); 49 50 static { 51 52 GERONIMO_SCHEMA_CONVERSIONS.put("gbean-ref", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 53 GERONIMO_SCHEMA_CONVERSIONS.put("ejb-ref", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 54 GERONIMO_SCHEMA_CONVERSIONS.put("ejb-local-ref", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 55 GERONIMO_SCHEMA_CONVERSIONS.put("service-ref", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 56 GERONIMO_SCHEMA_CONVERSIONS.put("resource-ref", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 57 GERONIMO_SCHEMA_CONVERSIONS.put("resource-env-ref", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 58 GERONIMO_SCHEMA_CONVERSIONS.put("message-destination", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 59 GERONIMO_SCHEMA_CONVERSIONS.put("cmp-connection-factory", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 60 GERONIMO_SCHEMA_CONVERSIONS.put("workmanager", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 61 GERONIMO_SCHEMA_CONVERSIONS.put("resource-adapter", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 62 GERONIMO_SCHEMA_CONVERSIONS.put("web-container", new NamespaceElementConverter(GERONIMO_NAMING_NAMESPACE)); 63 64 GERONIMO_SCHEMA_CONVERSIONS.put("security", new SecurityElementConverter()); 65 GERONIMO_SCHEMA_CONVERSIONS.put("default-principal", new NamespaceElementConverter(GERONIMO_SECURITY_NAMESPACE)); 66 67 GERONIMO_SCHEMA_CONVERSIONS.put("gbean", new GBeanElementConverter()); 68 GERONIMO_SCHEMA_CONVERSIONS.put("environment", new NamespaceElementConverter(GERONIMO_SERVICE_NAMESPACE)); 69 GERONIMO_SCHEMA_CONVERSIONS.put("client-environment", new NamespaceElementConverter(GERONIMO_SERVICE_NAMESPACE)); 70 GERONIMO_SCHEMA_CONVERSIONS.put("server-environment", new NamespaceElementConverter(GERONIMO_SERVICE_NAMESPACE)); 71 } 72 73 private SchemaConversionUtils() { 74 } 75 76 public static void registerNamespaceConversions(Map conversions) { 77 GERONIMO_SCHEMA_CONVERSIONS.putAll(conversions); 78 } 79 80 public static void convertToGeronimoSubSchemas(XmlCursor cursor) { 81 cursor.toStartDoc(); 82 XmlCursor end = cursor.newCursor(); 83 try { 84 while (cursor.hasNextToken()) { 85 convertSingleElementToGeronimoSubSchemas(cursor, end); 86 cursor.toNextToken(); 87 } 88 } finally { 89 end.dispose(); 90 } 91 } 92 93 public static boolean convertSingleElementToGeronimoSubSchemas(XmlCursor cursor, XmlCursor end) { 94 if (cursor.isStart()) { 95 String localName = cursor.getName().getLocalPart(); 96 ElementConverter converter = (ElementConverter) GERONIMO_SCHEMA_CONVERSIONS.get(localName); 97 if (converter != null) { 98 converter.convertElement(cursor, end); 99 return true; 100 } 101 return false; 102 } 103 return false; 105 } 106 107 public static XmlObject fixGeronimoSchema(XmlObject rawPlan, QName desiredElement, SchemaType desiredType) throws XmlException { 108 XmlCursor cursor = rawPlan.newCursor(); 109 try { 110 if (findNestedElement(cursor, desiredElement)) { 111 cursor.push(); 112 convertToGeronimoSubSchemas(cursor); 113 cursor.pop(); 114 XmlObject temp = cursor.getObject(); 115 116 XmlObject result = temp.changeType(desiredType); 117 if (result == null || result.schemaType() != desiredType) { 118 result = temp.copy().changeType(desiredType); 119 } 120 XmlBeansUtil.validateDD(result); 121 return result; 122 } else { 123 return null; 124 } 125 } finally { 126 cursor.dispose(); 127 } 128 } 129 130 public static XmlObject getNestedObject(XmlObject xmlObject, QName desiredElement) { 131 XmlCursor cursor = xmlObject.newCursor(); 132 try { 133 if (findNestedElement(cursor, desiredElement)) { 134 XmlObject child = cursor.getObject(); 135 return child.copy(); 137 } 138 } finally { 139 cursor.dispose(); 140 } 141 throw new IllegalArgumentException ("xmlobject did not have desired element: " + desiredElement + "/n" + xmlObject); 142 } 143 144 public static boolean findNestedElement(XmlCursor cursor, QName desiredElement) { 145 while (cursor.hasNextToken()) { 146 if (cursor.isStart()) { 147 QName element = cursor.getName(); 148 if (element.equals(desiredElement)) { 149 return true; 150 } 151 } 152 cursor.toNextToken(); 153 } 154 return false; 155 } 156 157 public static boolean findNestedElement(XmlCursor cursor, String desiredElement) { 158 while (cursor.hasNextToken()) { 159 if (cursor.isStart()) { 160 String element = cursor.getName().getLocalPart(); 161 if (element.equals(desiredElement)) { 162 return true; 163 } 164 } 165 cursor.toNextToken(); 166 } 167 return false; 168 } 169 170 public static XmlObject getNestedObjectAsType(XmlObject xmlObject, QName desiredElement, SchemaType type) { 171 XmlCursor cursor = xmlObject.newCursor(); 172 try { 173 if (findNestedElement(cursor, desiredElement)) { 174 XmlObject child = cursor.getObject(); 175 XmlObject result = child.copy().changeType(type); 177 assert result.schemaType() == type; 178 return result; 179 } 180 } finally { 181 cursor.dispose(); 182 } 183 throw new IllegalArgumentException ("xmlobject did not have desired element: " + desiredElement + "\n" + xmlObject); 184 } 185 186 187 public static boolean convertToSchema(XmlCursor cursor, String namespace, String schemaLocationURL, String version) { 188 XmlDocumentProperties xmlDocumentProperties = cursor.documentProperties(); 190 xmlDocumentProperties.remove(XmlDocumentProperties.DOCTYPE_NAME); 191 xmlDocumentProperties.remove(XmlDocumentProperties.DOCTYPE_PUBLIC_ID); 192 xmlDocumentProperties.remove(XmlDocumentProperties.DOCTYPE_SYSTEM_ID); 193 boolean isFirstStart = true; 195 while (cursor.hasNextToken()) { 196 if (cursor.isStart()) { 197 if (namespace.equals(cursor.getName().getNamespaceURI())) { 198 return false; 200 } 201 cursor.setName(new QName (namespace, cursor.getName().getLocalPart())); 202 cursor.toNextToken(); 203 if (isFirstStart) { 204 cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 205 cursor.insertAttributeWithValue(new QName ("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi"), namespace + schemaLocationURL); 206 cursor.insertAttributeWithValue(new QName ("version"), version); 207 isFirstStart = false; 208 } 209 } else { 210 cursor.toNextToken(); 211 } 212 } 213 return true; 214 } 215 216 public static boolean convertSchemaVersion (XmlCursor cursor, String namespace, String schemaLocationURL, String version) { 217 boolean isFirstStart = true; 218 219 220 while (cursor.hasNextToken()) { 221 if (cursor.isStart()) { 222 cursor.setName(new QName (namespace, cursor.getName().getLocalPart())); 224 if (isFirstStart) { 225 cursor.setAttributeText(new QName ("version"), version); 227 cursor.setAttributeText(new QName ("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi"), namespace + " "+schemaLocationURL); 229 isFirstStart = false; 230 } 231 cursor.toNextToken(); 232 233 } else { 234 cursor.toNextToken(); 235 } 236 } 237 238 239 return true; 240 } 241 242 248 public static void convertToDescriptionGroup(String namespace, XmlCursor cursor, XmlCursor moveable) { 249 moveable.toCursor(cursor); 250 moveElements("description", namespace, moveable, cursor); 251 moveElements("display-name", namespace, moveable, cursor); 252 moveElements("icon", namespace, moveable, cursor); 253 } 254 255 public static void convertToJNDIEnvironmentRefsGroup(String namespace, XmlCursor cursor, XmlCursor moveable) { 256 moveElements("env-entry", namespace, moveable, cursor); 257 moveElements("ejb-ref", namespace, moveable, cursor); 258 moveElements("ejb-local-ref", namespace, moveable, cursor); 259 moveElements("resource-ref", namespace, moveable, cursor); 260 moveElements("resource-env-ref", namespace, moveable, cursor); 261 moveElements("message-destination-ref", namespace, moveable, cursor); 262 if (cursor.toPrevSibling()) { 263 do { 264 String name = cursor.getName().getLocalPart(); 265 if ("env-entry".equals(name)) { 266 cursor.push(); 267 cursor.toFirstChild(); 268 convertToDescriptionGroup(namespace, cursor, moveable); 269 convertToEnvEntryGroup(namespace, cursor, moveable); 270 cursor.pop(); 271 } 272 } while (cursor.toPrevSibling()); 273 } 274 } 275 276 public static void convertToEnvEntryGroup(String namespace, XmlCursor cursor, XmlCursor moveable) { 277 moveElements("env-entry-name", namespace, moveable, cursor); 278 moveElements("env-entry-type", namespace, moveable, cursor); 279 moveElements("env-entry-value", namespace, moveable, cursor); 280 } 281 282 private static void moveElements(String localName, String namespace, XmlCursor moveable, XmlCursor toHere) { 283 QName name = new QName (namespace, localName); 284 while (name.equals(toHere.getName()) && toHere.toNextSibling()) { 286 } 287 moveable.toCursor(toHere); 288 while (moveable.toNextSibling(name)) { 289 moveable.moveXml(toHere); 290 } 291 } 292 293 } 294 | Popular Tags |