| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| VariableNameGenerator |
|
| 3.3333333333333335;3.333 |
| 1 | /* |
|
| 2 | * Copyright (c) 2006-2007 David Holroyd |
|
| 3 | */ |
|
| 4 | ||
| 5 | package uk.co.badgersinfoil.asxsd; |
|
| 6 | ||
| 7 | import java.util.HashSet; |
|
| 8 | import java.util.Set; |
|
| 9 | import org.eclipse.xsd.XSDConcreteComponent; |
|
| 10 | import org.eclipse.xsd.XSDNamedComponent; |
|
| 11 | ||
| 12 | ||
| 13 | 20 | public class VariableNameGenerator { |
| 14 | ||
| 15 | private static final Set KEYWORDS; |
|
| 16 | static { |
|
| 17 | 1 | KEYWORDS = new HashSet(); |
| 18 | 1 | KEYWORDS.add("return"); |
| 19 | 1 | KEYWORDS.add("if"); |
| 20 | 1 | KEYWORDS.add("else"); |
| 21 | 1 | KEYWORDS.add("public"); |
| 22 | 1 | KEYWORDS.add("private"); |
| 23 | 1 | KEYWORDS.add("protected"); |
| 24 | 1 | } |
| 25 | ||
| 26 | public String fieldName(XSDConcreteComponent component) { |
|
| 27 | 0 | if (component instanceof XSDNamedComponent) { |
| 28 | 0 | return fieldName((XSDNamedComponent)component); |
| 29 | } |
|
| 30 | 0 | throw new IllegalArgumentException("Components of type " + component.getClass().getName() + " not supported"); |
| 31 | } |
|
| 32 | ||
| 33 | /** |
|
| 34 | * returns the name of the AS field which should hold values of the |
|
| 35 | * given element declaration. |
|
| 36 | */ |
|
| 37 | private String fieldName(XSDNamedComponent named) { |
|
| 38 | 0 | if (named.getName() == null) { |
| 39 | 0 | throw new IllegalArgumentException("Component has no name: " + XSDUtils.path(named)); |
| 40 | } |
|
| 41 | 0 | return fieldName(named.getName()); |
| 42 | } |
|
| 43 | ||
| 44 | public String fieldName(String name) { |
|
| 45 | // TODO: do we need to ensure unique names? |
|
| 46 | 319 | if (KEYWORDS.contains(name)) { |
| 47 | 0 | return "_" + name; |
| 48 | } |
|
| 49 | 319 | return StringUtils.sanitize(name); |
| 50 | } |
|
| 51 | ||
| 52 | ||
| 53 | // /** |
|
| 54 | // * create a field name based on the given attribute declaration |
|
| 55 | // */ |
|
| 56 | // public String fieldName(XSDAttributeDeclaration attrDecl) { |
|
| 57 | // // TODO: do we need to ensure unique names? |
|
| 58 | // if (KEYWORDS.contains(attrDecl.getName())) { |
|
| 59 | // return "_" + attrDecl.getName(); |
|
| 60 | // } |
|
| 61 | // return StringUtils.sanitize(attrDecl.getName()); |
|
| 62 | // } |
|
| 63 | } |