001: /**
002: * Copyright 2004 Carlos Silva A.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */package com.csa.lib.xml.dom;
017:
018: import java.util.Enumeration;
019: import java.util.Hashtable;
020: import java.util.Iterator;
021: import java.util.StringTokenizer;
022: import java.util.Vector;
023:
024: import org.xml.sax.AttributeList;
025:
026: /**
027: * Node implementa un arbol tipo DOM pero NO es DOM.
028: * No soporta contenidos mezclados (por ejemplo) ni sub esquemas.
029: *
030: * <P>Se uso para utilizar un parser SAX para leer los contenidos de
031: * los archivos XML sin usar Xerces.</p>
032: *
033: * <P>Esta basado en MinML (ver licencia en META-INF/)</p>
034: *
035: * <p>$Date: 2006/09/14 08:14:17 $</p>
036: * @version $Revision: 1.4 $
037: * @author Carlos Silva
038: */
039: public class Node {
040: String name;
041: StringBuffer value;
042: Hashtable attributes;
043: Hashtable attributeTypes;
044: Vector childs;
045: Node parent = null;
046: Node next = null;
047: Node prev = null;
048:
049: public Node(String name) {
050: this .name = name;
051: }
052:
053: public Node(String name, AttributeList attributes) {
054: this .name = name;
055: if (attributes.getLength() > 0) {
056: this .attributes = new Hashtable();
057: this .attributeTypes = new Hashtable();
058: for (int i = 0; i < attributes.getLength(); i++) {
059: this .attributes.put(attributes.getName(i), attributes
060: .getValue(i));
061: this .attributeTypes.put(attributes.getName(i),
062: attributes.getType(i));
063: }
064: }
065: }
066:
067: public Node(String name, Hashtable attributes) {
068: this .name = name;
069: this .attributes = attributes;
070: }
071:
072: public Node(String name, String value) {
073: this .name = name;
074: if (value != null)
075: this .value = new StringBuffer(value);
076: }
077:
078: public void setAttribute(String name, String value) {
079: if (attributes == null)
080: attributes = new Hashtable();
081: attributes.put(name, value);
082:
083: }
084:
085: public void addValue(String s) {
086: if (value == null)
087: value = new StringBuffer();
088: value.append(s);
089: }
090:
091: public String getValue() {
092: if (value == null)
093: return null;
094: return value.toString();
095: }
096:
097: public void appendChild(Node child) {
098: if (childs == null)
099: childs = new Vector();
100: child.parent = this ;
101: child.next = null;
102: if (childs.size() > 0) {
103: Node p = (Node) childs.lastElement();
104: child.prev = p;
105: p.next = child;
106: }
107: childs.add(child);
108: }
109:
110: public void appendChild(String childName, String childValue) {
111: appendChild(new Node(childName, childValue));
112: }
113:
114: public Iterator getChildElements() {
115: if (childs == null)
116: return (new Vector()).iterator();
117: return childs.iterator();
118: }
119:
120: public String getNodeName() {
121: return name;
122: }
123:
124: public Node getParentNode() {
125: return parent;
126: }
127:
128: public Node getNextSibling() {
129: return next;
130: }
131:
132: public Node getPrevSibling() {
133: return prev;
134: }
135:
136: public Node getFirstChild() {
137: if (childs == null)
138: return null;
139: return (Node) childs.firstElement();
140: }
141:
142: public String getAttribute(String n) {
143: if (attributes == null)
144: return null;
145: return (String) attributes.get(n);
146: }
147:
148: Node getChildByName(String childName) {
149: for (Iterator i = getChildElements(); i.hasNext();) {
150: Node c = (Node) i.next();
151: if (childName.equals(c.getNodeName())) {
152: return c;
153: }
154: }
155: return null;
156: }
157:
158: String getChildText(String childName) {
159: Node n = getChildByName(childName);
160: if (n == null)
161: return null;
162: return n.getValue();
163: }
164:
165: Node getElementByPath(String path) {
166: StringTokenizer st = new StringTokenizer(path, "/");
167: Node n = this ;
168: while (st.hasMoreTokens()) {
169: String t = st.nextToken();
170: n = n.getChildByName(t);
171: if (n == null)
172: return null;
173: }
174: return n;
175: }
176:
177: String getElementText(String path) {
178: Node n = getElementByPath(path);
179: return n.getValue();
180: }
181:
182: public void show() {
183: show(" ");
184: }
185:
186: void show(String pre) {
187: System.out.println(pre + name + ": " + getValue());
188: if (attributes != null)
189: for (Enumeration e = attributes.keys(); e.hasMoreElements();) {
190: String key = (String) e.nextElement();
191: String val = (String) attributes.get(key);
192: System.out.println(pre + " @" + key + "= \"" + val
193: + "\"");
194: }
195:
196: if (childs != null)
197: for (int i = 0; i < childs.size(); i++) {
198: ((Node) childs.get(i)).show(pre + " ");
199: }
200: }
201: }
|