KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > util > xml_parser


1 /* $Id: xml_parser.java,v 1.8 2001/03/03 18:31:26 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.util;
16
17 import java.io.*;
18 import java.util.Enumeration JavaDoc;
19 import java.util.Hashtable JavaDoc;
20
21 //import javax.xml.parsers.*;
22
import org.w3c.dom.*;
23
24 import org.apache.xerces.dom.*;
25 import org.apache.xerces.parsers.DOMParser;
26
27 /**
28  * Generic parser implementation for webEditor
29  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
30  */

31 public class xml_parser
32 {
33    /**
34     * Perform the initial parse of a configuration file
35     * @param confFile Configuration file
36     * @return Hashtable sorted content
37     */

38    public Hashtable JavaDoc parseConfFile (String JavaDoc confFile)
39    {
40     Hashtable JavaDoc myTable = new Hashtable JavaDoc();
41     Hashtable JavaDoc auxTable = null;
42     String JavaDoc categoryName = null;
43
44 try {
45     // The parser begins!
46
DOMParser parser = new DOMParser();
47     confFile = new File (confFile).getAbsolutePath();
48     confFile = "file:" + confFile;
49     parser.parse(confFile);
50     Document doc = parser.getDocument();
51     
52     // Get the root's childs
53
Element root = doc.getDocumentElement();
54     String JavaDoc rootTag = root.getTagName();
55     NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
56
57     // For every child category, creates a hashtable with the content
58
for (int i=0; i < list.getLength(); i++) {
59         Node myCategory = list.item(i);
60         if (myCategory.getNodeName().equals("category")) {
61             categoryName = myCategory.getAttributes().getNamedItem("name").getNodeValue();
62             NodeList itemNodes = myCategory.getChildNodes();
63
64             // For every child item, adds the code:value pairs
65
auxTable = new Hashtable JavaDoc();
66             for (int j=0; j < itemNodes.getLength(); j++) {
67                 Node myItem = itemNodes.item(j);
68                 if (myItem.getNodeName().equals("data")) {
69                     auxTable.put(myItem.getAttributes().getNamedItem("code").getNodeValue(),
70                     myItem.getFirstChild().getNodeValue());
71                     }
72             }
73             // And then, we add the auxTable to the main table
74
myTable.put (categoryName, auxTable);
75             }
76         }
77
78 } catch (Exception JavaDoc e) {
79     e.printStackTrace();
80 }
81     return (myTable);
82    }
83
84    /**
85     * Translates a simple DOM tree into a HashTable
86     * @param content DOM tree of the document
87     * @return Hashtable sorted content of the document
88     */

89    public Hashtable JavaDoc parseDOM (Document content)
90    {
91     Hashtable JavaDoc myTable;
92     myTable = new Hashtable JavaDoc();
93
94 try {
95     Element root = content.getDocumentElement();
96     String JavaDoc rootTag = root.getTagName();
97     NodeList list = content.getElementsByTagName(rootTag).item(0).getChildNodes();
98
99     // For every child add the codes and the words
100
for (int i=0; i < list.getLength(); i++) {
101         if (list.item(i).getNodeName().equals("data")) {
102             myTable.put(list.item(i).getAttributes().getNamedItem("code").getNodeValue(),
103             list.item(i).getFirstChild().getNodeValue());
104             }
105         }
106
107 } catch (Exception JavaDoc e) {
108     e.printStackTrace();
109 }
110     return (myTable);
111   }
112
113   /**
114     * Given a multilevel Hashtable with pairs <key,value>, returns a DOM tree with the "same" structure.
115     * @param docRoot It could be empty if you want to process the hole tree
116     * @param content Values to write
117     * @return Document output DOM tree
118     */

119    public Document writeDOM (String JavaDoc docRoot, Hashtable JavaDoc content)
120    {
121     Document doc= new DocumentImpl();
122     Element item;
123     Element itemCategory;
124     Element root = doc.createElement(docRoot);
125
126     Enumeration JavaDoc myList = content.keys();
127
128     for (; myList.hasMoreElements() ;) {
129         String JavaDoc name = (String JavaDoc) myList.nextElement();
130         // We are going to assume that there is no configuration items
131
// outside a category.
132
itemCategory = doc.createElement ("category");
133         itemCategory.setAttribute ("name", name);
134
135         Hashtable JavaDoc category = (Hashtable JavaDoc) content.get(name);
136         Enumeration JavaDoc myItems = category.keys();
137         for (; myItems.hasMoreElements() ;) {
138             String JavaDoc code = (String JavaDoc) myItems.nextElement();
139             String JavaDoc value = (String JavaDoc) category.get(code);
140
141             item = doc.createElement ("data");
142             item.setAttribute ("code", code);
143             item.appendChild (doc.createTextNode(value));
144             itemCategory.appendChild (item);
145         }
146         root.appendChild (itemCategory);
147     }
148     doc.appendChild (root);
149
150     return (doc);
151    }
152  
153 }
154
Popular Tags