Skip to main content
summaryrefslogtreecommitdiffstats
blob: 19791d64d69aa06cbc14bc2b7a0cc7e21d3d0646 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 * Copyright (c) 2001, 2007 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.validation.internal.appconfig;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jst.jsf.facesconfig.emf.FacesConfigPackage;
import org.eclipse.jst.jsf.facesconfig.emf.ListEntriesType;
import org.eclipse.jst.jsf.facesconfig.emf.ManagedBeanClassType;
import org.eclipse.jst.jsf.facesconfig.emf.ManagedBeanScopeType;
import org.eclipse.jst.jsf.facesconfig.emf.ManagedBeanType;
import org.eclipse.jst.jsf.facesconfig.emf.MapEntriesType;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

/**
 * Managed bean validator
 * 
 * @author cbateman
 *
 */
public class ManagedBeanValidationVisitor extends EObjectValidationVisitor 
{
    /**
     * @param version
     */
    public ManagedBeanValidationVisitor(final String version) 
    {
        super(FacesConfigPackage.eINSTANCE.getFacesConfigType_ManagedBean()
                ,version);
    }

    protected void doValidate(EObject object, List messages, IFile file) 
    {
        final ManagedBeanType managedBean = (ManagedBeanType) object;
        
        // TODO: validate managedBeanName is a valid Java id
//        final String managedBeanName = 
//            managedBean.getManagedBeanName().getTextContent();
        validateScope(managedBean.getManagedBeanScope(), messages, file);
        validateClass(managedBean.getManagedBeanClass(), messages, file);
        validateEntryTypes(managedBean, messages, file);
    }
    
    private void validateScope(ManagedBeanScopeType scope, List messages, IFile file)
    {
        if (scope != null && scope.getTextContent() != null)
        {
            addMessageInfo(messages
                    , AppConfigValidationUtil.validateManagedBeanScope(scope)
                    , scope, file);
        }
    }
    
    private void validateClass(ManagedBeanClassType classType, List messages, IFile file)
    {
        if (classType != null)
        {
            String classTypeValue = classType.getTextContent();
            addMessageInfo(messages
                , AppConfigValidationUtil.validateClassName
                    (classTypeValue == null ? "" : classTypeValue, 
                            null, file.getProject()), classType, file);
        }
    }
    
    private void validateEntryTypes(ManagedBeanType managedBeanType, List messages, IFile file)
    {
        // TODO: do a bean look-up for targetName to verify that it a) matches the type
        // and b) exists on the bean
        if (managedBeanType.getManagedBeanName()!= null
                && managedBeanType.getManagedBeanClass() != null)
        {
            final String propertyName =
                managedBeanType.getManagedBeanName().getTextContent();
            final String propertyClass =
                managedBeanType.getManagedBeanClass().getTextContent();
            
            if (propertyName == null || propertyClass == null
                    || "".equals(propertyName.trim())
                    || "".equals(propertyClass.trim()))
            {
                return;
            }
            
            IMessage message = null;
            EObject eObj = null;
            if (managedBeanType.getMapEntries() != null)
            {
                eObj = managedBeanType.getMapEntries();
                message =
                    AppConfigValidationUtil
                        .validateMapEntries
                            (propertyName
                            , propertyClass
                            , (MapEntriesType) eObj
                            , file.getProject());
            }
            else if (managedBeanType.getListEntries() != null)
            {
                eObj = managedBeanType.getListEntries();
                message =
                    AppConfigValidationUtil
                        .validateListEntries(
                            propertyName
                            , propertyClass
                            , (ListEntriesType) eObj
                            , file.getProject());
            }
            addMessageInfo(messages, message, eObj, file);
        }
    }
    
    protected EObjectValidationVisitor[] getChildNodeValidators() {
        return new EObjectValidationVisitor[]
        {
            new ManagedPropertyValidationVisitor(getVersion())
        };
    }
}

Back to the top