Skip to main content
summaryrefslogtreecommitdiffstats
blob: f1de9168af0154b39c30586f8b76ce5187674e84 (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
/*******************************************************************************
 * Copyright (c) 2001, 2005 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.jst.j2ee.commonarchivecore.internal;



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.command.AbstractCommand;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jst.j2ee.internal.xml.CollectingErrorHandler;
import org.eclipse.jst.j2ee.internal.xml.XmlDocumentReader;
import org.xml.sax.InputSource;

/**
 * Insert the type's description here. Creation date: (03/19/01 10:04:08 AM)
 * 
 * @author: Administrator
 */
public class ValidateXmlCommand extends AbstractCommand {
	protected List results;
	protected ModuleFile archive;
	public boolean validateNested = true;

	/**
	 * ValidateXmlCommand constructor comment.
	 */
	protected ValidateXmlCommand() {
		super();
	}

	/**
	 * ValidateXmlCommand constructor comment.
	 * 
	 * @param label
	 *            java.lang.String
	 * @param description
	 *            java.lang.String
	 */
	public ValidateXmlCommand(ModuleFile m) {
		super();
		archive = m;
	}

	/**
	 * ValidateXmlCommand constructor comment.
	 * 
	 * @param label
	 *            java.lang.String
	 */
	protected ValidateXmlCommand(String label) {
		super(label);
	}

	/**
	 * ValidateXmlCommand constructor comment.
	 * 
	 * @param label
	 *            java.lang.String
	 * @param description
	 *            java.lang.String
	 */
	protected ValidateXmlCommand(String label, String description) {
		super(label, description);
	}

	/**
	 * @see Command
	 */
	public void execute() {
		results = new ArrayList();
		validatateXml();
		if (isValidateNested()) {
			List archives = archive.getArchiveFiles();
			for (int i = 0; i < archives.size(); i++) {
				Archive a = (Archive) archives.get(i);
				if (!a.isModuleFile())
					continue;
				ModuleFile m = (ModuleFile) a;
				ValidateXmlCommand cmd = new ValidateXmlCommand(m);
				cmd.execute();
				results.addAll(cmd.getResult());
			}
		}
	}

	/**
	 * @return List of XmlValidationResult; 1 for the archive, and one for each nested module file
	 */
	public Collection getResult() {
		return results;
	}

	/**
	 * Insert the method's description here. Creation date: (10/22/2001 1:06:52 PM)
	 * 
	 * @return boolean
	 */
	public boolean isValidateNested() {
		return validateNested;
	}

	protected boolean prepare() {
		return true;
	}

	/**
	 * @see Command
	 */
	public void redo() {
		//Default
	}

	/**
	 * Insert the method's description here. Creation date: (10/22/2001 1:06:52 PM)
	 * 
	 * @param newValidateNested
	 *            boolean
	 */
	public void setValidateNested(boolean newValidateNested) {
		validateNested = newValidateNested;
	}

	protected void validatateXml() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Resource res = archive.getStandardDeploymentDescriptor().eResource();
		XmlValidationResult result = new XmlValidationResult();
		result.setArchive(archive);
		try {
			res.save(bos, new java.util.HashMap());
		} catch (Exception ex) {
			throw new org.eclipse.jst.j2ee.commonarchivecore.internal.exception.ArchiveRuntimeException(ex);
		}
		ByteArrayInputStream inStream = new ByteArrayInputStream(bos.toByteArray());
		InputSource source = new InputSource(inStream);
		CollectingErrorHandler handler = new CollectingErrorHandler();
		XmlDocumentReader parseAdapter = new XmlDocumentReader(source, null, handler);

		// the following try/catch clause is added to handle the case
		// when SAX parser throws a fatal exception (type SAXException)
		// for unmatching end tag that results in a RuntimeException to
		// be thrown. Need to catch it so we can get the parser exceptions
		// and display them to the user.
		try {
			parseAdapter.parseDocument();
		} catch (RuntimeException re) {

			if (handler.getCaughtExceptions() != null) {
				result.setArchive(archive);
				result.setCaughtExceptions(handler.getCaughtExceptions());
				results.add(result);
			}

			throw re;
		}

		result.setArchive(archive);
		result.setCaughtExceptions(handler.getCaughtExceptions());
		results.add(result);
	}
}

Back to the top