001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.description.java2wsdl;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
023: import org.apache.axis2.description.java2wsdl.NamespaceGenerator;
024: import org.apache.axis2.description.java2wsdl.DefaultNamespaceGenerator;
025:
026: import javax.xml.transform.Source;
027: import javax.xml.transform.Templates;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.stream.StreamResult;
031: import javax.xml.transform.stream.StreamSource;
032: import java.io.ByteArrayInputStream;
033: import java.io.ByteArrayOutputStream;
034: import java.io.OutputStream;
035:
036: public class Java2WSDLUtils {
037:
038: public static final String HTTP = "http://";
039:
040: public static final char PACKAGE_CLASS_DELIMITER = '.';
041:
042: public static final String SCHEMA_NAMESPACE_EXTN = "/xsd";
043:
044: private static NamespaceGenerator defaultNsGenerator = new DefaultNamespaceGenerator();
045:
046: /**
047: * check the entry for a URL. This is a simple check and need to be improved
048: *
049: * @param entry
050: */
051:
052: public static boolean isURL(String entry) {
053: return entry.startsWith("http://");
054: }
055:
056: /**
057: * A method to strip the fully qualified className to a simple classname
058: *
059: * @param qualifiedName
060: */
061: public static String getSimpleClassName(String qualifiedName) {
062: int index = qualifiedName.lastIndexOf(".");
063: if (index > 0) {
064: return qualifiedName.substring(index + 1, qualifiedName
065: .length());
066: }
067: return qualifiedName;
068: }
069:
070: public static StringBuffer namespaceFromClassName(String className,
071: ClassLoader classLoader) throws Exception {
072: return namespaceFromClassName(className, classLoader,
073: defaultNsGenerator);
074: }
075:
076: public static StringBuffer namespaceFromClassName(String className,
077: ClassLoader classLoader, NamespaceGenerator nsGen)
078: throws Exception {
079: Class clazz = Class.forName(className, true, classLoader);
080: Package pkg = clazz.getPackage();
081: String name;
082:
083: if (pkg != null)
084: name = pkg.getName();
085: else
086: name = packageNameFromClass(className);
087:
088: return nsGen.namespaceFromPackageName(name);
089: }
090:
091: public static StringBuffer schemaNamespaceFromClassName(
092: String packageName, ClassLoader loader) throws Exception {
093: return schemaNamespaceFromClassName(packageName, loader,
094: defaultNsGenerator);
095: }
096:
097: public static StringBuffer schemaNamespaceFromClassName(
098: String packageName, ClassLoader loader,
099: NamespaceGenerator nsGen) throws Exception {
100: StringBuffer stringBuffer = namespaceFromClassName(packageName,
101: loader, nsGen);
102: if (stringBuffer.length() == 0) {
103: stringBuffer
104: .append(Java2WSDLConstants.DEFAULT_TARGET_NAMESPACE);
105: }
106: // stringBuffer.append(SCHEMA_NAMESPACE_EXTN);
107: return stringBuffer;
108: }
109:
110: public static StringBuffer targetNamespaceFromClassName(
111: String packageName, ClassLoader loader,
112: NamespaceGenerator nsGen) throws Exception {
113: StringBuffer stringBuffer = namespaceFromClassName(packageName,
114: loader, nsGen);
115: if (stringBuffer.length() == 0) {
116: stringBuffer
117: .append(Java2WSDLConstants.DEFAULT_TARGET_NAMESPACE);
118: }
119: return stringBuffer;
120: }
121:
122: public static String getPackageName(String className,
123: ClassLoader classLoader) throws Exception {
124: Class clazz = Class.forName(className, true, classLoader);
125: Package pkg = clazz.getPackage();
126: String name;
127:
128: if (pkg != null)
129: name = pkg.getName();
130: else
131: name = packageNameFromClass(className);
132: return name;
133: }
134:
135: protected static String packageNameFromClass(String name) {
136: String ret = "";
137: int lastDot = name.lastIndexOf('.');
138:
139: if (lastDot != -1)
140: ret = name.substring(0, lastDot);
141: return ret;
142: }
143: }
|