Skip to main content
summaryrefslogtreecommitdiffstats
blob: eb8865c7bc64aa20bb6ec4ff915bd7936521ce09 (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
/*******************************************************************************
 * Copyright (c) 2007 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:
 * 		Oracle - initial API and implementation
 *    
 ********************************************************************************/
package org.eclipse.jst.jsf.taglibprocessing.attributevalues;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jst.jsf.metadataprocessors.IMetaDataEnabledFeature;
import org.eclipse.jst.jsf.metadataprocessors.features.IValidValues;
import org.eclipse.jst.jsf.metadataprocessors.features.ValidationMessage;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;

/**
 * Path relative to web root
 * <b>EXPERIMENTAL</b> - may change or dissappear
 *
 */
public class WebPathType extends PathType implements
	IMetaDataEnabledFeature, IValidValues{

	public boolean isValidValue(String value) {
		//is this a relative path, or absolute url?
		try {
			URL url = new URL(value);			
			//if file protocol, see if it is valid?  Otherwise assume ok.
			if (url.getProtocol().equals("file")){ //$NON-NLS-1$
				validateFileRelativeToWebRoot(url.getPath());
			} 
		} catch (MalformedURLException e) {
			//is this a valid path relative to the
			if (value != null && value.length() > 1 && value.charAt(0) == '/') { 
				validateFileRelativeToWebRoot(value);						
			} else {
				//Bug 325490 - [JSF2.0] False warning from facelet validator when working with facelet pages in a sub-folder
				validateFileRelativeToCurrentFile(value);
			}
		}
		return getValidationMessages().size() == 0;

	}

    private void validateFileRelativeToWebRoot(String value) {
		IContainer webRoot = getWebRoot();
		if (webRoot == null)
		{
		    return;
		}
		if (! webRoot.exists()){
			getValidationMessages().add(new ValidationMessage( Messages.WebPathType_1));
		} 
		else {
			IFile file = webRoot.getFile(new Path(value));
			if (!file.exists()) {
				//was this a valid file path string, or bogus url?
				getValidationMessages().add(new ValidationMessage(Messages.WebPathType_2));
			}									
			//we could also validate the expected file-extensions from meta data
		}
		
	}

	/**
	 * @return the web root
	 */
	protected IContainer getWebRoot()
    {
        IProject project = getProject2();
        if (project != null)
        {
            IVirtualComponent component = ComponentCore.createComponent(project);
            if (component != null)
            {
                return component.getRootFolder().getUnderlyingFolder();
            }
        }
        return null;
    }

	//Bug 325490 - [JSF2.0] False warning from facelet validator when working with facelet pages in a sub-folder
    private void validateFileRelativeToCurrentFile(String value) {
		IProject project = getProject2();
		if (project != null)
		{
            IVirtualComponent component = ComponentCore.createComponent(project);
    		if (component != null)
    		{
                IPath webContentPath = component.getRootFolder().getUnderlyingFolder().getFullPath();
        		IResource resource = getFile2();
        		if (resource != null)
        		{
                    IPath filePath = resource.getFullPath();
            		if (filePath.matchingFirstSegments(webContentPath) == webContentPath.segmentCount()) {
            			filePath = filePath.removeFirstSegments(webContentPath.segmentCount());
            			filePath = filePath.removeLastSegments(1);
            			filePath = filePath.append(value);
            			IFile file = getWebRoot().getFile(filePath);
            			if (!file.exists()){
            				getValidationMessages().add(new ValidationMessage(Messages.WebPathType_2));
            			}
            		}
        		}
    		}
		}
	}

}

Back to the top