Package org.apache.muse.tools.generator.synthesizer

Source Code of org.apache.muse.tools.generator.synthesizer.ClassInfo

/*
* 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.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 {

  Capability _capability = null;
 
  Set _imports = new HashSet();

  Map _conflicts = new HashMap();

  public ClassInfo(Capability capability) {     
    _capability = capability;   
    findImports();
  }

  public ClassInfo() {
    // do nothing
  }

  public void setCapability(Capability capability) {
    _capability = capability;   
    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 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;
  }

  public boolean needsInitializer() {
    for(Iterator i = _capability.getProperties().iterator(); i.hasNext(); ) {
      JavaProperty next = (JavaProperty)i.next();
      if(!next.getJavaType().isPrimitive()) {
        return true;
      }
    }
   
    return _capability.isEmpty();
  }

  public void addImports(Set set) {
    for(Iterator i=set.iterator(); i.hasNext(); ) {
      addImport((Class) i.next());
    }
  }
}
TOP

Related Classes of org.apache.muse.tools.generator.synthesizer.ClassInfo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.