Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: c1cfcc1502fd8893f694389c6245ff4d527d7519 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.html.core.internal.document;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.html.core.internal.cleanup.HTMLCleanupProcessorImpl;
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences;
import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocumentType;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

/**
 */
public class HTMLConverter {

	/**
	 */
	public HTMLConverter() {
		super();

	}

	public void cleanupModel(IDOMModel model) {
		if (model == null)
			return;

		HTMLCleanupProcessorImpl processor = new HTMLCleanupProcessorImpl();
		IStructuredCleanupPreferences pref = processor.getCleanupPreferences();

		// backup options
		boolean compressEmptyElementTags = pref.getCompressEmptyElementTags();
		boolean insertRequiredAttrs = pref.getInsertRequiredAttrs();
		boolean insertMissingTags = pref.getInsertMissingTags();
		boolean quoteAttrValues = pref.getQuoteAttrValues();
		boolean formatSource = pref.getFormatSource();
		int tagNameCase = pref.getTagNameCase();
		int attrNameCase = pref.getAttrNameCase();

		// setup options
		pref.setCompressEmptyElementTags(true);
		pref.setInsertRequiredAttrs(true);
		pref.setInsertMissingTags(true);
		pref.setQuoteAttrValues(true);
		pref.setFormatSource(false);
		if (model.getDocument().isXMLType()) { // XHTML
			pref.setTagNameCase(HTMLCorePreferenceNames.LOWER);
			pref.setAttrNameCase(HTMLCorePreferenceNames.LOWER);
		}
		else {
			pref.setTagNameCase(HTMLCorePreferenceNames.ASIS);
			pref.setAttrNameCase(HTMLCorePreferenceNames.ASIS);
		}

		processor.cleanupModel(model);

		// set back options
		pref.setCompressEmptyElementTags(compressEmptyElementTags);
		pref.setInsertRequiredAttrs(insertRequiredAttrs);
		pref.setInsertMissingTags(insertMissingTags);
		pref.setQuoteAttrValues(quoteAttrValues);
		pref.setFormatSource(formatSource);
		pref.setTagNameCase(tagNameCase);
		pref.setAttrNameCase(attrNameCase);
	}

	/**
	 * declaratoin: "data" for XML declaration, such as "version=\"1.0\""
	 * publicId: publicId for DOCTYPE declaration
	 */
	public void convert(IDOMModel model, String declaration, String publicId) {
		if (model == null)
			return;
		setDeclaration(model, declaration, publicId);
		cleanupModel(model);
	}

	/**
	 * declaratoin: "data" for XML declaration, such as "version=\"1.0\""
	 * publicId: publicId for DOCTYPE declaration
	 */
	public void convert(InputStream input, OutputStream output, String declaration, String publicId) throws UnsupportedEncodingException, IOException, CoreException {
		IDOMModel model = readModel(input);
		if (model == null)
			return;
		try {
			convert(model, declaration, publicId);
			writeModel(model, output);
		}
		finally {
			if (model != null)
				model.releaseFromEdit();
		}
	}

	/**
	 * declaratoin: "data" for XML declaration, such as "version=\"1.0\""
	 * publicId: publicId for DOCTYPE declaration
	 */
	public void convert(IFile file, String declaration, String publicId) throws IOException, CoreException {
		IDOMModel model = readModel(file);
		if (model == null)
			return;
		try {
			convert(model, declaration, publicId);
			writeModel(model, file);
		}
		finally {
			if (model != null)
				model.releaseFromEdit();
		}
	}

	/**
	 */
	private static void insertBreak(IDOMModel model, Node node) {
		if (model == null || node == null)
			return;
		if (node.getNodeType() == Node.TEXT_NODE)
			return;

		// get delimiter string
		IStructuredDocument structuredDocument = model.getStructuredDocument();
		if (structuredDocument == null)
			return;
		String delim = structuredDocument.getLineDelimiter();
		if (delim == null || delim.length() == 0)
			return;

		Node parent = node.getParentNode();
		if (parent == null)
			return;
		Document document = node.getOwnerDocument();
		if (document == null)
			return;
		Text text = document.createTextNode(delim);
		parent.insertBefore(text, node);
	}

