Coverage Report - uk.co.badgersinfoil.asxsd.TypeNameGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
TypeNameGenerator
33% 
31% 
6
 
 1  
 /*
 2  
  * Copyright (c) 2006-2007 David Holroyd
 3  
  */
 4  
 
 5  
 package uk.co.badgersinfoil.asxsd;
 6  
 
 7  
 import java.util.HashMap;
 8  
 import java.util.Map;
 9  
 import javax.xml.namespace.QName;
 10  
 import org.eclipse.xsd.XSDComplexTypeDefinition;
 11  
 import org.eclipse.xsd.XSDConcreteComponent;
 12  
 import org.eclipse.xsd.XSDElementDeclaration;
 13  
 import org.eclipse.xsd.XSDSchema;
 14  
 import org.eclipse.xsd.XSDTypeDefinition;
 15  
 
 16  
 
 17  31
 public class TypeNameGenerator {
 18  31
         private Map names = new HashMap();
 19  
 
 20  
         public String typeName(XSDTypeDefinition type) {
 21  34
                 if (type instanceof XSDComplexTypeDefinition) {
 22  34
                         if (type.getName() == null) {
 23  0
                                 throw new IllegalArgumentException("Can't get type name for anonymous complex type: "+type+", at "+XSDUtils.path(type));
 24  
                         }
 25  34
                         return getNameFor(new QName(type.getTargetNamespace(), type.getName()));
 26  
                 }
 27  0
                 throw new IllegalArgumentException("unhandled type: "+type.getClass().getName());
 28  
         }
 29  
 
 30  
         private String getNameFor(QName name) {
 31  34
                 String existing = (String)names.get(name);
 32  34
                 if (existing != null) {
 33  18
                         return existing;
 34  
                 }
 35  16
                 String pkg = StringUtils.toPackageName(name.getNamespaceURI());
 36  16
                 String clazz = classNameFor(name.getLocalPart());
 37  16
                 String baseName = pkg + "." + clazz;
 38  16
                 int count = 1;
 39  16
                 while (names.containsValue(baseName)) {
 40  0
                         String mangled = baseName + "_" + (count++);
 41  0
                         if (!names.containsValue(baseName)) {
 42  0
                                 names.put(name, baseName);
 43  0
                                 return mangled;
 44  
                         }
 45  0
                         if (count > 5000) {
 46  
                                 // assume there's a bug,
 47  0
                                 throw new Error("Couldn't assign a unique name after 5000 iterations");
 48  
                         }
 49  0
                 }
 50  16
                 names.put(name, baseName);
 51  16
                 return baseName;
 52  
         }
 53  
 
 54  
         private String classNameFor(String localPart) {
 55  16
                 return StringUtils.sanitize(localPart);
 56  
         }
 57  
 
 58  
 
 59  
         public String getNameFor(XSDElementDeclaration elementDecl) {
 60  0
                 elementDecl = elementDecl.getResolvedElementDeclaration();
 61  0
                 if (elementDecl.getName() == null) {
 62  0
                         throw new IllegalArgumentException("Element declaration has no name "+XSDUtils.path(elementDecl));
 63  
                 }
 64  0
                 StringBuffer result = new StringBuffer(elementDecl.getName());
 65  0
                 XSDConcreteComponent container = elementDecl;
 66  0
                 String namespace = elementDecl.getTargetNamespace();
 67  0
                 while ((container = container.getContainer()) != null) {
 68  0
                         if (container instanceof XSDSchema) {
 69  
                                 // we're at the top level
 70  0
                                 break;
 71  
                         }
 72  0
                         if (container instanceof XSDElementDeclaration) {
 73  0
                                 XSDElementDeclaration elem = (XSDElementDeclaration)container;
 74  0
                                 result.insert(0, elem.getName());
 75  0
                                 namespace = elem.getTargetNamespace();
 76  
                         }
 77  0
                         if (container instanceof XSDComplexTypeDefinition) {
 78  0
                                 XSDComplexTypeDefinition complexType = (XSDComplexTypeDefinition)container;
 79  0
                                 if (complexType.getName() != null) {
 80  0
                                         result.insert(0, complexType.getName());
 81  
                                         // we're at the top level
 82  0
                                         break;
 83  
                                 }
 84  0
                         }
 85  
                 }
 86  0
                 result.insert(0, "Element_");
 87  0
                 if (namespace != null) {
 88  0
                         result.insert(0, ".");
 89  0
                         result.insert(0, StringUtils.toPackageName(namespace));
 90  
                 }
 91  0
                 return result.toString();
 92  
         }
 93  
 }