Skip to main content
summaryrefslogtreecommitdiffstats
blob: 619c74617052ab19139882cafcc067bc67e63944 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

/**
 *  
 * @author cbateman
 */
public abstract class EObjectValidationVisitor 
{
    private EStructuralFeature    _structuralFeature;
    private Map                   _childFeatures; // == null; lazy initialized in validate
    private final String          _version;
    
    /**
     * Used to indicate no child nodes
     */
    protected final static EObjectValidationVisitor[] NO_CHILDREN = 
        new EObjectValidationVisitor[0];
    
    /**
     * @param version 
     * 
     */
    public EObjectValidationVisitor(String version)
    {
        _version = version;
    }

    private EObjectValidationVisitor getVisitorForFeature(EStructuralFeature feature)
    {
        if (_childFeatures == null)
        {
            _childFeatures = new HashMap();

            EObjectValidationVisitor[] children = getChildNodeValidators();
            
            for (int i = 0; i < children.length; i++)
            {
                final EObjectValidationVisitor child = children[i];
                _childFeatures.put(child.getStructuralFeature(), child);
            }
        }

        return (EObjectValidationVisitor) _childFeatures.get(feature);
    }
    
    /**
     * @param structuralFeature 
     * @param version 
     */
    public EObjectValidationVisitor(EStructuralFeature structuralFeature, String version)
    {
        this(version);
        _structuralFeature = structuralFeature;
    }

    /**
     * @param eObject 
     * @param messages
     * @param file 
     */
    public final void validate(EObject eObject, List messages, IFile file)
    {
        doValidate(eObject, messages, file);
        
        List features = eObject.eClass().getEAllStructuralFeatures();
        
        for (final Iterator it = features.iterator(); it.hasNext();)
        {
            final EStructuralFeature feature = (EStructuralFeature) it.next();
            final EObjectValidationVisitor visitor = 
                getVisitorForFeature(feature);
            
            if (visitor != null)
            {
                final Object obj = eObject.eGet(feature);
                
                if (obj instanceof List
                        && feature.isMany())
                {
                    for (final Iterator childIt = ((List)obj).iterator(); 
                            childIt.hasNext();)
                    {
                        Object child = childIt.next();
                        if (child instanceof EObject)
                        {
                            visitor.validate((EObject)child, messages, file);
                        }
                    }
                }
                else if (obj instanceof EObject)
                {
                    visitor.validate((EObject)obj , messages, file);
                }
            }
        }
    }

    /**
     * Do the validation for this visitor on this node.  Add any Message's to
     * the messages list
     * 
     * @param eObject 
     * @param messages
     * @param file 
     */
    protected abstract void doValidate(EObject eObject, List messages, IFile file);
    
    /**
     * @return an array of visitors that validate children of the current node
     */
    protected abstract EObjectValidationVisitor[] getChildNodeValidators();

    /**
     * @return the structural feature
     */
    protected final EStructuralFeature getStructuralFeature() 
    {
        return _structuralFeature;
    }
    
    /**
     * @return the version of the runtime
     */
    protected final String getVersion()
    {
        return _version;
    }
    
    /**
     * @param messages 
     * @param message
     * @param eObj
     * @param file
     */
    protected static void addMessageInfo(List messages, IMessage message, EObject eObj, IFile file)
    {
        if (message != null)
        {
            message.setOffset(AppConfigValidationUtil.getStartOffset(eObj));
            message.setLength(AppConfigValidationUtil.getLength(eObj));
            message.setTargetObject(file);
            messages.add(message);
        }
    }
}

Back to the top