	/**
	 */
	private IDOMModel readModel(InputStream input) throws IOException, UnsupportedEncodingException {
		if (input == null)
			return null;
		// create temporary model
		String id = input.toString() + ".html"; //$NON-NLS-1$
		IModelManager manager = StructuredModelManager.getModelManager();
		IStructuredModel model = manager.getModelForEdit(id, input, null);
		if (!(model instanceof IDOMModel)) {
			if (model != null)
				model.releaseFromEdit();
			return null;
		}
		return (IDOMModel) model;
	}

	/**
	 */
	private IDOMModel readModel(IFile file) throws IOException, CoreException {
		if (file == null)
			return null;
		IModelManager manager = StructuredModelManager.getModelManager();
		IStructuredModel model = manager.getModelForEdit(file);
		if (!(model instanceof IDOMModel)) {
			if (model != null)
				model.releaseFromEdit();
			return null;
		}
		return (IDOMModel) model;
	}

	/**
	 */
	public void setDeclaration(IDOMModel model, String declaration, String publicId) {
		if (model == null)
			return;
		IDOMDocument document = model.getDocument();
		if (document == null)
			return;

		try {
			model.aboutToChangeModel();

			ProcessingInstruction pi = null;
			Node child = document.getFirstChild();
			if (child != null && child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
				String target = child.getNodeName();
				if (target != null && target.equals("xml")) { //$NON-NLS-1$
					pi = (ProcessingInstruction) child;
					child = child.getNextSibling();
				}
			}
			IDOMDocumentType docType = (IDOMDocumentType) document.getDoctype();

			if (declaration != null) {
				if (pi != null) {
					pi.setData(declaration);
				}
				else {
					pi = document.createProcessingInstruction("xml", declaration); //$NON-NLS-1$
					document.insertBefore(pi, child);
					insertBreak(model, child);
				}
			}

			if (publicId != null) {
				HTMLDocumentTypeEntry entry = HTMLDocumentTypeRegistry.getInstance().getEntry(publicId);
				String name = (entry != null ? entry.getName() : null);
				if (name == null || name.length() == 0)
					name = "HTML"; // default //$NON-NLS-1$
				if (docType != null) {
					if (!name.equals(docType.getName())) { // replace
						Node parent = docType.getParentNode();
						child = docType;
						docType = (IDOMDocumentType) document.createDoctype(name);
						parent.insertBefore(docType, child);
						parent.removeChild(child);
					}
				}
				else {
					docType = (IDOMDocumentType) document.createDoctype(name);
					document.insertBefore(docType, child);
					insertBreak(model, child);
				}
				docType.setPublicId(publicId);
				if (entry != null) {
					String systemId = entry.getSystemId();
					if (systemId != null)
						docType.setSystemId(systemId);
					String namespaceURI = entry.getNamespaceURI();
					if (namespaceURI != null) {
						Element element = document.getDocumentElement();
						if (element != null) {
							element.setAttribute("xmlns", namespaceURI); //$NON-NLS-1$
						}
					}
				}
			}
		}
		finally {
			model.changedModel();
		}
	}

	/**
	 */
	private void writeModel(IDOMModel model, OutputStream output) throws UnsupportedEncodingException, IOException, CoreException {
		if (model == null || output == null)
			return;
		model.save();
	}

	/**
	 */
	private void writeModel(IDOMModel model, IFile file) throws IOException, CoreException {
		if (model == null || file == null)
			return;
		IStructuredDocument structuredDocument = model.getStructuredDocument();
		if (structuredDocument == null)
			return;
		//ByteArrayOutputStream output = null;
		ByteArrayInputStream input = null;
		try {
			//output = new
			// ByteArrayOutputStream(structuredDocument.getLength());
			model.save();
			//input = new ByteArrayInputStream(output.toByteArray());
			//file.setContents(input, true, true, null);
		}
		finally {
			//			if (output != null)
			//				output.close();
			if (input != null)
				input.close();
		}

	}
}

Back to the top