Skip to main content
summaryrefslogtreecommitdiffstats
blob: 88f80d108ff77d0b66976437c82e129a24128e9e (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
/*******************************************************************************
 * Copyright (c) 2001, 2008 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.ui.internal.jspeditor;

import org.eclipse.jface.text.Region;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IStructuredDocumentContextResolverFactory;
import org.eclipse.jst.jsf.context.resolver.structureddocument.internal.ITextRegionContextResolver;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.jst.jsf.core.internal.contentassist.el.ContentAssistParser;
import org.eclipse.jst.jsf.core.internal.contentassist.el.SymbolInfo;
import org.eclipse.jst.jsf.designtime.DTAppManagerUtil;
import org.eclipse.jst.jsf.designtime.internal.view.XMLViewDefnAdapter;
import org.eclipse.jst.jsf.designtime.internal.view.IDTViewHandler.ViewHandlerException;
import org.eclipse.jst.jsf.designtime.internal.view.XMLViewDefnAdapter.DTELExpression;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;

/**
 * Utility class to access EL symbols in jsp file.
 */
public final class JSPSourceUtil
{

    private JSPSourceUtil()
    {
        // utility class, no instances.
    }

    /**
     * Find the Region
     * 
     * @param context -
     *            the IStructuredDocumentContext
     * @return region of el expression, null if context doesn't point to an el
     *         expression
     */
    public static Region findELRegion(final IStructuredDocumentContext context)
    {
        if (context != null)
        {
            final DTELExpression expression = getELExpression(context);
            if (expression != null)
            {
                final ITextRegionContextResolver resolver = IStructuredDocumentContextResolverFactory.INSTANCE
                        .getTextRegionResolver(expression.getDocumentContext());

                if (resolver != null)
                {
                    final String regionType = resolver.getRegionType();

                    if (regionType != null)
                    {

                        if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
                        {
                            return new Region(expression.getDocumentContext()
                                    .getDocumentPosition(), expression
                                    .getText().length());
                        }
                        else if (resolver.matchesRelative(new String[]
                        { DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE }))
                        {
                            return new Region(resolver.getStartOffset(),
                                    resolver.getLength());
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * Determines symbol and symbol region at a given document position
     * 
     * @param context -
     *            the IStructuredDocumentContext
     * @param elRegion -
     *            the region of the el expression to consider
     * @param documentPosition -
     *            the document position to get the symbol for
     * @return SymbolInfo
     */
    public static SymbolInfo determineSymbolInfo(
            final IStructuredDocumentContext context, final Region elRegion,
            final int documentPosition)
    {
        if (context != null && elRegion != null)
        {
            final DTELExpression elExpression = getELExpression(context);
            
            final String elText = elExpression.getText().trim();
//                        context.getStructuredDocument().get(
//                                elRegion.getOffset(), elRegion.getLength());
            final SymbolInfo symbolInfo =
                    ContentAssistParser.getSymbolInfo(context,
                            documentPosition - elRegion.getOffset() + 1,
                            elText);
            return symbolInfo;
        }
        return null;
    }
    
    private static DTELExpression getELExpression(
            final IStructuredDocumentContext context)
    {
        final XMLViewDefnAdapter adapter = DTAppManagerUtil
                .getXMLViewDefnAdapter(context);
        DTELExpression expression = null;
        if (adapter != null)
        {
            try
            {
                expression = adapter.getELExpression(context);
            }
            catch (ViewHandlerException e)
            {
                expression = null;
            }
        }
        return expression;
    }
}

Back to the top