KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > compiler > PrintInfo


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: PrintInfo.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.compiler;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.enhydra.xml.xmlc.dom.XMLCDocument;
30 import org.w3c.dom.Attr JavaDoc;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 /**
37  * Gather and print information about an document.
38  */

39 class PrintInfo {
40     /**
41      * Table URLs.
42      */

43     private Vector JavaDoc urls = new Vector JavaDoc();
44
45     /**
46      * Table of element ids.
47      */

48     private Vector JavaDoc ids = new Vector JavaDoc();
49     
50     /**
51      * Construct document info from document.
52      */

53     public PrintInfo(Document doc, XMLCDocument xmlcDoc) {
54         getNodeInfo(doc, xmlcDoc);
55     }
56
57     /**
58      * Append URLs from an element.
59      */

60     private void getElementURLs(Element JavaDoc element, XMLCDocument xmlcDoc) {
61         NamedNodeMap JavaDoc attrs = element.getAttributes();
62         if (attrs != null) {
63             for (int idx = 0; idx < attrs.getLength(); idx++) {
64                 Attr JavaDoc attr = (Attr JavaDoc)attrs.item(idx);
65                 if (xmlcDoc.isURLAttribute(element, attr.getNodeName())) {
66                     urls.addElement(attr.getValue());
67                 }
68             }
69         }
70     }
71
72     /**
73      * Recursively scan nodes nodes and accumulate information.
74      */

75     private void getNodeInfo(Node JavaDoc node, XMLCDocument xmlcDoc) {
76         if (node instanceof Element JavaDoc) {
77             Element JavaDoc elem = (Element JavaDoc)node;
78             getElementURLs(elem, xmlcDoc);
79             String JavaDoc id = xmlcDoc.getElementId(elem);
80             if ((id != null) && (id.length() > 0)) {
81                 ids.addElement(id + " => "
82                                + xmlcDoc.nodeClassToInterface(node));
83             }
84         }
85
86         for (Node JavaDoc child = node.getFirstChild(); child != null;
87              child = child.getNextSibling()) {
88             getNodeInfo(child, xmlcDoc);
89         }
90     }
91
92     /**
93      * Print document information.
94      */

95     protected void printInfo(PrintWriter JavaDoc out) {
96         int idx;
97
98         out.println("Element IDs:");
99         for (idx = 0; idx < ids.size(); idx++) {
100             out.println(" " + ids.elementAt(idx));
101         }
102         out.println("Document URLs:");
103         for (idx = 0; idx < urls.size(); idx++) {
104             out.println(" " + urls.elementAt(idx));
105         }
106     }
107 }
108
Popular Tags