1 17 package org.apache.geronimo.deployment.xmlbeans; 18 19 import java.util.Map ; 20 import java.util.HashMap ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 30 import javax.xml.namespace.QName ; 31 32 import org.w3c.dom.Element ; 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 44 public class XmlBeansUtil { 45 private static final Map NAMESPACE_UPDATES = new HashMap (); 46 private static final Map substitutionGroups = new HashMap (); 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 file) throws IOException , XmlException { 91 ArrayList errors = new ArrayList (); 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 url, ClassLoader cl) throws IOException , XmlException { 100 ArrayList errors = new ArrayList (); 101 Thread currentThread = Thread.currentThread(); 102 ClassLoader 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 is) throws IOException , XmlException { 117 ArrayList errors = new ArrayList (); 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 xml) throws XmlException { 126 ArrayList errors = new ArrayList (); 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 element) throws XmlException { 135 ArrayList errors = new ArrayList (); 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 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 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 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 substitutionGroup) { 168 return (QNameSet) substitutionGroups.get(substitutionGroup); 169 } 170 171 public static XmlObject[] selectSubstitutionGroupElements(QName 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 errors = new ArrayList (); 189 xmlOptions.setErrorListener(errors); 190 try { 191 if (!dd.validate(xmlOptions)) { 192 193 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 194 Object o = iterator.next(); 195 if (o instanceof XmlValidationError) { 196 XmlValidationError validationError = (XmlValidationError) o; 197 List expected = validationError.getExpectedQNames(); 198 QName actual = validationError.getOffendingQName(); 199 if (expected != null) { 200 for (Iterator iterator1 = expected.iterator(); iterator1.hasNext();) { 201 QName expectedQName = (QName ) 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 e) { 217 } 219 } 221 } 222 | Popular Tags |