Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7e35aa641bd22ce5fe6d5c4774156997a0d09a44 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************************
 * Copyright (c) 2008 Oracle Corporation.
 * 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:
 *    Cameron Bateman - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.facelet.core.internal.validation;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jst.j2ee.model.ModelProviderManager;
import org.eclipse.jst.jsf.common.internal.componentcore.AbstractVirtualComponentQuery.DefaultVirtualComponentQuery;
import org.eclipse.jst.jsf.facelet.core.internal.FaceletCorePlugin;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.TagModelParser;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.WebappConfiguration;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.FaceletTaglibDefn;
import org.eclipse.jst.jsf.facelet.core.internal.util.ViewUtil;
import org.eclipse.jst.jsp.core.internal.Logger;
import org.eclipse.wst.common.componentcore.resources.IVirtualFolder;
import org.eclipse.wst.validation.internal.core.ValidationException;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;
import org.eclipse.wst.validation.internal.provisional.core.IValidationContext;
import org.eclipse.wst.validation.internal.provisional.core.IValidatorJob;
import org.xml.sax.SAXException;

/**
 * FUTURE USE: a build validator for source taglib definition files.
 * 
 * @author cbateman
 *
 */
public class TaglibValidator implements IValidatorJob
{

    public ISchedulingRule getSchedulingRule(IValidationContext helper)
    {
        // no scheduling rule
        return null;
    }

    public IStatus validateInJob(IValidationContext helper, IReporter reporter)
            throws ValidationException
    {
        IStatus status = Status.OK_STATUS;
        try
        {
            validate(helper, reporter);
        }
        catch (ValidationException e)
        {
            Logger.logException(e);
            status = new Status(IStatus.ERROR, FaceletCorePlugin.PLUGIN_ID,
                    IStatus.ERROR, e.getLocalizedMessage(), e);
        }
        return status;

    }

    public void cleanup(IReporter reporter)
    {
        // no cleanup
    }

    public void validate(IValidationContext helper, IReporter reporter)
            throws ValidationException
    {
        String[] uris = helper.getURIs();
        IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
        if (uris.length > 0)
        {
            IFile currentFile = null;

            for (int i = 0; i < uris.length && !reporter.isCancelled(); i++)
            {
                currentFile = wsRoot.getFile(new Path(uris[i]));
                if (currentFile != null && currentFile.exists())
                {
                    if (shouldValidate(currentFile))
                    {

                        validateFile(currentFile, reporter);
                    }
                }
            }
        }
    }

    private boolean shouldValidate(IFile currentFile)
    {
        final IProject project = currentFile.getProject();
        final IVirtualFolder folder = new DefaultVirtualComponentQuery().getWebContentFolder(project);
        final IPath filePath = currentFile.getProjectRelativePath();
        final IPath webFolderPath = folder.getUnderlyingFolder().getProjectRelativePath();
        boolean isInValidPath =  ViewUtil.isFaceletVDLFile(currentFile)
            && webFolderPath.isPrefixOf(filePath);
        
        if (isInValidPath)
        {
            for (final String configuredPath : WebappConfiguration.getConfigFilesFromContextParam(project, ModelProviderManager.getModelProvider(project)))
            {
                final IPath path = webFolderPath.append(configuredPath);
                if (path.equals(filePath))
                {
                    return true;
                }
            }
        }
        return false;
    }

    private void validateFile(IFile file, IReporter reporter)
    {
        InputStream is = null;
        try
        {
            is = file.getContents();
            FaceletTaglibDefn taglib = TagModelParser.loadFromInputStream(is, null);
            if (taglib != null)
            {
                validate(taglib);
            }
        }
        catch (CoreException e)
        {
            FaceletCorePlugin.log("Validating taglib file: "+file.getName(), e); //$NON-NLS-1$
        }
        catch (IOException e)
        {
            FaceletCorePlugin.log("Validating taglib file: "+file.getName(), e); //$NON-NLS-1$
        }
        catch (ParserConfigurationException e)
        {
            FaceletCorePlugin.log("Validating taglib file: "+file.getName(), e); //$NON-NLS-1$
        }
        catch (SAXException e)
        {
            FaceletCorePlugin.log("Validating taglib file: "+file.getName(), e); //$NON-NLS-1$
        }
        finally
        {
            if (is != null)
            {
                try
                {
                    is.close();
                }
                catch (IOException e)
                {
                    FaceletCorePlugin.log("Closing taglib file: "+file.getName(), e); //$NON-NLS-1$
                }
            }
        }
    }

    private void validate(FaceletTaglibDefn taglib)
    {
//        if (taglib instanceof FaceletXMLDefnTaglib)
//        {
//            
//        }
//        else if (taglib instanceof FaceletLibraryClassTagLib)
//        {
//            
//        }
    }
}

Back to the top