Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 129877 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/wst/html/core/internal/cleanup/ElementNodeCleanupHandler.java (+82 lines)
Lines 27-32 Link Here
27
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
27
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
28
import org.eclipse.wst.html.core.internal.Logger;
28
import org.eclipse.wst.html.core.internal.Logger;
29
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
29
import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
30
import org.eclipse.wst.html.core.internal.provisional.HTMLCMProperties;
30
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupHandler;
31
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupHandler;
31
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
32
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
32
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
33
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
Lines 52-57 Link Here
52
import org.w3c.dom.Element;
53
import org.w3c.dom.Element;
53
import org.w3c.dom.NamedNodeMap;
54
import org.w3c.dom.NamedNodeMap;
54
import org.w3c.dom.Node;
55
import org.w3c.dom.Node;
56
import org.w3c.dom.NodeList;
55
57
56
// nakamori_TODO: check and remove CSS formatting
58
// nakamori_TODO: check and remove CSS formatting
57
59
Lines 105-114 Link Here
105
		// there are any
107
		// there are any
106
		// insertMissingTags() will return the new missing start tag if one is
108
		// insertMissingTags() will return the new missing start tag if one is
107
		// missing
109
		// missing
110
		// then compress any empty element tags
108
		// applyTagNameCase() will return the renamed node.
111
		// applyTagNameCase() will return the renamed node.
109
		// The renamed/new node will be saved and returned to caller when all
112
		// The renamed/new node will be saved and returned to caller when all
110
		// cleanup is done.
113
		// cleanup is done.
111
		renamedNode = insertMissingTags(renamedNode);
114
		renamedNode = insertMissingTags(renamedNode);
115
		renamedNode = compressEmptyElementTag(renamedNode);
112
		renamedNode = insertRequiredAttrs(renamedNode);
116
		renamedNode = insertRequiredAttrs(renamedNode);
113
		renamedNode = applyTagNameCase(renamedNode);
117
		renamedNode = applyTagNameCase(renamedNode);
114
		applyAttrNameCase(renamedNode);
118
		applyAttrNameCase(renamedNode);
Lines 692-695 Link Here
692
696
693
		return result;
697
		return result;
694
	}
698
	}
699
	
700
	/**
701
	 * <p>Compress empty element tags if the prefence is set to do so</p>
702
	 * 
703
	 * @copyof org.eclipse.wst.xml.core.internal.cleanup.ElementNodeCleanupHandler#compressEmptyElementTag
704
	 * 
705
	 * @param node the {@link IDOMNode} to possible compress
706
	 * @return the compressed node if the given node should be compressed, else the node as it was given
707
	 */
708
	private IDOMNode compressEmptyElementTag(IDOMNode node) {
709
		boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
710
		IDOMNode newNode = node;
711
712
		IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
713
		IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
714
		
715
		//assume the end tag is omissible unless an CMElementDeclaration says otherwise
716
		boolean endTagOmissible = true;
717
		CMElementDeclaration dec = getDeclaration(newNode);
718
		if(dec != null) {
719
			endTagOmissible = isEndTagOmissible(dec);
720
		}
721
		
722
		//only compress tags if it is allowed and they are empty
723
		if ((compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion &&
724
				startTagStructuredDocumentRegion != null && endTagOmissible)) {
725
			
726
			ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
727
			ITextRegion lastRegion = regions.get(regions.size() - 1);
728
			// format children and end tag if not empty element tag
729
			if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
730
				NodeList childNodes = newNode.getChildNodes();
731
				if (childNodes == null || childNodes.getLength() == 0 || (childNodes.getLength() == 1 && (childNodes.item(0)).getNodeType() == Node.TEXT_NODE && ((childNodes.item(0)).getNodeValue().trim().length() == 0))) {
732
					IDOMModel structuredModel = newNode.getModel();
733
					IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
734
735
					int startTagStartOffset = newNode.getStartOffset();
736
					int offset = endTagStructuredDocumentRegion.getStart();
737
					int length = endTagStructuredDocumentRegion.getLength();
738
					structuredDocument.replaceText(structuredDocument, offset, length, ""); //$NON-NLS-1$
739
					newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset); // save
740
741
					offset = startTagStructuredDocumentRegion.getStart() + lastRegion.getStart();
742
					structuredDocument.replaceText(structuredDocument, offset, 0, "/"); //$NON-NLS-1$
743
					newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset); // save
744
				}
