1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.xml.security.uno;
29 
30 import org.w3c.dom.Node;
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.NamedNodeMap;
33 
34 
35 /*
36  * This class wraps a DOM node and returns the text we want to
37  * display in the tree. It also returns children, index values,
38  * and child counts.
39  */
40 class AdapterNode
41 {
42 	private Node m_domNode;
43 	static final int ELEMENT_TYPE =   Node.ELEMENT_NODE;
44 
45 	/*
46 	 * An array of names for DOM node-types
47 	 */
48 	static final String[] typeName = {
49 		"none",
50 		"Element",
51 		"Attr",
52 		"Text",
53 		"CDATA",
54 		"EntityRef",
55 		"Entity",
56 		"ProcInstr",
57 		"Comment",
58 		"Document",
59 		"DocType",
60 		"DocFragment",
61 		"Notation",
62 		};
63 
64 	protected Node getNode()
65 	{
66 		return m_domNode;
67 	}
68 
69 	/*
70 	 * Construct an Adapter node from a DOM node
71 	 */
72 	protected AdapterNode(org.w3c.dom.Node node)
73 	{
74 		m_domNode = node;
75 	}
76 
77 	/*
78 	 * Return children, index, and count values
79 	 */
80 	protected int index(AdapterNode child)
81 	{
82 		int count = childCount();
83 		for (int i=0; i<count; ++i)
84 		{
85 			AdapterNode n = this.child(i);
86 			if (child.m_domNode == n.m_domNode) return i;
87 		}
88 		return -1;
89 	}
90 
91 	protected AdapterNode child(int searchIndex)
92 	{
93 		if (m_domNode == null) return null;
94 
95 		/*
96 		 * Note: JTree index is zero-based.
97 		 */
98 		org.w3c.dom.Node node =
99 			m_domNode.getChildNodes().item(searchIndex);
100 
101 		return new AdapterNode(node);
102 	}
103 
104 	protected int childCount()
105 	{
106 		int rc = 0;
107 
108 		if (m_domNode != null)
109 		{
110 			rc = m_domNode.getChildNodes().getLength();
111 		}
112 
113 		return rc;
114 	}
115 
116 	/*
117 	 * Return a string that identifies this node in the tree
118 	 */
119 	public String toString()
120 	{
121 		String rc = null;
122 
123 		if (m_domNode != null)
124 		{
125 			String s = typeName[m_domNode.getNodeType()];
126 			String nodeName = m_domNode.getNodeName();
127 
128 			if (! nodeName.startsWith("#"))
129 			{
130 				s += ": " + nodeName;
131 			}
132 
133 			if (m_domNode.getNodeValue() != null)
134 			{
135 				if (s.startsWith("ProcInstr"))
136 				{
137 					s += ", ";
138 				}
139 				else
140 				{
141 					s += ": ";
142 				}
143 
144 				String t = m_domNode.getNodeValue();
145 				s += t;
146 			}
147 
148 			if (m_domNode.getNodeType() == ELEMENT_TYPE)
149 			{
150 				NamedNodeMap attrs = m_domNode.getAttributes();
151 
152 				int length = attrs.getLength();
153 				for (int i=0; i<length; ++i)
154 				{
155 					Attr attr = (Attr)(attrs.item(i));
156 					s += " "+ attr.getName()+"='"+attr.getValue() + "'";
157 				}
158 			}
159 			rc = s;
160 		}
161 
162 		return rc;
163 	}
164 }
165 
166