org.apache.cxf.common.jaxb.JAXBUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cxf-bundle-minimal Show documentation
Show all versions of cxf-bundle-minimal Show documentation
Apache CXF Minimal Bundle Jar
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.common.jaxb;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.attachment.AttachmentMarshaller;
import javax.xml.bind.attachment.AttachmentUnmarshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.apache.cxf.common.classloader.ClassLoaderUtils;
import org.apache.cxf.common.util.ASMHelper;
import org.apache.cxf.common.util.ASMHelper.ClassWriter;
import org.apache.cxf.common.util.ASMHelper.FieldVisitor;
import org.apache.cxf.common.util.ASMHelper.Label;
import org.apache.cxf.common.util.ASMHelper.MethodVisitor;
import org.apache.cxf.common.util.ASMHelper.Opcodes;
import org.apache.cxf.common.util.CachedClass;
import org.apache.cxf.common.util.PackageUtils;
import org.apache.cxf.common.util.ReflectionInvokationHandler;
import org.apache.cxf.common.util.ReflectionInvokationHandler.WrapReturn;
import org.apache.cxf.common.util.ReflectionUtil;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.common.util.SystemPropertyAction;
import org.apache.cxf.common.xmlschema.SchemaCollection;
import org.apache.cxf.helpers.JavaUtils;
public final class JAXBUtils {
public enum IdentifierType {
CLASS,
INTERFACE,
GETTER,
SETTER,
VARIABLE,
CONSTANT
};
public static final String JAXB_URI = "http://java.sun.com/xml/ns/jaxb";
private static final char[] XML_NAME_PUNCTUATION_CHARS = new char[] {
/* hyphen */ '\u002D',
/* period */ '\u002E',
/* colon */'\u003A',
/* dot */ '\u00B7',
/* greek ano teleia */ '\u0387',
/* arabic end of ayah */ '\u06DD',
/* arabic start of rub el hizb */'\u06DE',
/* underscore */ '\u005F',
};
private static final String XML_NAME_PUNCTUATION_STRING = new String(XML_NAME_PUNCTUATION_CHARS);
private static final Map BUILTIN_DATATYPES_MAP;
private static final Map> HOLDER_TYPES_MAP;
private static ClassLoader jaxbXjcLoader;
static {
BUILTIN_DATATYPES_MAP = new HashMap();
BUILTIN_DATATYPES_MAP.put("string", "java.lang.String");
BUILTIN_DATATYPES_MAP.put("integer", "java.math.BigInteger");
BUILTIN_DATATYPES_MAP.put("int", "int");
BUILTIN_DATATYPES_MAP.put("long", "long");
BUILTIN_DATATYPES_MAP.put("short", "short");
BUILTIN_DATATYPES_MAP.put("decimal", "java.math.BigDecimal");
BUILTIN_DATATYPES_MAP.put("float", "float");
BUILTIN_DATATYPES_MAP.put("double", "double");
BUILTIN_DATATYPES_MAP.put("boolean", "boolean");
BUILTIN_DATATYPES_MAP.put("byte", "byte");
BUILTIN_DATATYPES_MAP.put("QName", "javax.xml.namespace.QName");
BUILTIN_DATATYPES_MAP.put("dateTime", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("base64Binary", "byte[]");
BUILTIN_DATATYPES_MAP.put("hexBinary", "byte[]");
BUILTIN_DATATYPES_MAP.put("unsignedInt", "long");
BUILTIN_DATATYPES_MAP.put("unsignedShort", "short");
BUILTIN_DATATYPES_MAP.put("unsignedByte", "byte");
BUILTIN_DATATYPES_MAP.put("time", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("date", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("gYear", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("gYearMonth", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("gMonth", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("gMonthDay", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("gDay", "javax.xml.datatype.XMLGregorianCalendar");
BUILTIN_DATATYPES_MAP.put("duration", "javax.xml.datatype.Duration");
BUILTIN_DATATYPES_MAP.put("NOTATION", "javax.xml.namespace.QName");
BUILTIN_DATATYPES_MAP.put("string", "java.lang.String");
HOLDER_TYPES_MAP = new HashMap>();
HOLDER_TYPES_MAP.put("int", java.lang.Integer.class);
HOLDER_TYPES_MAP.put("long", java.lang.Long.class);
HOLDER_TYPES_MAP.put("short", java.lang.Short.class);
HOLDER_TYPES_MAP.put("float", java.lang.Float.class);
HOLDER_TYPES_MAP.put("double", java.lang.Double.class);
HOLDER_TYPES_MAP.put("boolean", java.lang.Boolean.class);
HOLDER_TYPES_MAP.put("byte", java.lang.Byte.class);
}
/**
* prevents instantiation
*
*/
private JAXBUtils() {
}
public static String builtInTypeToJavaType(String type) {
return BUILTIN_DATATYPES_MAP.get(type);
}
public static Class> holderClass(String type) {
return HOLDER_TYPES_MAP.get(type);
}
/**
* Checks if the specified word is a Java keyword (as defined in JavaUtils).
*
* @param word the word to check.
* @return true if the word is a keyword.
* @see org.apache.cxf.helpers.JavaUtils
*/
protected static boolean isJavaKeyword(String word) {
return JavaUtils.isJavaKeyword(word);
}
/**
* Generates a Java package name from a URI according to the
* algorithm outlined in JAXB 2.0.
*
* @param namespaceURI the namespace URI.
* @return the package name.
*/
public static String namespaceURIToPackage(String namespaceURI) {
try {
return nameSpaceURIToPackage(new URI(namespaceURI));
} catch (URISyntaxException ex) {
return null;
}
}
/**
* Generates a Java package name from a URI according to the
* algorithm outlined in Appendix D of JAXB (2.0+).
*
* @param uri the namespace URI.
* @return the package name.
*/
public static String nameSpaceURIToPackage(URI uri) {
StringBuilder packageName = new StringBuilder();
String authority = uri.getAuthority();
String scheme = uri.getScheme();
if (authority == null && "urn".equals(scheme)) {
authority = uri.getSchemeSpecificPart();
}
if (null != authority && !"".equals(authority)) {
if ("urn".equals(scheme)) {
packageName.append(authority);
/* JAXB 2.2 D.5.1, Rule #5 */
for (int i = 0; i < packageName.length(); i++) {
if (packageName.charAt(i) == '-') {
packageName.setCharAt(i, '.');
}
}
authority = packageName.toString();
packageName.setLength(0);
StringTokenizer st = new StringTokenizer(authority, ":");
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (packageName.length() > 0) {
packageName.insert(0, ".");
packageName.insert(0, normalizePackageNamePart(token));
} else {
packageName.insert(0, token);
}
}
authority = packageName.toString();
packageName.setLength(0);
}
StringTokenizer st = new StringTokenizer(authority, ".");
if (st.hasMoreTokens()) {
String token = null;
while (st.hasMoreTokens()) {
token = st.nextToken();
if (packageName.length() == 0) {
if ("www".equals(token)) {
continue;
}
} else {
packageName.insert(0, ".");
}
packageName.insert(0, normalizePackageNamePart(token));
}
}
if (!("http".equalsIgnoreCase(scheme) || "urn".equalsIgnoreCase(scheme))) {
packageName.insert(0, ".");
packageName.insert(0, normalizePackageNamePart(scheme));
}
}
String path = uri.getPath();
if (path == null) {
path = "";
}
/* JAXB 2.2 D.5.1 Rule 2 - remove trailing .??, .???, or .html only. */
int index = path.lastIndexOf('.');
if (index < 0) {
index = path.length();
} else {
String ending = path.substring(index + 1);
if (ending.length() < 2 || (ending.length() > 3
&& !"html".equalsIgnoreCase(ending))) {
index = path.length();
}
}
StringTokenizer st = new StringTokenizer(path.substring(0, index), "/");
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (packageName.length() > 0) {
packageName.append('.');
}
packageName.append(normalizePackageNamePart(token));
}
return packageName.toString();
}
private static String normalizePackageNamePart(String name) {
StringBuilder sname = new StringBuilder(name.toLowerCase());
for (int i = 0; i < sname.length(); i++) {
sname.setCharAt(i, Character.toLowerCase(sname.charAt(i)));
}
for (int i = 0; i < sname.length(); i++) {
if (!Character.isJavaIdentifierPart(sname.charAt(i))) {
sname.setCharAt(i, '_');
}
}
if (isJavaKeyword(sname.toString())) {
sname.insert(0, '_');
}
if (!Character.isJavaIdentifierStart(sname.charAt(0))) {
sname.insert(0, '_');
}
return sname.toString();
}
/**
* Converts an XML name to a Java identifier according to the mapping
* algorithm outlined in the JAXB specification
*
* @param name the XML name
* @return the Java identifier
*/
public static String nameToIdentifier(String name, IdentifierType type) {
if (null == name || name.length() == 0) {
return name;
}
// algorithm will not change an XML name that is already a legal and
// conventional (!) Java class, method, or constant identifier
boolean legalIdentifier = false;
StringBuilder buf = new StringBuilder(name);
legalIdentifier = Character.isJavaIdentifierStart(buf.charAt(0));
for (int i = 1; i < name.length() && legalIdentifier; i++) {
legalIdentifier = legalIdentifier && Character.isJavaIdentifierPart(buf.charAt(i));
}
boolean conventionalIdentifier = isConventionalIdentifier(buf, type);
if (legalIdentifier && conventionalIdentifier) {
if (JAXBUtils.isJavaKeyword(name) && type == IdentifierType.VARIABLE) {
name = normalizePackageNamePart(name);
}
return name;
}
// split into words
List words = new ArrayList();
StringTokenizer st = new StringTokenizer(name, XML_NAME_PUNCTUATION_STRING);
while (st.hasMoreTokens()) {
words.add(st.nextToken());
}
for (int i = 0; i < words.size(); i++) {
splitWord(words, i);
}
return makeConventionalIdentifier(words, type);
}
private static void splitWord(List words, int listIndex) {
String word = words.get(listIndex);
if (word.length() <= 1) {
return;
}
int index = listIndex + 1;
StringBuilder sword = new StringBuilder(word);
int first = 0;
char firstChar = sword.charAt(first);
if (Character.isLowerCase(firstChar)) {
sword.setCharAt(first, Character.toUpperCase(firstChar));
}
int i = 1;
while (i < sword.length()) {
if (Character.isDigit(firstChar)) {
while (i < sword.length() && Character.isDigit(sword.charAt(i))) {
i++;
}
} else if (isCasedLetter(firstChar)) {
boolean previousIsLower = Character.isLowerCase(firstChar);
while (i < sword.length() && isCasedLetter(sword.charAt(i))) {
if (Character.isUpperCase(sword.charAt(i)) && previousIsLower) {
break;
}
previousIsLower = Character.isLowerCase(sword.charAt(i));
i++;
}
} else {
// first must be a mark or an uncased letter
while (i < sword.length() && (isMark(sword.charAt(i)) || !isCasedLetter(sword.charAt(i)))) {
i++;
}
}
// characters from first to i are all either
// * digits
// * upper or lower case letters, with only the first one an upper
// * uncased letters or marks
String newWord = sword.substring(first, i);
words.add(index, newWord);
index++;
if (i >= sword.length()) {
break;
} else {
first = i;
firstChar = sword.charAt(first);
}
}
if (index > (listIndex + 1)) {
words.remove(listIndex);
}
}
private static boolean isMark(char c) {
return Character.isJavaIdentifierPart(c) && !Character.isLetter(c) && !Character.isDigit(c);
}
private static boolean isCasedLetter(char c) {
return Character.isUpperCase(c) || Character.isLowerCase(c);
}
private static boolean isConventionalIdentifier(StringBuilder buf, IdentifierType type) {
if (null == buf || buf.length() == 0) {
return false;
}
boolean result = false;
if (IdentifierType.CONSTANT == type) {
for (int i = 0; i < buf.length(); i++) {
if (Character.isLowerCase(buf.charAt(i))) {
return false;
}
}
result = true;
} else if (IdentifierType.VARIABLE == type) {
result = Character.isLowerCase(buf.charAt(0));
} else {
int pos = 3;
if (IdentifierType.GETTER == type
&& !(buf.length() >= pos
&& "get".equals(buf.subSequence(0, 3)))) {
return false;
} else if (IdentifierType.SETTER == type
&& !(buf.length() >= pos && "set".equals(buf.subSequence(0, 3)))) {
return false;
} else {
pos = 0;
}
result = Character.isUpperCase(buf.charAt(pos));
}
return result;
}
private static String makeConventionalIdentifier(List words, IdentifierType type) {
StringBuilder buf = new StringBuilder();
boolean firstWord = true;
if (IdentifierType.GETTER == type) {
buf.append("get");
} else if (IdentifierType.SETTER == type) {
buf.append("set");
}
for (String w : words) {
int l = buf.length();
if (l > 0 && IdentifierType.CONSTANT == type) {
buf.append('_');
l++;
}
buf.append(w);
if (IdentifierType.CONSTANT == type) {
for (int i = l; i < buf.length(); i++) {
if (Character.isLowerCase(buf.charAt(i))) {
buf.setCharAt(i, Character.toUpperCase(buf.charAt(i)));
}
}
} else if (IdentifierType.VARIABLE == type) {
if (firstWord && Character.isUpperCase(buf.charAt(l))) {
buf.setCharAt(l, Character.toLowerCase(buf.charAt(l)));
}
} else {
if (firstWord && Character.isLowerCase(buf.charAt(l))) {
buf.setCharAt(l, Character.toUpperCase(buf.charAt(l)));
}
}
firstWord = false;
}
return buf.toString();
}
public static Class> getValidClass(Class> cls) {
if (cls.isEnum()) {
return cls;
}
if (cls.isArray()) {
return cls;
}
if (cls == Object.class || cls == String.class
|| "javax.xml.ws.Holder".equals(cls.getName())) {
cls = null;
} else if (cls.isPrimitive() || cls.isAnnotation()) {
cls = null;
} else if (cls.isInterface()) {
return cls;
}
if (cls != null) {
if (cls.getName().equals("javax.xml.ws.wsaddressing.W3CEndpointReference")) {
return cls;
}
Constructor> cons = ReflectionUtil.getDeclaredConstructor(cls);
if (cons == null) {
cons = ReflectionUtil.getConstructor(cls);
}
if (cons == null) {
cls = null;
}
}
return cls;
}
private static synchronized ClassLoader getXJCClassLoader() {
if (jaxbXjcLoader == null) {
try {
Class.forName("com.sun.tools.internal.xjc.api.XJC");
jaxbXjcLoader = ClassLoader.getSystemClassLoader();
} catch (Exception t2) {
//couldn't find either, probably cause tools.jar isn't on
//the classpath. Let's see if we can find the tools jar
String s = SystemPropertyAction.getProperty("java.home");
if (!StringUtils.isEmpty(s)) {
File home = new File(s);
File jar = new File(home, "lib/tools.jar");
if (!jar.exists()) {
jar = new File(home, "../lib/tools.jar");
}
if (jar.exists()) {
try {
jaxbXjcLoader = new URLClassLoader(new URL[] {jar.toURI().toURL()});
Class.forName("com.sun.tools.internal.xjc.api.XJC", false, jaxbXjcLoader);
} catch (Exception e) {
jaxbXjcLoader = null;
}
}
}
}
}
return jaxbXjcLoader;
}
public static Object setNamespaceMapper(final Map nspref,
Marshaller marshaller) throws PropertyException {
Object mapper = createNamespaceWrapper(marshaller.getClass(), nspref);
if (marshaller.getClass().getName().contains(".internal.")) {
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
mapper);
} else if (marshaller.getClass().getName().contains("com.sun")) {
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
mapper);
} else if (marshaller.getClass().getName().contains("eclipse")) {
marshaller.setProperty("eclipselink.namespace-prefix-mapper",
mapper);
}
return mapper;
}
public static BridgeWrapper createBridge(Set> ctxClasses,
QName qname,
Class> refcls,
Annotation anns[]) throws JAXBException {
try {
Class> cls;
Class> refClass;
String pkg = "com.sun.xml.bind.";
try {
cls = Class.forName("com.sun.xml.bind.api.JAXBRIContext");
refClass = Class.forName(pkg + "api.TypeReference");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
cls = Class.forName("com.sun.xml.internal.bind.api.JAXBRIContext", true, getXJCClassLoader());
pkg = "com.sun.xml.internal.bind.";
refClass = Class.forName(pkg + "api.TypeReference", true, getXJCClassLoader());
}
Object ref = refClass.getConstructor(QName.class,
Type.class,
anns.getClass()).newInstance(qname, refcls, anns);
List