Annotation of FM2SQL/com/exploringxml/xml/Node.java, revision 1.1.1.1
1.1 rogo 1: /* Copyright (c) 2000 Michael Claßen <mclassen@internet.com>
2: *
3: * This program is free software; you can redistribute it and/or
4: * modify it under the terms of the GNU General Public License
5: * as published by the Free Software Foundation; either version 2
6: * of the License, or (at your option) any later version.
7: *
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: * GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public License
14: * along with this program; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16: *
17: * $Id: Node.java,v 1.2 2001/01/05 19:32:05 classen Exp $
18: */
19:
20: package com.exploringxml.xml;
21:
22: import java.util.Hashtable;
23:
24: /**
25: * A node of an XML DOM tree;
26: *
27: * @author Michael Claßen
28: * @version $Revision: 1.2 $
29: */
30: public class Node {
31:
32: public String type;
33: public String name;
34: public String value;
35: public Hashtable attributes;
36: public int uid;
37: public JSArray contents;
38: public JSArray index;
39:
40: // node types
41: static final String Element = "element";
42: static final String CharData = "chardata";
43: static final String PI = "pi";
44: static final String Comment = "comment";
45:
46: /////////////////////////
47: //// the object constructors for the hybrid DOM
48:
49: /**
50: * factory method for element nodes
51: *
52: * @return a Node of type element
53: */
54: static Node createElement() {
55: Node n = new Node();
56: n.type = Element;
57: n.name = new String();
58: n.attributes = new Hashtable();
59: n.contents = new JSArray();
60: n.uid = Xparse.count++;
61: Xparse.index.setElementAt(n, n.uid);
62: return n;
63: }
64:
65: /**
66: * factory method for the root element
67: *
68: * @return a rootelement Node
69: */
70: static Node createRootelement() {
71: return createElement();
72: }
73:
74: /**
75: * factory method for chardata nodes
76: *
77: * @return a chardata Node
78: */
79: static Node createChardata() {
80: Node n = new Node();
81: n.type = CharData;
82: n.value = new String();
83: return n;
84: }
85:
86: /**
87: * factory method for PI nodes
88: *
89: * @return a PI Node
90: */
91: static Node createPi() {
92: Node n = new Node();
93: n.type = PI;
94: n.value = new String();
95: return n;
96: }
97:
98: /**
99: * factory method for comment nodes
100: *
101: * @return a comment Node
102: */
103: static Node createComment()
104: {
105: Node n = new Node();
106: n.type = Comment;
107: n.value = new String();
108: return n;
109: }
110:
111: /**
112: * returns the character data in the first child element;
113: * returns nonsense if the first child element ist not chardata
114: *
115: * @return the characters following an element
116: */
117: public String getCharacters() {
118: if(contents.v.isEmpty())
119: return ""; else
120: return ((Node)contents.elementAt(0)).value;
121: }
122:
123: /**
124: * find the node matching a certain occurrence of the path description
125: *
126: * @param path an XPath style expression without leading slash
127: * @param occur array indicating the n'th occurrence of a node matching each simple path expression
128: *
129: * @return the n'th Node matching the path description
130: */
131: public Node find(String path, int[] occur) {
132: Node n = this;
133: JSArray a = new JSArray();
134: a.split(path, "/");
135: int i = 0;
136: while (i < a.length()) {
137: n = findChildElement(n, (String)a.elementAt(i), occur[i]);
138: if (n == null) return null;
139: i++;
140: }
141: return n;
142: }
143:
144: /**
145: * find the child node matching a certain occurrence of a simple path description
146: *
147: * @param parent the parent node to start from
148: * @param simplePath one element of an XPath style expression
149: * @param occur the n'th occurance of a node matching the path expression
150: *
151: * @return the n'th child Node matching the simple path description
152: */
153: Node findChildElement(Node parent, String simplePath, int occur) {
154: JSArray a = parent.contents;
155: Node n;
156: int found = 0;
157: int i = 0;
158: String tag;
159: do {
160: n = (Node)a.elementAt(i);
161: ++i;
162: tag = (n.name != null) ? n.name : "";
163: int colonPos = tag.indexOf(':');
164: tag = (colonPos == -1) ? tag : tag.substring(colonPos + 1);
165: if (simplePath.equals(tag)) ++found;
166: } while (i < a.length() && found < occur);
167: return (found == occur) ? n : null;
168: }
169:
170: }
171:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>