1 56 package org.objectstyle.cayenne.project.validator; 57 58 import java.util.Arrays ; 59 import java.util.Collection ; 60 import java.util.StringTokenizer ; 61 62 68 public class MappingNamesHelper { 69 70 static final Collection RESERVED_JAVA_KEYWORDS = Arrays.asList(new Object [] { 72 "abstract", "assert", "default", "if", "private", "this", "boolean", "do", 73 "implements", "protected", "throw", "break", "double", "import", "public", 74 "throws", "byte", "else", "instanceof", "return", "transient", "case", 75 "extends", "int", "short", "try", "catch", "final", "interface", "static", 76 "void", "char", "finally", "long", "strictfp", "volatile", "class", "float", 77 "native", "super", "while", "const", "for", "new", "switch", "continue", 78 "goto", "package", "synchronized" 79 }); 80 81 public boolean isReservedJavaKeyword(String word) 82 { 83 return RESERVED_JAVA_KEYWORDS.contains(word); 84 } 85 86 static final Collection INVALID_JAVA_PROPERTIES = Arrays.asList(new Object [] { 89 "class", "committedSnapshot", "currentSnapshot", "dataContext", "objectId", 90 "persistenceState", "snapshotVersion" 91 }); 92 93 static final MappingNamesHelper sharedInstance = new MappingNamesHelper(); 94 95 98 public static MappingNamesHelper getInstance() { 99 return sharedInstance; 100 } 101 102 106 String invalidCharsInDbPathComponent(String dbPathComponent) { 107 return (dbPathComponent.indexOf('.') >= 0) ? "." : null; 108 } 109 110 113 String invalidCharsInObjPathComponent(String objPathComponent) { 114 String invalidChars = validateJavaIdentifier(objPathComponent, ""); 115 return (invalidChars.length() > 0) ? invalidChars : null; 116 } 117 118 String invalidCharsInJavaClassName(String javaClassName) { 119 if (javaClassName == null) { 120 return null; 121 } 122 123 String invalidChars = ""; 124 125 StringTokenizer toks = new StringTokenizer (javaClassName, "."); 126 while (toks.hasMoreTokens()) { 127 invalidChars = validateJavaIdentifier(toks.nextToken(), invalidChars); 128 } 129 130 return (invalidChars.length() > 0) ? invalidChars : null; 131 } 132 133 boolean invalidDataObjectClass(String dataObjectClassFQN) { 134 if (dataObjectClassFQN == null) { 135 return true; 136 } 137 138 StringTokenizer toks = new StringTokenizer (dataObjectClassFQN, "."); 139 while (toks.hasMoreTokens()) { 140 if (RESERVED_JAVA_KEYWORDS.contains(toks.nextToken())) { 141 return true; 142 } 143 } 144 145 return false; 146 } 147 148 private String validateJavaIdentifier(String id, String invalidChars) { 149 152 int len = (id != null) ? id.length() : 0; 153 if (len == 0) { 154 return invalidChars; 155 } 156 157 if (!Character.isJavaIdentifierStart(id.charAt(0))) { 158 if (invalidChars.indexOf(id.charAt(0)) < 0) { 159 invalidChars = invalidChars + id.charAt(0); 160 } 161 } 162 163 for (int i = 1; i < len; i++) { 164 165 if (!Character.isJavaIdentifierPart(id.charAt(i))) { 166 if (invalidChars.indexOf(id.charAt(i)) < 0) { 167 invalidChars = invalidChars + id.charAt(i); 168 } 169 } 170 } 171 172 return invalidChars; 173 } 174 175 180 boolean invalidDataObjectProperty(String dataObjectProperty) { 181 return dataObjectProperty == null 182 || INVALID_JAVA_PROPERTIES.contains(dataObjectProperty); 183 } 184 } | Popular Tags |