File: DocumentImpl.java

package info (click to toggle)
herold 8.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,304 kB
  • sloc: java: 40,460; xml: 513; makefile: 37; sh: 29
file content (309 lines) | stat: -rw-r--r-- 7,540 bytes parent folder | download | duplicates (3)
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
/* 
 * Copyright (C) 2001-2013 Michael Fuchs
 *
 * This file is part of herold.
 * 
 * herold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * herold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with herold.  If not, see <http://www.gnu.org/licenses/>.  
 */
package org.dbdoclet.xiphias.dom;

import java.util.HashMap;

import org.dbdoclet.Sfv;
import org.dbdoclet.xiphias.W3cServices;
import org.dbdoclet.xiphias.XmlConstants;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

public class DocumentImpl extends NodeImpl implements Document {

	private DocumentType docType;
	private ElementImpl documentElement;
	private HashMap<String, String> namespacePrefixMap = new HashMap<>();
	private String xmlEncoding = "UTF-8";
	private String xmlVersion = "1.0";
	
	public DocumentImpl() {

		super("#document", null);
		setNodeType(DOCUMENT_NODE);

		setXmlEncoding("UTF-8");
		setXmlVersion("1.0");
		
		namespacePrefixMap.put(XmlConstants.NAMESPACE_XML, "xml");
	}

	@Override
	public Node adoptNode(Node source) throws DOMException {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public Attr createAttribute(String name) throws DOMException {
		
		AttrImpl attr = new AttrImpl();
		attr.setNodeName(name);
		return attr;
		
	}

	@Override
	public Attr createAttributeNS(String namespaceUri, String qualifiedName)
			throws DOMException {

		String prefix = manageNamespace(namespaceUri, qualifiedName);
		
		AttrImpl attr = new AttrImpl();
		attr.setPrefix(prefix);
		attr.setNamespaceURI(namespaceUri);
		attr.setNodeName(qualifiedName);
		return attr;
	}

	@Override
	public CDATASection createCDATASection(String data) throws DOMException {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public Comment createComment(String data) {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public DocumentFragment createDocumentFragment() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public ElementImpl createElement(String tagName) throws DOMException {

		ElementImpl elem = new ElementImpl(tagName);
		elem.setDocument(this);
		elem.setFormatType(FORMAT_BLOCK);
		return elem;
	}

	@Override
	public Element createElementNS(String namespaceUri, String qualifiedName)
			throws DOMException {

		String prefix = manageNamespace(namespaceUri, qualifiedName);
		
		ElementImpl elem = new ElementImpl();
		elem.setNamespaceURI(namespaceUri);
		elem.setPrefix(prefix);
		elem.setNodeName(qualifiedName);
		return elem;
	}

	@Override
	public EntityReference createEntityReference(String name)
			throws DOMException {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public ProcessingInstruction createProcessingInstruction(String target,
			String data) throws DOMException {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public Text createTextNode(String data) {

		TextImpl text = new TextImpl(data);
		text.setDocument(this);
		return text;
	}

	public String createXmlDeclaration() {
		return "<?xml version=\"" + xmlVersion + "\" encoding=\"" + xmlEncoding
				+ "\"?>" + Sfv.LSEP;
	}

	@Override
	public DocumentType getDoctype() {
		return docType;
	}

	@Override
	public Element getDocumentElement() {

		if (documentElement == null) {
			return (Element) getFirstChildElement();
		}

		return documentElement;
	}

	@Override
	public String getDocumentURI() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public DOMConfiguration getDomConfig() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public Element getElementById(String elementId) {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public NodeList getElementsByTagName(String tagname) {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public DOMImplementation getImplementation() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public String getInputEncoding() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public boolean getStrictErrorChecking() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public String getXmlEncoding() {
		return xmlEncoding;
	}

	@Override
	public boolean getXmlStandalone() {
		throw new IllegalStateException("Not yet implemented");
	}

	@Override
	public String getXmlVersion() {
		return xmlVersion;
	}

	@Override
	public Node importNode(Node importedNode, boolean deep) throws DOMException {

		Node clone = W3cServices.copyNode(this, importedNode);

		if (deep == false) {
			NodeList childList = clone.getChildNodes();
			for (int i = 0; i < childList.getLength(); i++) {
				clone.removeChild(childList.item(i));
			}
		}

		return clone;
	}

	@Override
	public void normalizeDocument() {
		throw new IllegalStateException("Not yet implemented");

	}

	@Override
	public Node renameNode(Node n, String namespaceURI, String qualifiedName)
			throws DOMException {
		throw new IllegalStateException("Not yet implemented");
	}

	public void setDoctype(DocumentType docType) {
		this.docType = docType;
	}

	public void setDocumentElement(ElementImpl documentElement) {
		this.documentElement = documentElement;

	}

	@Override
	public void setDocumentURI(String documentURI) {
		throw new IllegalStateException("Not yet implemented");

	}

	@Override
	public void setStrictErrorChecking(boolean strictErrorChecking) {
		throw new IllegalStateException("Not yet implemented");

	}

	public void setXmlEncoding(String xmlEncoding) {
		this.xmlEncoding = xmlEncoding;
	}

	@Override
	public void setXmlStandalone(boolean xmlStandalone) throws DOMException {
		throw new IllegalStateException("Not yet implemented");

	}

	@Override
	public void setXmlVersion(String xmlVersion) throws DOMException {
		this.xmlVersion = xmlVersion;

	}

	private String manageNamespace(String namespaceUri, String qualifiedName) {
		
		String prefix = null;

		if (qualifiedName.contains(":")) {
			String[] tokens = qualifiedName.split(":");
			prefix = tokens[0];
		}

		if (namespaceUri == null && prefix != null) {
			throw new DOMException(DOMException.NAMESPACE_ERR, String.format(
					"No namespace for prefix %s defined", prefix));
		}

		if (namespaceUri != null && prefix != null) {
			
			String value = namespacePrefixMap.get(namespaceUri);
			
			if (value == null) {
				namespacePrefixMap.put(namespaceUri, prefix);
			}
		}
		
		return prefix;
	}
}