745
			}
746
		}
747
748
		return newNode;
749
	}
750
	
751
	/**
752
	 * @param node {@link IDOMNode} to get a {@link CMElementDeclaration} for
753
	 * @return {@link CMElementDeclaration} for the given {@link IDOMNode} if one
754
	 * can be found, else <code>null</code>
755
	 */
756
	private static CMElementDeclaration getDeclaration(IDOMNode node) {
757
		CMElementDeclaration dec = null;
758
		if(node instanceof IDOMElement) {
759
			Document doc = node.getOwnerDocument();
760
			ModelQuery query = ModelQueryUtil.getModelQuery(doc);
761
			dec = query.getCMElementDeclaration((IDOMElement)node);
762
		}
763
		return dec;
764
	}
765
	
766
	/**
767
	 * @param decl determine if the end target is omissible for this {@link CMElementDeclaration}
768
	 * @return <code>true</code> if the end tag is omissible for the given
769
	 * {@link CMElementDeclaration}, <code>false</code> otherwise.
770
	 */
771
	private static boolean isEndTagOmissible(CMElementDeclaration decl) {
772
		if (!(decl.supports(HTMLCMProperties.OMIT_TYPE)))
773
			return false;
774
		String omitType = (String) decl.getProperty(HTMLCMProperties.OMIT_TYPE);
775
		return !omitType.equals(HTMLCMProperties.Values.OMIT_NONE);
776
	}
695
}
777
}
(-)src/org/eclipse/wst/html/ui/internal/edit/ui/CleanupDialogHTML.java (+9 lines)
Lines 28-33 Link Here
28
import org.eclipse.wst.html.ui.internal.HTMLUIMessages;
28
import org.eclipse.wst.html.ui.internal.HTMLUIMessages;
29
import org.eclipse.wst.html.ui.internal.editor.IHelpContextIds;
29
import org.eclipse.wst.html.ui.internal.editor.IHelpContextIds;
30
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
30
import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames;
31
import org.eclipse.wst.xml.ui.internal.XMLUIMessages;
31
32
32
public class CleanupDialogHTML extends Dialog implements SelectionListener {
33
public class CleanupDialogHTML extends Dialog implements SelectionListener {
33
34
Lines 37-42 Link Here
37
	protected Button fRadioButtonAttrNameCaseAsis;
38
	protected Button fRadioButtonAttrNameCaseAsis;
38
	protected Button fRadioButtonAttrNameCaseLower;
39
	protected Button fRadioButtonAttrNameCaseLower;
39
	protected Button fRadioButtonAttrNameCaseUpper;
40
	protected Button fRadioButtonAttrNameCaseUpper;
41
	protected Button fCheckBoxCompressEmptyElementTags;
40
	protected Button fCheckBoxInsertRequiredAttrs;
42
	protected Button fCheckBoxInsertRequiredAttrs;
41
	protected Button fCheckBoxInsertMissingTags;
43
	protected Button fCheckBoxInsertMissingTags;
42
	protected Button fCheckBoxQuoteAttrValues;
44
	protected Button fCheckBoxQuoteAttrValues;
Lines 118-123 Link Here
118
		layout.makeColumnsEqualWidth = true;
120
		layout.makeColumnsEqualWidth = true;
119
		composite.setLayout(layout);
121
		composite.setLayout(layout);
120
122
123
		// Compress empty element tags
124
		fCheckBoxCompressEmptyElementTags = new Button(composite, SWT.CHECK);
125
		fCheckBoxCompressEmptyElementTags.setText(XMLUIMessages.Compress_empty_element_tags_UI_);
126
		fCheckBoxCompressEmptyElementTags.addSelectionListener(this);
127
		
121
		// Insert missing required attrs
128
		// Insert missing required attrs
122
		fCheckBoxInsertRequiredAttrs = new Button(composite, SWT.CHECK);
129
		fCheckBoxInsertRequiredAttrs = new Button(composite, SWT.CHECK);
123
		fCheckBoxInsertRequiredAttrs.setText(HTMLUIMessages.Insert_required_attributes_UI_);
130
		fCheckBoxInsertRequiredAttrs.setText(HTMLUIMessages.Insert_required_attributes_UI_);
Lines 196-201 Link Here
196
	protected void initializeOptions() {
203
	protected void initializeOptions() {
197
		initializeOptionsForHTML();
204
		initializeOptionsForHTML();
198
205
206
		fCheckBoxCompressEmptyElementTags.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.COMPRESS_EMPTY_ELEMENT_TAGS));
199
		fCheckBoxInsertRequiredAttrs.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.INSERT_REQUIRED_ATTRS));
