KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > xml > FileXmlUtils


1 /*
2  * Created on 24 juin 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package com.bull.eclipse.jonas.utils.xml;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import org.w3c.dom.Node JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import com.bull.eclipse.jonas.JonasLauncherPlugin;
28
29 /**
30  * @author riase
31  *
32  * To change the template for this generated type comment go to
33  * Window>Preferences>Java>Code Generation>Code and Comments
34  */

35 public class FileXmlUtils {
36     
37     public static String JavaDoc getValueForNode(File JavaDoc xmlFile, String JavaDoc node) {
38         try {
39             FileInputStream JavaDoc is = new FileInputStream JavaDoc(xmlFile);
40             Document JavaDoc docXmlWeb = XMLUtils.newDocument(is,true);
41             
42             NodeList JavaDoc nlSC = docXmlWeb.getElementsByTagName(node);
43             for(int i = 0;i < nlSC.getLength(); i++) {
44                 JonasLauncherPlugin.log("Node Value = " + nlSC.item(i).getFirstChild().getNodeValue());
45                 return nlSC.item(i).getFirstChild().getNodeValue();
46             }
47
48         } catch (FileNotFoundException JavaDoc e) {
49             // TODO Auto-generated catch block
50
e.printStackTrace();
51         } catch (ParserConfigurationException JavaDoc e) {
52             // TODO Auto-generated catch block
53
e.printStackTrace();
54         } catch (SAXException JavaDoc e) {
55             // TODO Auto-generated catch block
56
e.printStackTrace();
57         } catch (IOException JavaDoc e) {
58             // TODO Auto-generated catch block
59
e.printStackTrace();
60         }
61         return null;
62     }
63
64     public static Hashtable JavaDoc getAllNameAndPackageEJBEntity(File JavaDoc xmlFile) {
65         Hashtable JavaDoc hashtable = new Hashtable JavaDoc();
66         try {
67             FileInputStream JavaDoc is = new FileInputStream JavaDoc(xmlFile);
68             Document JavaDoc docXmlWeb = XMLUtils.newDocument(is,true);
69             
70             NodeList JavaDoc entities = docXmlWeb.getDocumentElement().getElementsByTagName("entity");
71             for(int i = 0;i < entities.getLength(); i++) {
72                 Element JavaDoc entity = (Element JavaDoc) entities.item(i);
73                 NodeList JavaDoc names = entity.getElementsByTagName("ejb-name");
74                 Element JavaDoc name = (Element JavaDoc) names.item(0);
75                 String JavaDoc entityName = name.getFirstChild().getNodeValue();
76                 JonasLauncherPlugin.log("Entity Name = " + entityName);
77                 names = entity.getElementsByTagName("local");
78                 name = (Element JavaDoc) names.item(0);
79                 String JavaDoc packageName = name.getFirstChild().getNodeValue();
80                 int length = packageName.length();
81                 packageName = packageName.substring(0, length - 1 - entityName.length() - 5);
82                 JonasLauncherPlugin.log("Package Name = " + packageName);
83                 hashtable.put(entityName,packageName);
84             }
85         } catch (FileNotFoundException JavaDoc e) {
86             // TODO Auto-generated catch block
87
e.printStackTrace();
88         } catch (ParserConfigurationException JavaDoc e) {
89             // TODO Auto-generated catch block
90
e.printStackTrace();
91         } catch (SAXException JavaDoc e) {
92             // TODO Auto-generated catch block
93
e.printStackTrace();
94         } catch (IOException JavaDoc e) {
95             // TODO Auto-generated catch block
96
e.printStackTrace();
97         }
98         return hashtable;
99     }
100
101     public static Collection JavaDoc getAttributeValuesOfNode(File JavaDoc xmlFile, String JavaDoc node, String JavaDoc attribute) {
102         ArrayList JavaDoc al = new ArrayList JavaDoc();
103         try {
104             FileInputStream JavaDoc is = new FileInputStream JavaDoc(xmlFile);
105             Document JavaDoc docXmlWeb = XMLUtils.newDocument(is,true);
106             
107             NodeList JavaDoc nlSC = docXmlWeb.getElementsByTagName(node);
108             for(int i = 0;i < nlSC.getLength(); i++) {
109                 Node JavaDoc selectNode = nlSC.item(i);
110                 NamedNodeMap JavaDoc attributes = selectNode.getAttributes();
111                 al.add(attributes.getNamedItem(attribute).getNodeValue());
112             }
113         } catch (FileNotFoundException JavaDoc e) {
114             // TODO Auto-generated catch block
115
e.printStackTrace();
116         } catch (ParserConfigurationException JavaDoc e) {
117             // TODO Auto-generated catch block
118
e.printStackTrace();
119         } catch (SAXException JavaDoc e) {
120             // TODO Auto-generated catch block
121
e.printStackTrace();
122         } catch (IOException JavaDoc e) {
123             // TODO Auto-generated catch block
124
e.printStackTrace();
125         }
126         return al;
127     }
128
129 }
130
Popular Tags