Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53e4f1b655416247e2cffea50db85b30592e2d7c (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.converter;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.jst.jsf.common.ui.internal.utils.JSFSharedImages;
import org.eclipse.jst.jsf.core.internal.tld.CMUtil;
import org.eclipse.jst.jsf.core.internal.tld.ITLDConstants;
import org.eclipse.jst.pagedesigner.PageDesignerTraceOptions;
import org.eclipse.jst.pagedesigner.converter.html.HTMLConverterFactory;
import org.eclipse.jst.pagedesigner.converter.jsp.JSPConverterFactory;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.w3c.dom.Element;

/**
 * @author mengbo
 * @version 1.5
 */
public class ConverterFactoryRegistry
{
    List _factories = new ArrayList();

    private static ConverterFactoryRegistry _instance;

    /**
	 *
	 */
    private ConverterFactoryRegistry()
    {
        final List<IConverterFactory> facs = ConverterFacRegistryReader
                .getAllHandlers();
        if (facs != null)
        {
            for (final IConverterFactory fac : facs)
            {
                addFactory(fac);
            }
        }
        _factories.add(new JSPConverterFactory());
        _factories.add(new HTMLConverterFactory());
        
        // TODO: this is not ideal, but until we get a better system for 
        // doing converter factory ordering:
        // loop through the list and place the DTManager
    }

    /**
     * @param fac
     */
    public void addFactory(final IConverterFactory fac)
    {
        _factories.add(fac);
    }

    /**
     * @param ele
     * @param mode
     * @param targetDocument
     * @return the new btag converter
     */
    public ITagConverter createTagConverter(final Element ele, final int mode,
            final IDOMDocument targetDocument)
    {
        final ITagConverter converter = internalCreateTagConverter(ele, mode);
        if (converter != null)
        {
            converter.setDestDocument(targetDocument);
        }
        return converter;
    }

    /**
     * @param ele
     * @param mode
     * @return the new tag converter
     */
    protected final ITagConverter internalCreateTagConverter(final Element ele,
            final int mode)
    {
        final String uri = CMUtil.getElementNamespaceURI(ele);
        // first round, match uri
        for (int i = 0, size = _factories.size(); i < size; i++)
        {
            final IConverterFactory fac = (IConverterFactory) _factories.get(i);
            final String facuri = fac.getSupportedURI();
            if (facuri != null && facuri.equals(uri))
            {
                final ITagConverter converter = fac.createConverter(ele, mode);
                if (converter != null)
                {
                    if (PageDesignerTraceOptions.TRACE_CONVERTERSELECT)
                    {
                        PageDesignerTraceOptions
                                .log("ConverterFactoryRegistry: first loop, " //$NON-NLS-1$
                                        + String
                                                .format(
                                                        "Selected converter %s for uri=%s, tagname=%s", //$NON-NLS-1$
                                                        converter.getClass()
                                                                .getName(),
                                                        uri, ele.getLocalName()));
                    }
                    return converter;
                }
            }
        }
        // second round
        for (int i = 0, size = _factories.size(); i < size; i++)
        {
            final IConverterFactory fac = (IConverterFactory) _factories.get(i);
            final String facuri = fac.getSupportedURI();
            if (facuri == null)
            {
                final ITagConverter converter = fac.createConverter(ele, mode);
                if (converter != null)
                {
                    if (PageDesignerTraceOptions.TRACE_CONVERTERSELECT)
                    {
                        PageDesignerTraceOptions
                                .log("ConverterFactoryRegistry: second loop, " //$NON-NLS-1$
                                        + String
                                                .format(
                                                        "Selected converter %s for uri=%s, tagname=%s", //$NON-NLS-1$
                                                        converter.getClass()
                                                                .getName(),
                                                        uri, ele.getLocalName()));
                    }
                    return converter;
                }
            }
        }

        // can't find. We need some default tag converter for it.
        // if the tag is empty, show it as icon.
        if (uri == null || ITLDConstants.URI_HTML.equals(uri))
        {
            if (PageDesignerTraceOptions.TRACE_CONVERTERSELECT)
            {
                PageDesignerTraceOptions
                        .log("ConverterFactoryRegistry: factory not found, " //$NON-NLS-1$
                                + String
                                        .format(
                                                "Selected DumTagConverter for uri=%s, tagname=%s", //$NON-NLS-1$
                                                uri, ele.getLocalName()));
            }

            // basically, for HTML or non JSP tag, directly renders it.
            return new DumTagConverter(ele);
        }
        final CMElementDeclaration decl = CMUtil.getElementDeclaration(ele);
        if (decl == null)
        {
            if (PageDesignerTraceOptions.TRACE_CONVERTERSELECT)
            {
                PageDesignerTraceOptions
                        .log("ConverterFactoryRegistry: factory and decl not found, " //$NON-NLS-1$
                                + String
                                        .format(
                                                "Selected DumTagConverter for uri=%s, tagname=%s", //$NON-NLS-1$
                                                uri, ele.getLocalName()));
            }
            return new DumTagConverter(ele);
        }
        final int contentType = decl.getContentType();
        if (contentType == CMElementDeclaration.EMPTY)
        {
            if (PageDesignerTraceOptions.TRACE_CONVERTERSELECT)
            {
                PageDesignerTraceOptions
                        .log("ConverterFactoryRegistry: factory not found, content is EMPTY, " //$NON-NLS-1$
                                + String
                                        .format(
                                                "Selected HiddenTagConverter with UnknownImage for uri=%s, tagname=%s", //$NON-NLS-1$
                                                uri, ele.getLocalName()));
            }

            // if the tag is empty, show it as icon.
            return new HiddenTagConverter(ele, new LabelProvider()
            {
                @Override
                public Image getImage(final Object element)
                {
                    return getUnknownImage();
                }
            });
        }
        if (PageDesignerTraceOptions.TRACE_CONVERTERSELECT)
        {
            PageDesignerTraceOptions
                    .log("ConverterFactoryRegistry: fall-through to default case, " //$NON-NLS-1$
                            + String
                                    .format(
                                            "Selected DefaultUnknownTagConverter with UnknownImage for uri=%s, tagname=%s", //$NON-NLS-1$
                                            uri, ele.getLocalName()));
        }
        return new DefaultUnknownTagConverter(ele, mode);

    }

    private static Image getUnknownImage()
    {
        return JSFUICommonPlugin.getDefault().getImage(
                JSFSharedImages.DEFAULT_PALETTE_TAG_IMG);
    }

    /**
     * @return the singleton instance of the registry
     */
    public synchronized static ConverterFactoryRegistry getInstance()
    {
        if (_instance == null)
        {
            _instance = new ConverterFactoryRegistry();
        }
        return _instance;
    }
}

Back to the top