2bc6faaddba481ec408efd9265bd6cc3e458a0c2
[dcaegen2/services.git] /
1 package com.mongodb.internal.validator;\r
2 \r
3 //copy from https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidator.java\r
4 //allow inserting name with dot\r
5 /*\r
6 * ============LICENSE_START=======================================================\r
7 * ONAP : DataLake\r
8 * ================================================================================\r
9 * Copyright 2018 China Mobile\r
10 *=================================================================================\r
11 * Licensed under the Apache License, Version 2.0 (the "License");\r
12 * you may not use this file except in compliance with the License.\r
13 * You may obtain a copy of the License at\r
14 *\r
15 *     http://www.apache.org/licenses/LICENSE-2.0\r
16 *\r
17 * Unless required by applicable law or agreed to in writing, software\r
18 * distributed under the License is distributed on an "AS IS" BASIS,\r
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
20 * See the License for the specific language governing permissions and\r
21 * limitations under the License.\r
22 * ============LICENSE_END=========================================================\r
23 */\r
24 import org.bson.FieldNameValidator;\r
25 \r
26 import java.util.Arrays;\r
27 import java.util.List;\r
28 \r
29 /**\r
30  * A field name validator for document that are meant for storage in MongoDB collections.  It ensures that no fields contain a '.',\r
31  * or start with '$' (with the exception of "$db", "$ref", and "$id", so that DBRefs are not rejected).\r
32  *\r
33  * <p>This class should not be considered a part of the public API.</p>\r
34  */\r
35 public class CollectibleDocumentFieldNameValidator implements FieldNameValidator {\r
36     // Have to support DBRef fields\r
37     private static final List<String> EXCEPTIONS = Arrays.asList("$db", "$ref", "$id");\r
38 \r
39     @Override\r
40     public boolean validate(final String fieldName) {\r
41         if (fieldName == null) {\r
42             throw new IllegalArgumentException("Field name can not be null");\r
43         }\r
44 \r
45         /* dl change\r
46         if (fieldName.contains(".")) {\r
47             return false;\r
48         }*/\r
49 \r
50         if (fieldName.startsWith("$") && !EXCEPTIONS.contains(fieldName)) {\r
51             return false;\r
52         }\r
53         return true;\r
54     }\r
55 \r
56     @Override\r
57     public FieldNameValidator getValidatorForField(final String fieldName) {\r
58         return this;\r
59     }\r
60 }\r