Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ac6ac074ff4e0a87738a4f7f1ef92b3532ce24e (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
package org.eclipse.jst.jsf.facelet.core.internal.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jst.jsf.context.IModelContext;
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.context.structureddocument.IStructuredDocumentContextFactory;
import org.eclipse.jst.jsf.core.internal.CompositeTagRegistryFactory;
import org.eclipse.jst.jsf.core.internal.CompositeTagRegistryFactory.TagRegistryIdentifier;
import org.eclipse.jst.jsf.designtime.DesignTimeApplicationManager;
import org.eclipse.jst.jsf.designtime.internal.view.IDTViewHandler;
import org.eclipse.jst.jsf.designtime.internal.view.XMLViewDefnAdapter.DTELExpression;
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.jsp.core.internal.contentmodel.tld.CMDocumentFactoryTLD;
import org.eclipse.jst.jsp.core.internal.contentmodel.tld.provisional.TLDDocument;
import org.eclipse.jst.jsp.core.taglib.ITaglibRecord;
import org.eclipse.jst.jsp.core.taglib.TaglibIndex;
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;
import org.w3c.dom.Node;

/**
 * Facelet view utilities
 * 
 * @author cbateman
 */
public final class ViewUtil
{
    private static final String HTMLSOURCE_CONTENT_TYPE_ID = "org.eclipse.wst.html.core.htmlsource"; //$NON-NLS-1$
    private static final String XMLNS = "xmlns"; //$NON-NLS-1$

    /**
     * Warning! This call can be very expensive.  Cache results whenever
     * possible.
     * 
     * @param project
     * @param uri
     * @return the tld document for uri in project or null if not found
     */
    public static TLDDocument findTLDDocument(final IProject project,
            final String uri)
    {
        final ITaglibRecord[] tldrecs = TaglibIndex
                .getAvailableTaglibRecords(project.getFullPath());

        for (final ITaglibRecord rec : tldrecs)
        {
            final String matchUri = rec.getDescriptor().getURI();
            if (uri.equals(matchUri))
            {
                final CMDocumentFactoryTLD factory = new CMDocumentFactoryTLD();
                return (TLDDocument) factory.createCMDocument(rec);
            }
        }
        return null;
    }

    /**
     * @param attributes
     * @return the set of uri's that declared in attributes
     */
    public static Set<Attr> getDeclaredNamespaces(final NamedNodeMap attributes)
    {
        final Set<Attr> alreadyUsed = new HashSet<Attr>();
        for (int i = 0; i < attributes.getLength(); i++)
        {
            final Node node = attributes.item(i);
            if (XMLNS.equals(node.getNodeName())
                    || XMLNS.equals(node.getPrefix()))
            {
                final String attrValue = node.getNodeValue();

                if (attrValue != null && !"".equals(attrValue.trim()) //$NON-NLS-1$
                        && node instanceof Attr)
                {
                    alreadyUsed.add((Attr) node);
                }
            }
        }

        return alreadyUsed;
    }

    /**
     * @param attrSet
     * @param value
     * @return true if attrSet contains an attribute whose value is <i>value</i>
     */
    public static boolean hasAttributeValue(final Set<Attr> attrSet,
            final String value)
    {
        for (final Attr attr : attrSet)
        {
            if (value.equals(attr.getValue()))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * @param project
     * @return the html source type tag registry for project
     */
    public static ITagRegistry getHtmlTagRegistry(final IProject project)
    {
        final IContentType contentType = Platform.getContentTypeManager()
                .getContentType(HTMLSOURCE_CONTENT_TYPE_ID);
        final TagRegistryIdentifier id = new TagRegistryIdentifier(project,
                contentType);
        final ITagRegistry tagRegistry = CompositeTagRegistryFactory
                .getInstance().getRegistry(id);
        return tagRegistry;
    }

    /**
     * @param doc
     * @return all of the prefixed namespaces defined in doc
     */
    public static Map<String, PrefixEntry> getDocumentNamespaces(
            final Document doc)
    {
        final Map<String, PrefixEntry> namespaces = new HashMap<String, PrefixEntry>();

        final Element rootElement = doc.getDocumentElement();

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

    /**
     * @param file
     * @return true if the file is a Facelet VDL file
     */
    public static boolean isFaceletVDLFile(final IFile file)
    {
        final DesignTimeApplicationManager instance = DesignTimeApplicationManager.getInstance(file.getProject());
        if (instance != null)
        {
            final IDTViewHandler viewHandler = instance.getViewHandler();
            if (viewHandler != null)
            {
                return viewHandler.supportsViewDefinition(file);
            }
        }
        return false;
    }

    /**
     * @param project
     * @return the facelet tag registry for the project or null if none.
     */
    public static ITagRegistry getTagRegistry(final IProject project)
    {
        final MyRegistryFactory factory = new MyRegistryFactory();

        ITagRegistry registry = null;
        try
        {
            registry = factory.createTagRegistry(project);
        }
        catch (final TagRegistryFactoryException e)
        {
            // fall-through
        }
        return registry;
    }
    /**
     * Encapsulates a single namespace/prefix use declaration in an XML document
     * @author cbateman
     *
     */
    public static class PrefixEntry
    {
        private final String _uri;
        private final String _prefix;

        /**
         * @param attr
         * @return the prefix entry for attr or null
         */
        public static PrefixEntry parseNamespace(final 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;
        }

        /**
         * @param uri
         * @param prefix
         */
        public PrefixEntry(final String uri, final String prefix)
        {
            _uri = uri;
            _prefix = prefix;
        }

        /**
         * @return the namespace uri
         */
        public final String getUri()
        {
            return _uri;
        }

        /**
         * @return the namespace prefix
         */
        public final String getPrefix()
        {
            return _prefix;
        }

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

        @Override
        public boolean equals(final Object obj)
        {
            return _uri.equals(obj);
        }
    }
    /**
     * @param genericContext
     * @return the el expression from the context or null if none found.
     */
    public static DTELExpression getDTELExpression(IModelContext genericContext) {
        final IStructuredDocumentContext context = (IStructuredDocumentContext) genericContext
                .getAdapter(IStructuredDocumentContext.class);

//        if (context == null)
//        {
//            throw new ViewHandlerException(Cause.EL_NOT_FOUND);
//        }

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

        if (resolver != null)
        {
            final String regionType = resolver.getRegionType();
            int startOffset = resolver.getStartOffset();
            int relativeOffset = context.getDocumentPosition() - startOffset;
            
            if (DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE.equals(regionType))
            {
                final String attributeText = resolver.getRegionText();
                int elOpenIdx = attributeText.indexOf("#"); //$NON-NLS-1$
                
                if (elOpenIdx >= 0 && elOpenIdx < relativeOffset
                        && elOpenIdx+1 < attributeText.length()
                        && attributeText.charAt(elOpenIdx+1) == '{')
                {
                    // we may have a hit
                    int elCloseIdx = attributeText.indexOf('}', elOpenIdx+1);
                    if (elCloseIdx  != -1)
                    {
                        final IStructuredDocumentContext elContext =
                            IStructuredDocumentContextFactory.INSTANCE.getContext(
                                    context.getStructuredDocument(), resolver
                                    .getStartOffset()+elOpenIdx+2);
                        final String elText = attributeText.substring(
                                elOpenIdx + 2, elCloseIdx);
                        return new DTELExpression(elContext, elText);
                    }
                }
            }
        }

        return null;
    }

}

Back to the top