Skip to main content
summaryrefslogtreecommitdiffstats
blob: 47ac5910eeb0f2222699ef4885215c27ae7066ce (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package org.eclipse.jst.jsf.facelet.ui.internal.contentassist;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.jst.jsf.common.runtime.internal.view.model.common.ITagElement;
import org.eclipse.jst.jsf.common.runtime.internal.view.model.common.Namespace;
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.IWorkspaceContextResolver;
import org.eclipse.jst.jsf.context.resolver.structureddocument.internal.ITextRegionContextResolver;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory;
import org.eclipse.jst.jsf.designtime.internal.view.model.ITagRegistry;
import org.eclipse.jst.jsf.designtime.internal.view.model.TagRegistryFactory.TagRegistryFactoryException;
import org.eclipse.jst.jsf.facelet.core.internal.registry.FaceletRegistryManager.MyRegistryFactory;
import org.eclipse.jst.jsf.facelet.ui.internal.FaceletUiPlugin;
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

/**
 * Temporary.
 * 
 * @author cbateman
 *
 */
public class XHTMLContentAssistProcessor2 implements IContentAssistProcessor
{
    private final static ICompletionProposal[] NO_PROPOSALS = new ICompletionProposal[0];

    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
            int offset)
    {
        List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
        final IStructuredDocumentContext context = IStructuredDocumentContextFactory.INSTANCE
                .getContext(viewer, offset);

        if (context != null)
        {
            final IDOMContextResolver domContextResolver = IStructuredDocumentContextResolverFactory.INSTANCE
                    .getDOMContextResolver(context);

            final Document doc = domContextResolver.getDOMDocument();

            if (doc == null)
            {
                return NO_PROPOSALS;
            }

            // get the namespaces that are already declared
            final Map<String, PrefixEntry> documentNamespaces = getDocumentNamespaces(doc);

            final ITextRegionContextResolver resolver = IStructuredDocumentContextResolverFactory.INSTANCE
                    .getTextRegionResolver(context);

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

                if (DOMRegionContext.XML_CONTENT.equals(regionType))
                {
                    // TODO: this may be in the open it may occur on the inside
                    // of a
                    // "<" that doesn't have any further tag name yet
                    proposals = getTagCompletionsForDocument(context,
                            documentNamespaces, TagPrefix.NO_PREFIX);
                }
                else if (DOMRegionContext.XML_TAG_NAME.equals(regionType))
                {
                    final TagPrefix tagPrefix = new TagPrefix(resolver
                            .getRegionText());
                    proposals = getTagCompletionsForDocument(context,
                            documentNamespaces, tagPrefix);
                }
                else
                {
                    System.out.println(regionType);
                }
            }
        }

        return proposals.toArray(NO_PROPOSALS);
    }

    private List<ICompletionProposal> getTagCompletionsForDocument(
            final IStructuredDocumentContext context,
            final Map<String, PrefixEntry> namespaces, final TagPrefix tagPrefix)
    {
        final List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();

        final IWorkspaceContextResolver wkspaceResolver = IStructuredDocumentContextResolverFactory.INSTANCE
                .getWorkspaceContextResolver(context);
        final IProject project = wkspaceResolver.getProject();

        MyRegistryFactory factory = new MyRegistryFactory();
        ITagRegistry registry;
        try
        {
            registry = factory.createTagRegistry(project);
            if (registry == null)
            {
                return proposals;
            }

            final Collection<? extends Namespace> tagLibs = registry
                    .getAllTagLibraries();

            for (final Namespace tagLib : tagLibs)
            {
                final PrefixEntry ns = namespaces.get(tagLib.getNSUri());

                // if the tag library is not initialized, don't trigger
                // a potentially expensive operation during the content assist
                if (ns != null && tagLib.isInitialized())
                {
                    // only add if this namespace's prefix starts with
                    // the prefix that the user provided
                    if (ns._prefix.startsWith(tagPrefix.getPrefix()))
                    {
                        final Collection<?> tags = tagLib.getViewElements();

                        for (final Iterator<?> it = tags.iterator(); it
                                .hasNext();)
                        {
                            final ITagElement tagElement = (ITagElement) it
                                    .next();

                            // only add an element it starts with the prefix
                            // provided
                            if (tagElement.getName().startsWith(
                                    tagPrefix.getLocalName()))
                            {
                                final String userPrefix = tagPrefix
                                        .getUserPrefix();

                                final String tagName = ns._prefix + ":"
                                        + tagElement.getName();
                                String replacementText = "";
                                if ("".equals(userPrefix))
                                {
                                    replacementText = "<" + tagName + "></"
                                            + tagName + ">";
                                }
                                else
                                {
                                    replacementText = tagName
                                            .substring(userPrefix.length());
                                }
                                proposals.add(new CompletionProposal(
                                        replacementText, context
                                                .getDocumentPosition(), 0, 0,
                                        null, tagName, null, null));
                            }
                        }
                    }
                }
            }
        }
        catch (TagRegistryFactoryException e)
        {
            FaceletUiPlugin.log(e);
        }

        return proposals;
    }

    private Map<String, PrefixEntry> getDocumentNamespaces(Document doc)
    {
        final Map<String, PrefixEntry> namespaces = new HashMap<String, PrefixEntry>();

        Element rootElement = doc.getDocumentElement();

        if (rootElement != null)
        {
            NamedNodeMap attrs = rootElement.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++)
            {
                Attr a = (Attr) attrs.item(i);
                PrefixEntry ns = PrefixEntry.parseNamespace(a);
                if (ns != null)
                {
                    namespaces.put(ns.getUri(), ns);
                }
            }
        }

        return namespaces;
    }

    private static class TagPrefix
    {
        public final static TagPrefix NO_PREFIX = new TagPrefix("");

        private final String          _prefix;
        private final String          _localName;
        private final boolean         _hasColon;

        public TagPrefix(final String tagName)
        {
            int prefixIdx = tagName.indexOf(':');

            if (prefixIdx != -1)
            {
                _prefix = tagName.substring(0, prefixIdx);
                _hasColon = true;
                if (tagName.length() > prefixIdx)
                {
                    _localName = tagName.substring(prefixIdx + 1);
                }
                else
                {
                    _localName = "";
                }
            }
            else
            {
                _hasColon = false;
                _prefix = tagName;
                _localName = "";
            }
        }

        public String getUserPrefix()
        {
            String userPrefix = _prefix;

            if (_hasColon)
            {
                userPrefix += ":";
                userPrefix += _localName;
            }

            return userPrefix;
        }

        public String getPrefix()
        {
            return _prefix;
        }

        public String getLocalName()
        {
            return _localName;
        }
    }

    private static class PrefixEntry
    {
        private final String _uri;
        private final String _prefix;

        public static PrefixEntry parseNamespace(Attr attr)
        {
            final String prefix = attr.getPrefix();

            if ("xmlns".equals(prefix))
            {
                final String prefixName = attr.getLocalName();
                if (prefixName != null)
                {
                    final String uri = attr.getNodeValue();

                    if (uri != null)
                    {
                        return new PrefixEntry(uri, prefixName);
                    }
                }
            }

            return null;
        }

        public PrefixEntry(final String uri, final String prefix)
        {
            _uri = uri;
            _prefix = prefix;
        }

        public final String getUri()
        {
            return _uri;
        }

        public int hashCode()
        {
            return _uri.hashCode();
        }

        public boolean equals(Object obj)
        {
            return _uri.equals(obj);
        }
    }

    public IContextInformation[] computeContextInformation(ITextViewer viewer,
            int offset)
    {
        return null;
    }

    public char[] getCompletionProposalAutoActivationCharacters()
    {
        return null;
    }

    public char[] getContextInformationAutoActivationCharacters()
    {
        return null;
    }

    public IContextInformationValidator getContextInformationValidator()
    {
        return null;
    }

    public String getErrorMessage()
    {
        return null;
    }

}

Back to the top