Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0c90da9c7520efa81b9db6c99412976972746164 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package org.eclipse.jst.jsf.facelet.core.internal.registry.taglib;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.jst.jsf.facelet.core.internal.FaceletCorePlugin;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.ComponentTagDefn;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.ConverterTagDefn;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.FaceletLibraryClassTagLib;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.FaceletTaglibDefn;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.FaceletTaglibFactory;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.FaceletXMLDefnTaglib;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.HandlerTagDefn;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.SourceTagDefn;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.TagDefn;
import org.eclipse.jst.jsf.facelet.core.internal.registry.taglib.faceletTaglib_1_0.ValidatorTagDefn;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * A Facelet tag library document parser (dtd 1.0).
 * 
 * @author cbateman
 * 
 */
public class TagModelParser
{
    private static final String FACELET_TAGLIB_DTD_PATH = "/dtd/facelet-taglib_1_0.dtd"; //$NON-NLS-1$


    private static final String ELEMENT_NAME_VALIDATOR_ID         = "validator-id";                                         //$NON-NLS-1$
    private static final String ELEMENT_NAME_CONVERTER_ID         = "converter-id";                                         //$NON-NLS-1$
    private static final String ELEMENT_NAME_RENDERER_TYPE        = "renderer-type";                                        //$NON-NLS-1$
    private static final String ELEMENT_NAME_COMPONENT_TYPE       = "component-type";                                       //$NON-NLS-1$
    private static final String ELEMENT_NAME_VALIDATOR            = "validator";                                            //$NON-NLS-1$
    private static final String ELEMENT_NAME_CONVERTER            = "converter";                                            //$NON-NLS-1$
    private static final String ELEMENT_NAME_COMPONENT            = "component";                                            //$NON-NLS-1$
    private static final String ELEMENT_NAME_HANDLER_CLASS        = "handler-class";                                        //$NON-NLS-1$
    private static final String ELEMENT_NAME_SOURCE               = "source";                                               //$NON-NLS-1$
    private static final String ELEMENT_NAME_TAG_NAME             = "tag-name";                                             //$NON-NLS-1$
    private static final String ELEMENT_NAME_TAG                  = "tag";                                                  //$NON-NLS-1$
    private static final String ELEMENT_NAME_NAMESPACE            = "namespace";                                            //$NON-NLS-1$
    private static final String ELEMENT_NAME_LIBRARY_CLASS        = "library-class";                                        //$NON-NLS-1$
    private static final String ELEMENT_NAME_FACELET_TAGLIB       = "facelet-taglib";                                       //$NON-NLS-1$
    private static final String URI_FACELET_TAGLIB_1_0_DTD        = "facelet-taglib_1_0.dtd";                               //$NON-NLS-1$
    private static final String PUBLIC_DTD_FACELET_TAGLIB_1_0_DTD = "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"; //$NON-NLS-1$

    /**
     * @param is
     * @param dtdSourcePath
     * @return the taglib definition or null
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    public static FaceletTaglibDefn loadFromInputStream(final InputStream is, final String dtdSourcePath) throws IOException, ParserConfigurationException, SAXException
    {
        final byte[] buffer = getBufferForEntry(is);
        final InputStream dtdSource = getDefaultDTDSource(dtdSourcePath != null ? dtdSourcePath : FACELET_TAGLIB_DTD_PATH);
        final FaceletTaglibDefn taglib = loadFromBuffer(buffer, dtdSource);
        return taglib;

    }
    /**
     * @param buffer
     * @param defaultDtdStream
     * @return the tag library definition (loaded EMF model) for the buffer
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    public static FaceletTaglibDefn loadFromBuffer(final byte[] buffer,
            final InputStream defaultDtdStream) throws IOException,
            ParserConfigurationException, SAXException
    {
        final InputSource inputSource = new InputSource(
                new ByteArrayInputStream(buffer));

        final Document doc = TagModelParser.getDefaultTaglibDocument(
                inputSource, new InputSource(defaultDtdStream));
        final FaceletTaglibDefn tagLib = TagModelParser.processDocument(doc);
        return tagLib;
    }

    /**
     * @param path 
     * @return the input stream for the default bundle Facelet dtd.
     * @throws IOException
     */
    protected static InputStream getDefaultDTDSource(final String path) throws IOException
    {
        final URL url = FaceletCorePlugin.getDefault().getBundle().getEntry(path);

        if (url != null)
        {
            return url.openStream();
        }
        return null;
    }

