/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed 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.muse.tools.generator.synthesizer;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.muse.tools.generator.util.Capability;
import org.apache.muse.tools.inspector.JavaMethod;
import org.apache.muse.tools.inspector.JavaProperty;
import org.apache.muse.util.ReflectUtils;
/**
* Wraps a <code>Capability</code> during the synthesis phase
* to keep track of imports that are needed for the implementations
* and the names of the implementing classes.
*
* @author Andrew Eberbach (aeberbac)
*/
public class ClassInfo {
private static final String JAVA_CLASS_NAME = "MyCapability";
String _packageName = null;
String _classShortName = null;
Capability _capability = null;
Set _imports = new HashSet();
Map _conflicts = new HashMap();
private String _classFullName;
public ClassInfo(Capability capability) {
_capability = capability;
String implementingClass = _capability.getImplementingClass();
if(implementingClass == null) {
_packageName = getPackageName(capability.getURI());
_classShortName = JAVA_CLASS_NAME;
_classFullName = _packageName + "." + _classShortName;
_capability.setImplementingClass(_classFullName);
} else {
_classFullName = implementingClass;
_classShortName = ReflectUtils.getShortName(_classFullName);
_packageName = ReflectUtils.getPackageName(_classFullName);
}
findImports();
}
private void findImports() {
for(Iterator i = _capability.getOperations().iterator(); i.hasNext();) {
JavaMethod method = (JavaMethod)i.next();
addImport(method.getReturnType());
Class[] parameterTypes = method.getParameterTypes();
for(int j = 0; j < parameterTypes.length; j++) {
addImport(parameterTypes[j]);
}
}
for(Iterator i = _capability.getProperties().iterator(); i.hasNext(); ) {
JavaProperty property = (JavaProperty)i.next();
addImport(property.getJavaType());
}
}
public void addImport(Class theClass) {
if(theClass.isArray()) {
theClass = ReflectUtils.getClassFromArrayClass(theClass);
}
String shortName = ReflectUtils.getShortName(theClass);
Set matches = (Set)_conflicts.get(shortName);
if(matches == null) {
matches = new HashSet();
matches.add(theClass);
} else {
if(!matches.contains(theClass)) {
matches.add(theClass);
}
}
_conflicts.put(shortName, matches);
_imports.add(theClass);
}
public String getClassShortName() {
return _classShortName;
}
public String getPackageName() {
return _packageName;
}
public String getClassFullName() {
return _classFullName;
}
public Set getImports() {
return _imports;
}
public static String getPackageName(String uri) {
try {
String result = new String();
URL url = new URL(uri);
String[] hostParts = url.getHost().split("\\.");
for (int i = hostParts.length - 1; i >= 0; i--) {
if (hostParts[i].matches("^[^a-zA-Z_].*$")) {
hostParts[i] = "_" + hostParts[i];
}
result += hostParts[i] + ".";
}
String[] fileParts = url.getPath().split("/");
for (int i = 0; i < fileParts.length; i++) {
if (fileParts[i].matches("^[^a-zA-Z_].*$")) {
fileParts[i] = "_" + fileParts[i];
}
if (fileParts[i].length() != 0) {
result += fileParts[i] + ((i != fileParts.length -1)?".":"");
}
}
result = result.replaceAll("[^.a-zA-Z0-9_]+", "_");
return result;
} catch (Exception e) {
return "org.tempuri";
}
}
public boolean hasConflict(Class theClass) {
Integer count = (Integer)_conflicts.get(theClass);
return (count != null) && (count.intValue() > 1);
}
public Capability getCapability() {
return _capability;
}
}