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: 16da926575d7e15d8e258d258185e5c4257c5eaf (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*******************************************************************************
 * Copyright (c) 2001, 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.internal.contentproperties;



import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.wst.sse.core.internal.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;

/**
* @deprecated This is package protected so no one cares anyways.
*/
class SimpleNodeOperator {

	class CreateContentSettingsFailureException extends Exception {
		/**
		 * Comment for <code>serialVersionUID</code>
		 */
		private static final long serialVersionUID = 1L;

		public CreateContentSettingsFailureException(String reason) {
			super(reason);
		}
	}


	class ReadContentSettingsFailureException extends Exception {
		/**
		 * Comment for <code>serialVersionUID</code>
		 */
		private static final long serialVersionUID = 1L;

		public ReadContentSettingsFailureException(String reason) {
			super(reason);
		}
	}

	static class WriteContentSettingsFailureException extends Exception {
		/**
		 * Comment for <code>serialVersionUID</code>
		 */
		private static final long serialVersionUID = 1L;

		public WriteContentSettingsFailureException(String reason) {
			super(reason);
		}
	}

	// writer class for .contentSettings.
	class XMLDocumentWriter {
		OutputStream fOut;

		protected XMLDocumentWriter(OutputStream out) {
			this.fOut = out;
		}

		protected final void close() {
			try {
				fOut.close();
			} catch (IOException e) {
				// do nothing, shouldn't matter
			}
		}

		protected void serialize(Document sourceDocument) throws WriteContentSettingsFailureException {
			// JAXP transformation
			Source domSource = new DOMSource(sourceDocument);
			try {
				Transformer serializer = TransformerFactory.newInstance().newTransformer();
				try {
					serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
					serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
				} catch (IllegalArgumentException e) {
					// unsupported properties
				}
				serializer.transform(domSource, new StreamResult(fOut));
			} catch (TransformerConfigurationException e) {
				throw new WriteContentSettingsFailureException(e.getMessage());
			} catch (TransformerFactoryConfigurationError e) {
				throw new WriteContentSettingsFailureException(e.getMessage());
			} catch (TransformerException e) {
				throw new WriteContentSettingsFailureException(e.getMessage());
			}
		}
	}

	public static void main(String[] args) {
		SimpleNodeOperator a = null;
		try {
			a = new SimpleNodeOperator("workspace/org.eclipse.examples.contentsettings/.contentsettings.xml");//$NON-NLS-1$
		} catch (Exception e) {
			System.exit(0);
		}

		// print all Elements
		//a.printTree(iter);

		// add Element
		Map attMap = new Hashtable();
		attMap.put("path", "hogepath");//$NON-NLS-1$ //$NON-NLS-2$
		attMap.put("fDocument-type", "documenthogehoge");//$NON-NLS-1$ //$NON-NLS-2$
		a.addElementUnderRoot("file", attMap);//$NON-NLS-1$

		try {
			a.writeDocument(System.out);
		} catch (Exception e) {
			System.err.println(e.toString());
		}

	}

	//never used
	//private DOMParser parser;
	private Document fDocument;
	private Node root;

	private String settingsFileName;


	public SimpleNodeOperator(Document doc) throws CreateContentSettingsFailureException {

		if (doc == null)
			throw new CreateContentSettingsFailureException("Document doc==null");//$NON-NLS-1$
		fDocument = doc;
		root = fDocument.getLastChild();
		if (root == null)
			throw new CreateContentSettingsFailureException("Node root==null");//$NON-NLS-1$
	}

	public SimpleNodeOperator(String fullPath) throws ReadContentSettingsFailureException {
		this.settingsFileName = fullPath;
		createObjectOfDocument();
	}

	// add attribute(attName=attValue) of ele without checking overlapping of
	// another attributes of ele.
	// if overlapping ,override
	protected Node addAttributeAt(Element ele, String attName, String attValue) {
		Attr att = fDocument.createAttribute(attName);
		att.setValue(attValue);
		if (ele != null)
			ele.setAttributeNode(att);
		return ele;
	}

	protected Node addElementUnder(Node parent, String tagName, Map attMap) {
		if (parent == null || tagName == null)
			return null;
		Element e = fDocument.createElement(tagName);
		if (attMap != null) {
			if (!attMap.isEmpty()) {
				Set attKeys = attMap.keySet();
				Iterator iter = attKeys.iterator();
				while (iter.hasNext()) {
					String key = (String) iter.next();
					e.setAttribute(key, (String) attMap.get(key));
				}
			}
		}
		parent.appendChild(e);
		return e;
	}

	protected final Node addElementUnderRoot(String tagName) {
		return addElementUnder(root, tagName, null);
	}

	// add element with attMap as attribute without checking overlapping.
	protected final Node addElementUnderRoot(String tagName, Map attMap) {
		return addElementUnder(root, tagName, attMap);
	}

	private void createObjectOfDocument() throws ReadContentSettingsFailureException {
		try {
			fDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(settingsFileName));
		} catch (SAXException e) {
			Logger.logException("exception parsing" + settingsFileName, e); //$NON-NLS-1$
		} catch (IOException e) {
			Logger.logException("I/O exception parsing" + settingsFileName, e); //$NON-NLS-1$
		} catch (ParserConfigurationException e) {
			Logger.logException("exception accessing DOMImplementation", e); //$NON-NLS-1$
		} catch (FactoryConfigurationError e) {
			Logger.logException("exception accessing DOMImplementation", e); //$NON-NLS-1$
		}
		//get the root of the XML fDocument
		root = fDocument.getLastChild();
		if (root == null) {
			throw new ReadContentSettingsFailureException("Error: Node root==null");//$NON-NLS-1$
		}
	}

	protected Map getAttributesOf(Node node) {
		if (!node.hasAttributes())
			return null;
		Map map = new HashMap();
		NamedNodeMap attrs = node.getAttributes();
		int size = attrs.getLength();
		for (int i = 0; i < size; i++) {
			Attr attr = (Attr) attrs.item(i);
			map.put(attr.getName(), attr.getValue());
		}
		return (map);
	}

	private Node getElementWithAttribute(Node first, String attName, String attValue) {
		Node navpoint = first;
		while (navpoint != null) {
			if (navpoint.getNodeType() == Node.ELEMENT_NODE) {
				NamedNodeMap m = navpoint.getAttributes();
				if (m == null)
					continue;
				if (m.getNamedItem(attName) != null) {
					if (attValue.equals(((Attr) m.getNamedItem(attName)).getNodeValue()))
						return navpoint;
				}
				NodeList childNodes = navpoint.getChildNodes();
				if (childNodes != null && childNodes.getLength() > 0) {
					Node holdNode = getElementWithAttribute(navpoint.getFirstChild(), attName, attValue);
					if (holdNode != null) {
						return holdNode;
					}
				}
			}
			navpoint = navpoint.getNextSibling();
		}
		return null;
	}


	// return a (first) Element with attr(attName=attValue) it if exists,
	// otherwise return null
	protected Node getElementWithAttribute(String attName, String attValue) {
		if (attName == null || attValue == null || !fDocument.hasChildNodes())
			return null;
		return getElementWithAttribute(fDocument.getFirstChild(), attName, attValue);
	}

	// retrun Element which has nodeName as Node Name
	protected Node getElementWithNodeName(String nodeName) {
		if (nodeName == null)
			return null;
		NodeList nodes = fDocument.getElementsByTagName(nodeName);
		if (nodes.getLength() > 0) {
			return nodes.item(0);
		}
		return null;
	}

	public void printTree(NodeIterator iter) {
		Node n;
		while ((n = iter.nextNode()) != null) {
			System.out.println(n.getNodeName() + ":");//$NON-NLS-1$
			NamedNodeMap m = n.getAttributes();
			if (m == null)
				continue;
			for (int i = 0; i < m.getLength(); i++) {
				String attName = m.item(i).getNodeName();
				System.out.print(" " + attName + "=" + m.item(i).getNodeValue());//$NON-NLS-1$ //$NON-NLS-2$
			}
			System.out.println("");//$NON-NLS-1$
		}
	}


	// remove attribute(attName) at ele.
	protected Attr removeAttributeAt(Element ele, String attName) {
		if (ele == null || attName == null)
			return null;
		Attr att = ele.getAttributeNode(attName);
		ele.removeAttribute(attName);
		return att;
	}

	protected Element removeElementWith(String nodeName) {
		NodeList nodes = fDocument.getElementsByTagName(nodeName);
		for (int i = 0; i < nodes.getLength(); i++) {
			nodes.item(i).getParentNode().removeChild(nodes.item(i));
		}
		return null;
	}

	// remove a (first) Element with attr(attName=attValue) and return it if
	// exists, otherwise return null
	protected Element removeElementWith(String attName, String attValue) {
		if (fDocument.hasChildNodes()) {
			Node element = getElementWithAttribute(attName, attValue);
			if (element != null && element.getNodeType() == Node.ELEMENT_NODE) {
				element.getParentNode().removeChild(element);
				return (Element) element;
			}
		}
		return null;

	}

	// update attribute(attName=newValue) at ele if both ele and attribute of
	// ele exist
	protected void updateAttributeAt(Element ele, String attName, String newValue) {
		Attr att = null;
		if (ele != null)
			if ((att = ele.getAttributeNode(attName)) != null)
				att.setValue(newValue);
	}

	protected void writeDocument(OutputStream out) throws WriteContentSettingsFailureException {
		XMLDocumentWriter writer = new XMLDocumentWriter(out);
		try {
			writer.serialize(fDocument);
		} finally {
			writer.close();
		}
	}


}

Back to the top