View Javadoc

1   /*
2    * Copyright 2010-2013 Capgemini
3    * Licensed under the Apache License, Version 2.0 (the "License"); 
4    * you may not use this file except in compliance with the License. 
5    * You may obtain a copy of the License at 
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0 
8    * 
9    * Unless required by applicable law or agreed to in writing, software 
10   * distributed under the License is distributed on an "AS IS" BASIS, 
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
12   * See the License for the specific language governing permissions and 
13   * limitations under the License. 
14   * 
15   */
16  package org.xmlfield.core.impl.dom;
17  
18  import org.w3c.dom.Node;
19  import org.xmlfield.core.api.XmlFieldNode;
20  import org.xmlfield.core.impl.dom.cleanup.InputSanitizer;
21  
22  /**
23   * Default xml field node implementation
24   * 
25   * @author Guillaume Mary <guillaume.mary@capgemini.com>
26   * 
27   */
28  public class DomNode implements XmlFieldNode {
29  
30  	private Node node;
31  
32  	public DomNode(Node node) {
33  		super();
34  		this.node = node;
35  	}
36  
37  	@Override
38  	public XmlFieldNode getDocumentNode() {
39  		if (this.node != null) {
40  			return new DomNode(this.node.getOwnerDocument()
41  					.getDocumentElement());
42  		}
43  		return null;
44  	}
45  
46  	@Override
47  	public Node getNode() {
48  		return this.node;
49  	}
50  
51  	@Override
52  	public String getNodeName() {
53  		if (this.node == null) {
54  			return null;
55  		}
56  		return this.node.getNodeName();
57  	}
58  
59  	@Override
60  	public short getNodeType() {
61  		if (this.node == null) {
62  			return XmlFieldNode.UNKNOW_NODE;
63  		}
64  		short nodeW3CType = this.node.getNodeType();
65  		switch (nodeW3CType) {
66  		case Node.ATTRIBUTE_NODE:
67  			return XmlFieldNode.ATTRIBUTE_NODE;
68  
69  		case Node.ELEMENT_NODE:
70  			return XmlFieldNode.ELEMENT_NODE;
71  
72  		case Node.TEXT_NODE:
73  			return XmlFieldNode.TEXT_NODE;
74  
75  		default:
76  			return XmlFieldNode.UNKNOW_NODE;
77  		}
78  	}
79  
80  	@Override
81  	public XmlFieldNode getParentNode() {
82  		if (this.node == null) {
83  			return null;
84  		}
85  		return new DomNode(this.node.getParentNode());
86  	}
87  
88  	@Override
89  	public String getTextContent() {
90  		if (this.node == null) {
91  			return null;
92  		}
93  		return this.node.getTextContent();
94  	}
95  
96  	@Override
97  	public boolean hasAttributes() {
98  		if (this.node == null) {
99  			return false;
100 		}
101 		return this.node.hasAttributes();
102 	}
103 
104 	public void setNode(Node node) {
105 		this.node = node;
106 	}
107 
108 	@Override
109 	public void setTextContent(String textContent) {
110 		if (this.node != null) {
111 			this.node.setTextContent(InputSanitizer.sanitizeText(textContent));
112 		}
113 	}
114 
115 }