KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > unit > PersistenceValidator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.unit;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.api.project.FileOwnerQuery;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
27 import org.netbeans.modules.j2ee.persistence.wizard.Util;
28 import org.netbeans.modules.xml.multiview.Error;
29
30 /**
31  * Validator for persistence.xml.
32  *
33  * @author Erno Mononen
34  */

35 public class PersistenceValidator {
36     
37     private final PUDataObject puDataObject;
38     private List JavaDoc<Error JavaDoc> errors = new ArrayList JavaDoc<Error JavaDoc>();
39     
40     /**
41      * Creates a new instance of PersistenceValidator
42      * @param puDataObject the PUDataObject whose model
43      * is to be validated.
44      */

45     public PersistenceValidator(PUDataObject puDataObject) {
46         this.puDataObject = puDataObject;
47     }
48     
49     /**
50      * Validates the model.
51      * @return list of errors or an empty list if there were no errors, never null.
52      */

53     public List JavaDoc<Error JavaDoc> validate(){
54         validateName();
55         validateExcludeUnlisted();
56         validateJarFiles();
57         return errors;
58     }
59     
60     /**
61      * Validates that name is not empty and that it is unique.
62      */

63     private void validateName(){
64         PersistenceUnit[] persistenceUnits = puDataObject.getPersistence().getPersistenceUnit();
65         for (int i=0 ;i < persistenceUnits .length; i++) {
66             String JavaDoc title = persistenceUnits[i].getName();
67             if (isEmpty(title)) {
68                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(persistenceUnits[i], "name");
69                 errors.add(new Error JavaDoc(Error.MISSING_VALUE_MESSAGE, "name", loc));
70             }
71             for (int j = 0; j < persistenceUnits.length; j++) {
72                 String JavaDoc tit = persistenceUnits[j].getName();
73                 if (!isEmpty(title) && i != j && title.equals(tit)) {
74                     Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(persistenceUnits[i], "name");
75                     errors.add(new Error JavaDoc(Error.TYPE_FATAL, Error.DUPLICATE_VALUE_MESSAGE, title, loc));
76                 }
77             }
78         }
79     }
80     
81     /**
82      * Validates that exclude-unlisted-classes is not used in Java SE environment.
83      */

84     private void validateExcludeUnlisted(){
85         if (!isJavaSE()){
86             return;
87         }
88         PersistenceUnit[] persistenceUnits = puDataObject.getPersistence().getPersistenceUnit();
89         for (int i=0 ;i < persistenceUnits .length; i++) {
90            if (persistenceUnits[i].isExcludeUnlistedClasses()){
91                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(persistenceUnits[i], "exclude-unlisted-classes");
92                 errors.add(new Error JavaDoc(Error.TYPE_FATAL, Error.WARNING_MESSAGE, "exclude-unlisted-classes property is not supported in Java SE environments", loc));
93             }
94         }
95     }
96     
97     /**
98      * Validates that jar-files is not used in Java SE environment.
99      */

100     private void validateJarFiles(){
101         if (!isJavaSE()){
102             return;
103         }
104         PersistenceUnit[] persistenceUnits = puDataObject.getPersistence().getPersistenceUnit();
105         for (int i=0 ;i < persistenceUnits .length; i++) {
106             if (persistenceUnits[i].getJarFile() != null && persistenceUnits[i].getJarFile().length > 0){
107                 Error.ErrorLocation JavaDoc loc = new Error.ErrorLocation JavaDoc(persistenceUnits[i], "jar-files");
108                 errors.add(new Error JavaDoc(Error.TYPE_FATAL, Error.WARNING_MESSAGE, "jar-files property is not supported in Java SE environments", loc));
109             }
110         }
111         
112     }
113     
114     /**
115      * @return true if the current environment is Java SE.
116      */

117     protected boolean isJavaSE(){
118         Project project = FileOwnerQuery.getOwner(puDataObject.getPrimaryFile());
119         return Util.isJavaSE(project);
120     }
121     
122     private boolean isEmpty(String JavaDoc str){
123         return null == str || "".equals(str.trim());
124     }
125     
126 }
127
Popular Tags