207
		fCheckBoxInsertRequiredAttrs.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.INSERT_REQUIRED_ATTRS));
200
		fCheckBoxInsertMissingTags.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.INSERT_MISSING_TAGS));
208
		fCheckBoxInsertMissingTags.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.INSERT_MISSING_TAGS));
201
		fCheckBoxQuoteAttrValues.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.QUOTE_ATTR_VALUES));
209
		fCheckBoxQuoteAttrValues.setSelection(getModelPreferences().getBoolean(HTMLCorePreferenceNames.QUOTE_ATTR_VALUES));
Lines 235-240 Link Here
235
	protected void storeOptions() {
243
	protected void storeOptions() {
236
		storeOptionsForHTML();
244
		storeOptionsForHTML();
237
245
246
		getModelPreferences().setValue(HTMLCorePreferenceNames.COMPRESS_EMPTY_ELEMENT_TAGS, fCheckBoxCompressEmptyElementTags.getSelection());
238
		getModelPreferences().setValue(HTMLCorePreferenceNames.INSERT_REQUIRED_ATTRS, fCheckBoxInsertRequiredAttrs.getSelection());
247
		getModelPreferences().setValue(HTMLCorePreferenceNames.INSERT_REQUIRED_ATTRS, fCheckBoxInsertRequiredAttrs.getSelection());
239
		getModelPreferences().setValue(HTMLCorePreferenceNames.INSERT_MISSING_TAGS, fCheckBoxInsertMissingTags.getSelection());
248
		getModelPreferences().setValue(HTMLCorePreferenceNames.INSERT_MISSING_TAGS, fCheckBoxInsertMissingTags.getSelection());
240
		getModelPreferences().setValue(HTMLCorePreferenceNames.QUOTE_ATTR_VALUES, fCheckBoxQuoteAttrValues.getSelection());
249
		getModelPreferences().setValue(HTMLCorePreferenceNames.QUOTE_ATTR_VALUES, fCheckBoxQuoteAttrValues.getSelection());
(-)src/org/eclipse/wst/html/core/tests/HTMLCoreTestSuite.java (+2 lines)
Lines 13-18 Link Here
13
import junit.framework.Test;
13
import junit.framework.Test;
14
import junit.framework.TestSuite;
14
import junit.framework.TestSuite;
15
15
16
import org.eclipse.wst.html.core.tests.cleanup.TestHTMLCleanupProcessor;
16
import org.eclipse.wst.html.core.tests.format.TestFormatProcessorHTML;
17
import org.eclipse.wst.html.core.tests.format.TestFormatProcessorHTML;
17
import org.eclipse.wst.html.core.tests.misc.HTMLCorePreferencesTest;
18
import org.eclipse.wst.html.core.tests.misc.HTMLCorePreferencesTest;
18
import org.eclipse.wst.html.core.tests.misc.HTMLTagInfoTest;
19
import org.eclipse.wst.html.core.tests.misc.HTMLTagInfoTest;
Lines 47-51 Link Here
47
		addTest(new TestSuite(BUG124835SetStyleAttributeValueTest.class));
48
		addTest(new TestSuite(BUG124835SetStyleAttributeValueTest.class));
48
		addTest(new TestSuite(TestFormatProcessorHTML.class));
49
		addTest(new TestSuite(TestFormatProcessorHTML.class));
49
		addTest(new TestSuite(TestCatalogContentModels.class));
50
		addTest(new TestSuite(TestCatalogContentModels.class));
51
		addTest(TestHTMLCleanupProcessor.suite());
50
	}
52
	}
