KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23
24 import org.apache.cayenne.access.DataDomain;
25 import org.apache.cayenne.access.DataNode;
26 import org.apache.cayenne.conf.DriverDataSourceFactory;
27 import org.apache.cayenne.project.ProjectPath;
28 import org.apache.cayenne.util.Util;
29
30 /**
31  * @author Andrus Adamchik
32  */

33 public class DataNodeValidator extends TreeNodeValidator {
34
35     /**
36      * Constructor for DataNodeValidator.
37      */

38     public DataNodeValidator() {
39         super();
40     }
41
42     public void validateObject(ProjectPath path, Validator validator) {
43         DataNode node = (DataNode) path.getObject();
44         validateName(node, path, validator);
45         validateConnection(node, path, validator);
46     }
47
48     protected void validateConnection(DataNode node, ProjectPath path, Validator validator) {
49         String JavaDoc factory = node.getDataSourceFactory();
50
51         // If direct factory, make sure the location is a valid file name.
52
if (Util.isEmptyString(factory)) {
53             validator.registerError("No DataSource factory.", path);
54         }
55         else if (!DriverDataSourceFactory.class.getName().equals(factory)) {
56             String JavaDoc location = node.getDataSourceLocation();
57             if (Util.isEmptyString(location)) {
58                 validator.registerError("DataNode has no location parameter.", path);
59             }
60         }
61     }
62
63     protected void validateName(DataNode node, ProjectPath path, Validator validator) {
64         String JavaDoc name = node.getName();
65
66         if (Util.isEmptyString(name)) {
67             validator.registerError("Unnamed DataNode.", path);
68             return;
69         }
70
71         DataDomain domain = (DataDomain) path.getObjectParent();
72         if (domain == null) {
73             return;
74         }
75
76         // check for duplicate names in the parent context
77
Iterator JavaDoc it = domain.getDataNodes().iterator();
78         while (it.hasNext()) {
79             DataNode otherNode = (DataNode) it.next();
80             if (otherNode == node) {
81                 continue;
82             }
83
84             if (name.equals(otherNode.getName())) {
85                 validator.registerError("Duplicate DataNode name: " + name + ".", path);
86                 break;
87             }
88         }
89     }
90 }
91
Popular Tags