KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > uddi4j > UDDIElement


1 /*
2  * The source code contained herein is licensed under the IBM Public License
3  * Version 1.0, which has been approved by the Open Source Initiative.
4  * Copyright (C) 2001, International Business Machines Corporation
5  * Copyright (C) 2001, Hewlett-Packard Company
6  * All Rights Reserved.
7  *
8  */

9
10 package org.uddi4j;
11
12 import java.io.Serializable JavaDoc;
13 import java.util.Vector JavaDoc;
14
15 import org.w3c.dom.Element JavaDoc;
16 import org.w3c.dom.Node JavaDoc;
17 import org.w3c.dom.NodeList JavaDoc;
18
19 /**
20  * Base class for an object representing UDDI XML elements.
21  *
22  * @author David Melgar (dmelgar@us.ibm.com)
23  * @author Ravi Trivedi (ravi_trivedi@hp.com)
24  */

25 public abstract class UDDIElement implements Serializable JavaDoc {
26
27    public static String JavaDoc GENERIC = "2.0";
28    public static String JavaDoc XMLNS = "urn:uddi-org:api_v2";
29    public static String JavaDoc SOAPNS = "http://schemas.xmlsoap.org/soap/envelope/";
30
31    public UDDIElement() {
32    }
33
34    public UDDIElement(Element JavaDoc el) throws UDDIException {
35       // Check that if an exception should be thrown
36
if (UDDIException.isValidElement(el)) {
37          // Looks like a fault, process and throw
38
throw new UDDIException(el, true);
39       }
40    }
41
42    abstract public void saveToXML(Element JavaDoc base);
43
44    /**
45     * Performs a utility function.
46     * Returns text contained in child elements of the
47     * passed in element.
48     *
49     * @param el Element
50     * @return java.lang.String
51     */

52    protected String JavaDoc getText(Node JavaDoc el) {
53       NodeList JavaDoc nl = el.getChildNodes();
54       String JavaDoc result = "";
55       for (int i = 0; i < nl.getLength(); i++) {
56          if (nl.item(i).getNodeType()==Element.TEXT_NODE) {
57             result += nl.item(i).getNodeValue();
58          }
59       }
60       // Trim result, to remove whitespace.
61
return result.trim();
62    }
63
64    public NodeList JavaDoc getChildElementsByTagName(Element JavaDoc el, String JavaDoc tag) {
65        // Do NOT traverse the tree. Only search within the immediate
66
// child nodes.
67
NodeList JavaDoc children = el.getChildNodes();
68        Vector JavaDoc result = new Vector JavaDoc();
69        for (int i = 0; i < children.getLength(); i++) {
70            Node JavaDoc node = children.item(i);
71            if (node.getNodeType() == Node.ELEMENT_NODE &&
72                node.getNodeName().equals(tag)) {
73                // This is a valid node
74
result.addElement(node);
75            }
76        }
77        return new VectorNodeList(result);
78    }
79
80    protected Element JavaDoc base = null;
81
82 }
83
Popular Tags