Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6023569f2fcb99f250b0d7aa7ee7d38d9ea77928 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * Copyright (c) 2006 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/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.designtime.internal.jsp;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jst.jsf.common.internal.types.TypeConstants;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IDOMContextResolver;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IStructuredDocumentContextResolverFactory;
import org.eclipse.jst.jsf.context.resolver.structureddocument.ITaglibContextResolver;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IWorkspaceContextResolver;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.jst.jsf.context.symbol.IBoundedJavaTypeDescriptor;
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.AbstractContextSymbolFactory;
import org.eclipse.jst.jsf.context.symbol.source.IAdditionalContextSymbolInfo;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.internal.tld.IJSFConstants;
import org.eclipse.jst.jsf.core.internal.tld.ITLDConstants;
import org.eclipse.jst.jsf.designtime.internal.symbols.ResourceBundleMapSourceFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * @author cbateman
 *
 */
public class JSPDefaultSymbolFactory extends AbstractContextSymbolFactory 
{
    protected ISymbol internalCreate(String symbolName,int scope,IAdaptable context,List problems, IAdditionalContextSymbolInfo additionInfo) 
    {
        final IStructuredDocumentContext sContext =
            (IStructuredDocumentContext) 
                context.getAdapter(IStructuredDocumentContext.class);
        
        final IDOMContextResolver domResolver = 
            IStructuredDocumentContextResolverFactory.INSTANCE.getDOMContextResolver(sContext);
        
        if (domResolver != null)
        {
            final Node curNode = domResolver.getNode();
            
            if (curNode instanceof Attr)
            {
                final Attr attr = (Attr) curNode;
                final Node owningElement = attr.getOwnerElement();
                
                if (owningElement != null)
                {
                    return handleSymbolCreation(symbolName, sContext, attr, owningElement, problems);
                }
            }
        }
        
        return null;
    }

    
    private ISymbol handleSymbolCreation(final String symbolName, 
                                         final IStructuredDocumentContext context,
                                         final Attr attr, 
                                         final Node owningElement,
                                         final List problems)
    {
        final ITaglibContextResolver resolver = 
            IStructuredDocumentContextResolverFactory.INSTANCE.getTaglibContextResolver(context);
        
        
        if (resolver == null
                || !resolver.canResolveContext(context))
        {
            return null;
        }
        
        final String uri = resolver.getTagURIForNodeName(owningElement);
        
        // process core taglib
        if (ITLDConstants.URI_JSF_CORE.equals(uri) || ITLDConstants.URI_JSF_CORE_JCP.equals(uri))
        {
            return handleCoreTags(symbolName, owningElement, attr, context, problems);
        }
        else if (ITLDConstants.URI_JSF_HTML.equals(uri) || ITLDConstants.URI_JSF_HTML_JCP.equals(uri))
        {
            return handleHtmlTags(symbolName, owningElement, attr, context, problems);
        }
        
        return null;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.jst.jsf.context.symbol.source.AbstractContextSymbolFactory#supports(org.eclipse.core.runtime.IAdaptable)
     */
    public boolean supports(IAdaptable context) 
    {
        return context != null && 
        	context.getAdapter(IStructuredDocumentContext.class) != null;
    }
    
    private ISymbol handleHtmlTags(String symbolName, Node owningElement, Attr attr, IStructuredDocumentContext context, List problems)
    {
        final String elementName = owningElement.getLocalName();
        final String attrName = attr.getName();
        
        if (IJSFConstants.TAG_DATATABLE.equals(elementName))
        {
            if (IJSFConstants.ATTR_VAR.equals(attrName))
            {
                return DataModelVariableFactory.getInstance()
                    .createSymbolForDataTableValue(symbolName, (Element) owningElement, context);
            }
        }

        return null;
    }
    
    private ISymbol handleCoreTags(String symbolName, Node owningElement, Attr attr, IStructuredDocumentContext context, List problems)
    {
        final String elementName = owningElement.getLocalName();
        
        if (IJSFConstants.TAG_LOADBUNDLE.equals(elementName))
        {
//            long startTime = System.currentTimeMillis();
            ISymbol symbol = handleLoadBundleTag(symbolName, owningElement, attr, context, problems);
            
//            long endTime = System.currentTimeMillis();
//            long totalTime = endTime-startTime;
            //System.out.println("Loadbundle resolution for symbol "+symbolName+" took: "+totalTime+"ms");
            return symbol;
        }
        return null;
    }
    
    private ISymbol handleLoadBundleTag(String symbolName, Node owningElement, Attr attr, IStructuredDocumentContext context, List problems)
    {
        final String attrName = attr.getName();
        if (IJSFConstants.ATTR_VAR.equals(attrName))
        {
            final NamedNodeMap attrMap = owningElement.getAttributes();
            final Node baseNameNode = attrMap.getNamedItem(IJSFConstants.ATTR_BASENAME);

            
            if (baseNameNode != null)
            {
                try
                {
                    final IWorkspaceContextResolver wkspaceResolver =
                        IStructuredDocumentContextResolverFactory.INSTANCE.getWorkspaceContextResolver(context);
                    IProject project = wkspaceResolver.getProject();
                    
                    if (project == null)
                    {
                        throw new RuntimeException("Error acquiring project"); //$NON-NLS-1$
                    }
                    
                    final Map source = 
                        ResourceBundleMapSourceFactory
                            .getResourceBundleMapSource(project, baseNameNode.getNodeValue());
                    final IMapTypeDescriptor typeDesc = 
                        SymbolFactory.eINSTANCE.createIMapTypeDescriptor();
                    typeDesc.setMapSource(source);
                    final IComponentSymbol symbol = 
                        SymbolFactory.eINSTANCE.createIComponentSymbol();
                    symbol.setName(symbolName);
                    symbol.setTypeDescriptor(typeDesc);
                    symbol.setDetailedDescription(Messages.getString("JSPDefaultSymbolFactory.Resource.bundle.map.detailedDescription")+baseNameNode.getNodeValue()+"</i>"); //$NON-NLS-1$ //$NON-NLS-2$
                    
                    return symbol;
                }
                catch (IOException ioe)
                {
                    problems.add(new Status(IStatus.ERROR, JSFCorePlugin.PLUGIN_ID, 0,Messages.getString("JSPDefaultSymbolFactory.Problem.ErrorCreatingVariable"), ioe)); //$NON-NLS-1$
                }
                catch (JavaModelException jme)
                {
                    problems.add(new Status(IStatus.ERROR, JSFCorePlugin.PLUGIN_ID, 0,Messages.getString("JSPDefaultSymbolFactory.Problem.ErrorCreatingVariable"), jme)); //$NON-NLS-1$
                }
                catch (CoreException ce)
                {
                    problems.add(new Status(IStatus.ERROR, JSFCorePlugin.PLUGIN_ID, 0,Messages.getString("JSPDefaultSymbolFactory.Problem.ErrorCreatingVariable"), ce)); //$NON-NLS-1$
                }
                
                final IBoundedJavaTypeDescriptor typeDesc = SymbolFactory.eINSTANCE.createIBoundedJavaTypeDescriptor();
                typeDesc.setTypeSignatureDelegate( TypeConstants.TYPE_JAVAOBJECT );
                final IComponentSymbol symbol = SymbolFactory.eINSTANCE.createIComponentSymbol();
                symbol.setName(symbolName);
                symbol.setTypeDescriptor(typeDesc);
                symbol.setDetailedDescription(Messages.getString("JSPDefaultSymbolFactory.Resource.bundle.map.detailedDescription")+baseNameNode.getNodeValue()+"</i>"); //$NON-NLS-1$ //$NON-NLS-2$
                
                return symbol;
            }
        }
        return null;
    }
}

Back to the top