    /**
     * @param is must be open.  Caller is responsible for closing.
     * @return load the stream into a byte buffer.  
     */
    protected static byte[] getBufferForEntry(final InputStream is)
    {
        final ByteArrayOutputStream stream = new ByteArrayOutputStream();
        final byte[] buffer = new byte[2048];

        int bytesRead = 0;

        try
        {
            while (((bytesRead = is.read(buffer))) != -1)
            {
                stream.write(buffer, 0, bytesRead);
            }
        }
        catch (final IOException e)
        {
            FaceletCorePlugin.log("Error loading buffer", e); //$NON-NLS-1$
            return null;
        }

        return stream.toByteArray();
    }

    /**
     * @param taglibFile
     * @param defaultDTDSource
     * @return the default taglib dom Document
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    public static Document getDefaultTaglibDocument(
            final InputSource taglibFile, final InputSource defaultDTDSource)
            throws IOException, ParserConfigurationException, SAXException
    {
        final DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);

        final DocumentBuilder builder = factory.newDocumentBuilder();

        final DefaultHandler handler = new DefaultHandler()
        {
            @Override
            public InputSource resolveEntity(final String publicId,
                    final String systemId) throws IOException, SAXException
            {
                if (PUBLIC_DTD_FACELET_TAGLIB_1_0_DTD.equals(publicId)
                        || (systemId != null && systemId
                                .endsWith(URI_FACELET_TAGLIB_1_0_DTD)))

                {
                    return defaultDTDSource;
                }
                return super.resolveEntity(publicId, systemId);
            }
        };

        builder.setEntityResolver(handler);
        return builder.parse(taglibFile);
    }

    /**
     * @param doc
     * @return the facelet tag library or null
     */
    public static FaceletTaglibDefn processDocument(final Document doc)
    {
        Node curNode = null;

        for (int i = 0; i < doc.getChildNodes().getLength(); i++)
        {
            curNode = doc.getChildNodes().item(i);

            if (curNode.getNodeType() == Node.ELEMENT_NODE
                    && ELEMENT_NAME_FACELET_TAGLIB
                            .equals(curNode.getNodeName()))
            {
                break;
            }
        }

        if (curNode == null)
        {
            throw new IllegalArgumentException(
                    "Couldn't find facelet-taglib node"); //$NON-NLS-1$
        }

        for (int i = 0; i < curNode.getChildNodes().getLength(); i++)
        {
            final Node node = curNode.getChildNodes().item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE)
            {
                if (node.getTextContent() != null && ELEMENT_NAME_LIBRARY_CLASS.equals(node.getNodeName()))
                {
                    final FaceletLibraryClassTagLib faceletLibraryClassTagLib = FaceletTaglibFactory.eINSTANCE
                            .createFaceletLibraryClassTagLib();
                    faceletLibraryClassTagLib.setLibraryClass(node
                            .getTextContent().trim());
                    return faceletLibraryClassTagLib;
                }
                return processFaceletTaglibWithTags(curNode);
            }
        }

