Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b03c055a87e36db98d310f4a7cfa591c2b7d9e1 (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
/*******************************************************************************
 * 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.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.jsf.common.ui.internal.utils.ResourceUtils;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.dom.DOMUtil;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.Element;

/**
 * For some tag, could generate some XML code.
 * 
 * @author mengbo
 * @version 1.5
 */
public abstract class HTMLStringTagConverter extends AbstractTagConverter {

	static Logger _log = PDPlugin.getLogger(HTMLStringTagConverter.class);

	/**
	 * @param host
	 */
	public HTMLStringTagConverter(Element host) {
		super(host);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.converter.AbstractTagConverter#doConvertRefresh()
	 */
	protected Element doConvertRefresh() {
		// following are XML implementation. Assume the string is welformed HTML
		// try
		// {
		// String html = getGeneratedHTML();
		// DocumentBuilder builder =
		// DocumentBuilderFactory.newInstance().newDocumentBuilder();
		// Element result = builder.parse(new InputSource(new
		// StringReader(html))).getDocumentElement();
		// return (Element)DOMUtil.cloneNodeDeep(this.getDestDocument(),
		// result);
		// }
		// catch(Exception ex)
		// {
		// Element temp = createElement("div");
		// temp.appendChild(createText("ERROR: "+ex.getMessage()));
		// return temp;
		// }
		InputStream stream = null;
		try {
			String id = "" + System.currentTimeMillis() + ".html";
			IModelManager manager = StructuredModelManager.getModelManager();
			stream = new ByteArrayInputStream(getGeneratedHTML().getBytes());
			IDOMModel model = (IDOMModel) manager.getModelForRead(id, stream,
					null);
			Element root = model.getDocument().getDocumentElement();
			Element resultEle = (Element) DOMUtil.cloneNodeDeepIgnoreError(
					getDestDocument(), root);
			model.releaseFromRead();
			return resultEle;
		} catch (Exception ex) {
			_log.error("Log.Error.HTMLStringTagConverter.Error", ex);
			Element temp = createElement("div");
			temp.appendChild(createText("Error loading: " + ex.getMessage()));
			return temp;
		} finally {
			ResourceUtils.ensureClosed(stream);
		}
	}

	/**
	 * @return the generated HTML string
	 * @throws Exception
	 */
	public abstract String getGeneratedHTML() throws Exception;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.converter.ITagConverter#isMultiLevel()
	 */
	public boolean isMultiLevel() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.style.ITagEditInfo#isWidget()
	 */
	public boolean isWidget() {
		return true;
	}
}

Back to the top