Skip to main content
summaryrefslogtreecommitdiffstats
blob: b96b8a89a29bfb97133644e4075ec9e9f459d0b6 (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
/*******************************************************************************
 * 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.designtime.internal.symbols;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jst.jsf.context.symbol.IComponentSymbol;
import org.eclipse.jst.jsf.context.symbol.IMapTypeDescriptor;
import org.eclipse.jst.jsf.context.symbol.ISymbol;
import org.eclipse.jst.jsf.context.symbol.SymbolFactory;
import org.eclipse.jst.jsf.context.symbol.source.AbstractSymbolSourceProviderFactory;
import org.eclipse.jst.jsf.context.symbol.source.ISymbolSourceProvider;
import org.eclipse.jst.jsf.core.IJSFCoreConstants;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.jsfappconfig.JSFAppConfigManager;
import org.eclipse.jst.jsf.core.jsfappconfig.JSFAppConfigUtils;
import org.eclipse.jst.jsf.designtime.symbols.FileContextUtil;
import org.eclipse.jst.jsf.designtime.symbols.SymbolUtil;
import org.eclipse.jst.jsf.facesconfig.emf.BaseNameType;
import org.eclipse.jst.jsf.facesconfig.emf.ResourceBundleType;
import org.eclipse.jst.jsf.facesconfig.emf.VarType;

/**
 * WARNING: this is an interim solution to supporting Faces 1.2 resource
 * bundle variables.  This class will become obsolete by design once
 * the dt framework for Unified EL symbol resolution is added
 * 
 * DO NOT USE THIS CLASS EXTERNALLY, IT WILL BE REMOVED WITHOUT WARNING IN THE FUTURE
 * WITH NO MIGRATION PATH
 * 
 * Self-factory for symbol source providers that contribute symbols for
 * the resource bundle variables declared in Faces 1.2 and greater 
 * app configuration files.
 * 
 * @author cbateman
 *
 */
public final class ResourceBundleSymbolSourceProvider extends
        AbstractSymbolSourceProviderFactory implements ISymbolSourceProvider {

    protected final ISymbolSourceProvider create(IProject project) {
        return this;
    }

    public final ISymbol[] getSymbols(IAdaptable context, int symbolScopeMask) 
    {
       if (isProvider(context))
       {
           final List symbols = new ArrayList();
           final IFile   fileContext = FileContextUtil.deriveIFileFromContext(context);
           final IProject project = fileContext.getProject();
           
           final JSFAppConfigManager appconfigMgr = JSFAppConfigManager.getInstance(project);
           final List resourceBundles = appconfigMgr.getResourceBundles();
           
           for (final Iterator it = resourceBundles.iterator(); it.hasNext();)
           {
               ResourceBundleType  resBundle = (ResourceBundleType) it.next();
               final String  basename = getBaseName(resBundle);
               final String  name = getVarName(resBundle);
               
               if (basename != null && name != null)
               {
                   try 
                   {
                       symbols.add(createSymbolForResourceBundle(project, name, basename));
                   } catch (JavaModelException e) {
                       JSFCorePlugin.log(e, "Error creating base name for: "+basename); //$NON-NLS-1$
                    } catch (IOException e) {
                        JSFCorePlugin.log(e, "Error creating base name for: "+basename); //$NON-NLS-1$
                    } catch (CoreException e) {
                        JSFCorePlugin.log(e, "Error creating base name for: "+basename); //$NON-NLS-1$
                    }
               }
           }     
           return (ISymbol[]) symbols.toArray(ISymbol.EMPTY_SYMBOL_ARRAY);
       }
       return ISymbol.EMPTY_SYMBOL_ARRAY;
    }

    private ISymbol createSymbolForResourceBundle(IProject project, 
                                                  final String name,
                                                  final String basename) throws JavaModelException, IOException, CoreException
    {
        // TODO: push down into ResourceBundleMapSourceFactory and share
        // with loadBundle
        final Map mapSource = ResourceBundleMapSourceFactory
            .getResourceBundleMapSource(project, basename);
        final IMapTypeDescriptor typeDesc = 
            SymbolFactory.eINSTANCE.createIMapTypeDescriptor();
        typeDesc.setMapSource(mapSource);
        final IComponentSymbol symbol = 
            SymbolFactory.eINSTANCE.createIComponentSymbol();
        symbol.setName(name);
        symbol.setTypeDescriptor(typeDesc);
        symbol.setDetailedDescription(Messages.getString("ResourceBundleSymbolSourceProvider.DetailedDescription")+basename+"</i>");  //$NON-NLS-1$//$NON-NLS-2$
        return symbol;
    }

    private String getBaseName(ResourceBundleType resBundle)
    {
        final BaseNameType  baseNameType = resBundle.getBaseName();
        if (baseNameType != null)
        {
            return baseNameType.getTextContent();
        }
        return null;
    }

    private String getVarName(ResourceBundleType resBundle)
    {
        final VarType  varName = resBundle.getVar();
        if (varName != null)
        {
            return varName.getTextContent();
        }
        return null;
    }
    
    public final ISymbol[] getSymbols(String prefix, IAdaptable context,
            int symbolScopeMask) {
        return SymbolUtil.
            filterSymbolsByPrefix(getSymbols(context, symbolScopeMask), prefix);
    }

    public final boolean isProvider(IAdaptable context) {
        IFile file = FileContextUtil.deriveIFileFromContext(context);
        
        if (file != null)
        {
            final IProject project = file.getProject();
            
            if (project != null && project.isAccessible())
            {
                // to be valid, the jsf project must be at least version 1.2
                return JSFAppConfigUtils.isValidJSFProject(project, IJSFCoreConstants.FACET_VERSION_1_2);
            }
        }
        
        return false;
    }
}

Back to the top