KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > validator > MappingNamesHelper


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project.validator;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * Defines a set of rules for validating java and db mapping identifiers.
28  *
29  * @author Andrus Adamchik
30  * @since 1.1
31  */

32 public class MappingNamesHelper {
33
34     // TODO: used by StringUtils and ClassGenerationInfo... need to refactor..
35
static final Collection JavaDoc RESERVED_JAVA_KEYWORDS = Arrays.asList(new Object JavaDoc[] {
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 JavaDoc word)
46     {
47         return RESERVED_JAVA_KEYWORDS.contains(word);
48     }
49     
50     // a property is considered invalid if there is a getter or a setter for it in
51
// java.lang.Object or CayenneDataObject
52
static final Collection JavaDoc INVALID_JAVA_PROPERTIES = Arrays.asList(new Object JavaDoc[] {
53             "class", "committedSnapshot", "currentSnapshot", "dataContext", "objectId",
54             "persistenceState", "snapshotVersion"
55     });
56
57     static final MappingNamesHelper sharedInstance = new MappingNamesHelper();
58
59     /**
60      * Returns shared instance of the validator.
61      */

62     public static MappingNamesHelper getInstance() {
63         return sharedInstance;
64     }
65
66     /**
67      * This is more of a sanity check than a real validation. As different DBs allow
68      * different chars in identifiers, here we simply check for dots.
69      */

70     String JavaDoc invalidCharsInDbPathComponent(String JavaDoc dbPathComponent) {
71         return (dbPathComponent.indexOf('.') >= 0) ? "." : null;
72     }
73
74     /**
75      * Scans a name of ObjAttribute or ObjRelationship for invalid characters.
76      */

77     String JavaDoc invalidCharsInObjPathComponent(String JavaDoc objPathComponent) {
78         String JavaDoc invalidChars = validateJavaIdentifier(objPathComponent, "");
79         return (invalidChars.length() > 0) ? invalidChars : null;
80     }
81
82     String JavaDoc invalidCharsInJavaClassName(String JavaDoc javaClassName) {
83         if (javaClassName == null) {
84             return null;
85         }
86
87         String JavaDoc invalidChars = "";
88
89         StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(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 JavaDoc dataObjectClassFQN) {
98         if (dataObjectClassFQN == null) {
99             return true;
100         }
101
102         StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(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 JavaDoc validateJavaIdentifier(String JavaDoc id, String JavaDoc invalidChars) {
113         // TODO: Java spec seems to allow "$" char in identifiers... Cayenne expressions do
114
// not, so we should probably check for this char presence...
115

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     /**
140      * Returns whether a given String is a valid DataObject property. A property is
141      * considered invalid if there is a getter or a setter for it in java.lang.Object or
142      * CayenneDataObject.
143      */

144     boolean invalidDataObjectProperty(String JavaDoc dataObjectProperty) {
145         return dataObjectProperty == null
146                 || INVALID_JAVA_PROPERTIES.contains(dataObjectProperty);
147     }
148 }
149
Popular Tags