        return null;
    }

    private static FaceletXMLDefnTaglib processFaceletTaglibWithTags(
            final Node node)
    {
        String namespace = null;
        final List<Node> tagNodes = new ArrayList<Node>();

        for (int i = 0; i < node.getChildNodes().getLength(); i++)
        {
            final Node childNode = node.getChildNodes().item(i);

            if (childNode.getNodeType() == Node.ELEMENT_NODE)
            {
                if (ELEMENT_NAME_NAMESPACE.equals(childNode.getNodeName()))
                {
                    namespace = childNode.getTextContent();
                }
                else if (ELEMENT_NAME_TAG.equals(childNode.getNodeName()))
                {
                    tagNodes.add(childNode);
                }
            }
        }

        if (namespace != null)
        {
            final FaceletXMLDefnTaglib xmlDefnTaglib = FaceletTaglibFactory.eINSTANCE
                    .createFaceletXMLDefnTaglib();
            xmlDefnTaglib.setNamespace(namespace);

            for (final Node tagNode : tagNodes)
            {
                final TagDefn tag = createTagFromNode(namespace, tagNode);

                if (tag != null)
                {
                    xmlDefnTaglib.getTags().add(tag);
                }
            }

            return xmlDefnTaglib;
        }
        return null;
    }

    private static TagDefn createTagFromNode(final String namespace,
            final Node tagNode)
    {
        final Map<String, Node> children = indexChildren(tagNode);

        Node node = children.get(ELEMENT_NAME_TAG_NAME);

        if (node != null)
        {
            final String name = safeGetTextContext(node);

            if (name == null)
            {
                return null;
            }

            node = children.get(ELEMENT_NAME_HANDLER_CLASS);

            if (node != null)
            {
                final HandlerTagDefn handlerTag = FaceletTaglibFactory.eINSTANCE
                        .createHandlerTagDefn();
                handlerTag.setHandlerClass(safeGetTextContext(node));
                handlerTag.setName(name);
                return handlerTag;
            }

            node = children.get(ELEMENT_NAME_SOURCE);

            if (node != null)
            {
                final SourceTagDefn sourceTag = FaceletTaglibFactory.eINSTANCE
                        .createSourceTagDefn();
                sourceTag.setSource(safeGetTextContext(node));
                sourceTag.setName(name);
                return sourceTag;
            }

            node = children.get(ELEMENT_NAME_COMPONENT);

            if (node != null)
            {
                return createTagWithComponentType(namespace, name, node);
            }

            node = children.get(ELEMENT_NAME_CONVERTER);

            if (node != null)
            {
                return createTagWithConverter(namespace, name, node);
            }

            node = children.get(ELEMENT_NAME_VALIDATOR);

            if (node != null)
            {
                return createValidatorTag(namespace, name, node);
            }
        }

        return null;
    }

    private static ComponentTagDefn createTagWithComponentType(
            final String uri, final String tagName, final Node paramNode)
    {
        final Map<String, Node> componentChildren = indexChildren(paramNode);
        Node node = componentChildren.get(ELEMENT_NAME_COMPONENT_TYPE);

        if (node != null)
        {
            final String componentType = safeGetTextContext(node);
            String rendererType = null;
            String handlerClass = null;

            node = componentChildren.get(ELEMENT_NAME_RENDERER_TYPE);
            if (node != null)
            {
                rendererType = safeGetTextContext(node);
            }

            node = componentChildren.get(ELEMENT_NAME_HANDLER_CLASS);
            if (node != null)
            {
                handlerClass = safeGetTextContext(node);
            }

            final ComponentTagDefn componentTag = FaceletTaglibFactory.eINSTANCE
                    .createComponentTagDefn();
            componentTag.setName(tagName);
            componentTag.setComponentType(componentType);
            componentTag.setHandlerClass(handlerClass);
            componentTag.setRendererType(rendererType);
            return componentTag;
        }
        return null;
    }

    private static ConverterTagDefn createTagWithConverter(final String uri,
            final String tagName, final Node paramNode)
    {
        final Map<String, Node> converterChildren = indexChildren(paramNode);
        Node node = converterChildren.get(ELEMENT_NAME_CONVERTER_ID);

        if (node != null)
        {
            final String converterId = safeGetTextContext(node);
            String handlerClass = null;

            node = converterChildren.get(ELEMENT_NAME_HANDLER_CLASS);
            if (node != null)
            {
                handlerClass = safeGetTextContext(node);
            }

            final ConverterTagDefn converterTag = FaceletTaglibFactory.eINSTANCE
                    .createConverterTagDefn();
            converterTag.setName(tagName);
            converterTag.setConverterId(converterId);
            converterTag.setHandlerClass(handlerClass);
            return converterTag;
        }
        return null;
    }

    private static ValidatorTagDefn createValidatorTag(final String uri,
            final String tagName, final Node paramNode)
    {
        final Map<String, Node> converterChildren = indexChildren(paramNode);
        Node node = converterChildren.get(ELEMENT_NAME_VALIDATOR_ID);

        if (node != null)
        {
            final String validatorId = safeGetTextContext(node);
            String handlerClass = null;

            node = converterChildren.get(ELEMENT_NAME_HANDLER_CLASS);
            if (node != null)
            {
                handlerClass = safeGetTextContext(node);
            }

            final ValidatorTagDefn validatorTag = FaceletTaglibFactory.eINSTANCE
                    .createValidatorTagDefn();
            validatorTag.setName(tagName);
            validatorTag.setHandlerClass(handlerClass);
            validatorTag.setValidatorId(validatorId);
            return validatorTag;
        }
        return null;
    }

    private static Map<String, Node> indexChildren(final Node node)
    {
        final Map<String, Node> children = new HashMap<String, Node>();
        final NodeList nodeList = node.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++)
        {
            final Node childNode = nodeList.item(i);

            if (childNode.getNodeType() == Node.ELEMENT_NODE)
            {
                children.put(childNode.getNodeName(), childNode);
            }
        }

        return children;
    }

    private static String safeGetTextContext(final Node node)
    {
        String textContent = node.getTextContent();
        if (textContent == null)
        {
            return null;
        }

        textContent = textContent.trim();

        if ("".equals(textContent)) //$NON-NLS-1$
        {
            return null;
        }

        return textContent;
    }
}

Back to the top