merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / uno / AdapterNode.java
blobbed8e2a3631c495a73bd37bc9af2cf4ceadba401
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package com.sun.star.xml.security.uno;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.NamedNodeMap;
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.
40 class AdapterNode
42 private Node m_domNode;
43 static final int ELEMENT_TYPE = Node.ELEMENT_NODE;
46 * An array of names for DOM node-types
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",
64 protected Node getNode()
66 return m_domNode;
70 * Construct an Adapter node from a DOM node
72 protected AdapterNode(org.w3c.dom.Node node)
74 m_domNode = node;
78 * Return children, index, and count values
80 protected int index(AdapterNode child)
82 int count = childCount();
83 for (int i=0; i<count; ++i)
85 AdapterNode n = this.child(i);
86 if (child.m_domNode == n.m_domNode) return i;
88 return -1;
91 protected AdapterNode child(int searchIndex)
93 if (m_domNode == null) return null;
96 * Note: JTree index is zero-based.
98 org.w3c.dom.Node node =
99 m_domNode.getChildNodes().item(searchIndex);
101 return new AdapterNode(node);
104 protected int childCount()
106 int rc = 0;
108 if (m_domNode != null)
110 rc = m_domNode.getChildNodes().getLength();
113 return rc;
117 * Return a string that identifies this node in the tree
119 public String toString()
121 String rc = null;
123 if (m_domNode != null)
125 String s = typeName[m_domNode.getNodeType()];
126 String nodeName = m_domNode.getNodeName();
128 if (! nodeName.startsWith("#"))
130 s += ": " + nodeName;
133 if (m_domNode.getNodeValue() != null)
135 if (s.startsWith("ProcInstr"))
137 s += ", ";
139 else
141 s += ": ";
144 String t = m_domNode.getNodeValue();
145 s += t;
148 if (m_domNode.getNodeType() == ELEMENT_TYPE)
150 NamedNodeMap attrs = m_domNode.getAttributes();
152 int length = attrs.getLength();
153 for (int i=0; i<length; ++i)
155 Attr attr = (Attr)(attrs.item(i));
156 s += " "+ attr.getName()+"='"+attr.getValue() + "'";
159 rc = s;
162 return rc;