Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba738678e47c706d9d64e4353e4143028151a9b2 (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
/*******************************************************************************
 * 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.jst.pagedesigner.IJMTConstants;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.converter.html.HTMLConverterFactory;
import org.eclipse.jst.pagedesigner.converter.jsp.JSPConverterFactory;
import org.eclipse.jst.pagedesigner.utils.CMUtil;
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() {
		_factories.add(new JSPConverterFactory());
		_factories.add(new HTMLConverterFactory());

		IConverterFactory facs[] = ConverterFacRegistryReader.getAllHandlers();
		if (facs != null) {
			for (int i = 0; i < facs.length; i++) {
				addFactory(facs[i]);
			}
		}
	}

	public void addFactory(IConverterFactory fac) {
		_factories.add(fac);
	}

	public ITagConverter createTagConverter(Element ele, int mode,
			IDOMDocument targetDocument) {
		ITagConverter converter = internalCreateTagConverter(ele, mode);
		if (converter != null) {
			converter.setDestDocument(targetDocument);
		}
		return converter;
	}

	public ITagConverter internalCreateTagConverter(Element ele, int mode) {
		String uri = CMUtil.getElementNamespaceURI(ele);
		// first round, match uri
		for (int i = 0, size = _factories.size(); i < size; i++) {
			IConverterFactory fac = (IConverterFactory) _factories.get(i);
			String facuri = fac.getSupportedURI();
			if (facuri != null && facuri.equals(uri)) {
				ITagConverter converter = fac.createConverter(ele, mode);
				if (converter != null) {
					return converter;
				}
			}
		}
		// second round
		for (int i = 0, size = _factories.size(); i < size; i++) {
			IConverterFactory fac = (IConverterFactory) _factories.get(i);
			String facuri = fac.getSupportedURI();
			if (facuri == null) {
				ITagConverter converter = fac.createConverter(ele, mode);
				if (converter != null) {
					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 || IJMTConstants.URI_HTML.equals(uri)) {
			// basically, for HTML or non JSP tag, directly renders it.
			return new DumTagConverter(ele);
		} else {
			CMElementDeclaration decl = CMUtil.getElementDeclaration(ele);
			if (decl == null) {
				return new DumTagConverter(ele);
			}
			int contentType = decl.getContentType();
			if (contentType == CMElementDeclaration.EMPTY) {
				// if the tag is empty, show it as icon.
				return new HiddenTagConverter(ele, getUnknownImage());
			} else {
				return new DefaultUnknownTagConverter(ele);
			}
		}

	}

	Image getUnknownImage() {
		return PDPlugin.getDefault().getImage(
				"palette/GENERIC/small/PD_Palette_Default.gif");
	}

	public static ConverterFactoryRegistry getInstance() {
		if (_instance == null) {
			_instance = new ConverterFactoryRegistry();
		}
		return _instance;
	}
}

Back to the top