1 19 20 package org.apache.cayenne.project.validator; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.StringTokenizer ; 25 26 32 public class MappingNamesHelper { 33 34 static final Collection RESERVED_JAVA_KEYWORDS = Arrays.asList(new Object [] { 36 "abstract", "assert", "default", "if", "private", "this", "boolean", "do", 37 "implements", "protected", "throw", "break", "double", "import", "public", 38 "throws", "byte", "else", "instanceof", "return", "transient", "case", 39 "extends", "int", "short", "try", "catch", "final", "interface", "static", 40 "void", "char", "finally", "long", "strictfp", "volatile", "class", "float", 41 "native", "super", "while", "const", "for", "new", "switch", "continue", 42 "goto", "package", "synchronized" 43 }); 44 45 public boolean isReservedJavaKeyword(String word) 46 { 47 return RESERVED_JAVA_KEYWORDS.contains(word); 48 } 49 50 static final Collection INVALID_JAVA_PROPERTIES = Arrays.asList(new Object [] { 53 "class", "committedSnapshot", "currentSnapshot", "dataContext", "objectId", 54 "persistenceState", "snapshotVersion" 55 }); 56 57 static final MappingNamesHelper sharedInstance = new MappingNamesHelper(); 58 59 62 public static MappingNamesHelper getInstance() { 63 return sharedInstance; 64 } 65 66 70 String invalidCharsInDbPathComponent(String dbPathComponent) { 71 return (dbPathComponent.indexOf('.') >= 0) ? "." : null; 72 } 73 74 77 String invalidCharsInObjPathComponent(String objPathComponent) { 78 String invalidChars = validateJavaIdentifier(objPathComponent, ""); 79 return (invalidChars.length() > 0) ? invalidChars : null; 80 } 81 82 String invalidCharsInJavaClassName(String javaClassName) { 83 if (javaClassName == null) { 84 return null; 85 } 86 87 String invalidChars = ""; 88 89 StringTokenizer toks = new StringTokenizer (javaClassName, "."); 90 while (toks.hasMoreTokens()) { 91 invalidChars = validateJavaIdentifier(toks.nextToken(), invalidChars); 92 } 93 94 return (invalidChars.length() > 0) ? invalidChars : null; 95 } 96 97 boolean invalidDataObjectClass(String dataObjectClassFQN) { 98 if (dataObjectClassFQN == null) { 99 return true; 100 } 101 102 StringTokenizer toks = new StringTokenizer (dataObjectClassFQN, "."); 103 while (toks.hasMoreTokens()) { 104 if (RESERVED_JAVA_KEYWORDS.contains(toks.nextToken())) { 105 return true; 106 } 107 } 108 109 return false; 110 } 111 112 private String validateJavaIdentifier(String id, String invalidChars) { 113 116 int len = (id != null) ? id.length() : 0; 117 if (len == 0) { 118 return invalidChars; 119 } 120 121 if (!Character.isJavaIdentifierStart(id.charAt(0))) { 122 if (invalidChars.indexOf(id.charAt(0)) < 0) { 123 invalidChars = invalidChars + id.charAt(0); 124 } 125 } 126 127 for (int i = 1; i < len; i++) { 128 129 if (!Character.isJavaIdentifierPart(id.charAt(i))) { 130 if (invalidChars.indexOf(id.charAt(i)) < 0) { 131 invalidChars = invalidChars + id.charAt(i); 132 } 133 } 134 } 135 136 return invalidChars; 137 } 138 139 144 boolean invalidDataObjectProperty(String dataObjectProperty) { 145 return dataObjectProperty == null 146 || INVALID_JAVA_PROPERTIES.contains(dataObjectProperty); 147 } 148 } 149 | Popular Tags |