KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > xmlbeans > XmlBeansUtil


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 package org.apache.geronimo.deployment.xmlbeans;
18
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31
32 import org.w3c.dom.Element JavaDoc;
33 import org.apache.xmlbeans.XmlOptions;
34 import org.apache.xmlbeans.XmlException;
35 import org.apache.xmlbeans.XmlObject;
36 import org.apache.xmlbeans.SchemaTypeLoader;
37 import org.apache.xmlbeans.SchemaType;
38 import org.apache.xmlbeans.QNameSet;
39 import org.apache.xmlbeans.XmlValidationError;
40
41 /**
42  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
43  */

44 public class XmlBeansUtil {
45     private static final Map JavaDoc NAMESPACE_UPDATES = new HashMap JavaDoc();
46     //TODO thread safe? conncurrentReaderMap?
47
private static final Map JavaDoc substitutionGroups = new HashMap JavaDoc();
48     private static final XmlObject[] NO_ELEMENTS = new XmlObject[]{};
49
50     static {
51         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-client", "http://geronimo.apache.org/xml/ns/j2ee/application-client-1.2");
52         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-client-1.1", "http://geronimo.apache.org/xml/ns/j2ee/application-client-1.2");
53         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application", "http://geronimo.apache.org/xml/ns/j2ee/application-1.2");
54         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/application-1.1", "http://geronimo.apache.org/xml/ns/j2ee/application-1.2");
55         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment", "http://geronimo.apache.org/xml/ns/deployment-1.2");
56         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment-1.1", "http://geronimo.apache.org/xml/ns/deployment-1.2");
57         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/connector", "http://geronimo.apache.org/xml/ns/j2ee/connector-1.2");
58         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/connector-1.1", "http://geronimo.apache.org/xml/ns/j2ee/connector-1.2");
59         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/deployment/javabean", "http://geronimo.apache.org/xml/ns/deployment/javabean-1.0");
60         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/loginconfig", "http://geronimo.apache.org/xml/ns/loginconfig-1.2");
61         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/loginconfig-1.1", "http://geronimo.apache.org/xml/ns/loginconfig-1.2");
62         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming", "http://geronimo.apache.org/xml/ns/naming-1.2");
63         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/naming-1.1", "http://geronimo.apache.org/xml/ns/naming-1.2");
64         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/security", "http://geronimo.apache.org/xml/ns/security-1.2");
65         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/security-1.1", "http://geronimo.apache.org/xml/ns/security-1.2");
66         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web", "http://geronimo.apache.org/xml/ns/j2ee/web-1.2");
67         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web-1.1", "http://geronimo.apache.org/xml/ns/j2ee/web-1.2");
68         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/jetty", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.2");
69         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.1", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty-1.2");
70         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/jetty/config", "http://geronimo.apache.org/xml/ns/j2ee/web/jetty/config-1.0");
71         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/tomcat", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2");
72         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.1", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2");
73         NAMESPACE_UPDATES.put("http://geronimo.apache.org/xml/ns/web/tomcat/config", "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat/config-1.0");
74         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/openejb-jar", "http://openejb.apache.org/xml/ns/openejb-jar-2.2");
75         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/openejb-jar-2.1", "http://openejb.apache.org/xml/ns/openejb-jar-2.2");
76         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/openejb-jar-2.2", "http://openejb.apache.org/xml/ns/openejb-jar-2.2");
77         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/pkgen", "http://openejb.apache.org/xml/ns/pkgen-2.1");
78         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/pkgen-2.0", "http://openejb.apache.org/xml/ns/pkgen-2.1");
79         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-css-config_1_0", "http://openejb.apache.org/xml/ns/corba-css-config-2.1");
80         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-css-config-2.0", "http://openejb.apache.org/xml/ns/corba-css-config-2.1");
81         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-tss-config_1_0", "http://openejb.apache.org/xml/ns/corba-tss-config-2.1");
82         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-tss-config-2.0", "http://openejb.apache.org/xml/ns/corba-tss-config-2.1");
83         NAMESPACE_UPDATES.put("http://www.openejb.org/xml/ns/corba-tss-config-2.1", "http://openejb.apache.org/xml/ns/corba-tss-config-2.1");
84     }
85
86     private XmlBeansUtil() {
87     }
88
89
90     public static XmlObject parse(File JavaDoc file) throws IOException JavaDoc, XmlException {
91         ArrayList JavaDoc errors = new ArrayList JavaDoc();
92         XmlObject parsed = XmlObject.Factory.parse(file, createXmlOptions(errors));
93         if (errors.size() != 0) {
94             throw new XmlException(errors.toArray().toString());
95         }
96         return parsed;
97     }
98
99     public static XmlObject parse(URL JavaDoc url, ClassLoader JavaDoc cl) throws IOException JavaDoc, XmlException {
100         ArrayList JavaDoc errors = new ArrayList JavaDoc();
101         Thread JavaDoc currentThread = Thread.currentThread();
102         ClassLoader JavaDoc oldcl = currentThread.getContextClassLoader();
103         currentThread.setContextClassLoader(cl);
104         XmlObject parsed;
105         try {
106             parsed = XmlObject.Factory.parse(url, createXmlOptions(errors));
107         } finally {
108             currentThread.setContextClassLoader(oldcl);
109         }
110         if (errors.size() != 0) {
111             throw new XmlException(errors.toArray().toString());
112         }
113         return parsed;
114     }
115
116     public static XmlObject parse(InputStream JavaDoc is) throws IOException JavaDoc, XmlException {
117         ArrayList JavaDoc errors = new ArrayList JavaDoc();
118         XmlObject parsed = XmlObject.Factory.parse(is, createXmlOptions(errors));
119         if (errors.size() != 0) {
120             throw new XmlException(errors.toArray().toString());
121         }
122         return parsed;
123     }
124
125     public static XmlObject parse(String JavaDoc xml) throws XmlException {
126         ArrayList JavaDoc errors = new ArrayList JavaDoc();
127         XmlObject parsed = XmlObject.Factory.parse(xml, createXmlOptions(errors));
128         if (errors.size() != 0) {
129             throw new XmlException(errors.toArray().toString());
130         }
131         return parsed;
132     }
133
134     public static XmlObject parse(Element JavaDoc element) throws XmlException {
135         ArrayList JavaDoc errors = new ArrayList JavaDoc();
136         XmlObject parsed = XmlObject.Factory.parse(element, createXmlOptions(errors));
137         if (errors.size() != 0) {
138             throw new XmlException(errors.toArray().toString());
139         }
140         return parsed;
141     }
142
143     public static XmlOptions createXmlOptions(Collection JavaDoc errors) {
144         XmlOptions options = new XmlOptions();
145         options.setLoadLineNumbers();
146         options.setErrorListener(errors);
147         options.setLoadSubstituteNamespaces(NAMESPACE_UPDATES);
148         return options;
149     }
150
151     public static void registerSubstitutionGroupElements(QName JavaDoc substitutionGroup, QNameSet substitutions) {
152         QNameSet oldSubstitutions = (QNameSet) substitutionGroups.get(substitutionGroup);
153         if (oldSubstitutions != null) {
154             substitutions = oldSubstitutions.union(substitutions);
155         }
156         substitutionGroups.put(substitutionGroup, substitutions);
157     }
158
159     public static void unregisterSubstitutionGroupElements(QName JavaDoc substitutionGroup, QNameSet substitutions) {
160         QNameSet oldSubstitutions = (QNameSet) substitutionGroups.get(substitutionGroup);
161         if (oldSubstitutions != null && substitutions != null) {
162             QNameSet difference = oldSubstitutions.intersect(substitutions.inverse());
163             substitutionGroups.put(substitutionGroup, difference);
164         }
165     }
166
167     public static QNameSet getQNameSetForSubstitutionGroup(QName JavaDoc substitutionGroup) {
168         return (QNameSet) substitutionGroups.get(substitutionGroup);
169     }
170
171     public static XmlObject[] selectSubstitutionGroupElements(QName JavaDoc substitutionGroup, XmlObject container) {
172         QNameSet substitutionGroupMembers = getQNameSetForSubstitutionGroup(substitutionGroup);
173         if (substitutionGroupMembers == null) {
174             return NO_ELEMENTS;
175         }
176         return container.selectChildren(substitutionGroupMembers);
177     }
178
179     public static XmlObject typedCopy(XmlObject in, SchemaType type) throws XmlException {
180         XmlObject out = in.copy().changeType(type);
181         validateDD(out);
182         return out;
183     }
184
185     public static void validateDD(XmlObject dd) throws XmlException {
186         XmlOptions xmlOptions = new XmlOptions();
187         xmlOptions.setLoadLineNumbers();
188         Collection JavaDoc errors = new ArrayList JavaDoc();
189         xmlOptions.setErrorListener(errors);
190         try {
191             if (!dd.validate(xmlOptions)) {
192
193                 for (Iterator JavaDoc iterator = errors.iterator(); iterator.hasNext();) {
194                     Object JavaDoc o = iterator.next();
195                     if (o instanceof XmlValidationError) {
196                         XmlValidationError validationError = (XmlValidationError) o;
197                         List JavaDoc expected = validationError.getExpectedQNames();
198                         QName JavaDoc actual = validationError.getOffendingQName();
199                         if (expected != null) {
200                             for (Iterator JavaDoc iterator1 = expected.iterator(); iterator1.hasNext();) {
201                                 QName JavaDoc expectedQName = (QName JavaDoc) iterator1.next();
202                                 QNameSet substitutions = getQNameSetForSubstitutionGroup(expectedQName);
203                                 if (substitutions != null && substitutions.contains(actual)) {
204                                     iterator.remove();
205                                     break;
206                                 }
207                             }
208                         }
209                     }
210                 }
211
212                 if (!errors.isEmpty()) {
213                     throw new XmlException("Invalid deployment descriptor: " + errors + "\nDescriptor: " + dd.toString(), null, errors);
214                 }
215             }
216         } catch (NullPointerException JavaDoc e) {
217             //ignore
218
}
219 // System.out.println("descriptor: " + dd.toString());
220
}
221 }
222
Popular Tags