KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > schema > SchemaConversionUtils


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
18 package org.apache.geronimo.schema;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.xml.namespace.QName JavaDoc;
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 /**
38  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
39  */

40 public class SchemaConversionUtils {
41     public static final String JavaDoc J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
42     public static final String JavaDoc JAVAEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee";
43
44     static final String JavaDoc GERONIMO_NAMING_NAMESPACE = "http://geronimo.apache.org/xml/ns/naming-1.2";
45     private static final String JavaDoc GERONIMO_SECURITY_NAMESPACE = "http://geronimo.apache.org/xml/ns/security-1.2";
46     private static final String JavaDoc GERONIMO_SERVICE_NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment-1.2";
47
48     private static final Map JavaDoc GERONIMO_SCHEMA_CONVERSIONS = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc 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         //you should only call this method at a start token
104
return false;
105     }
106
107     public static XmlObject fixGeronimoSchema(XmlObject rawPlan, QName JavaDoc 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 JavaDoc desiredElement) {
131         XmlCursor cursor = xmlObject.newCursor();
132         try {
133             if (findNestedElement(cursor, desiredElement)) {
134                 XmlObject child = cursor.getObject();
135                 //The copy seems to be needed to make the type change work for some documents!
136
return child.copy();
137             }
138         } finally {
139             cursor.dispose();
140         }
141         throw new IllegalArgumentException JavaDoc("xmlobject did not have desired element: " + desiredElement + "/n" + xmlObject);
142     }
143
144     public static boolean findNestedElement(XmlCursor cursor, QName JavaDoc desiredElement) {
145         while (cursor.hasNextToken()) {
146             if (cursor.isStart()) {
147                 QName JavaDoc 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 JavaDoc desiredElement) {
158         while (cursor.hasNextToken()) {
159             if (cursor.isStart()) {
160                 String JavaDoc 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 JavaDoc desiredElement, SchemaType type) {
171         XmlCursor cursor = xmlObject.newCursor();
172         try {
173             if (findNestedElement(cursor, desiredElement)) {
174                 XmlObject child = cursor.getObject();
175                 //The copy seems to be needed to make the type change work for some documents!
176
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 JavaDoc("xmlobject did not have desired element: " + desiredElement + "\n" + xmlObject);
184     }
185
186
187     public static boolean convertToSchema(XmlCursor cursor, String JavaDoc namespace, String JavaDoc schemaLocationURL, String JavaDoc version) {
188         //remove dtd
189
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         //convert namespace
194
boolean isFirstStart = true;
195         while (cursor.hasNextToken()) {
196             if (cursor.isStart()) {
197                 if (namespace.equals(cursor.getName().getNamespaceURI())) {
198                     //already has correct schema, exit
199
return false;
200                 }
201                 cursor.setName(new QName JavaDoc(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 JavaDoc("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "xsi"), namespace + schemaLocationURL);
206                     cursor.insertAttributeWithValue(new QName JavaDoc("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 JavaDoc namespace, String JavaDoc schemaLocationURL, String JavaDoc version) {
217         boolean isFirstStart = true;
218
219
220         while (cursor.hasNextToken()) {
221             if (cursor.isStart()) {
222                 //convert namespace of each starting element
223
cursor.setName(new QName JavaDoc(namespace, cursor.getName().getLocalPart()));
224                 if (isFirstStart) {
225                     //if we are at the first element in the document, reset the version number ...
226
cursor.setAttributeText(new QName JavaDoc("version"), version);
227                     //... and also set the xsi:schemaLocation
228
cursor.setAttributeText(new QName JavaDoc("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     /**
243      * Reorders elements to match descriptionGroup
244      *
245      * @param namespace
246      * @param cursor XmlCursor positioned at first element of "group" to be reordered
247      */

248     public static void convertToDescriptionGroup(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc localName, String JavaDoc namespace, XmlCursor moveable, XmlCursor toHere) {
283         QName JavaDoc name = new QName JavaDoc(namespace, localName);
284         //skip elements already in the correct order.
285
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