1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.xml.security.uno;
25 
26 import org.w3c.dom.Node;
27 import org.w3c.dom.Attr;
28 import org.w3c.dom.NamedNodeMap;
29 
30 
31 /*
32  * This class wraps a DOM node and returns the text we want to
33  * display in the tree. It also returns children, index values,
34  * and child counts.
35  */
36 class AdapterNode
37 {
38 	private Node m_domNode;
39 	static final int ELEMENT_TYPE =   Node.ELEMENT_NODE;
40 
41 	/*
42 	 * An array of names for DOM node-types
43 	 */
44 	static final String[] typeName = {
45 		"none",
46 		"Element",
47 		"Attr",
48 		"Text",
49 		"CDATA",
50 		"EntityRef",
51 		"Entity",
52 		"ProcInstr",
53 		"Comment",
54 		"Document",
55 		"DocType",
56 		"DocFragment",
57 		"Notation",
58 		};
59 
getNode()60 	protected Node getNode()
61 	{
62 		return m_domNode;
63 	}
64 
65 	/*
66 	 * Construct an Adapter node from a DOM node
67 	 */
AdapterNode(org.w3c.dom.Node node)68 	protected AdapterNode(org.w3c.dom.Node node)
69 	{
70 		m_domNode = node;
71 	}
72 
73 	/*
74 	 * Return children, index, and count values
75 	 */
index(AdapterNode child)76 	protected int index(AdapterNode child)
77 	{
78 		int count = childCount();
79 		for (int i=0; i<count; ++i)
80 		{
81 			AdapterNode n = this.child(i);
82 			if (child.m_domNode == n.m_domNode) return i;
83 		}
84 		return -1;
85 	}
86 
child(int searchIndex)87 	protected AdapterNode child(int searchIndex)
88 	{
89 		if (m_domNode == null) return null;
90 
91 		/*
92 		 * Note: JTree index is zero-based.
93 		 */
94 		org.w3c.dom.Node node =
95 			m_domNode.getChildNodes().item(searchIndex);
96 
97 		return new AdapterNode(node);
98 	}
99 
childCount()100 	protected int childCount()
101 	{
102 		int rc = 0;
103 
104 		if (m_domNode != null)
105 		{
106 			rc = m_domNode.getChildNodes().getLength();
107 		}
108 
109 		return rc;
110 	}
111 
112 	/*
113 	 * Return a string that identifies this node in the tree
114 	 */
toString()115 	public String toString()
116 	{
117 		String rc = null;
118 
119 		if (m_domNode != null)
120 		{
121 			String s = typeName[m_domNode.getNodeType()];
122 			String nodeName = m_domNode.getNodeName();
123 
124 			if (! nodeName.startsWith("#"))
125 			{
126 				s += ": " + nodeName;
127 			}
128 
129 			if (m_domNode.getNodeValue() != null)
130 			{
131 				if (s.startsWith("ProcInstr"))
132 				{
133 					s += ", ";
134 				}
135 				else
136 				{
137 					s += ": ";
138 				}
139 
140 				String t = m_domNode.getNodeValue();
141 				s += t;
142 			}
143 
144 			if (m_domNode.getNodeType() == ELEMENT_TYPE)
145 			{
146 				NamedNodeMap attrs = m_domNode.getAttributes();
147 
148 				int length = attrs.getLength();
149 				for (int i=0; i<length; ++i)
150 				{
151 					Attr attr = (Attr)(attrs.item(i));
152 					s += " "+ attr.getName()+"='"+attr.getValue() + "'";
153 				}
154 			}
155 			rc = s;
156 		}
157 
158 		return rc;
159 	}
160 }
161 
162