51
}
53
}
(-)testresources/HTMLCleanupProcessor/test2.html (+15 lines)
Added Link Here
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
6
<title>Insert title here</title>
7
</head>
8
<body>
9
10
<br>
11
12
<br></br>
13
14
</body>
15
</html>
(-)testresources/HTMLCleanupProcessor/test1-expected.html (+14 lines)
Added Link Here
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2
<html>
3
<head>
4
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5
<title>Insert title here</title>
6
</head>
7
<body>
8
9
<br>
10
11
<br/>
12
13
</body>
14
</html>
(-)src/org/eclipse/wst/html/core/tests/cleanup/TestHTMLCleanupProcessor.java (+234 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.wst.html.core.tests.cleanup;
12
13
import java.io.IOException;
14
15
import junit.extensions.TestSetup;
16
import junit.framework.Test;
17
import junit.framework.TestCase;
18
import junit.framework.TestSuite;
19
20
import org.eclipse.core.resources.IFile;
21
import org.eclipse.core.resources.IProject;
22
import org.eclipse.core.runtime.CoreException;
23
import org.eclipse.core.runtime.NullProgressMonitor;
24
import org.eclipse.jface.text.IDocument;
25
import org.eclipse.wst.html.core.internal.cleanup.HTMLCleanupProcessorImpl;
26
import org.eclipse.wst.html.core.tests.ProjectUtil;
27
import org.eclipse.wst.sse.core.StructuredModelManager;
28
import org.eclipse.wst.sse.core.internal.cleanup.AbstractStructuredCleanupProcessor;
29
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
30
import org.eclipse.wst.sse.core.utils.StringUtils;
31
32
public class TestHTMLCleanupProcessor extends TestCase {
33
	/**
34
	 * The name of the project that all of these tests will use
35
	 */
36
	private static final String PROJECT_NAME = "TestHTMLCleanupProcessor";
37
	
38
	/**
39
	 * The location of the testing files
40
	 */
41
	private static final String PROJECT_FILES = "/testresources/HTMLCleanupProcessor";
42
	
43
	/**
44
	 * The project that all of the tests use
45
	 */
46
	private static IProject fProject;
47
	
48
	/**
49
	 * Default constructor
50
	 */
51
	public TestHTMLCleanupProcessor() {
52
		super("Test HTML Cleanup Processor");
53
	}
54
	
55
	/**
56
	 * Constructor that takes a test name.
57
	 * 
58
	 * @param name The name this test run should have.
59
	 */
60
	public TestHTMLCleanupProcessor(String name) {
61
		super(name);
62
	}
63
	
64
	/**
65
	 * <p>Use this method to add these tests to a larger test suite so set up
66
	 * and tear down can be performed</p>
67
	 * 
68
	 * @return a {@link TestSetup} that will run all of the tests in this class
69
	 * with set up and tear down.
70
	 */
71
	public static Test suite() {
72
		TestSuite ts = new TestSuite(TestHTMLCleanupProcessor.class);
73
		return new TestHTMLCleanupProcessorSetup(ts);
74
75
	}
76
	
77
	/**
78
	 * <p><b>TEST:</b> collapsing empty tags in an html document</p>
79
	 */
80
	public void testCollapseEmptyTagsHTML()throws Exception  {
81
		HTMLCleanupProcessorImpl cleanupProcessor = getProcessorForForEmptyTagsTest();
82
		runTest("test1.html", "test1-expected.html", cleanupProcessor);
83
	}
84
	
85
	/**
86
	 * <p><b>TEST:</b> collapsing empty tags in an xhtml document</p>
87
	 */
88
	public void testCollapseEmptyTagsXHTML()throws Exception  {
89
		HTMLCleanupProcessorImpl cleanupProcessor = getProcessorForForEmptyTagsTest();
90
		runTest("test2.html", "test2-expected.html", cleanupProcessor);
91
	}
92
	
93
	/**
94
	 * @return a configured {@link HTMLCleanupProcessorImpl} for testing compressing empty tags
95
	 */
96
	private static HTMLCleanupProcessorImpl getProcessorForForEmptyTagsTest() {
97
		HTMLCleanupProcessorImpl cleanupProcessor = new HTMLCleanupProcessorImpl();
98
		cleanupProcessor.getCleanupPreferences().setCompressEmptyElementTags(true);
99
		cleanupProcessor.getCleanupPreferences().setInsertRequiredAttrs(false);
100
		cleanupProcessor.getCleanupPreferences().setInsertMissingTags(true);
101
		cleanupProcessor.getCleanupPreferences().setQuoteAttrValues(false);
102
		cleanupProcessor.getCleanupPreferences().setFormatSource(false);
103
		cleanupProcessor.getCleanupPreferences().setConvertEOLCodes(false);
104
		
105
		return cleanupProcessor;
106
	}
107
	
108
	/**
109
	 * <p>Runs an {@link AbstractStructuredCleanupProcessor} test</p>
110
	 * 
111
	 * @param originalFile file to clean up
112
	 * @param expectedResultsFile expected results of cleaning up the original file
113
	 * @param configuredCleanupProcessor the processor to use to do the cleaning
114
	 * 
115
	 * @throws Exception tests can throw exceptions now and then
116
	 */
117
	private void runTest(String originalFile, String expectedResultsFile,
118
			AbstractStructuredCleanupProcessor configuredCleanupProcessor) throws Exception {
119
		
120
		IStructuredModel model = null;
121
		IStructuredModel expectedModel = null;
122
		try {
123
			model = getModelForEdit(originalFile);
124
			expectedModel = getModelForEdit(expectedResultsFile);
125
			
126
			configuredCleanupProcessor.refreshCleanupPreferences = false;
127
			configuredCleanupProcessor.cleanupModel(model);
128
			configuredCleanupProcessor.refreshCleanupPreferences = true;
129
			
130
			model.save();
131
			
132
			standardizeLineEndings(model.getStructuredDocument());
133
			standardizeLineEndings(expectedModel.getStructuredDocument());
134
			
135
			assertEquals("Clean up results did not match expected results",
136
					expectedModel.getStructuredDocument().get(),
137
					model.getStructuredDocument().get());
138
		} finally {
139
			if(model != null) {
140
				model.releaseFromEdit();
141
			}
142
			
143
			if(expectedModel != null) {
144
				expectedModel.releaseFromEdit();
145
			}
146
		}
147
	}
148
	
149
	/**
150
	 * <p>Given a file name in <code>fProject</code> attempts to get a model
151
	 * for it, if the file doesn't exist or it can't get the model the test fails.</p>
152
	 * 
153
	 * @param filename
154
	 * @return
155
	 * @throws CoreException 
156
	 * @throws IOException 
157
	 */
158
	private IStructuredModel getModelForEdit(final String filename) throws IOException, CoreException {
159
		IFile file = fProject.getFile(filename);
160
		assertTrue("Test file " + file + " can not be found", file.exists());
161
		
162
		IStructuredModel model = StructuredModelManager.getModelManager().getModelForEdit(file);
163
		assertNotNull("Could not get model for " + file, model);
164
		
165
		return model;
166
	}
167
	
168
	
169
	/**
170
	 * <p>Line endings can be an issue when running tests on different OSs.
171
	 * This function standardizes the line endings to use <code>\n</code></p>
172
	 * 
173
	 * <p>It will get the text from the given editor, change the line endings,
174
	 * and then save the editor</p>
175
	 * 
176
	 * @param editor standardize the line endings of the text presented in this
177
	 * editor.
178
	 */
179
	private void standardizeLineEndings(IDocument doc) {
180
		String contents = doc.get();
181
		contents = StringUtils.replace(contents, "\r\n", "\n");
182
		contents = StringUtils.replace(contents, "\r", "\n");
183
		doc.set(contents);
184
	}
185
	
186
	/**
187
	 * <p>This inner class is used to do set up and tear down before and
188
	 * after (respectively) all tests in the inclosing class have run.</p>
189
	 */
190
	private static class TestHTMLCleanupProcessorSetup extends TestSetup {
191
		private static final String WTP_AUTOTEST_NONINTERACTIVE = "wtp.autotest.noninteractive";
192
		private static String previousWTPAutoTestNonInteractivePropValue = null;
193
		
194
		/**
195
		 * Default constructor
196
		 * 
197
		 * @param test do setup for the given test
198
		 */
199
		public TestHTMLCleanupProcessorSetup(Test test) {
200
			super(test);
201
		}
202
203
		/**
204
		 * <p>This is run once before all of the tests</p>
205
		 * 
206
		 * @see junit.extensions.TestSetup#setUp()
207
		 */
208
		public void setUp() throws Exception {
209
			fProject = ProjectUtil.createProject(PROJECT_NAME, null, null);
210
			ProjectUtil.copyBundleEntriesIntoWorkspace(PROJECT_FILES, PROJECT_NAME);
211
			
212
			String noninteractive = System.getProperty(WTP_AUTOTEST_NONINTERACTIVE);
213
			if (noninteractive != null) {
214
				previousWTPAutoTestNonInteractivePropValue = noninteractive;
215
			} else {
216
				previousWTPAutoTestNonInteractivePropValue = "false";
217
			}
218
			System.setProperty(WTP_AUTOTEST_NONINTERACTIVE, "true");
219
		}
220
221
		/**
222
		 * <p>This is run once after all of the tests have been run</p>
223
		 * 
224
		 * @see junit.extensions.TestSetup#tearDown()
225
		 */
226
		public void tearDown() throws Exception {
227
			fProject.delete(true, new NullProgressMonitor());
228
			
229
			if (previousWTPAutoTestNonInteractivePropValue != null) {
230
				System.setProperty(WTP_AUTOTEST_NONINTERACTIVE, previousWTPAutoTestNonInteractivePropValue);
231
			}
232
		}
233
	}
234
}
(-)testresources/HTMLCleanupProcessor/test2-expected.html (+15 lines)
Added Link Here
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
6
<title>Insert title here</title>
7
</head>
8
<body>
9
10
<br />
11
12
<br/>
13
14
</body>
15
</html>
(-)testresources/HTMLCleanupProcessor/test1.html (+14 lines)
Added Link Here
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2
<html>
3
<head>
4
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5
<title>Insert title here</title>
6
</head>
7
<body>
8
9
<br>
10
11
<br></br>
12
13
</body>
14
</html>
(-)src/org/eclipse/wst/html/core/tests/ProjectUtil.java (+127 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *     
11
 *******************************************************************************/
12
package org.eclipse.wst.html.core.tests;
13
14
import java.io.ByteArrayInputStream;
15
import java.io.ByteArrayOutputStream;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.net.URL;
19
import java.util.Enumeration;
20
21
import org.eclipse.core.resources.IFile;
22
import org.eclipse.core.resources.IFolder;
23
import org.eclipse.core.resources.IProject;
24
import org.eclipse.core.resources.IProjectDescription;
25
import org.eclipse.core.resources.IWorkspaceRunnable;
26
import org.eclipse.core.resources.ResourcesPlugin;
27
import org.eclipse.core.runtime.CoreException;
28
import org.eclipse.core.runtime.IPath;
29
import org.eclipse.core.runtime.IProgressMonitor;
30
import org.eclipse.core.runtime.NullProgressMonitor;
31
import org.eclipse.core.runtime.Path;
32
33
/**
34
 * 
35
 * @see org.eclipse.wst.xml.ui.tests.ProjectUtil Similar Project Utils
36
 * @see org.eclipse.wst.css.ui.tests.ProjectUtil Similar Project Utils
37
 * @see org.eclipse.wst.dtd.ui.tests.ProjectUtil Similar Project Utils
38
 */
39
public class ProjectUtil {
40
	public static IProject createProject(String name, IPath location, String[] natureIds) {
41
		IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(name);
42
		if (location != null) {
43
			description.setLocation(location);
44
		}
45
		if (natureIds != null) {
46
			description.setNatureIds(natureIds);
47
		}
48
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
49
		try {
50
			project.create(description, new NullProgressMonitor());
51
			project.open(new NullProgressMonitor());
52
		}
53
		catch (CoreException e) {
54
			e.printStackTrace();
55
		}
56
		return project;
57
	}
58
	
59
	/**
60
	 * @param rootEntry - avoid trailing separators
61
	 * @param fullTargetPath
62
	 */
63
	public static void copyBundleEntriesIntoWorkspace(final String rootEntry, final String fullTargetPath) {
64
		IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
65
			public void run(IProgressMonitor monitor) throws CoreException {
66
				_copyBundleEntriesIntoWorkspace(rootEntry, fullTargetPath);
67
				ResourcesPlugin.getWorkspace().checkpoint(true);
68
			}
69
		};
70
		try {
71
			ResourcesPlugin.getWorkspace().run(runnable, new NullProgressMonitor());
72
		}
73
		catch (CoreException e) {
74
			e.printStackTrace();
75
		}
76
	}
77
	
78
	static void _copyBundleEntriesIntoWorkspace(final String rootEntry, final String fullTargetPath) throws CoreException {
79
		Enumeration entries = HTMLCoreTestsPlugin.getDefault().getBundle().getEntryPaths(rootEntry);
80
		while (entries != null && entries.hasMoreElements()) {
81
			String entryPath = entries.nextElement().toString();
82
			String targetPath = new Path(fullTargetPath + "/" + entryPath.substring(rootEntry.length())).toString();
83
			if (entryPath.endsWith("/")) {
84
				IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(targetPath));
85
				if (!folder.exists()) {
86
					folder.create(true, true, new NullProgressMonitor());
87
				}
88
				_copyBundleEntriesIntoWorkspace(entryPath, targetPath);
89
			}
90
			else {
91
				_copyBundleEntryIntoWorkspace(entryPath, targetPath);
92
			}
93
		}
94
	}
95
	
96
	static IFile _copyBundleEntryIntoWorkspace(String entryname, String fullPath) throws CoreException {
97
		IFile file = null;
98
		URL entry = HTMLCoreTestsPlugin.getDefault().getBundle().getEntry(entryname);
99
		if (entry != null) {
100
			try {
101
				byte[] b = new byte[2048];
102
				InputStream input = entry.openStream();
103
				ByteArrayOutputStream output = new ByteArrayOutputStream();
104
				int i = -1;
105
				while ((i = input.read(b)) > -1) {
106
					output.write(b, 0, i);
107
				}
108
				file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fullPath));
109
				if (file != null) {
110
					if (!file.exists()) {
111
						file.create(new ByteArrayInputStream(output.toByteArray()), true, new NullProgressMonitor());
112
					}
113
					else {
114
						file.setContents(new ByteArrayInputStream(output.toByteArray()), true, false, new NullProgressMonitor());
115
					}
116
				}
117
			}
118
			catch (IOException e) {
119
				e.printStackTrace();
120
			}
121
			catch (CoreException e) {
122
				e.printStackTrace();
123
			}
124
		}
125
		return file;
126
	}
127
}

Return to bug 129877